1 #ifndef __DALI_TOOLKIT_UIBUILDER_H__
2 #define __DALI_TOOLKIT_UIBUILDER_H__
5 * Copyright (c) 2015 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/animation/linear-constrainer.h>
25 #include <dali/devel-api/animation/path-constrainer.h>
26 #include <dali/public-api/images/frame-buffer-image.h>
27 #include <dali/public-api/shader-effects/shader-effect.h>
35 namespace Internal DALI_INTERNAL
41 * This class provides the ability to load and style an actor tree from a string representation.
43 * The following is an example in JSON.
48 * "templates": // are named instantiable actor trees
54 * "parentOrigin":[0.5,0.5,0],
58 * "styles": // are named property sets applied to actor trees
62 * "size": [10,10,1] // root properties applied to a given root actor
63 * "actors": // properties applied to actors found by name from root
65 * "ok": // properties for an actor named "ok"
79 * "type":"defaultText",
80 * "text":"Hello World",
88 * The following shows a method to load the json file.
90 * Builder builder = Builder::New();
91 * std::string json_data(ReadFile("layout.json"));
92 * builder.LoadFromString(json_data);
95 * - Load all actors in the "stage" section to the root layer
97 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
100 * - Create an actor tree from the "templates" section
102 * TextActor actor = TextActor::DownCast( builder.Create( "defaultText" ) );
105 * - Style an actor tree from the "styles" section
107 * builder.ApplyStyle( "myStyle", actor );
110 * - Create an actor tree from json
112 * TextActor actor = TextActor::DownCast( builder.CreateFromJson("{\"type\":\"TextActor\",\"font\":\"\",\"scale\":[50,50,1]}") );
115 * - Apply a style to an actor tree from json
117 * builder.ApplyFromJson( textActor, ("{\"scale\":[5,5,1]}") );
122 class DALI_IMPORT_API Builder : public BaseHandle
126 * Create an Builder handle; this can be initialised with Builder::New()
127 * Calling member functions with an uninitialised handle is not allowed.
132 * Creates an Builder object.
133 * @return A handle to the Builder control.
135 static Builder New();
140 * This is non-virtual since derived Handle types must not contain data or virtual methods.
145 * UI string data format
149 JSON, ///< String is JSON
153 * Loads a string representation of an actor tree into memory.
154 * The Actor is not automatically added to the stage.
155 * This function will raise an exception for parse and logical structure errors.
156 * @pre The Builder has been initialized.
157 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
158 * @param data A string represenation of an Actor tree
159 * @param format The string representation format ie JSON
161 void LoadFromString( const std::string& data, UIFormat format = JSON );
164 * @brief Adds user defined constants to all future style template or animation expansions
168 * map["IMAGE_DIRECTORY"] = "/usr/share/images";
169 * builder.AddConstants( map );
171 * @pre The Builder has been initialized.
172 * @param map The user defined constants used in template expansions.
174 void AddConstants( const Property::Map& map );
177 * @brief Adds or modifies a user defined constant to all future style template or animation expansions
181 * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
184 * @pre The Builder has been initialized.
185 * @param key The constant name to add or update
186 * @param value The new value for the constant.
188 void AddConstant( const std::string& key, const Property::Value& value );
191 * @brief Gets all currently defined constants.
195 * Property::Map map = builder.GetConstants(); // get copy of current constants
196 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
197 * builder.AddConstants( map ); // write back changes
200 * @pre The Builder has been initialized.
201 * @return A reference to the currently defined constants.
203 const Property::Map& GetConstants() const;
206 * @brief Gets a currently defined constant, or returns Property::INVALID
210 * Property::Map map = builder.GetConstants(); // get copy of current constants
211 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
212 * builder.AddConstants( map ); // write back changes
215 * @pre The Builder has been initialized.
216 * @param key The constant name to search for.
218 const Property::Value& GetConstant( const std::string& key ) const;
221 * Creates an animation from the set of known animations
223 * Animation a = builder.CreateAnimation( "wobble");
225 * @pre The Builder has been initialized.
226 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
227 * @pre The animationName exists in the animations section of the data representation
228 * @param animationName The animation name to create
229 * @returns The base handle of the created object
231 Animation CreateAnimation( const std::string& animationName );
234 * @brief Creates an animation from the set of known animations with user defined constants
238 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
239 * Animation a = builder.CreateAnimation( "wobble");
241 * @pre The Builder has been initialized.
242 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
243 * @pre The animationName exists in the animations section of the data representation
244 * @pre The map contains all the constant expansions in the style template
245 * @param animationName The animation name to create
246 * @param map The user defined constants used in style template expansion.
247 * @returns The base handle of the created object
249 Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
252 * @brief Creates an animation from the set of known animations.
254 * The animation is applied to a specific actor.
256 * Actor myInstance = builder.Create( "templateActorTree" )
257 * Animation a = builder.CreateAnimation( "wobble", myInstance );
259 * @pre The Builder has been initialized.
260 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
261 * @pre The animationName exists in the animations section of the data representation
262 * @param animationName The animation name to create
263 * @param sourceActor The starting point in an actor tree, from which to look for property owners
264 * @returns The base handle of the created object
266 Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
269 * @brief Creates an animation from the set of known animations with user defined constants
271 * The animation is applied to a specific actor.
274 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
275 * Actor myInstance = builder.Create( "templateActorTree" )
276 * Animation a = builder.CreateAnimation( "wobble", myInstance);
278 * @pre The Builder has been initialized.
279 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
280 * @pre The animationName exists in the animations section of the data representation
281 * @pre The map contains all the constant expansions in the style template
282 * @param animationName The animation name to create
283 * @param map The user defined constants used in style template expansion.
284 * @param sourceActor The starting point in an actor tree, from which to look for property owners
285 * @returns The base handle of the created object
287 Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
290 * @brief Creates an object (e.g. an actor) from the set of known style templates
293 * mActor.Add( Actor::DownCast(builder.Create( "defaultText")) );
295 * @pre The Builder has been initialized.
296 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
297 * @pre The templateName exists in the templates section of the data representation
298 * and contains 'type' property used to create the object.
299 * @param templateName The template to apply in creation.
300 * @returns The base handle of the created object
302 BaseHandle Create( const std::string& templateName );
305 * @brief Creates an object from the style templates with user defined constants
309 * map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
310 * mActor.Add( Actor::DownCast(builder.Create( "defaultImage", map) ) );
312 * @pre The Builder has been initialized.
313 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
314 * @pre The templateName exists in the templates section of the data representation
315 * and contains 'type' property used to create the object.
316 * @param templateName The template used to create the object.
317 * @param map The user defined constants used in template expansion.
318 * @returns The base handle of the created object
320 BaseHandle Create( const std::string& templateName, const Property::Map& map );
323 * @brief Creates an object (e.g. an actor) from given json snippet
326 * Actor a = Actor::DownCast(builder.CreateFromJson( "{\"type\":\"TextActor\"}"));
328 * @pre The Builder has been initialized.
329 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
330 * @param json The json snippet used to create the object.
331 * @returns The base handle of the created object if any
333 BaseHandle CreateFromJson( const std::string& json );
336 * Apply a style (a collection of properties) to an actor.
337 * @pre The Builder has been initialized.
338 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
339 * @param styleName The name of the set of style properties to set on the handle object.
340 * @param handle Then handle of the object on which to set the properties.
342 * @return Return true if the style was found
344 bool ApplyStyle( const std::string& styleName, Handle& handle );
347 * Apply a style (a collection of properties) to an actor from the given json snippet
348 * @pre The Builder has been initialized.
349 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
350 * @param handle Then handle of the object on which to set the properties.
351 * @param json The json snippet used to create the object.
353 * @return Return true if the json snippet was parsed
355 bool ApplyFromJson( Handle& handle, const std::string& json );
359 * Add the actor tree in the "stage" section to the actor toActor.
360 * ie if the representation has a 'stage' section that contains a tree of
362 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
363 * will create and add the actors to the stage root layer.
364 * @param toActor The actor to add the created actors to
366 void AddActors( Actor toActor );
369 * Adds actors in the sectionName to the actor toActor.
370 * ie if the representation has a sectionName section that contains a tree of
372 * builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
373 * will create and add the actors to the stage root layer.
374 * @param sectionName The section name to search for the actor tree
375 * @param toActor The actor to add the created actors to
377 void AddActors( const std::string §ionName, Actor toActor );
380 * Create a render task set.
381 * @pre The Builder has been initialized.
382 * @param name The library name of the render task set.
384 void CreateRenderTask( const std::string &name );
387 * Get or create ShaderEffect from the ShaderEffect instance library.
388 * An empty handle is returned otherwise.
389 * @pre The Builder has been initialized.
390 * @param name The name of a ShaderEffect in the loaded representation
391 * @return A handle to a ShaderEffect if found, otherwise empty
393 ShaderEffect GetShaderEffect( const std::string &name );
396 * Get or create FrameBufferImage from the FrameBufferImage instance library.
397 * An empty handle is returned otherwise.
398 * @pre The Builder has been initialized.
399 * @param name The name of a FrameBufferImage in the loaded representation
400 * @return A handle to a FrameBufferImage if found, otherwise empty
402 FrameBufferImage GetFrameBufferImage( const std::string &name );
405 * Get or create Path from the Path instance library.
406 * An empty handle is returned otherwise.
407 * @pre The Builder has been initialized.
408 * @param name The name of a Path in the loaded representation
409 * @return A handle to a Path if found, otherwise empty
411 Path GetPath( const std::string &name );
414 * Get or create a PathConstrainer from the set of known PathConstrainers
416 * PathConstrainer a = builder.GetPathConstrainer( "myPathConstrainer");
418 * @pre The Builder has been initialized.
419 * @pre The pathConstrainerName exists in the Constrainers section of the data representation
420 * @param pathConstrainerName The name of the PathConstrainer
421 * @returns A handle to a PathConstrainer if found, otherwise empty
423 PathConstrainer GetPathConstrainer( const std::string& pathConstrainerName );
426 * Get or create a LinearConstrainer from the set of known LinearConstrainers
428 * LinearConstrainer a = builder.GetLinearConstrainer( "myLinearConstrainer");
430 * @pre The Builder has been initialized.
431 * @pre The linearConstrainerName exists in the Constrainers section of the data representation
432 * @param linearConstrainerName The name of the LinearConstrainer
433 * @returns A handle to a LinearConstrainer if found, otherwise empty
435 LinearConstrainer GetLinearConstrainer( const std::string& linearConstrainerName );
440 * @brief Builder signal type
442 typedef Signal< void () > BuilderSignalType;
445 * @brief Signal emitted when a quit action is requested by the builder.
447 BuilderSignalType& QuitSignal();
450 explicit DALI_INTERNAL Builder(Internal::Builder *impl);
454 } // namespace Toolkit
458 #endif // __DALI_TOOLKIT_UIBUILDER_H__