5e963528aa05ac9f0beb6fffea9142910dabb6c2
[platform/core/uifw/dali-toolkit.git] / optional / 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    * Virtual destructor.
105    */
106   virtual ~Builder();
107
108   /**
109    * UI string data format
110    */
111   enum UIFormat
112   {
113     JSON,                 ///< String is JSON
114   };
115
116   /**
117    * Loads a string representation of an actor tree into memory.
118    * The Actor is not automatically added to the stage.
119    * This function will raise an exception for parse and logical structure errors.
120    * @pre The Builder has been initialized.
121    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
122    * @param data A string represenation of an Actor tree
123    * @param format The string representation format ie JSON
124    */
125   void LoadFromString( const std::string& data, UIFormat format = JSON );
126
127   /**
128    * @brief Adds user defined constants to all future style template or animation expansions
129    *
130    * e.g.
131    *   PropertyValueMap map;
132    *   map["IMAGE_DIRECTORY"] = "/usr/share/images";
133    *   builder.AddConstants( map );
134    *
135    * @pre The Builder has been initialized.
136    * @param map The user defined constants used in template expansions.
137    */
138   void AddConstants( const PropertyValueMap& map );
139
140   /**
141    * @brief Adds or modifies a user defined constant to all future style template or animation expansions
142    *
143    * e.g.
144    *  builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
145    *
146    * @pre The Builder has been initialized.
147    * @param key The constant name to add or update
148    * @param value The new value for the constant.
149    */
150   void AddConstant( const std::string& key, const Property::Value& value );
151
152   /**
153    * @brief Gets all currently defined constants.
154    *
155    * e.g.
156    *  PropertyValueMap map = builder.GetConstants(); // get copy of current constants
157    *  map["IMAGE_DIRECTORY"] = "/usr/share/images";  // make modification
158    *  builder.AddConstants( map );                   // write back changes
159    *
160    * @pre The Builder has been initialized.
161    * @return A reference to the currently defined constants.
162    */
163   const PropertyValueMap& GetConstants() const;
164
165   /**
166    * @brief Gets a currently defined constant, or returns Property::INVALID
167    *
168    * e.g.
169    *  PropertyValueMap map = builder.GetConstants(); // get copy of current constants
170    *  map["IMAGE_DIRECTORY"] = "/usr/share/images";  // make modification
171    *  builder.AddConstants( map );                   // write back changes
172    *
173    * @pre The Builder has been initialized.
174    * @param key The constant name to search for.
175    */
176   const Property::Value& GetConstant( const std::string& key ) const;
177
178   /**
179    * Creates an animation from the set of known animations
180    * e.g.
181    *   Animation a = builder.CreateAnimation( "wobble");
182    *
183    * @pre The Builder has been initialized.
184    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
185    * @pre The animationName exists in the animations section of the data representation
186    * @param animationName The animation name to create
187    * @returns The base handle of the created object
188    */
189   Animation CreateAnimation( const std::string& animationName );
190
191   /**
192    * @brief Creates an animation from the set of known animations with user defined constants
193    *
194    * e.g.
195    *   PropertyValueMap map;
196    *   map["ACTOR"] = actor.GetName();       // replaces '{ACTOR} in the template
197    *   Animation a = builder.CreateAnimation( "wobble");
198    *
199    * @pre The Builder has been initialized.
200    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
201    * @pre The animationName exists in the animations section of the data representation
202    * @pre The map contains all the constant expansions in the style template
203    * @param animationName The animation name to create
204    * @param map The user defined constants used in style template expansion.
205    * @returns The base handle of the created object
206    */
207   Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map );
208
209   /**
210    * @brief Creates an animation from the set of known animations.
211    *
212    * The animation is applied to a specific actor.
213    * e.g.
214    *   Actor myInstance = builder.Create( "template-actor-tree" )
215    *   Animation a = builder.CreateAnimation( "wobble", myInstance );
216    *
217    * @pre The Builder has been initialized.
218    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
219    * @pre The animationName exists in the animations section of the data representation
220    * @param animationName The animation name to create
221    * @param sourceActor The starting point in an actor tree, from which to look for property owners
222    * @returns The base handle of the created object
223    */
224   Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
225
226   /**
227    * @brief Creates an animation from the set of known animations with user defined constants
228    *
229    * The animation is applied to a specific actor.
230    * e.g.
231    *   PropertyValueMap map;
232    *   map["ACTOR"] = actor.GetName();       // replaces '{ACTOR} in the template
233    *   Actor myInstance = builder.Create( "template-actor-tree" )
234    *   Animation a = builder.CreateAnimation( "wobble", myInstance);
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    * @param sourceActor The starting point in an actor tree, from which to look for property owners
243    * @returns The base handle of the created object
244    */
245   Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map, Dali::Actor sourceActor );
246
247   /**
248    * @deprecated Use Create()
249    */
250   BaseHandle CreateFromStyle( const std::string& styleName );
251
252   /**
253    * @deprecated Use Create()
254    */
255   BaseHandle CreateFromStyle( const std::string& styleName, const PropertyValueMap& map );
256
257   /**
258    * @brief Creates an object (e.g. an actor) from the set of known style templates
259    *
260    * e.g.
261    *   mActor.Add( Actor::DownCast(builder.Create( "default-text")) );
262    *
263    * @pre The Builder has been initialized.
264    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
265    * @pre The templateName exists in the templates section of the data representation
266    *      and contains 'type' property used to create the object.
267    * @param templateName The template to apply in creation.
268    * @returns The base handle of the created object
269    */
270   BaseHandle Create( const std::string& templateName );
271
272   /**
273    * @brief Creates an object from the style templates with user defined constants
274    *
275    * e.g.
276    *   PropertyValueMap map;
277    *   map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
278    *   mActor.Add( Actor::DownCast(builder.Create( "default-image", map) ) );
279    *
280    * @pre The Builder has been initialized.
281    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
282    * @pre The templateName exists in the templates section of the data representation
283    *      and contains 'type' property used to create the object.
284    * @param templateName The template used to create the object.
285    * @param map The user defined constants used in template expansion.
286    * @returns The base handle of the created object
287    */
288   BaseHandle Create( const std::string& templateName, const PropertyValueMap& map );
289
290   /**
291    * Apply a style (a collection of properties) to an actor.
292    * @pre The Builder has been initialized.
293    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
294    * @param styleName The name of the set of style properties to set on the handle object.
295    * @param handle Then handle of the object on which to set the properties.
296    */
297   void 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    * @deprecated Font as a separate asset is no longer supported
322    * Get's a Font asset previously created at load time
323    * An empty handle is returned otherwise.
324    * @pre The Builder has been initialized.
325    * @param name The name given to a Font in the loaded representation
326    * @return A handle to a Font if found, otherwise empty.
327    */
328   Font GetFont( const std::string &name ) const;
329
330   /**
331    * Get's a TextStyle asset previously created at load time
332    * @pre The Builder has been initialized.
333    * @param name The name given to a TextStyle in the loaded representation
334    * @return a Created TextStyle if found, otherwise return a TextStyle created by Default constructor
335    */
336   TextStyle GetTextStyle( const std::string &name ) const;
337
338   /**
339    * @deprecated Images as a separate asset is no longer supported
340    * Get's an Image asset previously created at load time
341    * An empty handle is returned otherwise.
342    * @pre The Builder has been initialized.
343    * @param name The name given to an Image in the loaded representation
344    * @return A handle to an Image if found, otherwise empty
345    */
346   Image GetImage( const std::string &name ) const;
347
348   /**
349    * @deprecated Actors no longer held by builder
350    * Get's an Actor previously created at load time
351    * An empty handle is returned otherwise.
352    * @pre The Builder has been initialized.
353    * @param name The name given to an Actor in the loaded representation
354    * @return A handle to an Actor if found, otherwise empty
355    */
356   Actor GetActor( const std::string &name ) const;
357
358   /**
359    * @deprecated Animations no longer held by builder
360    * Get's an Animation previously created at load time
361    * An empty handle is returned otherwise.
362    * @pre The Builder has been initialized.
363    * @param name The name given to an Animation in the loaded representation
364    * @return A handle to an Animation if found, otherwise empty
365    */
366   Animation GetAnimation( const std::string &name ) const;
367
368   /**
369    * Create a render task set.
370    * @pre The Builder has been initialized.
371    * @param name The library name of the render task set.
372    */
373   void CreateRenderTask( const std::string &name );
374
375   /**
376    * Get or create ShaderEffect from the ShaderEffect instance library.
377    * An empty handle is returned otherwise.
378    * @pre The Builder has been initialized.
379    * @param name The name of a ShaderEffect in the loaded representation
380    * @return A handle to a ShaderEffect if found, otherwise empty
381    */
382   ShaderEffect GetShaderEffect( const std::string &name );
383
384   /**
385    * Get or create FrameBufferImage from the FrameBufferImage instance library.
386    * An empty handle is returned otherwise.
387    * @pre The Builder has been initialized.
388    * @param name The name of a FrameBufferImage in the loaded representation
389    * @return A handle to a FrameBufferImage if found, otherwise empty
390    */
391   FrameBufferImage GetFrameBufferImage( const std::string &name );
392
393   /**
394    * @deprecated. Builder no longer holds actor handles/references
395    * Provides a list of the top level actors previously created at load time
396    * @return A container of Actors found at the top level of the loaded representation
397    */
398   ActorContainer GetTopLevelActors( void ) const;
399
400 private:
401   Builder(Internal::Builder *impl);
402
403 }; // class Builder
404
405 } // namespace Toolkit
406
407 } // namespace Dali
408
409 #endif // __DALI_TOOLKIT_UIBUILDER_H__