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