size optimization on Control::Impl, reduces size from 232b to 200b on 64bit x86,...
[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    * 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    * @code
145    * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
146    * @endcode
147    *
148    * @pre The Builder has been initialized.
149    * @param key The constant name to add or update
150    * @param value The new value for the constant.
151    */
152   void AddConstant( const std::string& key, const Property::Value& value );
153
154   /**
155    * @brief Gets all currently defined constants.
156    *
157    * e.g.
158    * @code
159    * PropertyValueMap map = builder.GetConstants(); // get copy of current constants
160    * map["IMAGE_DIRECTORY"] = "/usr/share/images";  // make modification
161    * builder.AddConstants( map );                   // write back changes
162    * @endcode
163    *
164    * @pre The Builder has been initialized.
165    * @return A reference to the currently defined constants.
166    */
167   const PropertyValueMap& GetConstants() const;
168
169   /**
170    * @brief Gets a currently defined constant, or returns Property::INVALID
171    *
172    * e.g.
173    * @code
174    * PropertyValueMap map = builder.GetConstants(); // get copy of current constants
175    * map["IMAGE_DIRECTORY"] = "/usr/share/images";  // make modification
176    * builder.AddConstants( map );                   // write back changes
177    * @endcode
178    *
179    * @pre The Builder has been initialized.
180    * @param key The constant name to search for.
181    */
182   const Property::Value& GetConstant( const std::string& key ) const;
183
184   /**
185    * Creates an animation from the set of known animations
186    * e.g.
187    *   Animation a = builder.CreateAnimation( "wobble");
188    *
189    * @pre The Builder has been initialized.
190    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
191    * @pre The animationName exists in the animations section of the data representation
192    * @param animationName The animation name to create
193    * @returns The base handle of the created object
194    */
195   Animation CreateAnimation( const std::string& animationName );
196
197   /**
198    * @brief Creates an animation from the set of known animations with user defined constants
199    *
200    * e.g.
201    *   PropertyValueMap map;
202    *   map["ACTOR"] = actor.GetName();       // replaces '{ACTOR} in the template
203    *   Animation a = builder.CreateAnimation( "wobble");
204    *
205    * @pre The Builder has been initialized.
206    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
207    * @pre The animationName exists in the animations section of the data representation
208    * @pre The map contains all the constant expansions in the style template
209    * @param animationName The animation name to create
210    * @param map The user defined constants used in style template expansion.
211    * @returns The base handle of the created object
212    */
213   Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map );
214
215   /**
216    * @brief Creates an animation from the set of known animations.
217    *
218    * The animation is applied to a specific actor.
219    * e.g.
220    *   Actor myInstance = builder.Create( "template-actor-tree" )
221    *   Animation a = builder.CreateAnimation( "wobble", myInstance );
222    *
223    * @pre The Builder has been initialized.
224    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
225    * @pre The animationName exists in the animations section of the data representation
226    * @param animationName The animation name to create
227    * @param sourceActor The starting point in an actor tree, from which to look for property owners
228    * @returns The base handle of the created object
229    */
230   Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
231
232   /**
233    * @brief Creates an animation from the set of known animations with user defined constants
234    *
235    * The animation is applied to a specific actor.
236    * e.g.
237    *   PropertyValueMap map;
238    *   map["ACTOR"] = actor.GetName();       // replaces '{ACTOR} in the template
239    *   Actor myInstance = builder.Create( "template-actor-tree" )
240    *   Animation a = builder.CreateAnimation( "wobble", myInstance);
241    *
242    * @pre The Builder has been initialized.
243    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
244    * @pre The animationName exists in the animations section of the data representation
245    * @pre The map contains all the constant expansions in the style template
246    * @param animationName The animation name to create
247    * @param map The user defined constants used in style template expansion.
248    * @param sourceActor The starting point in an actor tree, from which to look for property owners
249    * @returns The base handle of the created object
250    */
251   Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map, Dali::Actor sourceActor );
252
253   /**
254    * @deprecated Use Create()
255    */
256   BaseHandle CreateFromStyle( const std::string& styleName );
257
258   /**
259    * @deprecated Use Create()
260    */
261   BaseHandle CreateFromStyle( const std::string& styleName, const PropertyValueMap& map );
262
263   /**
264    * @brief Creates an object (e.g. an actor) from the set of known style templates
265    *
266    * e.g.
267    *   mActor.Add( Actor::DownCast(builder.Create( "default-text")) );
268    *
269    * @pre The Builder has been initialized.
270    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
271    * @pre The templateName exists in the templates section of the data representation
272    *      and contains 'type' property used to create the object.
273    * @param templateName The template to apply in creation.
274    * @returns The base handle of the created object
275    */
276   BaseHandle Create( const std::string& templateName );
277
278   /**
279    * @brief Creates an object from the style templates with user defined constants
280    *
281    * e.g.
282    *   PropertyValueMap map;
283    *   map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
284    *   mActor.Add( Actor::DownCast(builder.Create( "default-image", map) ) );
285    *
286    * @pre The Builder has been initialized.
287    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
288    * @pre The templateName exists in the templates section of the data representation
289    *      and contains 'type' property used to create the object.
290    * @param templateName The template used to create the object.
291    * @param map The user defined constants used in template expansion.
292    * @returns The base handle of the created object
293    */
294   BaseHandle Create( const std::string& templateName, const PropertyValueMap& map );
295
296   /**
297    * Apply a style (a collection of properties) to an actor.
298    * @pre The Builder has been initialized.
299    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
300    * @param styleName The name of the set of style properties to set on the handle object.
301    * @param handle Then handle of the object on which to set the properties.
302    *
303    * @return Return true if the style was found
304    */
305   bool ApplyStyle( const std::string& styleName, Handle& handle );
306
307   /**
308    * Add the actor tree in the "stage" section to the actor toActor.
309    * ie if the representation has a 'stage' section that contains a tree of
310    * actors then
311    *    builder.AddActors( Stage::GetCurrent().GetRootLayer() );
312    * will create and add the actors to the stage root layer.
313    * @param toActor The actor to add the created actors to
314    */
315   void AddActors( Actor toActor );
316
317   /**
318    * Adds actors in the sectionName to the actor toActor.
319    * ie if the representation has a sectionName section that contains a tree of
320    * actors then
321    *    builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
322    * will create and add the actors to the stage root layer.
323    * @param sectionName The section name to search for the actor tree
324    * @param toActor The actor to add the created actors to
325    */
326   void AddActors( const std::string &sectionName, Actor toActor );
327
328   /**
329    * @deprecated Font as a separate asset is no longer supported
330    * Get's a Font asset previously created at load time
331    * An empty handle is returned otherwise.
332    * @pre The Builder has been initialized.
333    * @param name The name given to a Font in the loaded representation
334    * @return A handle to a Font if found, otherwise empty.
335    */
336   Font GetFont( const std::string &name ) const;
337
338   /**
339    * Get's a TextStyle asset previously created at load time
340    * @pre The Builder has been initialized.
341    * @param name The name given to a TextStyle in the loaded representation
342    * @return a Created TextStyle if found, otherwise return a TextStyle created by Default constructor
343    */
344   TextStyle GetTextStyle( const std::string &name ) const;
345
346   /**
347    * @deprecated Images as a separate asset is no longer supported
348    * Get's an Image asset previously created at load time
349    * An empty handle is returned otherwise.
350    * @pre The Builder has been initialized.
351    * @param name The name given to an Image in the loaded representation
352    * @return A handle to an Image if found, otherwise empty
353    */
354   Image GetImage( const std::string &name ) const;
355
356   /**
357    * @deprecated Actors no longer held by builder
358    * Get's an Actor previously created at load time
359    * An empty handle is returned otherwise.
360    * @pre The Builder has been initialized.
361    * @param name The name given to an Actor in the loaded representation
362    * @return A handle to an Actor if found, otherwise empty
363    */
364   Actor GetActor( const std::string &name ) const;
365
366   /**
367    * @deprecated Animations no longer held by builder
368    * Get's an Animation previously created at load time
369    * An empty handle is returned otherwise.
370    * @pre The Builder has been initialized.
371    * @param name The name given to an Animation in the loaded representation
372    * @return A handle to an Animation if found, otherwise empty
373    */
374   Animation GetAnimation( const std::string &name ) const;
375
376   /**
377    * Create a render task set.
378    * @pre The Builder has been initialized.
379    * @param name The library name of the render task set.
380    */
381   void CreateRenderTask( const std::string &name );
382
383   /**
384    * Get or create ShaderEffect from the ShaderEffect instance library.
385    * An empty handle is returned otherwise.
386    * @pre The Builder has been initialized.
387    * @param name The name of a ShaderEffect in the loaded representation
388    * @return A handle to a ShaderEffect if found, otherwise empty
389    */
390   ShaderEffect GetShaderEffect( const std::string &name );
391
392   /**
393    * Get or create FrameBufferImage from the FrameBufferImage instance library.
394    * An empty handle is returned otherwise.
395    * @pre The Builder has been initialized.
396    * @param name The name of a FrameBufferImage in the loaded representation
397    * @return A handle to a FrameBufferImage if found, otherwise empty
398    */
399   FrameBufferImage GetFrameBufferImage( const std::string &name );
400
401   /**
402    * @deprecated. Builder no longer holds actor handles/references
403    * Provides a list of the top level actors previously created at load time
404    * @return A container of Actors found at the top level of the loaded representation
405    */
406   ActorContainer GetTopLevelActors( void ) const;
407
408 private:
409   Builder(Internal::Builder *impl);
410
411 }; // class Builder
412
413 } // namespace Toolkit
414
415 } // namespace Dali
416
417 #endif // __DALI_TOOLKIT_UIBUILDER_H__