Programming guide of DebugRenderer
[platform/core/uifw/dali-toolkit.git] / docs / content / shared-javascript-and-cpp-documentation / script-json-specification.md
1 <!--
2 /**-->
3
4 [TOC]
5
6 # DALi JSON Specification  {#script-json-specification}
7
8 ## Overview {#overview}
9
10 This document describes the DALi JSON specification.
11 The format is not yet formally versioned within the JSON.
12
13 # General format {#format}
14
15 The JSON format supports
16
17 - Named templates for on demand creation of
18  - Actors/ Controls
19  - Animations
20 - Style sets
21  - Dynamically change the style of Actor hierarchies
22  - Animate to a style set
23 - Includes
24  - Build style sets up by merging JSON
25 - Creating Scene content on load
26  - Controls created without any code
27
28
29 Concrete Actors and Controls can be created from types registered in the
30 DALi Type Registry.
31
32 Template, style and scene sections all configure Actors and Controls via
33 the DALi property system.
34
35 The JSON format deviates from the formal JSON specification and allows C style comments.
36
37 ~~~
38     {                                        //
39       "version": 0,                          // Version Number
40       "includes":                            // Include section
41       [                                      //
42        "base-theme.json"                     // Include file to merge into the
43                                              // JSON
44       ]                                      //
45       "constants":                           // Constant replacement section
46       {                                      //
47         ...                                  //
48       },                                     //
49       "templates":                           // Template library section
50       {                                      //
51         "basic-text":                        // A named template
52         {                                    // } key value properties
53          ...                                 //
54          "actors":                           // A tree of sub actors
55          [                                   //
56          ...                                 //
57          ]                                   //
58         }                                    //
59         },                                   //
60         "styles":                            // Named Style set section
61         {                                    //
62         "light-theme":                       // Style set name
63         {                                    //
64          ...                                 //
65         },                                   //
66         dark-theme":                         //
67         {                                    //
68          ...                                 //
69         },                                   //
70       }                                      //
71       "stage":                               // Stage section
72       [                                      //
73        {                                     // Actors|Controls to create on JSON file load
74        "type": "basic-text",                 // A DALi Control or a template name
75        "styles":["base-theme","light-theme"] // Style list to apply to this instance
76        }                                     //
77       ]                                      //
78     }                                        //
79 ~~~
80
81 # Top level sections {#sections}
82
83 ## Includes {#includes}
84
85 The "includes" section is an array of filenames to be merged in order to
86 create a final in memory JSON tree.
87
88 The merge process will walk key, value attributes from the root JSON
89 node and overwrite previous values with the newer values.
90
91 - If the key does not exist then it will be created.
92 - If the newer value is a different type then the newer type will persist.
93 - Where both values are objects the merge will descend into the object.
94 - Where both values are arrays the merge will replace the old array entirely.
95
96 The merge is relatively simple and is expected to be used to build up
97 from base themes to custom themes where there are few conflicts and
98 simple direct replacements are desired.
99
100 ### Constants {#constants}
101
102 The merge behaviour when a file has constants and includes is
103
104 1. Constants are loaded first
105 2. Includes file list is merged in order
106 3. All other (non constant) data is merged
107
108 The include merge is recursive, so step (2) above will cause the
109 constants in the first included file to be merged first.
110
111 ## Constants {#constantdetail}
112
113 The constants section supports sub-string and full property replacement.
114
115 ~~~
116     {
117     "constants":                        // Constant replacement section
118     {                                   //
119       "IMAGES": "/usr/share/images/",   // Constants can be set here or in code.
120       "SIZE": [100,100,1]               //
121     },                                  //
122     ...                                 //
123     {                                   //
124       "type":"ImageActor"               // An DALi type or a template name
125       "image":                          //
126       {                                 //
127         "filename":"{IMAGES}b.jpg"      // Image filename substring replacement
128       },                                //
129       "size": "{SIZE}"                  //
130     }                                   //  Property replacement
131     }                                   //
132 ~~~
133
134 The type of the constant should match the expected type of the property.
135
136 A property constant cannot be used for sub string replacement; a string
137 constant should be used.
138
139 With a property replacement, the replace site must contain a string
140 where the first and last positions are braces.
141
142 ## Templates {#templates}
143
144 The template section supports the creation of actor instances. The
145 template name can be used in code to create an actor tree.
146
147 ~~~{.js}
148 // JavaScript
149
150 var builder = new dali.Builder();
151 builder.loadFromFile( "my-app.json");
152 var userActorTree = builder.create( { template:"basic-text"} );
153 ~~~
154
155 ~~~{.cpp}
156 // C++
157
158 Builder builder = Builder::New();
159 std::string jsonData = loadFile("my-app.json");
160 builder.LoadFromString( jsonData );
161
162 actorTree = builder.Create("basic-text");
163 ~~~
164
165 Templates consist of a name, multiple property-value configurations and
166 an optional actor sub hierarchy.
167
168 ~~~{.json}
169    {                                    //
170    "templates":                         //  Template library section
171    {                                    //
172    "basic-text":                        //  The template name
173    {                                    //
174      "type":"ImageActor",               //  Concrete DALi Type/Class to create
175      "styles":["base-style"],           //  Style list to apply
176      "name":"image",                    //  }
177      "image":                           //  } property name : value
178      {                                  //  }
179      "filename":"{IMAGES}/b.jpg"        //
180      },                                 //
181      "parentOrigin": "CENTER"           //
182      ...                                //
183      "actors":                          //  A tree of sub actors
184      [                                  //
185      {                                  //
186      "type":"TextView"                  //
187      "name":"text",                     //
188      "text":"Hello World",              //
189      "parentOrigin": "CENTER",          //
190      }                                  //
191      ]                                  //
192    }                                    //
193    }                                    //
194 ~~~
195
196 A template has a special 'type' property which must contain a concrete
197 DALi Actor or Control type name.
198
199 A template has a special 'styles' property which contains a list of
200 styles to apply when creating using the template.
201
202 ## Styles {#styles}
203
204 The styles section supports a named set of properties that can be
205 applied to an actor or actor tree.
206
207 ~~~{.cpp}
208 // C++
209
210 Builder.ApplyStyle("light-theme", myActor);
211 ~~~
212
213 ~~~{.js}
214 // JavaScript
215
216 builder.applyStyle("light-theme", myActor);
217 ~~~
218
219
220 The styles can also be applied as an animation.
221
222 ~~~{.cpp}
223 Builder.AnimateTo("light-theme", myActor, TimePeriod(0, 10));
224 ~~~
225
226
227
228 ~~~
229    {                                   //
230    "styles":                           // Style set section
231    {                                   //
232    "light-theme":                      // Style-set name
233    {                                   //
234      "color":[1,1,1,1]                 // }
235      "position":[0,-120,0],            // } properties to set on the given actor
236      "rotation":[0,0,30],              // }
237      "actors":                         //
238      {                                 // Sub Actors are referenced by name
239        "title-text":                   // Actor name to search for under given actor
240        {                               //
241          "color":[1,1,1,1]             // }
242          "position":[0,-120,0],        // } properties to set if 'title-text' is found
243          "rotation":[0,0,30],          // }
244        }
245      },                                //
246      "icon":                           //
247      {                                 //
248        "color":[1,1,1,1]               //
249      }                                 //
250     },                                 //
251     "dark-theme":                      //
252     {                                  //
253     }                                  //
254    }                                   //
255 ~~~
256
257 When applied to an actor tree the actors are referenced by name. Names
258 are not unique in Dali.
259
260 When a style is applied in code DALi will perform a depth first search
261 stopping with the first matching name.
262
263 Typically an application developer will apply the style to the template
264 root actor and not the stage root actor. Therefore in most uses cases
265 name conflicts are not expected.
266
267 ## Animations {#animations}
268
269 The animation section defines a library of animation definitions.
270
271 The animations can be created by name from code.
272
273 They can also be created automatically from JSON in an actor signal.
274
275 ~~~
276     {                                    //
277     "animations":                        // Animation library
278     {                                    //
279      "rotate":                           // An Animation named rotate
280      {                                   //
281      "duration": 10,                     // Duration in seconds
282      "loop": false,                      // Whether to loop.
283      "endAction": "Bake",                // Whether to set final value(bake) or
284                                          // reset
285      "disconnectAction": "Discard",      // Whether 'Bake' or 'Discard' when disconnected
286      "properties":
287      [
288                                          // Properties changed in this animation
289      {
290      "actor":"image",                    // Actor found by name from the stage
291      "property":"rotation",              // Property to change
292      "value":[0, 0.1, 0, 0],             // Value to set
293      "alphaFunction": "EASE\_IN\_OUT",   // Interpolation function
294                                          //
295      "timePeriod":                       // Time period for change
296      {"delay": 0,
297       "duration": 3
298       }
299      },
300      ...                                 // 1 or more property changes per animation
301      },                                  //
302      ...                                 //
303     },                                   //
304 ~~~
305
306 ### Splines {#splines}
307
308 An animation property can be defined with a path and forward direction
309 instead of a single final value.
310
311 Paths are defined in a top level path section and referenced by the
312 animation property.
313
314 ~~~
315     {                                    //
316     "paths":                             // Path library
317      {                                   //
318      "path0":                            // Path definition by name
319      {                                   //
320      "points":[                          // points
321        [-150, -50, 0],
322        [0.0,70.0,0.0],
323        [190.0,-150.0,0.0]
324       ],
325                                          // curvature automatically creates
326      "curvature":0.35,                   // controlPoints
327                                          //
328      "controlPoints": [...]              // Otherwise controlPoints can be
329                                          // directly specified.
330      }                                   //
331      },                                  //
332     "animations":                        //
333     {                                    //
334      "pathAnimation":
335      {
336      "duration": 3.0,
337      "properties":
338      [
339      {
340      "actor": "greeting2",
341                                          // Path is mandatory for spline
342      "path":"path0",                     // animation.
343      "forward":[1,0,0],                  // Forward vector specifies orientation
344                                          // whilst travelling along the path
345      "alphaFunction": "EASE\_IN\_OUT",   // (optional)
346      "timePeriod":
347      {
348      "delay": 0,
349      "duration": 3
350      }
351      },
352      ...
353                                          // Other properties changes can use
354      ]                                   // paths or values in the same
355                                          // animation.
356     },                                   //
357     }                                    //
358 ~~~
359
360 ## Shaders {#shaders}
361
362 The shader section of the JSON file defines a library of shader effect
363 instances that are created on demand.
364
365 The shaders are referred to by name from the template, style, stage or
366 animation sections.
367
368 Multiple actors can set the same shader as the name refers to a single
369 instance.
370
371 Similarly one named shader instance can be set to several actors and can
372 be animated by one animation.
373
374 ~~~
375     {                                             //
376     "shaderEffects":                              // Shader Effect section
377     {                                             //
378       "myshader1":                                // Shader  instance  name
379       {                                           //
380        "program":                                 //
381        {                                          // Prefixs are placed before DALi uniforms.
382          "vertexPrefix": "",                      // (Useful for \#defines.)
383          "vertex":"",                             // Glsl vertex program
384          "fragmentPrefix": "",
385          "fragment": "",                          // Glsl fragment program.
386          "geometryType": "GEOMETRY_TYPE_IMAGE",   // Geometry type(see DALi documentation)
387        },
388        "geometryHints": "HINT_NONE":              // Geometry hints (see DALi documentation)
389        "gridDensity": 0,                          // Grid density(see DALi documentation)
390        "image":
391        {
392          "filename": ""                           // Effect image available as a second texture unit.
393        }
394      },
395      ...
396     },
397     "stage":
398     [{
399      "type": "ImageActor",
400      "effect": "myshader1",
401      ...
402     }]
403     }
404 ~~~
405
406 At least one of the vertex or fragment fields is mandatory. All
407 other fields are optional will use internal defaults.
408
409 Actors have an "effect" field that refers to the shader effect
410 instance to use with that actor.
411
412 ### Animating shaders {#animatingshaders}
413
414 Shader uniforms can be animated as if they are properties of the actor.
415
416 When the animation is created from code (or from a signal) the property
417 name search begins on the actor, if it isn't found the search continues
418 on the attached shader object.
419
420 The actor property names and shader uniform names must not clash for the
421 uniform to animate correctly. The convention in DALi is to prepend
422 uniforms with 'u'.
423
424 ~~~
425     {                                    \\
426     "animations":                        \\ Animation library
427     {                                    \\
428      "rotate":                           \\ An Animation named rotate
429      {                                   \\
430      "duration": 10,                     \\
431      "properties":                       \\ Properties changed in this animation
432      [                                   \\
433      {
434      "actor":"image",                    \\ Actor found by name from the stage
435      "property":"uTranslate",            \\ Uniform name specified as if it is a
436                                          \\ property of the object.
437      "value":[10, 20],
438      ...
439      },
440      ...
441      ]
442      },
443      "shaderEffects":
444      {
445      "myshader1":
446      {
447                                         \\ Shader program with uniform
448      "program": {...}                   \\ "uTranslate"
449      }
450      },
451      "actors":
452      [
453      {
454      "name": "image",
455      "effect": "myshader1"              \\ Actor using shader effect instance
456                                         \\ "myshader1"
457      }
458      ]
459     },
460 ~~~
461
462 ## Stage {#stage}
463
464 The stage section supports the immediate creation of actors at the time
465 the JSON is loaded.
466
467 The stage is a tree of actors that can be added to Dali's stage object.
468
469 ~~~
470 // C++
471 builder = Dali.Builder();
472 json_text = load("layout.json");
473 builder.Load(json\_text);
474 stage = Dali.Stage.GetCurrent();
475 builder.AddActors( stage.GetRootLayer()); // Add actors to the stage root layer
476 ~~~
477
478 ~~~{.js}
479 // JavaScript
480
481 builder.addActors( dali.stage.getRootLayer() );
482 ~~~
483
484 ~~~
485     {                                    \\
486     "stage":                             \\  Stage Section Number
487     [                                    \\  An array of actors
488      {
489      "type": "ImageActor",
490      ...
491      "actors":                           \\  Each actor can have children
492                                          \\ creating a tree
493      [
494      {
495      "type": "TextView",
496                                          \\  The Type to create; this can be a
497      ...                                 \\ concrete DALi type (actor/control)
498                                          \\ or a template name.
499      "styles": ["base-style"]
500                                          \\  A list of styles to apply to the
501      }                                   \\ created type.
502      ]
503      }
504     ]
505     }
506 ~~~
507
508 # Actor and Control Properties {#actorprop}
509
510 Each control has a set of supported properties documented in the "Dali
511 UI Control Specification".
512
513 Please refer to the above document for further information about specific
514 controls.
515
516 @class _Guide_JSON_Specification
517 */