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/public-api/actors/actor.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/images/frame-buffer-image.h>
25 #include <dali/public-api/shader-effects/shader-effect.h>
33 namespace Internal DALI_INTERNAL
39 * This class provides the ability to load and style an actor tree from a string representation.
41 * The following is an example in JSON.
46 * "templates": // are named instantiable actor trees
52 * "parent-origin":[0.5,0.5,0],
56 * "styles": // are named property sets applied to actor trees
60 * "size": [10,10,1] // root properties applied to a given root actor
61 * "actors": // properties applied to actors found by name from root
63 * "ok": // properties for an actor named "ok"
77 * "type":"default-text",
78 * "text":"Hello World",
86 * The following shows a method to load the json file.
88 * Builder builder = Builder::New();
89 * std::string json_data(ReadFile("layout.json"));
90 * builder.LoadFromString(json_data);
93 * - Load all actors in the "stage" section to the root layer
95 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
98 * - Create an actor tree from the "templates" section
100 * TextActor actor = TextActor::DownCast( builder.Create( "default-text" ) );
103 * - Style an actor tree from the "styles" section
105 * builder.ApplyStyle( "my-style", actor );
108 * - Create an actor tree from json
110 * TextActor actor = TextActor::DownCast( builder.CreateFromJson("{\"type\":\"TextActor\",\"font\":\"\",\"scale\":[50,50,1]}") );
113 * - Apply a style to an actor tree from json
115 * builder.ApplyFromJson( textActor, ("{\"scale\":[5,5,1]}") );
120 class DALI_IMPORT_API Builder : public BaseHandle
124 * Create an Builder handle; this can be initialised with Builder::New()
125 * Calling member functions with an uninitialised handle is not allowed.
130 * Creates an Builder object.
131 * @return A handle to the Builder control.
133 static Builder New();
138 * This is non-virtual since derived Handle types must not contain data or virtual methods.
143 * UI string data format
147 JSON, ///< String is JSON
151 * Loads a string representation of an actor tree into memory.
152 * The Actor is not automatically added to the stage.
153 * This function will raise an exception for parse and logical structure errors.
154 * @pre The Builder has been initialized.
155 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
156 * @param data A string represenation of an Actor tree
157 * @param format The string representation format ie JSON
159 void LoadFromString( const std::string& data, UIFormat format = JSON );
162 * @brief Adds user defined constants to all future style template or animation expansions
166 * map["IMAGE_DIRECTORY"] = "/usr/share/images";
167 * builder.AddConstants( map );
169 * @pre The Builder has been initialized.
170 * @param map The user defined constants used in template expansions.
172 void AddConstants( const Property::Map& map );
175 * @brief Adds or modifies a user defined constant to all future style template or animation expansions
179 * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
182 * @pre The Builder has been initialized.
183 * @param key The constant name to add or update
184 * @param value The new value for the constant.
186 void AddConstant( const std::string& key, const Property::Value& value );
189 * @brief Gets all currently defined constants.
193 * Property::Map map = builder.GetConstants(); // get copy of current constants
194 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
195 * builder.AddConstants( map ); // write back changes
198 * @pre The Builder has been initialized.
199 * @return A reference to the currently defined constants.
201 const Property::Map& GetConstants() const;
204 * @brief Gets a currently defined constant, or returns Property::INVALID
208 * Property::Map map = builder.GetConstants(); // get copy of current constants
209 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
210 * builder.AddConstants( map ); // write back changes
213 * @pre The Builder has been initialized.
214 * @param key The constant name to search for.
216 const Property::Value& GetConstant( const std::string& key ) const;
219 * Creates an animation from the set of known animations
221 * Animation a = builder.CreateAnimation( "wobble");
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 * @returns The base handle of the created object
229 Animation CreateAnimation( const std::string& animationName );
232 * @brief Creates an animation from the set of known animations with user defined constants
236 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
237 * Animation a = builder.CreateAnimation( "wobble");
239 * @pre The Builder has been initialized.
240 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
241 * @pre The animationName exists in the animations section of the data representation
242 * @pre The map contains all the constant expansions in the style template
243 * @param animationName The animation name to create
244 * @param map The user defined constants used in style template expansion.
245 * @returns The base handle of the created object
247 Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
250 * @brief Creates an animation from the set of known animations.
252 * The animation is applied to a specific actor.
254 * Actor myInstance = builder.Create( "template-actor-tree" )
255 * Animation a = builder.CreateAnimation( "wobble", myInstance );
257 * @pre The Builder has been initialized.
258 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
259 * @pre The animationName exists in the animations section of the data representation
260 * @param animationName The animation name to create
261 * @param sourceActor The starting point in an actor tree, from which to look for property owners
262 * @returns The base handle of the created object
264 Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
267 * @brief Creates an animation from the set of known animations with user defined constants
269 * The animation is applied to a specific actor.
272 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
273 * Actor myInstance = builder.Create( "template-actor-tree" )
274 * Animation a = builder.CreateAnimation( "wobble", myInstance);
276 * @pre The Builder has been initialized.
277 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
278 * @pre The animationName exists in the animations section of the data representation
279 * @pre The map contains all the constant expansions in the style template
280 * @param animationName The animation name to create
281 * @param map The user defined constants used in style template expansion.
282 * @param sourceActor The starting point in an actor tree, from which to look for property owners
283 * @returns The base handle of the created object
285 Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
288 * @brief Creates an object (e.g. an actor) from the set of known style templates
291 * mActor.Add( Actor::DownCast(builder.Create( "default-text")) );
293 * @pre The Builder has been initialized.
294 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
295 * @pre The templateName exists in the templates section of the data representation
296 * and contains 'type' property used to create the object.
297 * @param templateName The template to apply in creation.
298 * @returns The base handle of the created object
300 BaseHandle Create( const std::string& templateName );
303 * @brief Creates an object from the style templates with user defined constants
307 * map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
308 * mActor.Add( Actor::DownCast(builder.Create( "default-image", map) ) );
310 * @pre The Builder has been initialized.
311 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
312 * @pre The templateName exists in the templates section of the data representation
313 * and contains 'type' property used to create the object.
314 * @param templateName The template used to create the object.
315 * @param map The user defined constants used in template expansion.
316 * @returns The base handle of the created object
318 BaseHandle Create( const std::string& templateName, const Property::Map& map );
321 * @brief Creates an object (e.g. an actor) from given json snippet
324 * Actor a = Actor::DownCast(builder.CreateFromJson( "{\"type\":\"TextActor\"}"));
326 * @pre The Builder has been initialized.
327 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
328 * @param json The json snippet used to create the object.
329 * @returns The base handle of the created object if any
331 BaseHandle CreateFromJson( const std::string& json );
334 * Apply a style (a collection of properties) to an actor.
335 * @pre The Builder has been initialized.
336 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
337 * @param styleName The name of the set of style properties to set on the handle object.
338 * @param handle Then handle of the object on which to set the properties.
340 * @return Return true if the style was found
342 bool ApplyStyle( const std::string& styleName, Handle& handle );
345 * Apply a style (a collection of properties) to an actor from the given json snippet
346 * @pre The Builder has been initialized.
347 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
348 * @param handle Then handle of the object on which to set the properties.
349 * @param json The json snippet used to create the object.
351 * @return Return true if the json snippet was parsed
353 bool ApplyFromJson( Handle& handle, const std::string& json );
357 * Add the actor tree in the "stage" section to the actor toActor.
358 * ie if the representation has a 'stage' section that contains a tree of
360 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
361 * will create and add the actors to the stage root layer.
362 * @param toActor The actor to add the created actors to
364 void AddActors( Actor toActor );
367 * Adds actors in the sectionName to the actor toActor.
368 * ie if the representation has a sectionName section that contains a tree of
370 * builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
371 * will create and add the actors to the stage root layer.
372 * @param sectionName The section name to search for the actor tree
373 * @param toActor The actor to add the created actors to
375 void AddActors( const std::string §ionName, Actor toActor );
378 * Create a render task set.
379 * @pre The Builder has been initialized.
380 * @param name The library name of the render task set.
382 void CreateRenderTask( const std::string &name );
385 * Get or create ShaderEffect from the ShaderEffect instance library.
386 * An empty handle is returned otherwise.
387 * @pre The Builder has been initialized.
388 * @param name The name of a ShaderEffect in the loaded representation
389 * @return A handle to a ShaderEffect if found, otherwise empty
391 ShaderEffect GetShaderEffect( const std::string &name );
394 * Get or create FrameBufferImage from the FrameBufferImage instance library.
395 * An empty handle is returned otherwise.
396 * @pre The Builder has been initialized.
397 * @param name The name of a FrameBufferImage in the loaded representation
398 * @return A handle to a FrameBufferImage if found, otherwise empty
400 FrameBufferImage GetFrameBufferImage( const std::string &name );
403 * Get or create Path from the Path instance library.
404 * An empty handle is returned otherwise.
405 * @pre The Builder has been initialized.
406 * @param name The name of a Path in the loaded representation
407 * @return A handle to a Path if found, otherwise empty
409 Path GetPath( const std::string &name );
414 * @brief Builder signal type
416 typedef Signal< void () > BuilderSignalType;
419 * @brief Signal emitted when a quit action is requested by the builder.
421 BuilderSignalType& QuitSignal();
424 explicit DALI_INTERNAL Builder(Internal::Builder *impl);
428 } // namespace Toolkit
432 #endif // __DALI_TOOLKIT_UIBUILDER_H__