Merge "Print line numbers with shader source."
[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              = 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;
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;
109
110   if(type == AMBIENT)
111   {
112     ret = "AMBIENT" ;
113   }
114   else if(type == DIRECTIONAL)
115   {
116     ret = "DIRECTIONAL" ;
117   }
118   else if(type == SPOT)
119   {
120     ret = "SPOT" ;
121   }
122   else if(type == POINT)
123   {
124     ret = "POINT" ;
125   }
126   else
127   {
128     DALI_LOG_WARNING("Unknown Light Type:%d\n", type);
129     ret = "AMBIENT";
130   }
131
132   return ret;
133 }
134
135
136 }
137
138
139 LightActorPtr LightActor::New()
140 {
141   LightActorPtr actor(new LightActor());
142
143   // Second-phase construction
144
145   actor->Initialize();
146
147   // Create the attachment
148   actor->mLightAttachment = LightAttachment::New( *actor->mNode );
149   actor->Attach(*actor->mLightAttachment);
150   actor->mLightAttachment->SetName(actor->GetName());
151
152   return actor;
153 }
154
155
156 void LightActor::OnInitialize()
157 {
158   if(LightActor::mFirstInstance)
159   {
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 )
163     {
164       (*mDefaultLightActorPropertyLookup)[DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[i]] = i + start;
165     }
166     LightActor::mFirstInstance = false ;
167   }
168 }
169
170 LightActor::LightActor()
171 : Actor( Actor::BASIC ),
172   mIsActive(false)
173 {
174 }
175
176 LightActor::~LightActor()
177 {
178 }
179
180 void LightActor::SetLight(Dali::Light light)
181 {
182   Internal::LightPtr lightPtr(&GetImplementation(light));
183   mLightAttachment->SetLight(lightPtr);
184   mLightAttachment->SetActive(true);
185 }
186
187 Dali::Light LightActor::GetLight() const
188 {
189   Internal::LightPtr lightPtr(mLightAttachment->GetLight());
190   return Dali::Light(lightPtr.Get());
191 }
192
193 void LightActor::SetActive(bool active)
194 {
195   mLightAttachment->SetActive(active);
196   mIsActive = active;
197 }
198
199 bool LightActor::GetActive()
200 {
201   return mIsActive;
202 }
203
204 unsigned int LightActor::GetDefaultPropertyCount() const
205 {
206   return Actor::GetDefaultPropertyCount() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT;
207 }
208
209 void LightActor::GetDefaultPropertyIndices( Property::IndexContainer& indices ) const
210 {
211   Actor::GetDefaultPropertyIndices( indices ); // Actor class properties
212
213   indices.reserve( indices.size() + DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT );
214
215   int index = DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
216   for ( int i = 0; i < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT; ++i, ++index )
217   {
218     indices.push_back( index );
219   }
220 }
221
222 bool LightActor::IsDefaultPropertyWritable( Property::Index index ) const
223 {
224   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
225   {
226     return Actor::IsDefaultPropertyWritable(index) ;
227   }
228   else
229   {
230     return true ;
231   }
232 }
233
234 bool LightActor::IsDefaultPropertyAnimatable( Property::Index index ) const
235 {
236   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
237   {
238     return Actor::IsDefaultPropertyAnimatable(index) ;
239   }
240   else
241   {
242     return false ;
243   }
244 }
245
246 Property::Type LightActor::GetDefaultPropertyType( Property::Index index ) const
247 {
248   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
249   {
250     return Actor::GetDefaultPropertyType(index) ;
251   }
252   else
253   {
254     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
255
256     if ( ( index >= 0 ) && ( index < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT ) )
257     {
258       return DEFAULT_LIGHT_ACTOR_PROPERTY_TYPES[index];
259     }
260     else
261     {
262       // index out-of-bounds
263       return Property::NONE;
264     }
265   }
266 }
267
268 const std::string& LightActor::GetDefaultPropertyName( Property::Index index ) const
269 {
270   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
271   {
272     return Actor::GetDefaultPropertyName(index) ;
273   }
274   else
275   {
276     index -= DEFAULT_ACTOR_PROPERTY_MAX_COUNT;
277
278     if ( ( index >= 0 ) && ( index < DEFAULT_LIGHT_ACTOR_PROPERTY_COUNT ) )
279     {
280       return DEFAULT_LIGHT_ACTOR_PROPERTY_NAMES[index];
281     }
282     else
283     {
284       // index out-of-bounds
285       static const std::string INVALID_PROPERTY_NAME;
286       return INVALID_PROPERTY_NAME;
287     }
288   }
289 }
290
291 Property::Index LightActor::GetDefaultPropertyIndex(const std::string& name) const
292 {
293   Property::Index index = Property::INVALID_INDEX;
294
295   DALI_ASSERT_DEBUG( NULL != mDefaultLightActorPropertyLookup );
296
297   // Look for name in current class' default properties
298   DefaultPropertyLookup::const_iterator result = mDefaultLightActorPropertyLookup->find( name );
299   if ( mDefaultLightActorPropertyLookup->end() != result )
300   {
301     index = result->second;
302   }
303   else
304   {
305     // If not found, check in base class
306     index = Actor::GetDefaultPropertyIndex( name );
307   }
308
309   return index;
310 }
311
312 void LightActor::SetDefaultProperty( Property::Index index, const Property::Value& propertyValue )
313 {
314   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
315   {
316     Actor::SetDefaultProperty(index, propertyValue) ;
317   }
318   else
319   {
320     switch(index)
321     {
322       case Dali::LightActor::LIGHT_TYPE:
323       {
324         mLightAttachment->SetType(LightTypeEnum(propertyValue.Get<std::string>()));
325         break;
326       }
327       case Dali::LightActor::ENABLE:
328       {
329         SetActive(propertyValue.Get<bool>());
330         break;
331       }
332       case Dali::LightActor::FALL_OFF:
333       {
334         mLightAttachment->SetFallOff(propertyValue.Get<Vector2>());
335         break;
336       }
337       case Dali::LightActor::SPOT_ANGLE:
338       {
339         mLightAttachment->SetSpotAngle(propertyValue.Get<Vector2>());
340         break;
341       }
342       case Dali::LightActor::AMBIENT_COLOR:
343       {
344         mLightAttachment->SetAmbientColor(propertyValue.Get<Vector3>());
345         break;
346       }
347       case Dali::LightActor::DIFFUSE_COLOR:
348       {
349         mLightAttachment->SetDiffuseColor(propertyValue.Get<Vector3>());
350         break;
351       }
352       case Dali::LightActor::SPECULAR_COLOR:
353       {
354         mLightAttachment->SetSpecularColor(propertyValue.Get<Vector3>());
355         break;
356       }
357       case Dali::LightActor::DIRECTION:
358       {
359         mLightAttachment->SetDirection(propertyValue.Get<Vector3>());
360         break;
361       }
362       default:
363       {
364         DALI_LOG_WARNING("Unknown property (%d)\n", index);
365         break;
366       }
367     } // switch(index)
368
369   } // else
370 }
371
372 Property::Value LightActor::GetDefaultProperty( Property::Index index ) const
373 {
374   Property::Value ret ;
375   if(index < DEFAULT_ACTOR_PROPERTY_MAX_COUNT)
376   {
377     ret = Actor::GetDefaultProperty(index) ;
378   }
379   else
380   {
381     switch(index)
382     {
383       case Dali::LightActor::LIGHT_TYPE:
384       {
385         ret = LightTypeString(mLightAttachment->GetType());
386         break;
387       }
388       case Dali::LightActor::ENABLE:
389       {
390         ret = mIsActive;
391         break;
392       }
393       case Dali::LightActor::FALL_OFF:
394       {
395         ret = mLightAttachment->GetFallOff();
396         break;
397       }
398       case Dali::LightActor::SPOT_ANGLE:
399       {
400         ret = mLightAttachment->GetSpotAngle();
401         break;
402       }
403       case Dali::LightActor::AMBIENT_COLOR:
404       {
405         ret = mLightAttachment->GetAmbientColor();
406         break;
407       }
408       case Dali::LightActor::DIFFUSE_COLOR:
409       {
410         ret = mLightAttachment->GetDiffuseColor();
411         break;
412       }
413       case Dali::LightActor::SPECULAR_COLOR:
414       {
415         ret = mLightAttachment->GetSpecularColor();
416         break;
417       }
418       case Dali::LightActor::DIRECTION:
419       {
420         ret = mLightAttachment->GetDirection();
421         break;
422       }
423       default:
424       {
425         DALI_LOG_WARNING("Unknown property (%d)\n", index);
426         break;
427       }
428     } // switch(index)
429   }
430
431   return ret ;
432 }
433
434
435 } // namespace Internal
436
437 } // namespace Dali