1 #ifndef __DALI_TOOLKIT_UIBUILDER_H__
2 #define __DALI_TOOLKIT_UIBUILDER_H__
5 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/dali.h>
24 namespace Dali DALI_IMPORT_API
30 namespace Internal DALI_INTERNAL
35 typedef std::map<std::string, Property::Value> PropertyValueMap;
39 * This class provides the ability to load an actor tree from a string representation.
41 * The following example is hello world in JSON.
52 * "parent-origin":[0.5,0.5,0],
59 * "type":"default-text",
60 * "text":"Hello World",
70 * The following is how to load the json data.
74 * Builder builder = Builder::New();
76 * std::string json_data(ReadFile("layout.json"));
78 * builder.LoadFromString(json_data);
80 * // 1) load all actors in the "stage" section to the root layer
81 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
83 * // or 2) create an actor from the library "templates" section
84 * TextActor actor = TextActor::DownCast( builder.Create( "default-text" ) );
88 class Builder : public BaseHandle
92 * Create an Builder handle; this can be initialised with Builder::New()
93 * Calling member functions with an uninitialised handle is not allowed.
98 * Creates an Builder object.
99 * @return A handle to the Builder control.
101 static Builder New();
104 * Virtual destructor.
109 * UI string data format
113 JSON, ///< String is JSON
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
125 void LoadFromString( const std::string& data, UIFormat format = JSON );
128 * @brief Adds user defined constants to all future style template or animation expansions
131 * PropertyValueMap map;
132 * map["IMAGE_DIRECTORY"] = "/usr/share/images";
133 * builder.AddConstants( map );
135 * @pre The Builder has been initialized.
136 * @param map The user defined constants used in template expansions.
138 void AddConstants( const PropertyValueMap& map );
141 * @brief Adds or modifies a user defined constant to all future style template or animation expansions
144 * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
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.
150 void AddConstant( const std::string& key, const Property::Value& value );
153 * @brief Gets all currently defined constants.
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
160 * @pre The Builder has been initialized.
161 * @return A reference to the currently defined constants.
163 const PropertyValueMap& GetConstants() const;
166 * @brief Gets a currently defined constant, or returns Property::INVALID
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
173 * @pre The Builder has been initialized.
174 * @param key The constant name to search for.
176 const Property::Value& GetConstant( const std::string& key ) const;
179 * Creates an animation from the set of known animations
181 * Animation a = builder.CreateAnimation( "wobble");
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
189 Animation CreateAnimation( const std::string& animationName );
192 * @brief Creates an animation from the set of known animations with user defined constants
195 * PropertyValueMap map;
196 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
197 * Animation a = builder.CreateAnimation( "wobble");
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
207 Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map );
210 * @brief Creates an animation from the set of known animations.
212 * The animation is applied to a specific actor.
214 * Actor myInstance = builder.Create( "template-actor-tree" )
215 * Animation a = builder.CreateAnimation( "wobble", myInstance );
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
224 Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
227 * @brief Creates an animation from the set of known animations with user defined constants
229 * The animation is applied to a specific actor.
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);
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
245 Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map, Dali::Actor sourceActor );
248 * @deprecated Use Create()
250 BaseHandle CreateFromStyle( const std::string& styleName );
253 * @deprecated Use Create()
255 BaseHandle CreateFromStyle( const std::string& styleName, const PropertyValueMap& map );
258 * @brief Creates an object (e.g. an actor) from the set of known style templates
261 * mActor.Add( Actor::DownCast(builder.Create( "default-text")) );
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
270 BaseHandle Create( const std::string& templateName );
273 * @brief Creates an object from the style templates with user defined constants
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) ) );
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
288 BaseHandle Create( const std::string& templateName, const PropertyValueMap& map );
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.
297 void ApplyStyle( const std::string& styleName, Handle& handle );
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
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
307 void AddActors( Actor toActor );
310 * Adds actors in the sectionName to the actor toActor.
311 * ie if the representation has a sectionName section that contains a tree of
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
318 void AddActors( const std::string §ionName, Actor toActor );
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.
328 Font GetFont( const std::string &name ) const;
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
336 TextStyle GetTextStyle( const std::string &name ) const;
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
346 Image GetImage( const std::string &name ) const;
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
356 Actor GetActor( const std::string &name ) const;
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
366 Animation GetAnimation( const std::string &name ) const;
369 * Create a render task set.
370 * @pre The Builder has been initialized.
371 * @param name The library name of the render task set.
373 void CreateRenderTask( const std::string &name );
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
382 ShaderEffect GetShaderEffect( const std::string &name );
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
391 FrameBufferImage GetFrameBufferImage( const std::string &name );
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
398 ActorContainer GetTopLevelActors( void ) const;
401 Builder(Internal::Builder *impl);
405 } // namespace Toolkit
409 #endif // __DALI_TOOLKIT_UIBUILDER_H__