Builder templated constant expansion
[platform/core/uifw/dali-toolkit.git] / 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 "styles" section
83  * TextActor actor = TextActor::DownCast( builder.CreateFromStyle( "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    * Creates an animation from the set of known animations
141    * e.g.
142    *   Animation a = builder.CreateAnimation( "wobble");
143    *
144    * @pre The Builder has been initialized.
145    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
146    * @pre The animationName exists in the animations section of the data representation
147    * @param animationName The animation name to create
148    * @returns The base handle of the created object
149    */
150   Animation CreateAnimation( const std::string& animationName );
151
152   /**
153    * @brief Creates an animation from the set of known animations with user defined constants
154    *
155    * e.g.
156    *   PropertyValueMap map;
157    *   map["ACTOR"] = actor.GetName();       // replaces '{ACTOR} in the template
158    *   Animation a = builder.CreateAnimation( "wobble");
159    *
160    * @pre The Builder has been initialized.
161    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
162    * @pre The animationName exists in the animations section of the data representation
163    * @pre The map contains all the constant expansions in the style template
164    * @param animationName The animation name to create
165    * @param map The user defined constants used in style template expansion.
166    * @returns The base handle of the created object
167    */
168   Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map );
169
170   /**
171    * @brief Creates an object (e.g. an actor) from the set of known style templates
172    *
173    * e.g.
174    *   mActor.Add( Actor::DownCast(builder.CreateFromStyle( "default-text")) );
175    *
176    * @pre The Builder has been initialized.
177    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
178    * @pre The styleName has been loaded from the styles section of the data representation
179    *      and contains 'type' property used to create the object.
180    * @param styleName The set of styles/properties to set on the handle object.
181    * @returns The base handle of the created object
182    */
183   BaseHandle CreateFromStyle( const std::string& styleName );
184
185   /**
186    * @brief Creates an object from the style templates with user defined constants
187    *
188    * e.g.
189    *   PropertyValueMap map;
190    *   map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
191    *   mActor.Add( Actor::DownCast(builder.CreateFromStyle( "default-image", map) ) );
192    *
193    * @pre The Builder has been initialized.
194    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
195    * @pre The styleName has been loaded from the styles section of the data representation
196    *      and contains 'type' property used to create the object.
197    * @pre The map contains all the constant expansions in the style template
198    * @param styleName The set of styles/properties to set on the handle object.
199    * @param map The user defined constants used in style template expansion.
200    * @returns The base handle of the created object
201    */
202   BaseHandle CreateFromStyle( const std::string& styleName, const PropertyValueMap& map );
203
204   /**
205    * Apply a style (a collection of properties) to an actor.
206    * @pre The Builder has been initialized.
207    * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
208    * @param styleName The name of the set of style properties to set on the handle object.
209    * @param handle Then handle of the object on which to set the properties.
210    */
211   void ApplyStyle( const std::string& styleName, Handle& handle );
212
213   /**
214    * Add the actor tree in the "stage" section to the actor toActor.
215    * ie if the representation has a 'stage' section that contains a tree of
216    * actors then
217    *    builder.AddActors( Stage::GetCurrent().GetRootLayer() );
218    * will create and add the actors to the stage root layer.
219    * @param toActor The actor to add the created actors to
220    */
221   void AddActors( Actor toActor );
222
223   /**
224    * Adds actors in the sectionName to the actor toActor.
225    * ie if the representation has a sectionName section that contains a tree of
226    * actors then
227    *    builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
228    * will create and add the actors to the stage root layer.
229    * @param sectionName The section name to search for the actor tree
230    * @param toActor The actor to add the created actors to
231    */
232   void AddActors( const std::string &sectionName, Actor toActor );
233
234   /**
235    * @deprecated Font as a separate asset is no longer supported
236    * Get's a Font asset previously created at load time
237    * An empty handle is returned otherwise.
238    * @pre The Builder has been initialized.
239    * @param name The name given to a Font in the loaded representation
240    * @return A handle to a Font if found, otherwise empty.
241    */
242   Font GetFont( const std::string &name ) const;
243
244   /**
245    * Get's a TextStyle asset previously created at load time
246    * @pre The Builder has been initialized.
247    * @param name The name given to a TextStyle in the loaded representation
248    * @return a Created TextStyle if found, otherwise return a TextStyle created by Default constructor
249    */
250   TextStyle GetTextStyle( const std::string &name ) const;
251
252   /**
253    * @deprecated Images as a separate asset is no longer supported
254    * Get's an Image asset previously created at load time
255    * An empty handle is returned otherwise.
256    * @pre The Builder has been initialized.
257    * @param name The name given to an Image in the loaded representation
258    * @return A handle to an Image if found, otherwise empty
259    */
260   Image GetImage( const std::string &name ) const;
261
262   /**
263    * @deprecated Actors no longer held by builder
264    * Get's an Actor previously created at load time
265    * An empty handle is returned otherwise.
266    * @pre The Builder has been initialized.
267    * @param name The name given to an Actor in the loaded representation
268    * @return A handle to an Actor if found, otherwise empty
269    */
270   Actor GetActor( const std::string &name ) const;
271
272   /**
273    * @deprecated Animations no longer held by builder
274    * Get's an Animation previously created at load time
275    * An empty handle is returned otherwise.
276    * @pre The Builder has been initialized.
277    * @param name The name given to an Animation in the loaded representation
278    * @return A handle to an Animation if found, otherwise empty
279    */
280   Animation GetAnimation( const std::string &name ) const;
281
282   /**
283    * Create a render task set.
284    * @pre The Builder has been initialized.
285    * @param name The library name of the render task set.
286    */
287   void CreateRenderTask( const std::string &name );
288
289   /**
290    * Get or create ShaderEffect from the ShaderEffect instance library.
291    * An empty handle is returned otherwise.
292    * @pre The Builder has been initialized.
293    * @param name The name of a ShaderEffect in the loaded representation
294    * @return A handle to a ShaderEffect if found, otherwise empty
295    */
296   ShaderEffect GetShaderEffect( const std::string &name );
297
298   /**
299    * Get or create FrameBufferImage from the FrameBufferImage instance library.
300    * An empty handle is returned otherwise.
301    * @pre The Builder has been initialized.
302    * @param name The name of a FrameBufferImage in the loaded representation
303    * @return A handle to a FrameBufferImage if found, otherwise empty
304    */
305   FrameBufferImage GetFrameBufferImage( const std::string &name );
306
307   /**
308    * @deprecated. Builder no longer holds actor handles/references
309    * Provides a list of the top level actors previously created at load time
310    * @return A container of Actors found at the top level of the loaded representation
311    */
312   ActorContainer GetTopLevelActors( void ) const;
313
314 private:
315   Builder(Internal::Builder *impl);
316
317 }; // class Builder
318
319 } // namespace Toolkit
320
321 } // namespace Dali
322
323 #endif // __DALI_TOOLKIT_UIBUILDER_H__