Emscripten workarounds and llvm syntax fixes
[platform/core/uifw/dali-core.git] / dali / internal / update / node-attachments / scene-graph-light-attachment.h
1 #ifndef __DALI_INTERNAL_SCENE_GRAPH_LIGHT_ATTACHMENT_H__
2 #define __DALI_INTERNAL_SCENE_GRAPH_LIGHT_ATTACHMENT_H__
3
4 //
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
6 //
7 // Licensed under the Flora License, Version 1.0 (the License);
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //     http://floralicense.org/license/
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an AS IS BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/math/rect.h>
22 #include <dali/public-api/math/vector2.h>
23 #include <dali/internal/common/buffer-index.h>
24 #include <dali/internal/common/event-to-update.h>
25 #include <dali/internal/event/modeling/light-impl.h>
26 #include <dali/internal/update/node-attachments/node-attachment.h>
27 #include <dali/internal/update/controllers/light-controller.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 // value types used by messages
36 template <> struct ParameterType< LightType > : public BasicType< LightType > {};
37
38 namespace SceneGraph
39 {
40
41 class RenderableAttachment;
42
43 /**
44  * An attachment for light objects and their properties.
45  */
46 class LightAttachment : public NodeAttachment
47 {
48 public:
49   /**
50    * Construct a new LightAttachment.
51    * @return a new LightAttachment.
52    */
53   static LightAttachment* New()
54   {
55     return new LightAttachment();
56   }
57
58   /**
59    * @copydoc NodeAttachment::ConnectToSceneGraph().
60    */
61   virtual void ConnectToSceneGraph( SceneController& sceneController, BufferIndex updateBufferIndex );
62
63   /**
64    * @copydoc NodeAttachment::DisconnectFromSceneGraph().
65    */
66   virtual void OnDestroy();
67
68   /**
69    * Virtual destructor
70    */
71   virtual ~LightAttachment();
72
73   /**
74    * @copydoc NodeAttachment::GetRenderable.
75    * @return Always NULL.
76    */
77   virtual RenderableAttachment* GetRenderable()
78   {
79     return NULL;
80   }
81
82   /**
83    * @copydoc Dali::Internal::LightAttachment::SetLight
84    */
85   void SetLight(Internal::LightPtr light)
86   {
87     mLight = new Internal::Light(*light.Get());
88     mUpdateRequired = true;
89   }
90
91   /**
92    * @copydoc Dali::Internal::LightAttachment::GetLight
93    */
94   Internal::LightPtr GetLight() const
95   {
96     return mLight;
97   }
98
99   /**
100    * @copydoc Dali::Light::SetName
101    */
102   void SetName(const std::string& name)
103   {
104     mLight->SetName(name);
105   }
106   /**
107    * @copydoc Dali::Light::GetName
108    */
109   const std::string& GetName() const
110   {
111     return mLight->GetName();
112   }
113
114   /**
115    * @copydoc Dali::Light::SetType
116    */
117   void SetType(Dali::LightType type)
118   {
119     if (mLight->GetType() != type)
120     {
121       mLight->SetType(type);
122       mUpdateRequired = true;
123     }
124   }
125
126   /**
127    * @copydoc Dali::Light::GetType
128    */
129   Dali::LightType GetType() const
130   {
131     return mLight->GetType();
132   }
133
134   /**
135    * @copydoc Dali::Light::SetFallOff
136    */
137   void SetFallOff(const Vector2& fallOff)
138   {
139     if (mLight->GetFallOff() != fallOff)
140     {
141       mLight->SetFallOff(fallOff);
142       mUpdateRequired = true;
143     }
144   }
145
146   /**
147    * @copydoc Dali::Light::GetFallOff
148    */
149   const Vector2& GetFallOff() const
150   {
151     return mLight->GetFallOff();
152   }
153
154   /**
155    * @copydoc Dali::Light::SetSpotAngle
156    */
157   void SetSpotAngle(const Vector2& angle)
158   {
159     if (mLight->GetSpotAngle() != angle)
160     {
161       mLight->SetSpotAngle(angle);
162       mUpdateRequired = true;
163     }
164   }
165
166   /**
167    * @copydoc Dali::Light::GetSpotAngle
168    */
169   const Vector2& GetSpotAngle() const
170   {
171     return mLight->GetSpotAngle();
172   }
173
174   /**
175    * @copydoc Dali::Light::SetAmbientColor
176    */
177   void SetAmbientColor(const Vector3& color)
178   {
179     if (mLight->GetAmbientColor() != color)
180     {
181       mLight->SetAmbientColor(color);
182       mUpdateRequired = true;
183     }
184   }
185
186   /**
187    * @copydoc Dali::Light::GetAmbientColor
188    */
189   const Vector3& GetAmbientColor() const
190   {
191     return mLight->GetAmbientColor();
192   }
193
194   /**
195    * @copydoc Dali::Light::SetDiffuseColor
196    */
197   void SetDiffuseColor(const Vector3& color)
198   {
199     if (mLight->GetDiffuseColor() != color)
200     {
201       mLight->SetDiffuseColor(color);
202       mUpdateRequired = true;
203     }
204   }
205
206   /**
207    * @copydoc Dali::Light::GetDiffuseColor
208    */
209   const Vector3& GetDiffuseColor() const
210   {
211     return mLight->GetDiffuseColor();
212   }
213
214   /**
215    * @copydoc Dali::Light::SetSpecularColor
216    */
217   void SetSpecularColor(const Vector3& color)
218   {
219     if (mLight->GetSpecularColor() != color)
220     {
221       mLight->SetSpecularColor(color);
222       mUpdateRequired = true;
223     }
224   }
225
226   /**
227    * @copydoc Dali::Light::GetSpecularColor
228    */
229   const Vector3& GetSpecularColor() const
230   {
231     return mLight->GetSpecularColor();
232   }
233
234   /**
235    * @copydoc Dali::Light::SetDirection
236    */
237   void SetDirection(const Vector3& direction)
238   {
239     if (mLight->GetDirection() != direction)
240     {
241       mLight->SetDirection(direction);
242       mUpdateRequired = true;
243     }
244   }
245
246   /**
247    * @copydoc Dali::Light::GetDirection
248    */
249   const Vector3& GetDirection() const
250   {
251     return mLight->GetDirection();
252   }
253
254
255   /**
256    * @copydoc Dali::LightActor::SetActive
257    */
258   void SetActive(bool active)
259   {
260     Node& lightNode(GetParent());
261     if (active)
262     {
263       mLightController->AddLight(lightNode);        // Add to list of active lights
264     }
265     else
266     {
267       mLightController->RemoveLight(lightNode);     // Remove from list of active lights
268     }
269   }
270
271   virtual void Update( BufferIndex /*updateBufferIndex*/, const Node& /*owningNode*/, int /*nodeDirtyFlags*/ )
272   {
273     if (mUpdateRequired)
274     {
275       mUpdateRequired = false;
276     }
277   }
278
279 protected:
280
281   /**
282    * Protected constructor; see also LightAttachment::New().
283    */
284   LightAttachment();
285
286 private:
287
288   // Undefined
289   LightAttachment(const LightAttachment&);
290
291   // Undefined
292   LightAttachment& operator=(const LightAttachment& rhs);
293
294 private:
295   Internal::LightPtr mLight;
296   bool               mUpdateRequired;                ///< This is set to true if the projection matrix requires an update
297   LightController*   mLightController;               ///< required to add / remove lights
298 };
299
300 // Messages for LightAttachment
301
302 inline void SetLightMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const Internal::LightPtr& parameter )
303 {
304   typedef MessageValue1< LightAttachment, Internal::LightPtr > LocalType;
305
306   // Reserve some memory inside the message queue
307   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
308
309   // Construct message in the message queue memory; note that delete should not be called on the return value
310   new (slot) LocalType( &attachment, &LightAttachment::SetLight, parameter );
311 }
312
313 inline void SetNameMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const std::string& parameter )
314 {
315   typedef MessageValue1< LightAttachment, std::string > LocalType;
316
317   // Reserve some memory inside the message queue
318   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
319
320   // Construct message in the message queue memory; note that delete should not be called on the return value
321   new (slot) LocalType( &attachment, &LightAttachment::SetName, parameter );
322 }
323
324 inline void SetTypeMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, LightType parameter )
325 {
326   typedef MessageValue1< LightAttachment, LightType > LocalType;
327
328   // Reserve some memory inside the message queue
329   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
330
331   // Construct message in the message queue memory; note that delete should not be called on the return value
332   new (slot) LocalType( &attachment, &LightAttachment::SetType, parameter );
333 }
334
335 inline void SetFallOffMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const Vector2 &parameter )
336 {
337   typedef MessageValue1< LightAttachment, Vector2 > LocalType;
338
339   // Reserve some memory inside the message queue
340   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
341
342   // Construct message in the message queue memory; note that delete should not be called on the return value
343   new (slot) LocalType( &attachment, &LightAttachment::SetFallOff, parameter );
344 }
345
346 inline void SetSpotAngleMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const Vector2 &parameter )
347 {
348   typedef MessageValue1< LightAttachment, Vector2 > LocalType;
349
350   // Reserve some memory inside the message queue
351   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
352
353   // Construct message in the message queue memory; note that delete should not be called on the return value
354   new (slot) LocalType( &attachment, &LightAttachment::SetSpotAngle, parameter );
355 }
356
357 inline void SetAmbientColorMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const Vector3 &parameter )
358 {
359   typedef MessageValue1< LightAttachment, Vector3 > LocalType;
360
361   // Reserve some memory inside the message queue
362   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
363
364   // Construct message in the message queue memory; note that delete should not be called on the return value
365   new (slot) LocalType( &attachment, &LightAttachment::SetAmbientColor, parameter );
366 }
367
368 inline void SetDiffuseColorMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const Vector3 &parameter )
369 {
370   typedef MessageValue1< LightAttachment, Vector3 > LocalType;
371
372   // Reserve some memory inside the message queue
373   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
374
375   // Construct message in the message queue memory; note that delete should not be called on the return value
376   new (slot) LocalType( &attachment, &LightAttachment::SetDiffuseColor, parameter );
377 }
378
379 inline void SetSpecularColorMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const Vector3 &parameter )
380 {
381   typedef MessageValue1< LightAttachment, Vector3 > LocalType;
382
383   // Reserve some memory inside the message queue
384   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
385
386   // Construct message in the message queue memory; note that delete should not be called on the return value
387   new (slot) LocalType( &attachment, &LightAttachment::SetSpecularColor, parameter );
388 }
389
390 inline void SetDirectionMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, const Vector3 &parameter )
391 {
392   typedef MessageValue1< LightAttachment, Vector3 > LocalType;
393
394   // Reserve some memory inside the message queue
395   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
396
397   // Construct message in the message queue memory; note that delete should not be called on the return value
398   new (slot) LocalType( &attachment, &LightAttachment::SetDirection, parameter );
399 }
400
401 inline void SetActiveMessage( EventToUpdate& eventToUpdate, const LightAttachment& attachment, bool parameter )
402 {
403   typedef MessageValue1< LightAttachment, bool > LocalType;
404
405   // Reserve some memory inside the message queue
406   unsigned int* slot = eventToUpdate.ReserveMessageSlot( sizeof( LocalType ) );
407
408   // Construct message in the message queue memory; note that delete should not be called on the return value
409   new (slot) LocalType( &attachment, &LightAttachment::SetActive, parameter );
410 }
411
412 } // namespace SceneGraph
413
414 } // namespace Internal
415
416 } // namespace Dali
417
418 #endif // __DALI_INTERNAL_SCENE_GRAPH_LIGHT_ATTACHMENT_H__