Tizen Directory Migration
[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: Layer</b>
30
31 Within the public-api header file; layer.h:
32
33 @code
34   /**
35    * @brief An enumeration of properties belonging to the Layer class.
36    *
37    * Properties additional to Actor.
38    */
39   struct Property
40   {
41     enum
42     {
43       CLIPPING_ENABLE = DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX, ///< name "clippingEnable",   type bool @SINCE_1_0.0
44       CLIPPING_BOX,                                                 ///< name "clippingBox",      type Rect<int> @SINCE_1_0.0
45       BEHAVIOR,                                                     ///< name "behavior",         type String @SINCE_1_0.0
46     };
47   };
48 @endcode
49 From @ref Dali::Layer::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>layer-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( "clippingEnable",    BOOLEAN,    true,    false,   true,             Dali::Layer::Property::CLIPPING_ENABLE )
67 DALI_PROPERTY( "clippingBox",       RECTANGLE,  true,    false,   true,             Dali::Layer::Property::CLIPPING_BOX    )
68 DALI_PROPERTY( "behavior",          STRING,     true,    false,   false,            Dali::Layer::Property::BEHAVIOR        )
69 DALI_PROPERTY_TABLE_END( DEFAULT_DERIVED_ACTOR_PROPERTY_START_INDEX )
70 @endcode
71
72 <b>Notes:</b>
73
74 - The table lays within an unnamed namespace.
75 - The table should be in the same order as the enum.
76 - The table should be the only place where the text names of the properties are defined.
77 - The information in the table should be used within the classes IsDefaultPropertyWritable / Animatable / ConstraintInput methods for quick lookup.
78 - The last entry in the table is optionally used in debug builds for index checking.
79 - The parameter to DALI_PROPERTY_TABLE_END should match the start index of the property enumeration.
80
81 <br>
82 <h2 class="pg">How to implement a property within Dali-toolkit controls and application-side custom controls:</h2>
83
84 Macros are used to define properties for the following reasons:
85
86 - To standardise the way properties are defined.
87 - To handle type-registering for properties, signals and actions in one place.
88 - To facilitate the posibility of running the code with the type-registry disabled.
89
90 Two different macros are provided depending on whether the property is to be an event-side only property or an animatable property.
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: ImageView</b>
98
99 Source file: <b>image-view.h</b>:
100 Note that the “PropertyRange” contents “PROPERTY_START_INDEX” & "ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX" are 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,  ///< @SINCE_1_0.0
109     PROPERTY_END_INDEX =   PROPERTY_START_INDEX + 1000,              ///< Reserve property indices @SINCE_1_0.0
110
111     ANIMATABLE_PROPERTY_START_INDEX = ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX,        ///< @SINCE_1_1.18
112     ANIMATABLE_PROPERTY_END_INDEX =   ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX + 1000  ///< Reserve animatable property indices, @SINCE_1_1.18
113   };
114
115   /**
116    * @brief An enumeration of properties belonging to the Button class.
117    */
118   struct Property
119   {
120     enum
121     {
122       // Event side properties
123
124       /**
125        * @DEPRECATED_1_1.16. Use IMAGE instead.
126        * @brief name "resourceUrl", type string
127        * @SINCE_1_0.0
128        */
129       RESOURCE_URL = PROPERTY_START_INDEX,
130       /**
131        * @brief name "image", type string if it is a url, map otherwise
132        * @SINCE_1_0.0
133        */
134       IMAGE,
135       /**
136        * @brief name "preMultipliedAlpha", type Boolean
137        * @SINCE_1_1.18
138        * @pre image must be initialized.
139        */
140       PRE_MULTIPLIED_ALPHA,
141
142       // Animatable properties
143
144       /**
145        * @brief name "pixelArea", type Vector4
146        * @details Pixel area is a relative value with the whole image area as [0.0, 0.0, 1.0, 1.0].
147        * @SINCE_1_0.18
148        */
149       PIXEL_AREA = ANIMATABLE_PROPERTY_START_INDEX,
150     };
151   };
152 @endcode
153
154 Source file: <b>image-view-impl.cpp</b>, within an unnamed namespace:
155
156 @code
157 #include <dali/public-api/object/type-registry-helper.h>
158 @endcode
159
160 @clip{"image-view-impl.cpp",DALI_TYPE_REGISTRATION_BEGIN,DALI_TYPE_REGISTRATION_END}
161
162 <b>Notes:</b>
163
164 - The “Create” parameter to the begin macro is the function pointer to the creation function.
165 - Properties should be in the same order as in the enum.
166 - Signals and actions are registered likewise in that order.
167 - 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.
168     The error will look like this: " error: invalid application of 'sizeof' to incomplete type 'Dali::CompileTimeAssertBool<false>' "
169 - If using the Pimpl design pattern when creating a custom control from within an application, the Handle (public) and Object (internal) classes should have the same name. They can be separated by different namespaces.
170     This requirement is actually due to how the type-registry in DALi looks up properties.
171
172 <br>
173 <hr>
174 @section property-indices Property Indices
175
176 The properties are enumerated to give them a unique index. This index can be used to access them.
177 The indecies must be unique per flattened derivation heirachy.
178 EG:
179 - CameraActor derives from Actor. No property indicies in either CameraActor or Actor should collide with each other.
180 - ActiveConstraintBase derives from Object. It CAN have property indices that match Actor or CameraActor.
181
182 There are some predefined start indecies and ranges that should be used for common cases, these are defined below:
183
184
185 DALi has a property system and provides several different kinds of properties. The following table
186 shows the index range of the different properties in place.
187
188 | Kind                  | Description                                                                                       | Start Index                                                                                                | End Index                                                                                                                          |
189 |:----------------------|:--------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------:|
190 | Default               | Properties defined within DALi Core, e.g. Dali::Actor, Dali::ShaderEffect default properties etc. | \link Dali::DEFAULT_OBJECT_PROPERTY_START_INDEX DEFAULT_OBJECT_PROPERTY_START_INDEX\endlink                | \link Dali::DEFAULT_PROPERTY_MAX_COUNT DEFAULT_PROPERTY_MAX_COUNT\endlink (9999999)                                                |
191 | Registered            | Properties registered using Dali::PropertyRegistration                                            | \link Dali::PROPERTY_REGISTRATION_START_INDEX PROPERTY_REGISTRATION_START_INDEX\endlink (10000000)         | \link Dali::PROPERTY_REGISTRATION_MAX_INDEX PROPERTY_REGISTRATION_MAX_INDEX\endlink (19999999)                                     |
192 | Registered Animatable | Animatable properties registered using Dali::AnimatablePropertyRegistration                       | \link Dali::ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX\endlink (20000000) | \link Dali::ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX\endlink (29999999) |
193 | Registered Child      | Child properties (which parent supports in its children) registered using Dali::ChildPropertyRegistration   | \link Dali::ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX\endlink (20000000) | \link Dali::CHILD_PROPERTY_REGISTRATION_MAX_INDEX CHILD_PROPERTY_REGISTRATION_MAX_INDEX\endlink (49999999) |
194 | Control               | Property range reserved by Dali::Toolkit::Control                                                 | \link Dali::Toolkit::Control::CONTROL_PROPERTY_START_INDEX CONTROL_PROPERTY_START_INDEX\endlink (10000000) | \link Dali::Toolkit::Control::CONTROL_PROPERTY_END_INDEX CONTROL_PROPERTY_END_INDEX\endlink (10001000)                             |
195 | Derived Control       | Property range for control deriving directly from Dali::Toolkit::Control                          | 10001001                                                                                                   | \link Dali::PROPERTY_REGISTRATION_MAX_INDEX PROPERTY_REGISTRATION_MAX_INDEX\endlink (19999999)                                     |
196 | Custom                | Custom properties added to instance using Dali::Handle::RegisterProperty                          | \link Dali::PROPERTY_CUSTOM_START_INDEX PROPERTY_CUSTOM_START_INDEX\endlink (50000000)                     | Onwards...                                                                                                                         |
197
198 <br>
199 <hr>
200 @section property-use-example-cpp Property use example C++
201
202 Common uses for properties are constraints and animations.
203
204 An application developer can use an existing property, or, if necessary, register their own.
205
206 Here is a code example.
207
208 This example shows how to register and look-up custom properties.
209 An image is added to the screen which changes and a custom property is added to the image-view.
210 This value is incremented every time the image is touched and the text-label is updated.
211 When touched, the property is looked up by index (as this is much faster than a text lookup of the property name).
212
213 Property lookup via index should always be used unless the indicies cannot be known. If the property reader was completely decoupled from the creation, e.g. 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.
214
215 @clip{"properties.cpp", // C++ EXAMPLE, // C++ EXAMPLE END}
216
217 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.
218
219 <br>
220 <hr>
221 @section property-use-example-js Property use in JavaScript
222
223 Note that constraints cannot be used within JavaScript, so below is a simple example that sets one of the default properties; scale:
224
225 @code
226 var imageView = new dali.Control( "ImageView" );
227
228 // by default an actor is anchored to the top-left of it's parent actor
229 // change it to the middle
230 imageView.parentOrigin = dali.CENTER;
231
232 // Set an image view property
233 imageView.image = {
234   "rendererType" : "image",
235   "url": "images/icon-0.png",
236   "desiredWidth" : 100,
237   "desiredHeight" : 100
238 };
239
240 // add to the stage
241 dali.stage.add( imageView );
242 @endcode
243
244 For a more detailed example see the ShaderEffect example in the JavaScript documentation.
245
246 <br>
247 <hr>
248 @section property-use-example-json Property use in JSON
249
250 This is a basic example of a button defined in JSON by setting the default properties.
251
252 @code
253 {
254   "stage":
255   [
256     {
257       "type": "ImageView",
258       "parentOrigin": "CENTER",
259       "anchorPoint": "CENTER",
260       "position": [0, 0, 0],
261       "image":
262       {
263         "rendererType" : "image",
264         "url" : "images/icon-0.png",
265         "desiredWidth" : 100,
266         "desiredHeight" : 100
267       }
268     }
269   ]
270 }
271 @endcode
272
273 *
274 */