2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 // Licensed under the Flora License, Version 1.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://floralicense.org/license/
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an AS IS BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 #include <dali/internal/event/actors/light-actor-impl.h>
21 #include <dali/public-api/object/type-registry.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/internal/event/common/property-index-ranges.h>
28 const Property::Index LightActor::LIGHT_TYPE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
29 const Property::Index LightActor::ENABLE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 1;
30 const Property::Index LightActor::FALL_OFF = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 2;
31 const Property::Index LightActor::SPOT_ANGLE = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 3;
32 const Property::Index LightActor::AMBIENT_COLOR = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 4;
33 const Property::Index LightActor::DIFFUSE_COLOR = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 5;
34 const Property::Index LightActor::SPECULAR_COLOR = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 6;
35 const Property::Index LightActor::DIRECTION = DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 7;
39 bool LightActor::mFirstInstance = true ;
40 Actor::DefaultPropertyLookup* LightActor::mDefaultLightActorPropertyLookup = NULL;
47 return Dali::LightActor::New();
50 TypeRegistration mType( typeid(Dali::LightActor), typeid(Dali::Actor), Create );
52 const std::string DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[] =
63 const int DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
65 const Property::Type DEFAULT_LIGHT_ACTOR_PROPERTY_TYPES[DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT] =
67 Property::STRING, // "light-type",
68 Property::VECTOR2, // "enable",
69 Property::VECTOR2, // "fall-off",
70 Property::VECTOR2, // "spot-angle",
71 Property::VECTOR3, // "ambient-color",
72 Property::VECTOR3, // "diffuse-color",
73 Property::VECTOR3, // "specular-color",
74 Property::VECTOR3, // "direction",
78 LightType LightTypeEnum(const std::string &stringValue)
80 LightType ret(AMBIENT);
82 if(stringValue == "AMBIENT")
86 else if(stringValue == "DIRECTIONAL")
90 else if(stringValue == "SPOT")
94 else if(stringValue == "POINT")
100 DALI_LOG_WARNING("Unknown Light Type:%s\n", stringValue.c_str());
106 std::string LightTypeString(const LightType type)
114 else if(type == DIRECTIONAL)
116 ret = "DIRECTIONAL" ;
118 else if(type == SPOT)
122 else if(type == POINT)
128 DALI_LOG_WARNING("Unknown Light Type:%d\n", type);
139 LightActorPtr LightActor::New()
141 LightActorPtr actor(new LightActor());
143 // Second-phase construction
147 // Create the attachment
148 actor->mLightAttachment = LightAttachment::New( *actor->mNode );
149 actor->Attach(*actor->mLightAttachment);
150 actor->mLightAttachment->SetName(actor->GetName());
156 void LightActor::OnInitialize()
158 if(LightActor::mFirstInstance)
160 mDefaultLightActorPropertyLookup = new DefaultPropertyLookup();
161 const int start = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
162 for ( int i = 0; i < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT; ++i )
164 (*mDefaultLightActorPropertyLookup)[DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[i]] = i + start;
166 LightActor::mFirstInstance = false ;
170 LightActor::LightActor()
171 : Actor( Actor::BASIC ),
176 LightActor::~LightActor()
180 void LightActor::SetLight(Dali::Light light)
182 Internal::LightPtr lightPtr(&GetImplementation(light));
183 mLightAttachment->SetLight(lightPtr);
184 mLightAttachment->SetActive(true);
187 Dali::Light LightActor::GetLight() const
189 Internal::LightPtr lightPtr(mLightAttachment->GetLight());
190 return Dali::Light(lightPtr.Get());
193 void LightActor::SetActive(bool active)
195 mLightAttachment->SetActive(active);
199 bool LightActor::GetActive()
204 unsigned int LightActor::GetDefaultPropertyCount() const
206 return Actor::GetDefaultPropertyCount() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT;
209 void LightActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
211 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
213 indices.reserve( indices.size() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT );
215 int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
216 for ( int i = 0; i < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT; ++i, ++index )
218 indices.push_back( index );
222 bool LightActor::IsDefaultPropertyWritable( Property::Index index ) const
224 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
226 return Actor::IsDefaultPropertyWritable(index) ;
234 bool LightActor::IsDefaultPropertyAnimatable( Property::Index index ) const
236 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
238 return Actor::IsDefaultPropertyAnimatable(index) ;
246 Property::Type LightActor::GetDefaultPropertyType( Property::Index index ) const
248 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
250 return Actor::GetDefaultPropertyType(index) ;
254 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
256 if ( ( index >= 0 ) && ( index < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT ) )
258 return DEFAULT_LIGHT_ACTOR_PROPERTY_TYPES[index];
262 // index out-of-bounds
263 return Property::NONE;
268 const std::string& LightActor::GetDefaultPropertyName( Property::Index index ) const
270 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
272 return Actor::GetDefaultPropertyName(index) ;
276 index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
278 if ( ( index >= 0 ) && ( index < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT ) )
280 return DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[index];
284 // index out-of-bounds
285 static const std::string INVALID_PROPERTY_NAME;
286 return INVALID_PROPERTY_NAME;
291 Property::Index LightActor::GetDefaultPropertyIndex(const std::string& name) const
293 Property::Index index = Property::INVALID_INDEX;
295 DALI_ASSERT_DEBUG( NULL != mDefaultLightActorPropertyLookup );
297 // Look for name in current class' default properties
298 DefaultPropertyLookup::const_iterator result = mDefaultLightActorPropertyLookup->find( name );
299 if ( mDefaultLightActorPropertyLookup->end() != result )
301 index = result->second;
305 // If not found, check in base class
306 index = Actor::GetDefaultPropertyIndex( name );
312 void LightActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
314 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
316 Actor::SetDefaultProperty(index, propertyValue) ;
322 case Dali::LightActor::LIGHT_TYPE:
324 mLightAttachment->SetType(LightTypeEnum(propertyValue.Get<std::string>()));
327 case Dali::LightActor::ENABLE:
329 SetActive(propertyValue.Get<bool>());
332 case Dali::LightActor::FALL_OFF:
334 mLightAttachment->SetFallOff(propertyValue.Get<Vector2>());
337 case Dali::LightActor::SPOT_ANGLE:
339 mLightAttachment->SetSpotAngle(propertyValue.Get<Vector2>());
342 case Dali::LightActor::AMBIENT_COLOR:
344 mLightAttachment->SetAmbientColor(propertyValue.Get<Vector3>());
347 case Dali::LightActor::DIFFUSE_COLOR:
349 mLightAttachment->SetDiffuseColor(propertyValue.Get<Vector3>());
352 case Dali::LightActor::SPECULAR_COLOR:
354 mLightAttachment->SetSpecularColor(propertyValue.Get<Vector3>());
357 case Dali::LightActor::DIRECTION:
359 mLightAttachment->SetDirection(propertyValue.Get<Vector3>());
364 DALI_LOG_WARNING("Unknown property (%d)\n", index);
372 Property::Value LightActor::GetDefaultProperty( Property::Index index ) const
374 Property::Value ret ;
375 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
377 ret = Actor::GetDefaultProperty(index) ;
383 case Dali::LightActor::LIGHT_TYPE:
385 ret = LightTypeString(mLightAttachment->GetType());
388 case Dali::LightActor::ENABLE:
393 case Dali::LightActor::FALL_OFF:
395 ret = mLightAttachment->GetFallOff();
398 case Dali::LightActor::SPOT_ANGLE:
400 ret = mLightAttachment->GetSpotAngle();
403 case Dali::LightActor::AMBIENT_COLOR:
405 ret = mLightAttachment->GetAmbientColor();
408 case Dali::LightActor::DIFFUSE_COLOR:
410 ret = mLightAttachment->GetDiffuseColor();
413 case Dali::LightActor::SPECULAR_COLOR:
415 ret = mLightAttachment->GetSpecularColor();
418 case Dali::LightActor::DIRECTION:
420 ret = mLightAttachment->GetDirection();
425 DALI_LOG_WARNING("Unknown property (%d)\n", index);
435 } // namespace Internal