cf4073373f7a5769d0cfddc8647092aaee072b9f
[platform/core/uifw/dali-core.git] / dali / public-api / actors / custom-actor-impl.h
1 #ifndef __DALI_CUSTOM_ACTOR_IMPL_H__
2 #define __DALI_CUSTOM_ACTOR_IMPL_H__
3
4 /*
5  * Copyright (c) 2016 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/property.h>
23 #include <dali/public-api/object/ref-object.h>
24 #include <dali/public-api/actors/actor-enumerations.h>
25 #include <dali/public-api/math/compile-time-math.h>
26
27 namespace Dali
28 {
29 /**
30  * @addtogroup dali_core_actors
31  * @{
32  */
33
34 namespace Internal DALI_INTERNAL
35 {
36 class CustomActor;
37 }
38
39 class Actor;
40 class Animation;
41 class CustomActor;
42 class CustomActorImpl;
43 class RelayoutContainer;
44 struct KeyEvent;
45 struct TouchEvent;
46 struct HoverEvent;
47 struct WheelEvent;
48 struct Vector2;
49 struct Vector3;
50
51 /**
52  * @brief Pointer to Dali::CustomActorImpl object.
53  * @SINCE_1_0.0
54  */
55 typedef IntrusivePtr<CustomActorImpl> CustomActorImplPtr;
56
57 /**
58  * @brief CustomActorImpl is an abstract base class for custom control implementations.
59  *
60  * This provides a series of pure virtual methods, which are called when actor-specific events occur.
61  * And CustomActorImpl is typically owned by a single CustomActor instance; see also CustomActor::CustomActor( CustomActorImpl &implementation ).
62  * @SINCE_1_0.0
63  */
64 class DALI_IMPORT_API CustomActorImpl : public Dali::RefObject
65 {
66 public:
67
68   class Extension; ///< Forward declare future extension interface
69
70 protected:
71   /**
72    * @brief Virtual destructor
73    * @SINCE_1_0.0
74    */
75   virtual ~CustomActorImpl();
76
77 public:
78   /**
79    * @brief Used by derived CustomActorImpl instances, to access the public Actor interface.
80    *
81    * @SINCE_1_0.0
82    * @return A pointer to self, or an uninitialized pointer if the CustomActorImpl is not owned.
83    */
84   CustomActor Self() const;
85
86   /**
87    * @brief Called after the actor has been connected to the stage.
88    *
89    * When an actor is connected, it will be directly or indirectly parented to the root Actor.
90    * @SINCE_1_0.0
91    * @param[in] depth The depth in the hierarchy for the actor
92    *
93    * @note The root Actor is provided automatically by Dali::Stage, and is always considered to be connected.
94    * When the parent of a set of actors is connected to the stage, then all of the children
95    * will received this callback.
96    * For the following actor tree, the callback order will be A, B, D, E, C, and finally F.
97    *
98    * @code
99    *
100    *       A (parent)
101    *      / \
102    *     B   C
103    *    / \   \
104    *   D   E   F
105    *
106    * @endcode
107    * @param[in] depth The depth in the hierarchy for the actor
108    */
109   virtual void OnStageConnection( int depth ) = 0;
110
111   /**
112    * @brief Called after the actor has been disconnected from Stage.
113    *
114    * If an actor is disconnected it either has no parent, or is parented to a disconnected actor.
115    *
116    * @SINCE_1_0.0
117    * @note When the parent of a set of actors is disconnected to the stage, then all of the children
118    * will received this callback, starting with the leaf actors.
119    * For the following actor tree, the callback order will be D, E, B, F, C, and finally A.
120    *
121    * @code
122    *
123    *       A (parent)
124    *      / \
125    *     B   C
126    *    / \   \
127    *   D   E   F
128    *
129    * @endcode
130    */
131   virtual void OnStageDisconnection() = 0;
132
133   /**
134    * @brief Called after a child has been added to the owning actor.
135    *
136    * @SINCE_1_0.0
137    * @param[in] child The child which has been added
138    */
139   virtual void OnChildAdd(Actor& child) = 0;
140
141   /**
142    * @brief Called after the owning actor has attempted to remove a child( regardless of whether it succeeded or not ).
143    *
144    * @SINCE_1_0.0
145    * @param[in] child The child being removed
146    */
147   virtual void OnChildRemove(Actor& child) = 0;
148
149   /**
150    * @brief Called when the owning actor property is set.
151    *
152    * @SINCE_1_0.0
153    * @param[in] index The Property index that was set
154    * @param[in] propertyValue The value to set
155    */
156   virtual void OnPropertySet( Property::Index index, Property::Value propertyValue );
157
158   /**
159    * @brief Called when the owning actor's size is set e.g. using Actor::SetSize().
160    *
161    * @SINCE_1_0.0
162    * @param[in] targetSize The target size. Note that this target size may not match the size returned via @ref Actor::GetTargetSize.
163    */
164   virtual void OnSizeSet(const Vector3& targetSize) = 0;
165
166   /**
167    * @brief Called when the owning actor's size is animated e.g. using Animation::AnimateTo( Property( actor, Actor::Property::SIZE ), ... ).
168    *
169    * @SINCE_1_0.0
170    * @param[in] animation The object which is animating the owning actor.
171    * @param[in] targetSize The target size. Note that this target size may not match the size returned via @ref Actor::GetTargetSize.
172    */
173   virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize) = 0;
174
175   /**
176    * @DEPRECATED_1_1.37 Connect to TouchSignal() instead.
177    *
178    * @brief Called after a touch-event is received by the owning actor.
179    *
180    * @SINCE_1_0.0
181    * @param[in] event The touch event
182    * @return True if the event should be consumed.
183    * @note CustomActorImpl::REQUIRES_TOUCH_EVENTS must be enabled during construction. See CustomActorImpl::CustomActorImpl( ActorFlags flags ).
184    */
185   virtual bool OnTouchEvent(const TouchEvent& event) DALI_DEPRECATED_API = 0;
186
187   /**
188    * @brief Called after a hover-event is received by the owning actor.
189    *
190    * @SINCE_1_0.0
191    * @param[in] event The hover event
192    * @return True if the event should be consumed.
193    * @note CustomActorImpl::REQUIRES_HOVER_EVENTS must be enabled during construction. See CustomActorImpl::CustomActorImpl( ActorFlags flags ).
194    */
195   virtual bool OnHoverEvent(const HoverEvent& event) = 0;
196
197   /**
198    * @brief Called after a key-event is received by the actor that has had its focus set.
199    *
200    * @SINCE_1_0.0
201    * @param[in] event the Key Event
202    * @return True if the event should be consumed.
203    */
204   virtual bool OnKeyEvent(const KeyEvent& event) = 0;
205
206   /**
207    * @brief Called after a wheel-event is received by the owning actor.
208    *
209    * @SINCE_1_0.0
210    * @param[in] event The wheel event
211    * @return True if the event should be consumed.
212    * @note CustomActorImpl::REQUIRES_WHEEL_EVENTS must be enabled during construction. See CustomActorImpl::CustomActorImpl( ActorFlags flags ).
213    */
214   virtual bool OnWheelEvent(const WheelEvent& event) = 0;
215
216   /**
217    * @brief Called after the size negotiation has been finished for this control.
218    *
219    * The control is expected to assign this given size to itself/its children.
220    *
221    * Should be overridden by derived classes if they need to layout
222    * actors differently after certain operations like add or remove
223    * actors, resize or after changing specific properties.
224    *
225    * @SINCE_1_0.0
226    * @param[in]      size       The allocated size.
227    * @param[in,out]  container  The control should add actors to this container that it is not able
228    *                            to allocate a size for.
229    * @note  As this function is called from inside the size negotiation algorithm, you cannot
230    * call RequestRelayout (the call would just be ignored).
231    */
232   virtual void OnRelayout( const Vector2& size, RelayoutContainer& container ) = 0;
233
234   /**
235    * @brief Notification for deriving classes
236    *
237    * @SINCE_1_0.0
238    * @param[in] policy The policy being set
239    * @param[in] dimension The dimension the policy is being set for
240    */
241   virtual void OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) = 0;
242
243   /**
244    * @brief Return the natural size of the actor.
245    *
246    * @SINCE_1_0.0
247    * @return The actor's natural size
248    */
249   virtual Vector3 GetNaturalSize() = 0;
250
251   /**
252    * @brief Calculate the size for a child.
253    *
254    * @SINCE_1_0.0
255    * @param[in] child The child actor to calculate the size for
256    * @param[in] dimension The dimension to calculate the size for. E.g. width or height.
257    * @return Return the calculated size for the given dimension.
258    */
259   virtual float CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension ) = 0;
260
261   /**
262    * @brief This method is called during size negotiation when a height is required for a given width.
263    *
264    * Derived classes should override this if they wish to customize the height returned.
265    *
266    * @SINCE_1_0.0
267    * @param width Width to use.
268    * @return The height based on the width.
269    */
270   virtual float GetHeightForWidth( float width ) = 0;
271
272   /**
273    * @brief This method is called during size negotiation when a width is required for a given height.
274    *
275    * Derived classes should override this if they wish to customize the width returned.
276    *
277    * @SINCE_1_0.0
278    * @param height Height to use.
279    * @return The width based on the width.
280    */
281   virtual float GetWidthForHeight( float height ) = 0;
282
283   /**
284    * @brief Determine if this actor is dependent on it's children for relayout.
285    *
286    * @SINCE_1_0.0
287    * @param dimension The dimension(s) to check for
288    * @return Return if the actor is dependent on it's children.
289    */
290   virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS ) = 0;
291
292   /**
293    * @brief Virtual method to notify deriving classes that relayout dependencies have been
294    * met and the size for this object is about to be calculated for the given dimension
295    *
296    * @SINCE_1_0.0
297    * @param dimension The dimension that is about to be calculated
298    */
299   virtual void OnCalculateRelayoutSize( Dimension::Type dimension ) = 0;
300
301   /**
302    * @brief Virtual method to notify deriving classes that the size for a dimension
303    * has just been negotiated
304    *
305    * @SINCE_1_0.0
306    * @param[in] size The new size for the given dimension
307    * @param[in] dimension The dimension that was just negotiated
308    */
309   virtual void OnLayoutNegotiated( float size, Dimension::Type dimension ) = 0;
310
311   /**
312    * @brief Retrieve the extension for this control.
313    *
314    * @SINCE_1_0.0
315    * @return The extension if available, NULL otherwise
316    */
317   virtual Extension* GetExtension()
318   {
319     return NULL;
320   }
321
322 protected: // For derived classes
323
324   /**
325    * @brief Flags for the constructor
326    * @SINCE_1_0.0
327    */
328   enum ActorFlags
329   {
330     ACTOR_BEHAVIOUR_NONE          = 0,          ///< Use if no change to default behaviour is needed. @DEPRECATED_1_2_10
331     ACTOR_BEHAVIOUR_DEFAULT       = 0,          ///< Use to provide default behaviour (size negotiation is on, event callbacks are not called). @SINCE_1_2_10
332     DISABLE_SIZE_NEGOTIATION      = 1 << 0,     ///< True if control does not need size negotiation, i.e. it can be skipped in the algorithm @SINCE_1_0.0
333     REQUIRES_TOUCH_EVENTS         = 1 << 1,     ///< True if the OnTouchEvent() callback is required. @SINCE_1_0.0
334     REQUIRES_HOVER_EVENTS         = 1 << 2,     ///< True if the OnHoverEvent() callback is required. @SINCE_1_0.0
335     REQUIRES_WHEEL_EVENTS   = 1 << 3,     ///< True if the OnWheelEvent() callback is required. @SINCE_1_0.0
336
337     LAST_ACTOR_FLAG                             ///< Special marker for last actor flag @SINCE_1_0.0
338   };
339
340   static const int ACTOR_FLAG_COUNT = Log< LAST_ACTOR_FLAG - 1 >::value + 1;      ///< Value for deriving classes to continue on the flag enum
341
342   /**
343    * @brief Create a CustomActorImpl.
344    * @SINCE_1_0.0
345    * @param[in] flags Bitfield of ActorFlags to define behaviour
346    */
347   CustomActorImpl( ActorFlags flags );
348
349   // Size negotiation helpers
350
351   /**
352    * @brief Request a relayout, which means performing a size negotiation on this actor, its parent and children (and potentially whole scene).
353    *
354    * This method can also be called from a derived class every time it needs a different size.
355    * At the end of event processing, the relayout process starts and
356    * all controls which requested Relayout will have their sizes (re)negotiated.
357    *
358    * @SINCE_1_0.0
359    * @note RelayoutRequest() can be called multiple times; the size negotiation is still
360    * only performed once, i.e. there is no need to keep track of this in the calling side.
361    */
362   void RelayoutRequest();
363
364   /**
365    * @brief Provides the Actor implementation of GetHeightForWidth.
366    * @SINCE_1_0.0
367    * @param width Width to use.
368    * @return The height based on the width.
369    */
370   float GetHeightForWidthBase( float width );
371
372   /**
373    * @brief Provides the Actor implementation of GetWidthForHeight.
374    * @SINCE_1_0.0
375    * @param height Height to use.
376    * @return The width based on the height.
377    */
378   float GetWidthForHeightBase( float height );
379
380   /**
381    * @brief Calculate the size for a child using the base actor object.
382    *
383    * @SINCE_1_0.0
384    * @param[in] child The child actor to calculate the size for
385    * @param[in] dimension The dimension to calculate the size for. E.g. width or height
386    * @return Return the calculated size for the given dimension. If more than one dimension is requested, just return the first one found.
387    */
388   float CalculateChildSizeBase( const Dali::Actor& child, Dimension::Type dimension );
389
390   /**
391    * @brief Determine if this actor is dependent on it's children for relayout from the base class.
392    *
393    * @SINCE_1_0.0
394    * @param dimension The dimension(s) to check for
395    * @return Return if the actor is dependent on it's children.
396    */
397   bool RelayoutDependentOnChildrenBase( Dimension::Type dimension = Dimension::ALL_DIMENSIONS );
398
399 public: // Not intended for application developers
400
401   /**
402    * @brief Initialize a CustomActor.
403    * @SINCE_1_0.0
404    * @param[in] owner The owning object
405    * @pre The CustomActorImpl is not already owned.
406    * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
407    */
408   void Initialize(Internal::CustomActor& owner);
409
410   /**
411    * @brief Get the owner.
412    *
413    * This method is needed when creating additional handle objects to existing objects.
414    * Owner is the Dali::Internal::CustomActor that owns the implementation of the custom actor
415    * inside core. Creation of a handle to Dali public API Actor requires this pointer.
416    * @SINCE_1_0.0
417    * @return A pointer to the Actor implementation that owns this custom actor implementation
418    */
419   Internal::CustomActor* GetOwner() const;
420
421   /**
422    * @brief Returns whether the OnTouchEvent() callback is required.
423    * @SINCE_1_0.0
424    * @return True if the OnTouchEvent() callback is required.
425    * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
426    */
427   bool RequiresTouchEvents() const;
428
429   /**
430    * @brief Returns whether the OnHoverEvent() callback is required.
431    * @SINCE_1_0.0
432    * @return True if the OnHoverEvent() callback is required.
433    * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
434    */
435   bool RequiresHoverEvents() const;
436
437   /**
438    * @brief Returns whether the OnWheelEvent() callback is required.
439    * @SINCE_1_0.0
440    * @return True if the OnWheelEvent() callback is required.
441    * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
442    */
443   bool RequiresWheelEvents() const;
444
445   /**
446    * @brief Returns whether relayout is enabled.
447    * @SINCE_1_0.0
448    * @return Return true if relayout is enabled on the custom actor.
449    * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
450    */
451   bool IsRelayoutEnabled() const;
452
453 private:
454
455   Internal::CustomActor* mOwner;        ///< Internal owner of this custom actor implementation
456   ActorFlags mFlags :ACTOR_FLAG_COUNT;  ///< ActorFlags flags to determine behaviour
457 };
458
459 /**
460  * @}
461  */
462 } // namespace Dali
463
464 #endif // __DALI_CUSTOM_ACTOR_IMPL_H__