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