1 #ifndef __DALI_TOOLKIT_UIBUILDER_H__
2 #define __DALI_TOOLKIT_UIBUILDER_H__
5 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
11 // http://floralicense.org/license/
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.
21 #include <dali/dali.h>
23 namespace Dali DALI_IMPORT_API
29 namespace Internal DALI_INTERNAL
34 typedef std::map<std::string, Property::Value> PropertyValueMap;
38 * This class provides the ability to load an actor tree from a string representation.
40 * The following example is hello world in JSON.
51 * "parent-origin":[0.5,0.5,0],
58 * "type":"default-text",
59 * "text":"Hello World",
69 * The following is how to load the json data.
73 * Builder builder = Builder::New();
75 * std::string json_data(ReadFile("layout.json"));
77 * builder.LoadFromString(json_data);
79 * // 1) load all actors in the "stage" section to the root layer
80 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
82 * // or 2) create an actor from the library "styles" section
83 * TextActor actor = TextActor::DownCast( builder.CreateFromStyle( "default-text" ) );
87 class Builder : public BaseHandle
91 * Create an Builder handle; this can be initialised with Builder::New()
92 * Calling member functions with an uninitialised handle is not allowed.
97 * Creates an Builder object.
98 * @return A handle to the Builder control.
100 static Builder New();
103 * Virtual destructor.
108 * UI string data format
112 JSON, ///< String is JSON
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
124 void LoadFromString( const std::string& data, UIFormat format = JSON );
127 * @brief Adds user defined constants to all future style template or animation expansions
130 * PropertyValueMap map;
131 * map["IMAGE_DIRECTORY"] = "/usr/share/images";
132 * builder.AddConstants( map );
134 * @pre The Builder has been initialized.
135 * @param map The user defined constants used in template expansions.
137 void AddConstants( const PropertyValueMap& map );
140 * @brief Adds or modifies a user defined constant to all future style template or animation expansions
143 * @code builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
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.
149 void AddConstant( const std::string& key, const Property::Value& value );
152 * @brief Gets all currently defined constants.
155 * @code 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
159 * @pre The Builder has been initialized.
160 * @return A reference to the currently defined constants.
162 const PropertyValueMap& GetConstants() const;
165 * @brief Gets a currently defined constant, or returns Property::INVALID
168 * @code 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
172 * @pre The Builder has been initialized.
173 * @param key The constant name to search for.
175 const Property::Value& GetConstant( const std::string& key ) const;
178 * Creates an animation from the set of known animations
180 * Animation a = builder.CreateAnimation( "wobble");
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
188 Animation CreateAnimation( const std::string& animationName );
191 * @brief Creates an animation from the set of known animations with user defined constants
194 * PropertyValueMap map;
195 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
196 * Animation a = builder.CreateAnimation( "wobble");
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
206 Animation CreateAnimation( const std::string& animationName, const PropertyValueMap& map );
209 * @brief Creates an object (e.g. an actor) from the set of known style templates
212 * mActor.Add( Actor::DownCast(builder.CreateFromStyle( "default-text")) );
214 * @pre The Builder has been initialized.
215 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
216 * @pre The styleName has been loaded from the styles section of the data representation
217 * and contains 'type' property used to create the object.
218 * @param styleName The set of styles/properties to set on the handle object.
219 * @returns The base handle of the created object
221 BaseHandle CreateFromStyle( const std::string& styleName );
224 * @brief Creates an object from the style templates with user defined constants
227 * PropertyValueMap map;
228 * map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
229 * mActor.Add( Actor::DownCast(builder.CreateFromStyle( "default-image", map) ) );
231 * @pre The Builder has been initialized.
232 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
233 * @pre The styleName has been loaded from the styles section of the data representation
234 * and contains 'type' property used to create the object.
235 * @pre The map contains all the constant expansions in the style template
236 * @param styleName The set of styles/properties to set on the handle object.
237 * @param map The user defined constants used in style template expansion.
238 * @returns The base handle of the created object
240 BaseHandle CreateFromStyle( const std::string& styleName, const PropertyValueMap& map );
243 * Apply a style (a collection of properties) to an actor.
244 * @pre The Builder has been initialized.
245 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
246 * @param styleName The name of the set of style properties to set on the handle object.
247 * @param handle Then handle of the object on which to set the properties.
249 void ApplyStyle( const std::string& styleName, Handle& handle );
252 * Add the actor tree in the "stage" section to the actor toActor.
253 * ie if the representation has a 'stage' section that contains a tree of
255 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
256 * will create and add the actors to the stage root layer.
257 * @param toActor The actor to add the created actors to
259 void AddActors( Actor toActor );
262 * Adds actors in the sectionName to the actor toActor.
263 * ie if the representation has a sectionName section that contains a tree of
265 * builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
266 * will create and add the actors to the stage root layer.
267 * @param sectionName The section name to search for the actor tree
268 * @param toActor The actor to add the created actors to
270 void AddActors( const std::string §ionName, Actor toActor );
273 * @deprecated Font as a separate asset is no longer supported
274 * Get's a Font asset 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 a Font in the loaded representation
278 * @return A handle to a Font if found, otherwise empty.
280 Font GetFont( const std::string &name ) const;
283 * Get's a TextStyle asset previously created at load time
284 * @pre The Builder has been initialized.
285 * @param name The name given to a TextStyle in the loaded representation
286 * @return a Created TextStyle if found, otherwise return a TextStyle created by Default constructor
288 TextStyle GetTextStyle( const std::string &name ) const;
291 * @deprecated Images as a separate asset is no longer supported
292 * Get's an Image asset previously created at load time
293 * An empty handle is returned otherwise.
294 * @pre The Builder has been initialized.
295 * @param name The name given to an Image in the loaded representation
296 * @return A handle to an Image if found, otherwise empty
298 Image GetImage( const std::string &name ) const;
301 * @deprecated Actors no longer held by builder
302 * Get's an Actor previously created at load time
303 * An empty handle is returned otherwise.
304 * @pre The Builder has been initialized.
305 * @param name The name given to an Actor in the loaded representation
306 * @return A handle to an Actor if found, otherwise empty
308 Actor GetActor( const std::string &name ) const;
311 * @deprecated Animations no longer held by builder
312 * Get's an Animation previously created at load time
313 * An empty handle is returned otherwise.
314 * @pre The Builder has been initialized.
315 * @param name The name given to an Animation in the loaded representation
316 * @return A handle to an Animation if found, otherwise empty
318 Animation GetAnimation( const std::string &name ) const;
321 * Create a render task set.
322 * @pre The Builder has been initialized.
323 * @param name The library name of the render task set.
325 void CreateRenderTask( const std::string &name );
328 * Get or create ShaderEffect from the ShaderEffect instance library.
329 * An empty handle is returned otherwise.
330 * @pre The Builder has been initialized.
331 * @param name The name of a ShaderEffect in the loaded representation
332 * @return A handle to a ShaderEffect if found, otherwise empty
334 ShaderEffect GetShaderEffect( const std::string &name );
337 * Get or create FrameBufferImage from the FrameBufferImage instance library.
338 * An empty handle is returned otherwise.
339 * @pre The Builder has been initialized.
340 * @param name The name of a FrameBufferImage in the loaded representation
341 * @return A handle to a FrameBufferImage if found, otherwise empty
343 FrameBufferImage GetFrameBufferImage( const std::string &name );
346 * @deprecated. Builder no longer holds actor handles/references
347 * Provides a list of the top level actors previously created at load time
348 * @return A container of Actors found at the top level of the loaded representation
350 ActorContainer GetTopLevelActors( void ) const;
353 Builder(Internal::Builder *impl);
357 } // namespace Toolkit
361 #endif // __DALI_TOOLKIT_UIBUILDER_H__