Fixed signals for toggle button
[platform/core/uifw/dali-toolkit.git] / docs / content / programming-guide / properties.h
1 /*! \page properties Properties
2  *
3 @section what-is-a-property What is a property?
4
5 A property is a value used by an object that can be modified or read externally to that object.
6 This could be from within DALi or externally by an application.
7
8 <h2 class="pg">What is a property used for?</h2>
9
10 Properties can be set externally by an application, allowing that application to change the configuration or behaviour of an actor.
11 This could include the physical geometry of the actor, or how it is drawn or moves.
12
13 Properties can also be read. This feature can be used in conjunction with constraints to allow changes to a property within one actor to cause changes to the property of another actor. For example, an actor following the movement of another separate actor (that it is not a child of). 
14
15 Properties can be used to expose any useful information or behaviour of an actor.
16 Other actor variables that are used to implement this bevahiour, or do not make useful sense from an application developers point of view should not be exposed.
17
18 <h2 class="pg">How to implement a property within Dali-core:</h2>
19
20 <b>There are two stages:</b>
21
22 - Define the properties as an enum in the public-api header file.
23 - Define the property details using the pre-defined macros to build up a table of property information.
24
25 There are some pre-defined macros designed to help with and standardise the definition of the propery details table per class.
26
27 These macros generate an array of property details which allow efficient lookup of flags like "animatable" or "constraint input".
28
29 <b>Example: ImageActor</b>
30
31 Within the public-api header file; image-actor.h:
32
33 @code
34 /**
35  * @brief An enumeration of properties belonging to the ImageActor class.
36  * Properties additional to RenderableActor.
37  */
38 struct Property
39 {
40   enum
41   {
42     PIXEL_AREA = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX, ///< name "pixel-area",  type Rect<int>
43     STYLE,                                                   ///< name "style",       type std::string
44     BORDER,                                                  ///< name "border",      type Vector4
45     IMAGE,                                                   ///< name "image",       type Map {"filename":"", "load-policy":...}
46   };
47 };
48 @endcode
49 From @ref Dali::ImageActor::Property
50
51 <b>Notes:</b>
52
53 - The properties are enumerated within a named struct to give them a namespace.
54 - The properties are then refered to as &lt;OBJECT&gt;::%Property::&lt;PROPERTY_NAME&gt;.
55
56 Within the internal implementation; <b>image-actor-impl.cpp</b>:
57
58 @code
59 namespace // Unnamed namespace
60 {
61
62 // Properties
63
64 //              Name           Type   writable animatable constraint-input  enum for index-checking
65 DALI_PROPERTY_TABLE_BEGIN
66 DALI_PROPERTY( "pixel-area",   RECTANGLE, true,    false,   true,    Dali::ImageActor::Property::PIXEL_AREA )
67 DALI_PROPERTY( "style",        STRING,    true,    false,   true,    Dali::ImageActor::Property::STYLE      )
68 DALI_PROPERTY( "border",       VECTOR4,   true,    false,   true,    Dali::ImageActor::Property::BORDER     )
69 DALI_PROPERTY( "image",        MAP,       true,    false,   false,   Dali::ImageActor::Property::IMAGE      )
70 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
71 @endcode
72
73 <b>Notes:</b>
74
75 - The table lays within an unnamed namespace.
76 - The table should be in the same order as the enum.
77 - The table should be the only place where the text names of the properties are defined.
78 - The information in the table should be used within the classes IsDefaultPropertyWritable / Animatable / ConstraintInput methods for quick lookup.
79 - The last entry in the table is optionally used in debug builds for index checking.
80 - The parameter to DALI_PROPERTY_TABLE_END should match the start index of the property enumeration.
81
82 <br>
83 <h2 class="pg">How to implement a property within Dali-toolkit:</h2>
84
85 Note that toolkit properties have extra limitations in that they cannot be animated or used as a constraint input. For this reason there is no requirement for a table of property details.
86 Macros are still used to define properties, but for the following reasons:
87
88 To standardise the way properties are defined.
89 To handle type-registering for properties, signals and actions in one place.
90 To facilitate the posibility of running the code with the type-registry disabled.
91
92 <b>There are two stages:</b>
93
94 - Define the properties as an enum in the public-api header file, along with a definition of the property ranges.
95 - Define the property details using the pre-defined macros to perform the type-registering of the properties. This is done for signals and actions also.
96
97 <b>Example: Button</b>
98
99 Source file: <b>button.h</b>:
100 Note that the “PropertyRange” contents “PROPERTY_START_INDEX” is also used by the macro for order checking.
101
102 @code
103   /**
104    * @brief The start and end property ranges for this control.
105    */
106   enum PropertyRange
107   {
108     PROPERTY_START_INDEX = Control::CONTROL_PROPERTY_END_INDEX + 1,
109     PROPERTY_END_INDEX =   PROPERTY_START_INDEX + 1000              ///< Reserve property indices
110   };
111
112   /**
113    * @brief An enumeration of properties belonging to the Button class.
114    */
115   struct Property
116   {
117     enum
118     {
119       DISABLED = PROPERTY_START_INDEX, ///< name "disabled",                     @see SetDisabled(),                  type bool
120       AUTO_REPEATING,                  ///< name "auto-repeating",               @see SetAutoRepeating(),             type bool
121       INITIAL_AUTO_REPEATING_DELAY,    ///< name "initial-auto-repeating-delay", @see SetInitialAutoRepeatingDelay(), type float
122       NEXT_AUTO_REPEATING_DELAY,       ///< name "next-auto-repeating-delay",    @see SetNextAutoRepeatingDelay(),    type float
123       TOGGLABLE,                       ///< name "togglable",                    @see SetTogglableButton(),           type bool
124       SELECTED,                        ///< name "selected",                     @see SetSelected(),                  type bool
125       NORMAL_STATE_ACTOR,              ///< name "normal-state-actor",           @see SetButtonImage(),               type Map
126       SELECTED_STATE_ACTOR,            ///< name "selected-state-actor",         @see SetSelectedImage(),             type Map
127       DISABLED_STATE_ACTOR,            ///< name "disabled-state-actor",         @see SetDisabledImage(),             type Map
128       LABEL_ACTOR,                     ///< name "label-actor",                  @see SetLabel(),                     type Map
129     };
130   };
131 @endcode
132
133 Source file: <b>button-impl.cpp</b>, within an unnamed namespace:
134
135 @clip{"button-impl.cpp",DALI_TYPE_REGISTRATION_BEGIN,DALI_TYPE_REGISTRATION_END}
136
137 <b>Notes:</b>
138
139 - The “Create” parameter to the begin macro is the function pointer to the creation function.
140 - Properties should be in the same order as in the enum.
141 - Signals and actions are registered likewise in that order.
142 - Properties type-registered using these macros will have their order checked at compile time. If you get an indexing compile error, check the order matches the enum order.
143
144
145 <br>
146 <hr>
147 @section property-indices Property Indices
148
149 The properties are enumerated to give them a unique index. This index can be used to access them.
150 The indecies must be unique per flattened derivation heirachy.
151 EG:
152 - CameraActor derives from Actor. No property indicies in either CameraActor or Actor should collide with each other.
153 - ActiveConstraintBase derives from Object. It CAN have property indices that match Actor or CameraActor.
154
155 There are some predefined start indecies and ranges that should be used for common cases, these are defined below:
156
157
158 DALi has a property system and provides several different kinds of properties. The following table
159 shows the index range of the different properties in place.
160
161 <table>
162   <tr> <td><b>Kind</b></td>     <td><b>Description</b></td>                                                                                <td style="text-align:center;"><b>Start Index</b></td><td><b>End Index</b></td>         </tr>
163   <tr> <td>Default</td>         <td>Properties defined within DALi Core, e.g. Dali::Actor, Dali::ShaderEffect default properties etc.</td> <td style="text-align:center;">\link Dali::DEFAULT_OBJECT_PROPERTY_START_INDEX DEFAULT_OBJECT_PROPERTY_START_INDEX\endlink (0)</td><td>9999999</td>         </tr>
164   <tr> <td>Registered</td>      <td>Properties registered using Dali::PropertyRegistration</td>                                            <td style="text-align:center;">\link Dali::PROPERTY_REGISTRATION_START_INDEX PROPERTY_REGISTRATION_START_INDEX\endlink (10000000)</td><td>\link Dali::PROPERTY_REGISTRATION_MAX_INDEX PROPERTY_REGISTRATION_MAX_INDEX\endlink (19999999)</td> </tr>
165   <tr> <td>Control</td>         <td>Property range reserved by Dali::Toolkit::Control</td>                                                 <td style="text-align:center;">\link Dali::Toolkit::Control::CONTROL_PROPERTY_START_INDEX CONTROL_PROPERTY_START_INDEX\endlink (10000000)</td><td>
166   \link Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX CONTROL_PROPERTY_END_INDEX\endlink (10001000)</td></tr>
167   <tr> <td>Derived Control</td> <td>Property range for control deriving directly from Dali::Toolkit::Control</td>                          <td style="text-align:center;">10001001</td><td>\link Dali::PROPERTY_REGISTRATION_MAX_INDEX PROPERTY_REGISTRATION_MAX_INDEX\endlink (19999999)</td> </tr>
168   <tr> <td>Custom</td>          <td>Custom properties added to instance using Dali::Handle::RegisterProperty</td>                          <td style="text-align:center;">\link Dali::PROPERTY_CUSTOM_START_INDEX PROPERTY_CUSTOM_START_INDEX\endlink (50000000)</td><td>Onwards...</td>     </tr>
169 </table>
170
171
172 <br>
173 <hr>
174 @section property-use-example-cpp Property use example C++
175
176 Common uses for properties are constraints and animations.
177
178 An application developer can use an existing property, or, if necessary, register their own.
179
180 Here is a code example.
181
182 This example shows how to register and look-up custom properties.
183 A grid of buttons is created, each with a new "tag" property which is set to a unique value. The index to this property is cached for later use.
184 When pressed, the property is looked up by index (as this is much faster than a text lookup of the property name).
185
186 Property lookup via index should always be used unless the indecies cannot be known. If the property reader was completely decoupled from the creation, EG. A custom control with a custom property being used by external application code, then it may be necessary. In this case the application writer should aim to perform the text lookup once at start-up, and cache the property index locally.
187
188 @clip{"property-example.cpp", void Create, return true;}
189
190 Once run, a grid of buttons will appear. When a button is pressed, the unique number stored in the property (in this case the index) is displayed at the bottom of the screen.
191
192 <br>
193 <hr>
194 @section property-use-example-js Property use in JavaScript
195
196 Note that constraints cannot be used within JavaScript, so below is a simple example that sets one of the default properties; scale:
197
198 @code
199 var image = new dali.ResourceImage( {url:"background.png"} );
200 var imageActor = new dali.ImageActor( image );
201
202 // by default an actor is anchored to the top-left of it's parent actor
203 // change it to the middle
204 imageActor.parentOrigin = dali.CENTER;
205
206 // scale it up by 2 times  in x,y
207 imageActor.scale = [ 2, 2, 1  ];
208
209 // add to the stage
210 dali.stage.add( imageActor );
211 @endcode
212
213 For a more detailed example see the ShaderEffect example in the JavaScript documentation.
214
215 <br>
216 <hr>
217 @section property-use-example-json Property use in JSON
218
219 This is a basic example of a button defined in JSON by setting the default properties.
220
221 @code
222 {
223   "constants": {
224     "CONFIG_SCRIPT_LOG_LEVEL": "Verbose"
225   },
226   "stage": [
227     // First Button
228     {
229       "type": "PushButton",
230       "parent-origin": "TOP_CENTER",
231       "anchor-point": "TOP_CENTER",
232       "position": [0, 0, 0],
233       "size": [0, 200, 0],
234       "normal-state-actor": {
235         "type": "ImageActor",
236         "image": {
237           "filename": "{DALI_IMAGE_DIR}blocks-brick-1.png"
238         }
239       },
240       "selected-state-actor": {
241         "type": "ImageActor",
242         "image": {
243           "filename": "{DALI_IMAGE_DIR}blocks-brick-2.png"
244         }
245       },
246       "label-actor": {
247         "type": "TextLabel",
248         "text": "Normal"
249       }
250     }
251   ]
252 }
253 @endcode
254
255 *
256 */