cb65ee402b367131030fd2c28c1d2ae298855174
[platform/core/uifw/dali-core.git] / dali / public-api / actors / layer.h
1 #ifndef __DALI_LAYER_H__
2 #define __DALI_LAYER_H__
3
4 /*
5  * Copyright (c) 2015 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/object/ref-object.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/math/rect.h>
25 #include <dali/public-api/math/vector3.h>
26 #include <dali/public-api/images/frame-buffer-image.h>
27
28 namespace Dali
29 {
30 /**
31  * @addtogroup dali_core_actors
32  * @{
33  */
34
35 namespace Internal DALI_INTERNAL
36 {
37 class Layer;
38 }
39
40 /**
41  * @brief Rectangle describing area on screen that a layer can draw to
42  *
43  * @SINCE_1_0.0
44  * @see Dali::Layer::SetClippingBox()
45  */
46 typedef Rect<int> ClippingBox;
47
48 /**
49  * @brief Layers provide a mechanism for overlaying groups of actors on top of each other.
50  *
51  * When added to the stage, a layer can be ordered relative to other layers. The bottom
52  * layer is at depth zero. The stage provides a default layer for it's children (see Stage::GetRootLayer()).
53  *
54  * Layered actors inherit position etc. as normal, but are drawn in an order determined
55  * by the layers. In case of LAYER_3D, the depth buffer is cleared before each layer is rendered unless depth
56  * test is disabled or there's no need for it based on the layers contents;
57  * actors in lower layers cannot obscure actors in higher layers.
58  *
59  * A layer has either LAYER_2D or LAYER_3D mode. LAYER_2D has better performance,
60  * the depth test is disabled, and a child actor hides its parent actor.
61  * LAYER_3D uses the depth test, thus a close actor hides a farther one.
62  * LAYER_2D is the default mode and recommended for general cases.
63  * See Layer::Behavior and SetBehavior() for more information.
64  *
65  * Layer is a type of Actor, thus can have parent or children actors.
66  * A layer influences rendering of its all descendant actors,
67  * until another layer appears in the actor tree and manages its own subtree.
68  *
69  * If depth test is disabled, there is no performance overhead from clearing the depth buffer.
70  *
71  * Actions
72  * | %Action Name    | %Layer method called |
73  * |-----------------|----------------------|
74  * | raise           | @ref Raise()         |
75  * | lower           | @ref Lower()         |
76  * | raiseToTop      | @ref RaiseToTop()    |
77  * | lowerToBottom   | @ref LowerToBottom() |
78  * @SINCE_1_0.0
79  */
80 class DALI_IMPORT_API Layer : public Actor
81 {
82 public:
83
84   /**
85    * @brief An enumeration of properties belonging to the Layer class.
86    *
87    * Properties additional to Actor.
88    *
89    * @SINCE_1_0.0
90    */
91   struct Property
92   {
93     enum
94     {
95       CLIPPING_ENABLE = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX, ///< name "clippingEnable",   type bool @SINCE_1_0.0
96       CLIPPING_BOX,                                                 ///< name "clippingBox",      type Rect<int> @SINCE_1_0.0
97       BEHAVIOR,                                                     ///< name "behavior",         type String @SINCE_1_0.0
98     };
99   };
100
101   /**
102    * @brief Enumeration for the behavior of the layer.
103    *
104    * Check each value to see how it affects the layer.
105    * @SINCE_1_0.0
106    */
107   enum Behavior
108   {
109     /**
110      * @brief Layer doesn't make use of the depth test (default mode).
111      *
112      * This mode is expected to have better performance than the 3D mode.
113      * When using this mode any ordering would be with respect to tree level of each Actor.
114      *
115      * For the following actor tree of the Layer1 object, D and E hide B, B and C hides A,
116      * and F hides C, regardless of their Z positions.
117      * Rendering order between siblings, such as D & E or B & C, is determined based on the depth index.
118      * If you have two overlapped actors, just make them parent-child, not siblings.
119      *
120      * @code
121      *
122      *     Layer1 (parent)
123      *       |
124      *       A
125      *      / \
126      *     B   C
127      *    / \   \
128      *   D   E   F
129      *
130      * @endcode
131      *
132      * @SINCE_1_0.0
133      */
134     LAYER_2D,
135
136     /**
137      * @brief Layer will use depth test.
138      *
139      * When using this mode depth test will be used. A depth clear will happen for each layer,
140      * which means actors in a layer "above" other layers will be rendered in front of actors in
141      * those layers regardless of their Z positions (see Layer::Raise() and Layer::Lower()).
142      * Opaque renderers are drawn first and write to the depth buffer.
143      * Then transparent renderers are drawn with depth test enabled but depth write switched off.
144      * Transparent renderers are drawn based on their distance from the camera (painter's algorithm).
145      * A renderers DEPTH_INDEX property is used to offset the distance to the camera when ordering transparent renderers.
146      * This is useful if you want to define the draw order of two or more transparent renderers that are
147      * equal distance from the camera.
148      * Unlike LAYER_2D, parent-child relationship does not affect rendering order at all.
149      *
150      * @SINCE_1_0.0
151      */
152     LAYER_3D,
153   };
154
155   /**
156    * @brief TREE_DEPTH_MULTIPLIER is used by the rendering sorting algorithm to decide which actors to render first.
157    * @SINCE_1_0.0
158    */
159   enum TreeDepthMultiplier
160   {
161     TREE_DEPTH_MULTIPLIER = 10000,
162   };
163   /**
164    * @brief The sort function type
165    *
166    * @SINCE_1_0.0
167    * @param[in] position This is the actor translation from camera.
168    */
169   typedef float (*SortFunctionType)( const Vector3& position );
170
171   /**
172    * @brief Create an empty Layer handle.
173    *
174    * This can be initialised with Layer::New(...).
175    * @SINCE_1_0.0
176    */
177   Layer();
178
179   /**
180    * @brief Create a Layer object.
181    *
182    * @SINCE_1_0.0
183    * @return A handle to a newly allocated Layer
184    */
185   static Layer New();
186
187   /**
188    * @brief Downcast a handle to Layer handle.
189    *
190    * If handle points to a Layer the downcast produces valid
191    * handle. If not the returned handle is left uninitialized.
192    * @SINCE_1_0.0
193    * @param[in] handle Handle to An object
194    * @return Handle to a Layer or an uninitialized handle
195    */
196   static Layer DownCast( BaseHandle handle );
197
198   /**
199    * @brief Destructor
200    *
201    * This is non-virtual since derived Handle types must not contain data or virtual methods.
202    * @SINCE_1_0.0
203    */
204   ~Layer();
205
206   /**
207    * @brief Copy constructor
208    *
209    * @SINCE_1_0.0
210    * @param [in] copy The actor to copy
211    */
212   Layer(const Layer& copy);
213
214   /**
215    * @brief Assignment operator
216    *
217    * @SINCE_1_0.0
218    * @param [in] rhs The actor to copy
219    * @return A reference to this
220    */
221   Layer& operator=(const Layer& rhs);
222
223   /**
224    * @brief Query the depth of the layer
225    *
226    * 0 is bottom most layer, higher number is on top.
227    * @SINCE_1_0.0
228    * @return The current depth of the layer
229    * @pre Layer is on the stage.
230    * If layer is not added to the stage, returns 0.
231    */
232   unsigned int GetDepth() const;
233
234   /**
235    * @brief Increment the depth of the layer.
236    *
237    * @SINCE_1_0.0
238    * @pre Layer is on the stage.
239    */
240   void Raise();
241
242   /**
243    * @brief Decrement the depth of the layer.
244    *
245    * @SINCE_1_0.0
246    * @pre Layer is on the stage.
247    */
248   void Lower();
249
250   /**
251    * @brief Ensures the layers depth is greater than the target layer.
252    *
253    * If the layer already is above the target layer its depth is not changed.
254    * If the layer was below target, its new depth will be immediately above target.
255    * @SINCE_1_0.0
256    * @param target Layer to get above of
257    * @pre Layer is on the stage.
258    * @pre Target layer is on the stage.
259    * @note All layers between this layer and target get new depth values.
260    */
261   void RaiseAbove( Layer target );
262
263   /**
264    * @brief Ensures the layers depth is less than the target layer.
265    *
266    * If the layer already is below the target layer its depth is not changed.
267    * If the layer was above target, its new depth will be immediately below target.
268    * @SINCE_1_0.0
269    * @param target Layer to get below of
270    * @pre Layer is on the stage.
271    * @pre Target layer is on the stage.
272    * @note All layers between this layer and target get new depth values.
273    */
274   void LowerBelow( Layer target );
275
276   /**
277    * @brief Raises the layer to the top.
278    * @SINCE_1_0.0
279    * @pre Layer is on the stage.
280    */
281   void RaiseToTop();
282
283   /**
284    * @brief Lowers the layer to the bottom.
285    * @SINCE_1_0.0
286    * @pre layer is on the stage.
287    */
288   void LowerToBottom();
289
290   /**
291    * @brief Moves the layer directly above the given layer.
292    *
293    * After the call this layers depth will be immediately above target.
294    * @SINCE_1_0.0
295    * @param target Layer to get on top of
296    * @pre Layer is on the stage.
297    * @pre Target layer is on the stage.
298    * @note All layers between this layer and target get new depth values.
299    */
300   void MoveAbove( Layer target );
301
302   /**
303    * @brief Moves the layer directly below the given layer.
304    *
305    * After the call this layers depth will be immediately below target.
306    * @SINCE_1_0.0
307    * @param target Layer to get below of
308    * @pre Layer is on the stage.
309    * @pre Target layer is on the stage.
310    * @note All layers between this layer and target get new depth values.
311    */
312   void MoveBelow( Layer target );
313
314   /**
315    * @brief Set the behavior of the layer.
316    *
317    * @SINCE_1_0.0
318    * @param[in] behavior The desired behavior
319    */
320   void SetBehavior( Behavior behavior );
321
322   /**
323    * @brief Get the behavior of the layer.
324    *
325    * @SINCE_1_0.0
326    * @return The behavior of the layer
327    */
328   Behavior GetBehavior() const;
329
330   /**
331    * @brief Sets whether clipping is enabled for a layer.
332    *
333    * Clipping is initially disabled; see also SetClippingBox().
334    * @SINCE_1_0.0
335    * @param [in] enabled True if clipping is enabled.
336    *
337    * @note When clipping is enabled, the default clipping box is empty (0,0,0,0) which means everything is clipped.
338    */
339   void SetClipping(bool enabled);
340
341   /**
342    * @brief Query whether clipping is enabled for a layer.
343    * @SINCE_1_0.0
344    * @return True if clipping is enabled.
345    */
346   bool IsClipping() const;
347
348   /**
349    * @brief Sets the clipping box of a layer, in window coordinates.
350    *
351    * The contents of the layer will not be visible outside this box, when clipping is
352    * enabled. The default clipping box is empty (0,0,0,0) which means everything is clipped.
353    * You can only do rectangular clipping using this API in window coordinates.
354    * For other kinds of clipping, see Dali::Actor::SetDrawMode().
355    * @SINCE_1_0.0
356    * @param [in] x The X-coordinate of the top-left corner of the box
357    * @param [in] y The Y-coordinate of the top-left corner of the box
358    * @param [in] width  The width of the box
359    * @param [in] height The height of the box
360    */
361   void SetClippingBox(int x, int y, int width, int height);
362
363   /**
364    * @brief Sets the clipping box of a layer, in window coordinates.
365    *
366    * The contents of the layer will not be visible outside this box, when clipping is
367    * enabled. The default clipping box is empty (0,0,0,0).
368    * @SINCE_1_0.0
369    * @param [in] box The clipping box
370    */
371   void SetClippingBox(ClippingBox box);
372
373   /**
374    * @brief Retrieves the clipping box of a layer, in window coordinates.
375    *
376    * @SINCE_1_0.0
377    * @return The clipping box
378    */
379   ClippingBox GetClippingBox() const;
380
381   // Depth test
382
383   /**
384    * @brief Whether to disable the depth test.
385    *
386    * By default a layer enables depth test if there is more than one opaque actor or if there is one opaque actor and one, or more, transparent actors in LAYER_3D mode.
387    * However, it's possible to disable the depth test by calling this method.
388    *
389    * @SINCE_1_0.0
390    * @param[in] disable \e True disables depth test. \e false sets the default behavior.
391    */
392   void SetDepthTestDisabled( bool disable );
393
394   /**
395    * @brief Retrieves whether depth test is disabled.
396    *
397    * @SINCE_1_0.0
398    * @return \e True if depth test is disabled.
399    */
400   bool IsDepthTestDisabled() const;
401
402   // Sorting
403
404   /**
405    * @brief This allows the user to specify the sort function that the layer should use.
406    *
407    * The sort function is used to determine the order in which the actors are drawn
408    * and input is processed on the actors in the layer.
409    *
410    * A function of the following type should be used:
411    * @code
412    *  float YourSortFunction(const Vector3& position);
413    * @endcode
414    *
415    * @SINCE_1_0.0
416    * @param[in]  function  The sort function pointer
417    * @note If the sort function returns a low number, the actor with the data will be
418    * drawn in front of an actor whose data yields a high value from the sort function.
419    *
420    * @note All child layers use the same sort function.  If a child layer is added to this
421    * layer then the sort function used by the child layer will also be the same.
422    *
423   */
424   void SetSortFunction( SortFunctionType function );
425
426   /**
427    * @brief This allows the user to specify whether this layer should consume touch (including gestures).
428    *
429    * If set, any layers behind this layer will not be hit-test.
430    *
431    * @SINCE_1_0.0
432    * @param[in]  consume  Whether the layer should consume touch (including gestures).
433    */
434   void SetTouchConsumed( bool consume );
435
436   /**
437    * @brief Retrieves whether the layer consumes touch (including gestures).
438    *
439    * @SINCE_1_0.0
440    * @return True if consuming touch, false otherwise.
441    */
442   bool IsTouchConsumed() const;
443
444   /**
445    * @brief This allows the user to specify whether this layer should consume hover.
446    *
447    * If set, any layers behind this layer will not be hit-test.
448    *
449    * @SINCE_1_0.0
450    * @param[in]  consume  Whether the layer should consume hover.
451    */
452   void SetHoverConsumed( bool consume );
453
454   /**
455    * @brief Retrieves whether the layer consumes hover.
456    *
457    * @SINCE_1_0.0
458    * @return True if consuming hover, false otherwise.
459    */
460   bool IsHoverConsumed() const;
461
462 public: // Not intended for application developers
463
464   /**
465    * @internal
466    * @brief This constructor is used by Layer::New() methods.
467    *
468    * @SINCE_1_0.0
469    * @param [in] Layer A pointer to a newly allocated Dali resource
470    */
471   explicit DALI_INTERNAL Layer(Internal::Layer* Layer);
472 };
473
474 /**
475  * @}
476  */
477 } // namespace Dali
478
479 #endif //__DALI_LAYER_H__