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
36 * This class provides the ability to load and style an actor tree from a string representation.
38 * The following is an example in JSON.
43 * "templates": // are named instantiable actor trees
49 * "parent-origin":[0.5,0.5,0],
53 * "styles": // are named property sets applied to actor trees
57 * "size": [10,10,1] // root properties applied to a given root actor
58 * "actors": // properties applied to actors found by name from root
60 * "ok": // properties for an actor named "ok"
74 * "type":"default-text",
75 * "text":"Hello World",
83 * The following shows a method to load the json file.
85 * Builder builder = Builder::New();
86 * std::string json_data(ReadFile("layout.json"));
87 * builder.LoadFromString(json_data);
90 * - Load all actors in the "stage" section to the root layer
92 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
95 * - Create an actor tree from the "templates" section
97 * TextActor actor = TextActor::DownCast( builder.Create( "default-text" ) );
100 * - Style an actor tree from the "styles" section
102 * builder.ApplyStyle( "my-style", actor );
105 * - Create an actor tree from json
107 * TextActor actor = TextActor::DownCast( builder.CreateFromJson("{\"type\":\"TextActor\",\"font\":\"\",\"scale\":[50,50,1]}") );
110 * - Apply a style to an actor tree from json
112 * builder.ApplyFromJson( textActor, ("{\"scale\":[5,5,1]}") );
117 class Builder : public BaseHandle
121 * Create an Builder handle; this can be initialised with Builder::New()
122 * Calling member functions with an uninitialised handle is not allowed.
127 * Creates an Builder object.
128 * @return A handle to the Builder control.
130 static Builder New();
135 * This is non-virtual since derived Handle types must not contain data or virtual methods.
140 * UI string data format
144 JSON, ///< String is JSON
148 * Loads a string representation of an actor tree into memory.
149 * The Actor is not automatically added to the stage.
150 * This function will raise an exception for parse and logical structure errors.
151 * @pre The Builder has been initialized.
152 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
153 * @param data A string represenation of an Actor tree
154 * @param format The string representation format ie JSON
156 void LoadFromString( const std::string& data, UIFormat format = JSON );
159 * @brief Adds user defined constants to all future style template or animation expansions
163 * map["IMAGE_DIRECTORY"] = "/usr/share/images";
164 * builder.AddConstants( map );
166 * @pre The Builder has been initialized.
167 * @param map The user defined constants used in template expansions.
169 void AddConstants( const Property::Map& map );
172 * @brief Adds or modifies a user defined constant to all future style template or animation expansions
176 * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
179 * @pre The Builder has been initialized.
180 * @param key The constant name to add or update
181 * @param value The new value for the constant.
183 void AddConstant( const std::string& key, const Property::Value& value );
186 * @brief Gets all currently defined constants.
190 * Property::Map map = builder.GetConstants(); // get copy of current constants
191 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
192 * builder.AddConstants( map ); // write back changes
195 * @pre The Builder has been initialized.
196 * @return A reference to the currently defined constants.
198 const Property::Map& GetConstants() const;
201 * @brief Gets a currently defined constant, or returns Property::INVALID
205 * Property::Map map = builder.GetConstants(); // get copy of current constants
206 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
207 * builder.AddConstants( map ); // write back changes
210 * @pre The Builder has been initialized.
211 * @param key The constant name to search for.
213 const Property::Value& GetConstant( const std::string& key ) const;
216 * Creates an animation from the set of known animations
218 * Animation a = builder.CreateAnimation( "wobble");
220 * @pre The Builder has been initialized.
221 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
222 * @pre The animationName exists in the animations section of the data representation
223 * @param animationName The animation name to create
224 * @returns The base handle of the created object
226 Animation CreateAnimation( const std::string& animationName );
229 * @brief Creates an animation from the set of known animations with user defined constants
233 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
234 * Animation a = builder.CreateAnimation( "wobble");
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 * @returns The base handle of the created object
244 Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
247 * @brief Creates an animation from the set of known animations.
249 * The animation is applied to a specific actor.
251 * Actor myInstance = builder.Create( "template-actor-tree" )
252 * Animation a = builder.CreateAnimation( "wobble", myInstance );
254 * @pre The Builder has been initialized.
255 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
256 * @pre The animationName exists in the animations section of the data representation
257 * @param animationName The animation name to create
258 * @param sourceActor The starting point in an actor tree, from which to look for property owners
259 * @returns The base handle of the created object
261 Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
264 * @brief Creates an animation from the set of known animations with user defined constants
266 * The animation is applied to a specific actor.
269 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
270 * Actor myInstance = builder.Create( "template-actor-tree" )
271 * Animation a = builder.CreateAnimation( "wobble", myInstance);
273 * @pre The Builder has been initialized.
274 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
275 * @pre The animationName exists in the animations section of the data representation
276 * @pre The map contains all the constant expansions in the style template
277 * @param animationName The animation name to create
278 * @param map The user defined constants used in style template expansion.
279 * @param sourceActor The starting point in an actor tree, from which to look for property owners
280 * @returns The base handle of the created object
282 Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
285 * @brief Creates an object (e.g. an actor) from the set of known style templates
288 * mActor.Add( Actor::DownCast(builder.Create( "default-text")) );
290 * @pre The Builder has been initialized.
291 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
292 * @pre The templateName exists in the templates section of the data representation
293 * and contains 'type' property used to create the object.
294 * @param templateName The template to apply in creation.
295 * @returns The base handle of the created object
297 BaseHandle Create( const std::string& templateName );
300 * @brief Creates an object from the style templates with user defined constants
304 * map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
305 * mActor.Add( Actor::DownCast(builder.Create( "default-image", map) ) );
307 * @pre The Builder has been initialized.
308 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
309 * @pre The templateName exists in the templates section of the data representation
310 * and contains 'type' property used to create the object.
311 * @param templateName The template used to create the object.
312 * @param map The user defined constants used in template expansion.
313 * @returns The base handle of the created object
315 BaseHandle Create( const std::string& templateName, const Property::Map& map );
318 * @brief Creates an object (e.g. an actor) from given json snippet
321 * Actor a = Actor::DownCast(builder.CreateFromJson( "{\"type\":\"TextActor\"}"));
323 * @pre The Builder has been initialized.
324 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
325 * @param json The json snippet used to create the object.
326 * @returns The base handle of the created object if any
328 BaseHandle CreateFromJson( const std::string& json );
331 * Apply a style (a collection of properties) to an actor.
332 * @pre The Builder has been initialized.
333 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
334 * @param styleName The name of the set of style properties to set on the handle object.
335 * @param handle Then handle of the object on which to set the properties.
337 * @return Return true if the style was found
339 bool ApplyStyle( const std::string& styleName, Handle& handle );
342 * Apply a style (a collection of properties) to an actor from the given json snippet
343 * @pre The Builder has been initialized.
344 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
345 * @param handle Then handle of the object on which to set the properties.
346 * @param json The json snippet used to create the object.
348 * @return Return true if the json snippet was parsed
350 bool ApplyFromJson( Handle& handle, const std::string& json );
354 * Add the actor tree in the "stage" section to the actor toActor.
355 * ie if the representation has a 'stage' section that contains a tree of
357 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
358 * will create and add the actors to the stage root layer.
359 * @param toActor The actor to add the created actors to
361 void AddActors( Actor toActor );
364 * Adds actors in the sectionName to the actor toActor.
365 * ie if the representation has a sectionName section that contains a tree of
367 * builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
368 * will create and add the actors to the stage root layer.
369 * @param sectionName The section name to search for the actor tree
370 * @param toActor The actor to add the created actors to
372 void AddActors( const std::string §ionName, Actor toActor );
375 * Create a render task set.
376 * @pre The Builder has been initialized.
377 * @param name The library name of the render task set.
379 void CreateRenderTask( const std::string &name );
382 * Get or create ShaderEffect from the ShaderEffect instance library.
383 * An empty handle is returned otherwise.
384 * @pre The Builder has been initialized.
385 * @param name The name of a ShaderEffect in the loaded representation
386 * @return A handle to a ShaderEffect if found, otherwise empty
388 ShaderEffect GetShaderEffect( const std::string &name );
391 * Get or create FrameBufferImage from the FrameBufferImage instance library.
392 * An empty handle is returned otherwise.
393 * @pre The Builder has been initialized.
394 * @param name The name of a FrameBufferImage in the loaded representation
395 * @return A handle to a FrameBufferImage if found, otherwise empty
397 FrameBufferImage GetFrameBufferImage( const std::string &name );
402 * @brief Builder signal type
404 typedef SignalV2< void () > Signal;
407 * @brief Signal emitted when a quit action is requested by the builder.
409 Signal& QuitSignal();
412 Builder(Internal::Builder *impl);
416 } // namespace Toolkit
420 #endif // __DALI_TOOLKIT_UIBUILDER_H__