6 # Creating Custom UI Controls {#creating-custom-controls}
8 DALi provides the ability to create custom UI controls.
9 This can be done by extending Dali::Toolkit::Control and Dali::Toolkit::Internal::Control classes.
11 Custom controls are created using the [handle/body idiom](@ref handle-body-idiom) used in DALi.
13 ![ ](../assets/img/creating-custom-controls/control-handle-body.png)
14 ![ ](creating-custom-controls/control-handle-body.png)
16 Namespaces are important
17 + The handle & body classes should have the same name but in different namespaces
18 + TypeRegistry relies on this convention
19 + Here our custom control requires
23 ### General Guidelines:
24 + Try to avoid adding C++ APIs as they become difficult to maintain.
25 + Use **properties** as much as possible as Controls should be data driven.
26 + These controls will be used through JavaScript and JSON files so need to be compatible.
27 + Bear in mind that the Control can be updated when the properties change (e.g. style change)
28 + Ensure control deals with these property changes gracefully
29 + Not just the first time they are set
30 + Use Visuals rather than creating several child Actors
31 + DALi rendering pipeline more efficient
32 + Accessibility actions should be considered when designing the Control.
33 + Consider using signals if the application needs to be react to changes in the control state.
34 + Use of Gestures should be preferred over analysing raw touch events
35 + Check if you need to chain up to base class if overriding certain methods
37 ___________________________________________________________________________________________________
39 ## Rendering Content {#creating-controls-rendering-content}
41 To render content, the required actors can be created and added to the control itself as its children.
42 However, this solution is not fully optimised and means extra actors will be added, and thus, need to be processed by DALi.
44 Controls should be as generic as possible so the recommendation is to re-use visuals to create the content required as described in the [Visuals](@ref visuals) section.
45 Currently, this is devel-api though, so is subject to change.
47 ![ ](../assets/img/creating-custom-controls/rendering.png)
48 ![ ](creating-custom-controls/rendering.png)
50 ___________________________________________________________________________________________________
52 ## Ensuring Control is Stylable {#creating-controls-stylable}
54 DALi's property system allows custom controls to be easily styled.
55 The [JSON Syntax](@ref script-json-specification) is used in the stylesheets:
57 **JSON Styling Syntax Example:**
65 "primaryCursorColor":[0.0,0.72,0.9,1.0],
66 "secondaryCursorColor":[0.0,0.72,0.9,1.0],
68 "selectionHighlightColor":[0.75,0.96,1.0,1.0],
69 "grabHandleImage" : "{DALI_STYLE_IMAGE_DIR}cursor_handler_drop_center.png",
70 "selectionHandleImageLeft" : {"filename":"{DALI_STYLE_IMAGE_DIR}selection_handle_drop_left.png" },
71 "selectionHandleImageRight": {"filename":"{DALI_STYLE_IMAGE_DIR}selection_handle_drop_right.png" }
77 Styling gives the UI designer the ability to change the look and feel of the control without any code changes.
79 | Normal Style | Customized Style |
80 |:------------:|:----------------:|
81 |![ ](../assets/img/creating-custom-controls/popup-normal.png) ![ ](creating-custom-controls/popup-normal.png) | ![ ](../assets/img/creating-custom-controls/popup-styled.png) ![ ](creating-custom-controls/popup-styled.png)|
83 More information regarding styling can be found in the [Styling](@ref styling) section.
84 ___________________________________________________________________________________________________
86 ### Type Registration {#creating-controls-type-registration}
88 The TypeRegistry is used to register your custom control.
89 This allows the creation of the control via a JSON file, as well as registering properties, signals and actions.
91 To ensure your control is stylable, the process described in [Type Registration](@ref type-registration) should be followed.
92 To aid development, some macros are provided for registering properties which are described in the [Property](@ref properties) section.
94 Control properties can be one of three types:
95 + **Event-side only:** A function is called to set this property or to retrieve the value of this property.
96 Usually, the value is stored as a member parameter of the Impl class.
97 Other operations can also be done, as required, in this called function.
98 + **Animatable Properties:** These are double-buffered properties that can be animated.
99 + **Custom Properties:** These are dynamic properties that are created for every single instance of the control.
100 Therefore, these tend to take a lot of memory and are usually used by applications or other controls to dynamically set certain attributes on their children.
101 The index for these properties can also be different for every instance.
103 Careful consideration must be taken when choosing which property type to use for the properties of the custom control.
104 For example, an Animatable property type can be animated but requires a lot more resources (both in its execution and memory footprint) compared to an event-side only property.
105 ___________________________________________________________________________________________________
107 ## Control Services {#creating-controls-control-services}
109 ### Initialization {#creating-controls-init}
111 Controls are initialized in two steps: in the constructor, and then in the Initialize() method.
112 This is so that a handle/body connection is made within DALi Core.
113 See Dali::CustomActor & Dali::CustomActorImpl for more information.
115 It is recommended to do provide a New() method in the custom control implementation where the Initialize() method should be called.
119 MyUIControl MyUIControlImpl::New()
121 // Create the implementation, temporarily owned on stack
122 IntrusivePtr< MyUIControlImpl > controlImpl = new MyUIControlImpl;
124 // Pass ownership to handle
125 MyUIControl handle( *controlImpl );
127 // Second-phase init of the implementation
128 controlImpl->Initialize();
133 Another advantage of using a New() method is that the constructor for MyUIControl can be made private (or protected).
135 This will trigger the Dali::Toolkit::Internal::Control Initialize() method which will in-turn, call the virtual method OnInitialize().
136 This should be overridden by the custom ui control.
139 void MyUIControlImpl::OnInitialize()
141 // Create visuals using the VisualFactory, register events etc.
142 // Register any created visuals with Control base class
145 ___________________________________________________________________________________________________
147 ### Control Behaviour {#creating-controls-behaviour}
149 Dali::Toolkit::Internal::Control provides several behaviours which are specified through its constructor (@ref Dali::Toolkit::Internal::Control::Control()).
151 | Behaviour | Description |
152 |--------------------------------------|----------------------------------------------------------------------------------------------------------------|
153 | CONTROL_BEHAVIOUR_DEFAULT | Default behavior (size negotiation is on, style change is monitored, event callbacks are not called. |
154 | DISABLE_SIZE_NEGOTIATION | If our control does not need size negotiation, i.e. control will be skipped by the size negotiation algorithm. |
155 | REQUIRES_HOVER_EVENTS | If our control requires [hover events](@ref creating-controls-events). |
156 | REQUIRES_WHEEL_EVENTS | If our control requires [wheel events](@ref creating-controls-events). |
157 | DISABLE_STYLE_CHANGE_SIGNALS | True if control should not monitor style change signals such as Theme/Font change. |
158 | REQUIRES_KEYBOARD_NAVIGATION_SUPPORT | True if need to support keyboard navigation. |
159 ___________________________________________________________________________________________________
161 ### Touch, Hover & Wheel Events {#creating-controls-events}
163 + A **touch** is when any touch occurs within the bounds of the custom actor. Connect to Dali::Actor::TouchSignal().
164 + A **hover event** is when a pointer moves within the bounds of a custom actor (e.g. mouse pointer or hover pointer).
165 + A **wheel event** is when the mouse wheel (or similar) is moved while hovering over an actor (via a mouse pointer or hover pointer).
167 If the control needs to utilize hover and wheel events, then the correct behaviour flag should be used when constructing the control and then the appropriate method should be overridden.
170 bool MyUIControlImpl::OnHoverEvent( const HoverEvent& event )
172 bool consumed = false;
174 // Handle hover event
176 // Return true if handled/consumed, false otherwise
182 bool MyUIControlImpl::OnWheelEvent( const WheelEvent& event )
184 bool consumed = false;
186 // Handle wheel event
188 // Return true if handled/consumed, false otherwise
192 ___________________________________________________________________________________________________
194 ### Gestures {#creating-controls-gestures}
196 DALi has a gesture system which analyses a stream of touch events and attempts to determine the intention of the user.
197 The following gesture detectors are provided:
199 + **Pan:** When the user starts panning (or dragging) one or more fingers.
200 The panning should start from within the bounds of the control.
201 + **Pinch:** Detects when two touch points move towards or away from each other.
202 The center point of the pinch should be within the bounds of the control.
203 + **Tap:** When the user taps within the bounds of the control.
204 + **LongPress:** When the user presses and holds on a certain point within the bounds of a control.
206 The control base class provides basic set up to detect these gestures.
207 If any of these detectors are required then this can be specified in the OnInitialize() method (or as required).
211 void MyUIControlImpl::OnInitialize()
213 // Only enable pan gesture detection
214 EnableGestureDetection( Gesture::Pan );
216 // Or if several gestures are required
217 EnableGestureDetection( Gesture::Type( Gesture::Pinch | Gesture::Tap | Gesture::LongPress ) );
221 The above snippet of code will only enable the default gesture detection for each type.
222 If customization of the gesture detection is required, then the gesture-detector can be retrieved and set up accordingly in the same method.
226 PanGestureDetector panGestureDetector = GetPanGestureDetector();
227 panGestureDetector.AddDirection( PanGestureDetector::DIRECTION_VERTICAL );
230 Finally, the appropriate method should be overridden:
233 void MyUIControlImpl::OnPan( const PanGesture& pan )
235 // Handle pan-gesture
240 void MyUIControlImpl::OnPinch( const PinchGesture& pinch )
242 // Handle pinch-event
247 void MyUIControlImpl::OnTap( const TapGesture& tap )
249 // Handle tap-gesture
254 void MyUIControlImpl::OnLongPress( const LongPressGesture& longPress )
256 // Handle long-press-gesture
260 ___________________________________________________________________________________________________
262 ### Accessibility {#creating-controls-accessibility}
264 Accessibility is functionality that has been designed to aid usage by the visually impaired.
265 More information can be found in the [Accessibility](@ref accessibility) section.
267 Accessibility behaviour can be customized in the control by overriding certain virtual methods.
268 This is detailed [here](@ref accessibilitycustomcontrol).
270 ___________________________________________________________________________________________________
272 ### Signals {#creating-controls-signals}
274 If applications need to react to changes in the control state, controls can inform those applications using Dali::Signal.
276 First, create a signature of the function the signal will call in the handle header file:
278 // C++: my-ui-control.h
279 typedef Signal< void () > SignalType;
282 Then Create methods to get to the signal:
284 // C++: my-ui-control.h
285 MyUIControl::SignalType& MyCustomSignal();
288 The source file should just call the impl:
290 // C++: my-ui-control.cpp
291 MyUIControl::SignalType& MyUIControl::MyCustomSignal()
293 return Dali::Toolkit::GetImplementation( *this ).MyCustomSignal();
297 In the impl file, create an instance of the signal as follows and return it in the appropriate method:
299 // C++: my-ui-control-impl.h
302 MyUIControl::SignalType MyUIControl::MyCustomSignal()
304 return mMyCustomSignal;
309 MyUIControl::SignalType mMyCustomSignal;
312 Then, when you wish to emit this signal:
314 // C++: my-ui-control-impl.cpp
315 mMyCustomSignal.Emit();
317 There is no need to check if there is anything connected to this signal as this is done by the framework.
319 The application can then connect to the signal as follows:
328 customControl.MyCustomSignal.Connect( this, &AppFunction );
331 ___________________________________________________________________________________________________
333 ### Children Added/Removed {#creating-controls-children}
335 Methods are provided that can be overridden if notification is required when a child is added or removed from our control.
336 An up call to the Control class is necessary if these methods are overridden.
340 void MyUIControlImpl::OnChildAdd( Actor& child );
342 // Up call to Control first
343 Control::OnChildAdd( child );
345 // Do any other operations required upon child addition
350 void MyUIControlImpl::OnChildRemove( Actor& child );
352 // Do any other operations required upon child removal
354 // Up call to Control at the end
355 Control::OnChildRemove( child );
359 Avoid adding or removing the child again within these methods.
361 ___________________________________________________________________________________________________
363 ### Stage Connection {#creating-controls-stage}
365 Methods are provided that can be overridden if notification is required when our control is connected to or disconnected from the stage.
366 An up call to the Control class is necessary if these methods are overridden.
370 void MyUIControlImpl::OnStageConnection( int depth )
372 // Up call to Control first
373 Control::OnStageConnection( depth );
375 // Do any other operations required upon stage connection
380 void MyUIControlImpl::OnStageDisconnection()
382 // Do any other operations required upon stage disconnection
384 // Up call to Control at the end
385 Control::OnStageDisconnection();
389 ___________________________________________________________________________________________________
391 ### Size Negotiation {#creating-controls-size-negotiation}
393 The following methods must be overridden for size negotiation to work correctly with a custom control.
397 Vector3 MyUIControlImpl::GetNaturalSize()
399 // Return the natural size of the control
400 // This depends on our layout
401 // If we have one visual, then we can return the natural size of that
402 // If we have more visuals, then we need to calculate their positions within our control and work out the overall size we would like our control to be
404 // After working out the natural size of visuals that belong to this control,
405 // should also chain up to ensure other visuals belonging to the base class are
406 // also taken into account:
407 Vector2 baseSize = Control::GetNaturalSize(); // returns the size of the background.
412 float MyUIControlImpl::GetHeightForWidth( float width )
414 // Called by the size negotiation algorithm if we have a fixed width
415 // We should calculate the height we would like our control to be for that width
417 // Should also chain up to determine the base class's preferred height:
418 float baseHeight = Control::GetHeightForWidth( width );
424 float MyUIControlImpl::GetWidthForHeight( float height )
426 // Called by the size negotiation algorithm if we have a fixed height
427 // We should calculate the width we would like our control to be for that height
429 // Should also chain up to determine the base class's preferred width:
430 float baseWidth = Control::GetWidth( height );
435 void MyUIControlImpl::OnRelayout( const Vector2& size, RelayoutContainer& container )
437 // The size is what we have been given and what our control needs to fit into
438 // Here, we need to set the position and the size of our visuals
439 // If we have other controls/actors as children
440 // - Add the control/actor to the container paired with the size required
441 // - To ensure this works, you need to set up the control with a relayout policy of USE_ASSIGNED_SIZE
442 // - DO NOT CALL SetSize on this control: This will trigger another size negotiation calculation
443 // DO NOT chain up to base class.
446 More information on size negotiation can be found [here](@ref size-negotiation-controls).
448 ___________________________________________________________________________________________________
450 ### Other Features {#creating-controls-other}
452 + [Background](@ref background)
454 ___________________________________________________________________________________________________
456 @class _Guide_Creating_UI_Controls