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