2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/event/actors/light-actor-impl.h>
22 #include <dali/public-api/object/type-registry.h>
23 #include <dali/integration-api/debug.h>
24 #include <dali/internal/event/common/property-index-ranges.h>
29 const Property::Index LightActor::LIGHT_TYPE = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
30 const Property::Index LightActor::ENABLE = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 1;
31 const Property::Index LightActor::FALL_OFF = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 2;
32 const Property::Index LightActor::SPOT_ANGLE = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 3;
33 const Property::Index LightActor::AMBIENT_COLOR = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 4;
34 const Property::Index LightActor::DIFFUSE_COLOR = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 5;
35 const Property::Index LightActor::SPECULAR_COLOR = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 6;
36 const Property::Index LightActor::DIRECTION = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 7;
40 bool LightActor::mFirstInstance = true ;
41 Actor::DefaultPropertyLookup* LightActor::mDefaultLightActorPropertyLookup = NULL;
48 return Dali::LightActor::New();
51 TypeRegistration mType( typeid(Dali::LightActor), typeid(Dali::Actor), Create );
53 const Internal::PropertyDetails DEFAULT_LIGHT_ACTOR_PROPERTY_DETAILS[] =
55 // Name Type writable animatable constraint-input
56 { "light-type", Property::STRING, true, false, true }, // LIGHT_TYPE
57 { "enable", Property::BOOLEAN, true, false, true }, // ENABLE
58 { "fall-off", Property::VECTOR2, true, false, true }, // FALL_OFF
59 { "spot-angle", Property::VECTOR2, true, false, true }, // SPOT_ANGLE
60 { "ambient-color", Property::VECTOR3, true, false, true }, // AMBIENT_COLOR
61 { "diffuse-color", Property::VECTOR3, true, false, true }, // DIFFUSE_COLOR
62 { "specular-color", Property::VECTOR3, true, false, true }, // SPECULAR_COLOR
63 { "direction", Property::VECTOR3, true, false, true }, // DIRECTION
65 const int DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_LIGHT_ACTOR_PROPERTY_DETAILS ) / sizeof( Internal::PropertyDetails );
67 LightType LightTypeEnum(const std::string &stringValue)
69 LightType ret(AMBIENT);
71 if(stringValue == "AMBIENT")
75 else if(stringValue == "DIRECTIONAL")
79 else if(stringValue == "SPOT")
83 else if(stringValue == "POINT")
89 DALI_LOG_WARNING("Unknown Light Type:%s\n", stringValue.c_str());
95 std::string LightTypeString(const LightType type)
97 std::string ret("AMBIENT");
130 LightActorPtr LightActor::New()
132 LightActorPtr actor(new LightActor());
134 // Second-phase construction
138 // Create the attachment
139 actor->mLightAttachment = LightAttachment::New( *actor->mNode );
140 actor->Attach(*actor->mLightAttachment);
141 actor->mLightAttachment->SetName(actor->GetName());
147 void LightActor::OnInitialize()
149 if(LightActor::mFirstInstance)
151 mDefaultLightActorPropertyLookup = new DefaultPropertyLookup();
152 const int start = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
153 for ( int i = 0; i < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT; ++i )
155 (*mDefaultLightActorPropertyLookup)[DEFAULT_LIGHT_ACTOR_PROPERTY_DETAILS[i].name] = i + start;
157 LightActor::mFirstInstance = false ;
161 LightActor::LightActor()
162 : Actor( Actor::BASIC ),
167 LightActor::~LightActor()
171 void LightActor::SetLight(Dali::Light light)
173 Internal::LightPtr lightPtr(&GetImplementation(light));
174 mLightAttachment->SetLight(lightPtr);
175 mLightAttachment->SetActive(true);
178 Dali::Light LightActor::GetLight() const
180 Internal::LightPtr lightPtr(mLightAttachment->GetLight());
181 return Dali::Light(lightPtr.Get());
184 void LightActor::SetActive(bool active)
186 mLightAttachment->SetActive(active);
190 bool LightActor::GetActive()
195 unsigned int LightActor::GetDefaultPropertyCount() const
197 return Actor::GetDefaultPropertyCount() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT;
200 void LightActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
202 Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
204 indices.reserve( indices.size() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT );
206 int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
207 for ( int i = 0; i < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT; ++i, ++index )
209 indices.push_back( index );
213 bool LightActor::IsDefaultPropertyWritable( Property::Index index ) const
215 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
217 return Actor::IsDefaultPropertyWritable(index) ;
225 bool LightActor::IsDefaultPropertyAnimatable( Property::Index index ) const
227 if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
229 return Actor::IsDefaultPropertyAnimatable(index) ;
237 bool LightActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
239 if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
241 return Actor::IsDefaultPropertyAConstraintInput(index);
243 return true; // Our properties can be used as input to constraints.
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_DETAILS[index].type;
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_DETAILS[index].name;
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