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