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