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