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