Align data members, pointers first, classes then, next built in types and flags last...
[platform/core/uifw/dali-core.git] / dali / internal / event / actors / light-actor-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 //     http://floralicense.org/license/
9 //
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.
15 //
16
17 // CLASS HEADER
18 #include <dali/internal/event/actors/light-actor-impl.h>
19
20 // INTERNAL HEADER
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>
24
25 namespace Dali
26 {
27
28 const Property::Index LightActor::LIGHT_TYPE              = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
29 const Property::Index LightActor::ENABLE                  = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 1;
30 const Property::Index LightActor::FALL_OFF                = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 2;
31 const Property::Index LightActor::SPOT_ANGLE              = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 3;
32 const Property::Index LightActor::AMBIENT_COLOR           = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 4;
33 const Property::Index LightActor::DIFFUSE_COLOR           = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 5;
34 const Property::Index LightActor::SPECULAR_COLOR          = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 6;
35 const Property::Index LightActor::DIRECTION               = Internal::DEFAULT_ACTOR_PROPERTY_MAX_COUNT + 7;
36
37 namespace Internal
38 {
39 bool LightActor::mFirstInstance = true ;
40 Actor::DefaultPropertyLookup* LightActor::mDefaultLightActorPropertyLookup = NULL;
41
42 namespace
43 {
44
45 BaseHandle Create()
46 {
47   return Dali::LightActor::New();
48 }
49
50 TypeRegistration mType( typeid(Dali::LightActor), typeid(Dali::Actor), Create );
51
52 const std::string DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[] =
53 {
54   "light-type",
55   "enable",
56   "fall-off",
57   "spot-angle",
58   "ambient-color",
59   "diffuse-color",
60   "specular-color",
61   "direction"
62 };
63 const int DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT = sizeof( DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES ) / sizeof( std::string );
64
65 const Property::Type DEFAULT_LIGHT_ACTOR_PROPERTY_TYPES[DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT] =
66 {
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",
75 };
76
77
78 LightType LightTypeEnum(const std::string &stringValue)
79 {
80   LightType ret(AMBIENT);
81
82   if(stringValue == "AMBIENT")
83   {
84     ret =  AMBIENT;
85   }
86   else if(stringValue == "DIRECTIONAL")
87   {
88     ret =  DIRECTIONAL;
89   }
90   else if(stringValue == "SPOT")
91   {
92     ret =  SPOT;
93   }
94   else if(stringValue == "POINT")
95   {
96     ret =  POINT;
97   }
98   else
99   {
100     DALI_LOG_WARNING("Unknown Light Type:%s\n", stringValue.c_str());
101   }
102
103   return ret;
104 }
105
106 std::string LightTypeString(const LightType type)
107 {
108   std::string ret("AMBIENT");
109
110   switch ( type )
111   {
112     case AMBIENT:
113     {
114       ret = "AMBIENT";
115       break;
116     }
117
118     case DIRECTIONAL:
119     {
120       ret = "DIRECTIONAL";
121       break;
122     }
123
124     case SPOT:
125     {
126       ret = "SPOT";
127       break;
128     }
129     case POINT:
130     {
131       ret = "POINT";
132       break;
133     }
134   }
135   return ret;
136 }
137
138 } // namespace
139
140
141 LightActorPtr LightActor::New()
142 {
143   LightActorPtr actor(new LightActor());
144
145   // Second-phase construction
146
147   actor->Initialize();
148
149   // Create the attachment
150   actor->mLightAttachment = LightAttachment::New( *actor->mNode );
151   actor->Attach(*actor->mLightAttachment);
152   actor->mLightAttachment->SetName(actor->GetName());
153
154   return actor;
155 }
156
157
158 void LightActor::OnInitialize()
159 {
160   if(LightActor::mFirstInstance)
161   {
162     mDefaultLightActorPropertyLookup = new DefaultPropertyLookup();
163     const int start = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
164     for ( int i = 0; i < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT; ++i )
165     {
166       (*mDefaultLightActorPropertyLookup)[DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[i]] = i + start;
167     }
168     LightActor::mFirstInstance = false ;
169   }
170 }
171
172 LightActor::LightActor()
173 : Actor( Actor::BASIC ),
174   mIsActive(false)
175 {
176 }
177
178 LightActor::~LightActor()
179 {
180 }
181
182 void LightActor::SetLight(Dali::Light light)
183 {
184   Internal::LightPtr lightPtr(&GetImplementation(light));
185   mLightAttachment->SetLight(lightPtr);
186   mLightAttachment->SetActive(true);
187 }
188
189 Dali::Light LightActor::GetLight() const
190 {
191   Internal::LightPtr lightPtr(mLightAttachment->GetLight());
192   return Dali::Light(lightPtr.Get());
193 }
194
195 void LightActor::SetActive(bool active)
196 {
197   mLightAttachment->SetActive(active);
198   mIsActive = active;
199 }
200
201 bool LightActor::GetActive()
202 {
203   return mIsActive;
204 }
205
206 unsigned int LightActor::GetDefaultPropertyCount() const
207 {
208   return Actor::GetDefaultPropertyCount() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT;
209 }
210
211 void LightActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
212 {
213   Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
214
215   indices.reserve( indices.size() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT );
216
217   int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
218   for ( int i = 0; i < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT; ++i, ++index )
219   {
220     indices.push_back( index );
221   }
222 }
223
224 bool LightActor::IsDefaultPropertyWritable( Property::Index index ) const
225 {
226   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
227   {
228     return Actor::IsDefaultPropertyWritable(index) ;
229   }
230   else
231   {
232     return true ;
233   }
234 }
235
236 bool LightActor::IsDefaultPropertyAnimatable( Property::Index index ) const
237 {
238   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
239   {
240     return Actor::IsDefaultPropertyAnimatable(index) ;
241   }
242   else
243   {
244     return false ;
245   }
246 }
247
248 bool LightActor::IsDefaultPropertyAConstraintInput( Property::Index index ) const
249 {
250   if( index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT )
251   {
252     return Actor::IsDefaultPropertyAConstraintInput(index);
253   }
254   return true; // Our properties can be used as input to constraints.
255 }
256
257 Property::Type LightActor::GetDefaultPropertyType( Property::Index index ) const
258 {
259   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
260   {
261     return Actor::GetDefaultPropertyType(index) ;
262   }
263   else
264   {
265     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
266
267     if ( ( index >= 0 ) && ( index < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT ) )
268     {
269       return DEFAULT_LIGHT_ACTOR_PROPERTY_TYPES[index];
270     }
271     else
272     {
273       // index out-of-bounds
274       return Property::NONE;
275     }
276   }
277 }
278
279 const std::string& LightActor::GetDefaultPropertyName( Property::Index index ) const
280 {
281   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
282   {
283     return Actor::GetDefaultPropertyName(index) ;
284   }
285   else
286   {
287     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
288
289     if ( ( index >= 0 ) && ( index < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT ) )
290     {
291       return DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[index];
292     }
293     else
294     {
295       // index out-of-bounds
296       static const std::string INVALID_PROPERTY_NAME;
297       return INVALID_PROPERTY_NAME;
298     }
299   }
300 }
301
302 Property::Index LightActor::GetDefaultPropertyIndex(const std::string& name) const
303 {
304   Property::Index index = Property::INVALID_INDEX;
305
306   DALI_ASSERT_DEBUG( NULL != mDefaultLightActorPropertyLookup );
307
308   // Look for name in current class' default properties
309   DefaultPropertyLookup::const_iterator result = mDefaultLightActorPropertyLookup->find( name );
310   if ( mDefaultLightActorPropertyLookup->end() != result )
311   {
312     index = result->second;
313   }
314   else
315   {
316     // If not found, check in base class
317     index = Actor::GetDefaultPropertyIndex( name );
318   }
319
320   return index;
321 }
322
323 void LightActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
324 {
325   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
326   {
327     Actor::SetDefaultProperty(index, propertyValue) ;
328   }
329   else
330   {
331     switch(index)
332     {
333       case Dali::LightActor::LIGHT_TYPE:
334       {
335         mLightAttachment->SetType(LightTypeEnum(propertyValue.Get<std::string>()));
336         break;
337       }
338       case Dali::LightActor::ENABLE:
339       {
340         SetActive(propertyValue.Get<bool>());
341         break;
342       }
343       case Dali::LightActor::FALL_OFF:
344       {
345         mLightAttachment->SetFallOff(propertyValue.Get<Vector2>());
346         break;
347       }
348       case Dali::LightActor::SPOT_ANGLE:
349       {
350         mLightAttachment->SetSpotAngle(propertyValue.Get<Vector2>());
351         break;
352       }
353       case Dali::LightActor::AMBIENT_COLOR:
354       {
355         mLightAttachment->SetAmbientColor(propertyValue.Get<Vector3>());
356         break;
357       }
358       case Dali::LightActor::DIFFUSE_COLOR:
359       {
360         mLightAttachment->SetDiffuseColor(propertyValue.Get<Vector3>());
361         break;
362       }
363       case Dali::LightActor::SPECULAR_COLOR:
364       {
365         mLightAttachment->SetSpecularColor(propertyValue.Get<Vector3>());
366         break;
367       }
368       case Dali::LightActor::DIRECTION:
369       {
370         mLightAttachment->SetDirection(propertyValue.Get<Vector3>());
371         break;
372       }
373       default:
374       {
375         DALI_LOG_WARNING("Unknown property (%d)\n", index);
376         break;
377       }
378     } // switch(index)
379
380   } // else
381 }
382
383 Property::Value LightActor::GetDefaultProperty( Property::Index index ) const
384 {
385   Property::Value ret ;
386   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
387   {
388     ret = Actor::GetDefaultProperty(index) ;
389   }
390   else
391   {
392     switch(index)
393     {
394       case Dali::LightActor::LIGHT_TYPE:
395       {
396         ret = LightTypeString(mLightAttachment->GetType());
397         break;
398       }
399       case Dali::LightActor::ENABLE:
400       {
401         ret = mIsActive;
402         break;
403       }
404       case Dali::LightActor::FALL_OFF:
405       {
406         ret = mLightAttachment->GetFallOff();
407         break;
408       }
409       case Dali::LightActor::SPOT_ANGLE:
410       {
411         ret = mLightAttachment->GetSpotAngle();
412         break;
413       }
414       case Dali::LightActor::AMBIENT_COLOR:
415       {
416         ret = mLightAttachment->GetAmbientColor();
417         break;
418       }
419       case Dali::LightActor::DIFFUSE_COLOR:
420       {
421         ret = mLightAttachment->GetDiffuseColor();
422         break;
423       }
424       case Dali::LightActor::SPECULAR_COLOR:
425       {
426         ret = mLightAttachment->GetSpecularColor();
427         break;
428       }
429       case Dali::LightActor::DIRECTION:
430       {
431         ret = mLightAttachment->GetDirection();
432         break;
433       }
434       default:
435       {
436         DALI_LOG_WARNING("Unknown property (%d)\n", index);
437         break;
438       }
439     } // switch(index)
440   }
441
442   return ret ;
443 }
444
445
446 } // namespace Internal
447
448 } // namespace Dali