[dali_1.9.17] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / builder.h
1 #ifndef DALI_TOOLKIT_UIBUILDER_H
2 #define DALI_TOOLKIT_UIBUILDER_H
3
4 /*
5  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/actor.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/animation/linear-constrainer.h>
25 #include <dali/devel-api/animation/path-constrainer.h>
26
27 // INTERNAL INCLUDES
28 #include <dali-toolkit/public-api/dali-toolkit-common.h>
29
30 namespace Dali
31 {
32
33 namespace Toolkit
34 {
35
36 namespace Internal DALI_INTERNAL
37 {
38 class Builder;
39 }
40
41 /**
42  * This class provides the ability to load and style an actor tree from a string representation.
43  *
44  * The following is an example in JSON.
45  *
46  * @code
47  *
48  *  {
49  *    "templates": // are named instantiable actor trees
50  *    {
51  *      "defaultText":
52  *      {
53  *        "type":"TextActor",
54  *        "font":"",
55  *        "parentOrigin":[0.5,0.5,0],
56  *        "scale": [50,50,1]
57  *      }
58  *    },
59  *    "styles": // are named property sets applied to actor trees
60  *    {
61  *     "myStyle":
62  *      {
63  *        "size": [10,10,1] // root properties applied to a given root actor
64  *        "actors":         // properties applied to actors found by name from root
65  *        {
66  *          "ok":           // properties for an actor named "ok"
67  *          {
68  *            "scale":[5,5,1],
69  *          },
70  *          "cancel":
71  *          {
72  *            "scale":[50,50,1],
73  *          }
74  *       },
75  *      },
76  *    },
77  *    "stage":
78  *    [
79  *      {
80  *        "type":"defaultText",
81  *        "text":"Hello World",
82  *        "position":[0,0,0]
83  *      },
84  *    ]
85  *  }
86  *
87  * @endcode
88  *
89  * The following shows a method to load the json file.
90  * @code
91  * Builder builder = Builder::New();
92  * std::string json_data(ReadFile("layout.json"));
93  * builder.LoadFromString(json_data);
94  * @endcode
95  * Examples
96  * - Load all actors in the "stage" section to the root layer
97  * @code
98  * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
99  * @endcode
100  *
101  * - Create an actor tree from the "templates" section
102  * @code
103  * TextActor actor = TextActor::DownCast( builder.Create( "defaultText" ) );
104  * @endcode
105  *
106  * - Style an actor tree from the "styles" section
107  * @code
108  * builder.ApplyStyle( "myStyle",  actor );
109  * @endcode
110  *
111  * - Create an actor tree from json
112  * @code
113  * TextActor actor = TextActor::DownCast( builder.CreateFromJson("{\"type\":\"TextActor\",\"font\":\"\",\"scale\":[50,50,1]}") );
114  * @endcode
115  *
116  * - Apply a style to an actor tree from json
117  * @code
118  * builder.ApplyFromJson( textActor, ("{\"scale\":[5,5,1]}") );
119  * @endcode
120  *
121  */
122
123 class DALI_TOOLKIT_API Builder : public BaseHandle
124  {
125  public:
126    /**
127     * Create an Builder handle; this can be initialised with Builder::New()
128     * Calling member functions with an uninitialised handle is not allowed.
129     */
130   Builder();
131
132   /**
133    * Creates an Builder object.
134    * @return A handle to the Builder control.
135    */
136   static Builder New();
137
138   /**
139    * @brief Destructor
140    *
141    * This is non-virtual since derived Handle types must not contain data or virtual methods.
142    */
143   ~Builder();
144
145   /**
146    * UI string data format
147    */
148   enum UIFormat
149   {
150     JSON,                 ///< String is JSON
151   };
152
153   /**
154    * Loads a string representation of an actor tree into memory.
155    * The Actor is not automatically added to the stage.
156    * This function will raise an exception for parse and logical structure errors.
157    * @pre The Builder has been initialized.
158    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
159    * @param data A string represenation of an Actor tree
160    * @param format The string representation format ie JSON
161    */
162   void LoadFromString( const std::string& data, UIFormat format = JSON );
163
164   /**
165    * @brief Adds user defined constants to all future style template or animation expansions
166    *
167    * e.g.
168    *   Property::Map map;
169    *   map["IMAGE_DIRECTORY"] = "/usr/share/images";
170    *   builder.AddConstants( map );
171    *
172    * @pre The Builder has been initialized.
173    * @param map The user defined constants used in template expansions.
174    */
175   void AddConstants( const Property::Map& map );
176
177   /**
178    * @brief Adds or modifies a user defined constant to all future style template or animation expansions
179    *
180    * e.g.
181    * @code
182    * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
183    * @endcode
184    *
185    * @pre The Builder has been initialized.
186    * @param key The constant name to add or update
187    * @param value The new value for the constant.
188    */
189   void AddConstant( const std::string& key, const Property::Value& value );
190
191   /**
192    * @brief Gets all currently defined configurations.
193    *
194    * @pre The Builder has been initialized.
195    * @return A reference to the currently defined configurations.
196    */
197   const Property::Map& GetConfigurations() const;
198
199   /**
200    * @brief Gets all currently defined constants.
201    *
202    * e.g.
203    * @code
204    * Property::Map map = builder.GetConstants(); // get copy of current constants
205    * map["IMAGE_DIRECTORY"] = "/usr/share/images";  // make modification
206    * builder.AddConstants( map );                   // write back changes
207    * @endcode
208    *
209    * @pre The Builder has been initialized.
210    * @return A reference to the currently defined constants.
211    */
212   const Property::Map& GetConstants() const;
213
214   /**
215    * @brief Gets a currently defined constant, or returns Property::INVALID
216    *
217    * e.g.
218    * @code
219    * Property::Map map = builder.GetConstants(); // get copy of current constants
220    * map["IMAGE_DIRECTORY"] = "/usr/share/images";  // make modification
221    * builder.AddConstants( map );                   // write back changes
222    * @endcode
223    *
224    * @pre The Builder has been initialized.
225    * @param key The constant name to search for.
226    */
227   const Property::Value& GetConstant( const std::string& key ) const;
228
229   /**
230    * Creates an animation from the set of known animations
231    * e.g.
232    *   Animation a = builder.CreateAnimation( "wobble");
233    *
234    * @pre The Builder has been initialized.
235    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
236    * @pre The animationName exists in the animations section of the data representation
237    * @param animationName The animation name to create
238    * @returns The base handle of the created object
239    */
240   Animation CreateAnimation( const std::string& animationName );
241
242   /**
243    * @brief Creates an animation from the set of known animations with user defined constants
244    *
245    * e.g.
246    *   Property::Map map;
247    *   map["ACTOR"] = actor.GetProperty< std::string >( Dali::Actor::Property::NAME );       // replaces '{ACTOR} in the template
248    *   Animation a = builder.CreateAnimation( "wobble");
249    *
250    * @pre The Builder has been initialized.
251    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
252    * @pre The animationName exists in the animations section of the data representation
253    * @pre The map contains all the constant expansions in the style template
254    * @param animationName The animation name to create
255    * @param map The user defined constants used in style template expansion.
256    * @returns The base handle of the created object
257    */
258   Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
259
260   /**
261    * @brief Creates an animation from the set of known animations.
262    *
263    * The animation is applied to a specific actor.
264    * e.g.
265    *   Actor myInstance = builder.Create( "templateActorTree" )
266    *   Animation a = builder.CreateAnimation( "wobble", myInstance );
267    *
268    * @pre The Builder has been initialized.
269    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
270    * @pre The animationName exists in the animations section of the data representation
271    * @param animationName The animation name to create
272    * @param sourceActor The starting point in an actor tree, from which to look for property owners
273    * @returns The base handle of the created object
274    */
275   Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
276
277   /**
278    * @brief Creates an animation from the set of known animations with user defined constants
279    *
280    * The animation is applied to a specific actor.
281    * e.g.
282    *   Property::Map map;
283    *   map["ACTOR"] = actor.GetProperty< std::string >( Dali::Actor::Property::NAME );       // replaces '{ACTOR} in the template
284    *   Actor myInstance = builder.Create( "templateActorTree" )
285    *   Animation a = builder.CreateAnimation( "wobble", myInstance);
286    *
287    * @pre The Builder has been initialized.
288    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
289    * @pre The animationName exists in the animations section of the data representation
290    * @pre The map contains all the constant expansions in the style template
291    * @param animationName The animation name to create
292    * @param map The user defined constants used in style template expansion.
293    * @param sourceActor The starting point in an actor tree, from which to look for property owners
294    * @returns The base handle of the created object
295    */
296   Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
297
298   /**
299    * @brief Creates an object (e.g. an actor) from the set of known style templates
300    *
301    * e.g.
302    *   mActor.Add( Actor::DownCast(builder.Create( "defaultText")) );
303    *
304    * @pre The Builder has been initialized.
305    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
306    * @pre The templateName exists in the templates section of the data representation
307    *      and contains 'type' property used to create the object.
308    * @param templateName The template to apply in creation.
309    * @returns The base handle of the created object
310    */
311   BaseHandle Create( const std::string& templateName );
312
313   /**
314    * @brief Creates an object from the style templates with user defined constants
315    *
316    * e.g.
317    *   Property::Map map;
318    *   map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
319    *   mActor.Add( Actor::DownCast(builder.Create( "defaultImage",  map) ) );
320    *
321    * @pre The Builder has been initialized.
322    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
323    * @pre The templateName exists in the templates section of the data representation
324    *      and contains 'type' property used to create the object.
325    * @param templateName The template used to create the object.
326    * @param map The user defined constants used in template expansion.
327    * @returns The base handle of the created object
328    */
329   BaseHandle Create( const std::string& templateName, const Property::Map& map );
330
331   /**
332    * @brief Creates an object (e.g. an actor) from given json snippet
333    *
334    * e.g.
335    *   Actor a = Actor::DownCast(builder.CreateFromJson( "{\"type\":\"TextActor\"}"));
336    *
337    * @pre The Builder has been initialized.
338    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
339    * @param json The json snippet used to create the object.
340    * @returns The base handle of the created object if any
341    */
342   BaseHandle CreateFromJson( const std::string& json );
343
344   /**
345    * Apply a style (a collection of properties) to an actor.
346    * @pre The Builder has been initialized.
347    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
348    * @param styleName The name of the set of style properties to set on the handle object.
349    * @param handle Then handle of the object on which to set the properties.
350    *
351    * @return Return true if the style was found
352    */
353   bool ApplyStyle( const std::string& styleName, Handle& handle );
354
355   /**
356    * Apply a style (a collection of properties) to an actor from the given json snippet
357    * @pre The Builder has been initialized.
358    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
359    * @param handle Then handle of the object on which to set the properties.
360    * @param json The json snippet used to create the object.
361    *
362    * @return Return true if the json snippet was parsed
363    */
364   bool ApplyFromJson(  Handle& handle, const std::string& json );
365
366
367   /**
368    * Add the actor tree in the "stage" section to the actor toActor.
369    * ie if the representation has a 'stage' section that contains a tree of
370    * actors then
371    *    builder.AddActors( Stage::GetCurrent().GetRootLayer() );
372    * will create and add the actors to the stage root layer.
373    * @param toActor The actor to add the created actors to
374    */
375   void AddActors( Actor toActor );
376
377   /**
378    * Adds actors in the sectionName to the actor toActor.
379    * ie if the representation has a sectionName section that contains a tree of
380    * actors then
381    *    builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
382    * will create and add the actors to the stage root layer.
383    * @param sectionName The section name to search for the actor tree
384    * @param toActor The actor to add the created actors to
385    */
386   void AddActors( const std::string &sectionName, Actor toActor );
387
388   /**
389    * Create a render task set.
390    * @pre The Builder has been initialized.
391    * @param name The library name of the render task set.
392    */
393   void CreateRenderTask( const std::string &name );
394
395   /**
396    * Get or create Path from the Path instance library.
397    * An empty handle is returned otherwise.
398    * @pre The Builder has been initialized.
399    * @param name The name of a Path in the loaded representation
400    * @return A handle to a Path if found, otherwise empty
401    */
402   Path GetPath( const std::string &name );
403
404   /**
405    * Get or create a PathConstrainer from the set of known PathConstrainers
406    * e.g.
407    *   PathConstrainer a = builder.GetPathConstrainer( "myPathConstrainer");
408    *
409    * @pre The Builder has been initialized.
410    * @pre The pathConstrainerName exists in the Constrainers section of the data representation
411    * @param pathConstrainerName The name of the PathConstrainer
412    * @returns A handle to a PathConstrainer if found, otherwise empty
413    */
414   PathConstrainer GetPathConstrainer( const std::string& pathConstrainerName );
415
416   /**
417    * Get or create a LinearConstrainer from the set of known LinearConstrainers
418    * e.g.
419    *   LinearConstrainer a = builder.GetLinearConstrainer( "myLinearConstrainer");
420    *
421    * @pre The Builder has been initialized.
422    * @pre The linearConstrainerName exists in the Constrainers section of the data representation
423    * @param linearConstrainerName The name of the LinearConstrainer
424    * @returns A handle to a LinearConstrainer if found, otherwise empty
425    */
426   LinearConstrainer GetLinearConstrainer( const std::string& linearConstrainerName );
427
428   // Signals
429
430   /**
431    * @brief Builder signal type
432    */
433   typedef Signal< void () > BuilderSignalType;
434
435   /**
436    * @brief Signal emitted when a quit action is requested by the builder.
437    */
438   BuilderSignalType& QuitSignal();
439
440 private:
441   explicit DALI_INTERNAL Builder(Internal::Builder *impl);
442
443 }; // class Builder
444
445 } // namespace Toolkit
446
447 } // namespace Dali
448
449 #endif // DALI_TOOLKIT_UIBUILDER_H