1 #ifndef DALI_CUSTOM_ACTOR_IMPL_H
2 #define DALI_CUSTOM_ACTOR_IMPL_H
5 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <cstdint> // uint32_t
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>
33 * @addtogroup dali_core_actors
37 namespace Internal DALI_INTERNAL
45 class CustomActorImpl;
46 class RelayoutContainer;
54 * @brief Pointer to Dali::CustomActorImpl object.
57 typedef IntrusivePtr<CustomActorImpl> CustomActorImplPtr;
60 * @brief CustomActorImpl is an abstract base class for custom control implementations.
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 ).
66 class DALI_CORE_API CustomActorImpl : public Dali::RefObject
70 class Extension; ///< Forward declare future extension interface
74 * @brief Virtual destructor
77 virtual ~CustomActorImpl();
81 * @brief Used by derived CustomActorImpl instances, to access the public Actor interface.
84 * @return A pointer to self, or an uninitialized pointer if the CustomActorImpl is not owned.
86 CustomActor Self() const;
89 * @brief Called after the actor has been connected to the Scene.
91 * When an actor is connected, it will be directly or indirectly parented to the root Actor.
93 * @param[in] depth The depth in the hierarchy for the actor
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.
109 * @param[in] depth The depth in the hierarchy for the actor
111 virtual void OnSceneConnection( int32_t depth ) = 0;
114 * @brief Called after the actor has been disconnected from the Scene.
116 * If an actor is disconnected, it either has no parent or is parented to a disconnected actor.
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.
133 virtual void OnSceneDisconnection() = 0;
136 * @brief Called after a child has been added to the owning actor.
139 * @param[in] child The child which has been added
141 virtual void OnChildAdd(Actor& child) = 0;
144 * @brief Called after the owning actor has attempted to remove a child (regardless of whether it succeeded or not).
147 * @param[in] child The child being removed
149 virtual void OnChildRemove(Actor& child) = 0;
152 * @brief Called when the owning actor property is set.
155 * @param[in] index The Property index that was set
156 * @param[in] propertyValue The value to set
158 virtual void OnPropertySet( Property::Index index, Property::Value propertyValue );
161 * @brief Called when the owning actor's size is set e.g. using Actor::SetSize().
164 * @param[in] targetSize The target size. Note that this target size may not match the size returned via @ref Actor::GetTargetSize
166 virtual void OnSizeSet(const Vector3& targetSize) = 0;
169 * @brief Called when the owning actor's size is animated e.g. using Animation::AnimateTo( Property( actor, Actor::Property::SIZE ), ... ).
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
175 virtual void OnSizeAnimation(Animation& animation, const Vector3& targetSize) = 0;
178 * @brief Called after a hover-event is received by the owning actor.
181 * @param[in] event The hover event
182 * @return True if the event should be consumed
183 * @note CustomActorImpl::REQUIRES_HOVER_EVENTS must be enabled during construction. See CustomActorImpl::CustomActorImpl( ActorFlags flags ).
185 virtual bool OnHoverEvent(const HoverEvent& event) = 0;
188 * @brief Called after a key-event is received by the actor that has had its focus set.
191 * @param[in] event The Key Event
192 * @return True if the event should be consumed
194 virtual bool OnKeyEvent(const KeyEvent& event) = 0;
197 * @brief Called after a wheel-event is received by the owning actor.
200 * @param[in] event The wheel event
201 * @return True if the event should be consumed
202 * @note CustomActorImpl::REQUIRES_WHEEL_EVENTS must be enabled during construction. See CustomActorImpl::CustomActorImpl( ActorFlags flags ).
204 virtual bool OnWheelEvent(const WheelEvent& event) = 0;
207 * @brief Called after the size negotiation has been finished for this control.
209 * The control is expected to assign this given size to itself/its children.
211 * Should be overridden by derived classes if they need to layout
212 * actors differently after certain operations like add or remove
213 * actors, resize or after changing specific properties.
216 * @param[in] size The allocated size
217 * @param[in,out] container The control should add actors to this container that it is not able
218 * to allocate a size for
219 * @note As this function is called from inside the size negotiation algorithm, you cannot
220 * call RequestRelayout (the call would just be ignored).
222 virtual void OnRelayout( const Vector2& size, RelayoutContainer& container ) = 0;
225 * @brief Notification for deriving classes.
228 * @param[in] policy The policy being set
229 * @param[in] dimension The dimension the policy is being set for
231 virtual void OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dimension ) = 0;
234 * @brief Returns the natural size of the actor.
237 * @return The actor's natural size
239 virtual Vector3 GetNaturalSize() = 0;
242 * @brief Calculates the size for a child.
245 * @param[in] child The child actor to calculate the size for
246 * @param[in] dimension The dimension to calculate the size for. E.g. width or height
247 * @return Return the calculated size for the given dimension
249 virtual float CalculateChildSize( const Dali::Actor& child, Dimension::Type dimension ) = 0;
252 * @brief This method is called during size negotiation when a height is required for a given width.
254 * Derived classes should override this if they wish to customize the height returned.
257 * @param[in] width Width to use
258 * @return The height based on the width
260 virtual float GetHeightForWidth( float width ) = 0;
263 * @brief This method is called during size negotiation when a width is required for a given height.
265 * Derived classes should override this if they wish to customize the width returned.
268 * @param[in] height Height to use
269 * @return The width based on the width
271 virtual float GetWidthForHeight( float height ) = 0;
274 * @brief Determines if this actor is dependent on its children for relayout.
277 * @param[in] dimension The dimension(s) to check for
278 * @return Return if the actor is dependent on it's children
280 virtual bool RelayoutDependentOnChildren( Dimension::Type dimension = Dimension::ALL_DIMENSIONS ) = 0;
283 * @brief Virtual method to notify deriving classes that relayout dependencies have been
284 * met and the size for this object is about to be calculated for the given dimension.
287 * @param[in] dimension The dimension that is about to be calculated
289 virtual void OnCalculateRelayoutSize( Dimension::Type dimension ) = 0;
292 * @brief Virtual method to notify deriving classes that the size for a dimension
293 * has just been negotiated.
296 * @param[in] size The new size for the given dimension
297 * @param[in] dimension The dimension that was just negotiated
299 virtual void OnLayoutNegotiated( float size, Dimension::Type dimension ) = 0;
302 * @brief Retrieves the extension for this control.
305 * @return The extension if available, NULL otherwise
307 virtual Extension* GetExtension()
312 protected: // For derived classes
315 * @brief Enumeration for the constructor flags.
320 ACTOR_BEHAVIOUR_DEFAULT = 0, ///< Use to provide default behaviour (size negotiation is on, event callbacks are not called). @SINCE_1_2_10
321 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
322 REQUIRES_TOUCH_EVENTS = 1 << 1, ///< True if the OnTouchEvent() callback is required. @SINCE_1_0.0
323 REQUIRES_HOVER_EVENTS = 1 << 2, ///< True if the OnHoverEvent() callback is required. @SINCE_1_0.0
324 REQUIRES_WHEEL_EVENTS = 1 << 3, ///< True if the OnWheelEvent() callback is required. @SINCE_1_0.0
326 LAST_ACTOR_FLAG ///< Special marker for last actor flag @SINCE_1_0.0
329 static const int32_t ACTOR_FLAG_COUNT = Log< LAST_ACTOR_FLAG - 1 >::value + 1; ///< Value for deriving classes to continue on the flag enum
332 * @brief Creates a CustomActorImpl.
334 * @param[in] flags Bitfield of ActorFlags to define behaviour
336 CustomActorImpl( ActorFlags flags );
338 // Size negotiation helpers
341 * @brief Requests a relayout, which means performing a size negotiation on this actor, its parent and children (and potentially whole scene).
343 * This method can also be called from a derived class every time it needs a different size.
344 * At the end of event processing, the relayout process starts and
345 * all controls which requested Relayout will have their sizes (re)negotiated.
348 * @note RelayoutRequest() can be called multiple times; the size negotiation is still
349 * only performed once, i.e. there is no need to keep track of this in the calling side.
351 void RelayoutRequest();
354 * @brief Provides the Actor implementation of GetHeightForWidth.
356 * @param[in] width Width to use
357 * @return The height based on the width
359 float GetHeightForWidthBase( float width );
362 * @brief Provides the Actor implementation of GetWidthForHeight.
364 * @param[in] height Height to use
365 * @return The width based on the height
367 float GetWidthForHeightBase( float height );
370 * @brief Calculates the size for a child using the base actor object.
373 * @param[in] child The child actor to calculate the size for
374 * @param[in] dimension The dimension to calculate the size for. E.g. width or height
375 * @return Return the calculated size for the given dimension. If more than one dimension is requested, just return the first one found
377 float CalculateChildSizeBase( const Dali::Actor& child, Dimension::Type dimension );
380 * @brief Determines if this actor is dependent on its children for relayout from the base class.
383 * @param[in] dimension The dimension(s) to check for
384 * @return Return if the actor is dependent on it's children
386 bool RelayoutDependentOnChildrenBase( Dimension::Type dimension = Dimension::ALL_DIMENSIONS );
388 public: // Not intended for application developers
391 * @brief Initializes a CustomActor.
393 * @param[in] owner The owning object
394 * @pre The CustomActorImpl is not already owned.
395 * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
397 void Initialize(Internal::CustomActor& owner);
400 * @brief Gets the owner.
402 * This method is needed when creating additional handle objects to existing objects.
403 * Owner is the Dali::Internal::CustomActor that owns the implementation of the custom actor
404 * inside core. Creation of a handle to Dali public API Actor requires this pointer.
406 * @return A pointer to the Actor implementation that owns this custom actor implementation
408 Internal::CustomActor* GetOwner() const;
411 * @brief Returns whether the OnTouchEvent() callback is required.
413 * @return True if the OnTouchEvent() callback is required
414 * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
416 bool RequiresTouchEvents() const;
419 * @brief Returns whether the OnHoverEvent() callback is required.
421 * @return True if the OnHoverEvent() callback is required
422 * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
424 bool RequiresHoverEvents() const;
427 * @brief Returns whether the OnWheelEvent() callback is required.
429 * @return True if the OnWheelEvent() callback is required
430 * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
432 bool RequiresWheelEvents() const;
435 * @brief Returns whether relayout is enabled.
437 * @return Return true if relayout is enabled on the custom actor
438 * @note Called when ownership of the CustomActorImpl is passed to a CustomActor.
440 bool IsRelayoutEnabled() const;
444 Internal::CustomActor* mOwner; ///< Internal owner of this custom actor implementation
445 ActorFlags mFlags :ACTOR_FLAG_COUNT; ///< ActorFlags flags to determine behaviour
453 #endif // DALI_CUSTOM_ACTOR_IMPL_H