2d8d7e6014ec95d2e3fcb4644034ebcd8686b017
[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:</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 @clip{"image-view-impl.cpp",DALI_TYPE_REGISTRATION_BEGIN,DALI_TYPE_REGISTRATION_END}
157
158 <b>Notes:</b>
159
160 - The “Create” parameter to the begin macro is the function pointer to the creation function.
161 - Properties should be in the same order as in the enum.
162 - Signals and actions are registered likewise in that order.
163 - 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.
164
165 <br>
166 <hr>
167 @section property-indices Property Indices
168
169 The properties are enumerated to give them a unique index. This index can be used to access them.
170 The indecies must be unique per flattened derivation heirachy.
171 EG:
172 - CameraActor derives from Actor. No property indicies in either CameraActor or Actor should collide with each other.
173 - ActiveConstraintBase derives from Object. It CAN have property indices that match Actor or CameraActor.
174
175 There are some predefined start indecies and ranges that should be used for common cases, these are defined below:
176
177
178 DALi has a property system and provides several different kinds of properties. The following table
179 shows the index range of the different properties in place.
180
181 | Kind                  | Description                                                                                       | Start Index                                                                                                | End Index                                                                                                                          |
182 |:----------------------|:--------------------------------------------------------------------------------------------------|:----------------------------------------------------------------------------------------------------------:|:----------------------------------------------------------------------------------------------------------------------------------:|
183 | 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)                                                |
184 | 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)                                     |
185 | 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) |
186 | 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) |
187 | 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)                             |
188 | 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)                                     |
189 | Custom                | Custom properties added to instance using Dali::Handle::RegisterProperty                          | \link Dali::PROPERTY_CUSTOM_START_INDEX PROPERTY_CUSTOM_START_INDEX\endlink (50000000)                     | Onwards...                                                                                                                         |
190
191 <br>
192 <hr>
193 @section property-use-example-cpp Property use example C++
194
195 Common uses for properties are constraints and animations.
196
197 An application developer can use an existing property, or, if necessary, register their own.
198
199 Here is a code example.
200
201 This example shows how to register and look-up custom properties.
202 An image is added to the screen which changes and a custom property is added to the image-view.
203 This value is incremented every time the image is touched and the text-label is updated.
204 When touched, the property is looked up by index (as this is much faster than a text lookup of the property name).
205
206 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.
207
208 @clip{"properties.cpp", // C++ EXAMPLE, // C++ EXAMPLE END}
209
210 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.
211
212 <br>
213 <hr>
214 @section property-use-example-js Property use in JavaScript
215
216 Note that constraints cannot be used within JavaScript, so below is a simple example that sets one of the default properties; scale:
217
218 @code
219 var imageView = new dali.Control( "ImageView" );
220
221 // by default an actor is anchored to the top-left of it's parent actor
222 // change it to the middle
223 imageView.parentOrigin = dali.CENTER;
224
225 // Set an image view property
226 imageView.image = {
227   "rendererType" : "image",
228   "url": "images/icon-0.png",
229   "desiredWidth" : 100,
230   "desiredHeight" : 100
231 };
232
233 // add to the stage
234 dali.stage.add( imageView );
235 @endcode
236
237 For a more detailed example see the ShaderEffect example in the JavaScript documentation.
238
239 <br>
240 <hr>
241 @section property-use-example-json Property use in JSON
242
243 This is a basic example of a button defined in JSON by setting the default properties.
244
245 @code
246 {
247   "stage":
248   [
249     {
250       "type": "ImageView",
251       "parentOrigin": "CENTER",
252       "anchorPoint": "CENTER",
253       "position": [0, 0, 0],
254       "image":
255       {
256         "rendererType" : "image",
257         "url" : "images/icon-0.png",
258         "desiredWidth" : 100,
259         "desiredHeight" : 100
260       }
261     }
262   ]
263 }
264 @endcode
265
266 *
267 */