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