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