1 #ifndef __DALI_TOOLKIT_UIBUILDER_H__
2 #define __DALI_TOOLKIT_UIBUILDER_H__
5 * Copyright (c) 2016 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>
34 namespace Internal DALI_INTERNAL
40 * This class provides the ability to load and style an actor tree from a string representation.
42 * The following is an example in JSON.
47 * "templates": // are named instantiable actor trees
53 * "parentOrigin":[0.5,0.5,0],
57 * "styles": // are named property sets applied to actor trees
61 * "size": [10,10,1] // root properties applied to a given root actor
62 * "actors": // properties applied to actors found by name from root
64 * "ok": // properties for an actor named "ok"
78 * "type":"defaultText",
79 * "text":"Hello World",
87 * The following shows a method to load the json file.
89 * Builder builder = Builder::New();
90 * std::string json_data(ReadFile("layout.json"));
91 * builder.LoadFromString(json_data);
94 * - Load all actors in the "stage" section to the root layer
96 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
99 * - Create an actor tree from the "templates" section
101 * TextActor actor = TextActor::DownCast( builder.Create( "defaultText" ) );
104 * - Style an actor tree from the "styles" section
106 * builder.ApplyStyle( "myStyle", actor );
109 * - Create an actor tree from json
111 * TextActor actor = TextActor::DownCast( builder.CreateFromJson("{\"type\":\"TextActor\",\"font\":\"\",\"scale\":[50,50,1]}") );
114 * - Apply a style to an actor tree from json
116 * builder.ApplyFromJson( textActor, ("{\"scale\":[5,5,1]}") );
121 class DALI_IMPORT_API Builder : public BaseHandle
125 * Create an Builder handle; this can be initialised with Builder::New()
126 * Calling member functions with an uninitialised handle is not allowed.
131 * Creates an Builder object.
132 * @return A handle to the Builder control.
134 static Builder New();
139 * This is non-virtual since derived Handle types must not contain data or virtual methods.
144 * UI string data format
148 JSON, ///< String is JSON
152 * Loads a string representation of an actor tree into memory.
153 * The Actor is not automatically added to the stage.
154 * This function will raise an exception for parse and logical structure errors.
155 * @pre The Builder has been initialized.
156 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
157 * @param data A string represenation of an Actor tree
158 * @param format The string representation format ie JSON
160 void LoadFromString( const std::string& data, UIFormat format = JSON );
163 * @brief Adds user defined constants to all future style template or animation expansions
167 * map["IMAGE_DIRECTORY"] = "/usr/share/images";
168 * builder.AddConstants( map );
170 * @pre The Builder has been initialized.
171 * @param map The user defined constants used in template expansions.
173 void AddConstants( const Property::Map& map );
176 * @brief Adds or modifies a user defined constant to all future style template or animation expansions
180 * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
183 * @pre The Builder has been initialized.
184 * @param key The constant name to add or update
185 * @param value The new value for the constant.
187 void AddConstant( const std::string& key, const Property::Value& value );
190 * @brief Gets all currently defined configurations.
192 * @pre The Builder has been initialized.
193 * @return A reference to the currently defined configurations.
195 const Property::Map& GetConfigurations() const;
198 * @brief Gets all currently defined constants.
202 * Property::Map map = builder.GetConstants(); // get copy of current constants
203 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
204 * builder.AddConstants( map ); // write back changes
207 * @pre The Builder has been initialized.
208 * @return A reference to the currently defined constants.
210 const Property::Map& GetConstants() const;
213 * @brief Gets a currently defined constant, or returns Property::INVALID
217 * Property::Map map = builder.GetConstants(); // get copy of current constants
218 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
219 * builder.AddConstants( map ); // write back changes
222 * @pre The Builder has been initialized.
223 * @param key The constant name to search for.
225 const Property::Value& GetConstant( const std::string& key ) const;
228 * Creates an animation from the set of known animations
230 * Animation a = builder.CreateAnimation( "wobble");
232 * @pre The Builder has been initialized.
233 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
234 * @pre The animationName exists in the animations section of the data representation
235 * @param animationName The animation name to create
236 * @returns The base handle of the created object
238 Animation CreateAnimation( const std::string& animationName );
241 * @brief Creates an animation from the set of known animations with user defined constants
245 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
246 * Animation a = builder.CreateAnimation( "wobble");
248 * @pre The Builder has been initialized.
249 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
250 * @pre The animationName exists in the animations section of the data representation
251 * @pre The map contains all the constant expansions in the style template
252 * @param animationName The animation name to create
253 * @param map The user defined constants used in style template expansion.
254 * @returns The base handle of the created object
256 Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
259 * @brief Creates an animation from the set of known animations.
261 * The animation is applied to a specific actor.
263 * Actor myInstance = builder.Create( "templateActorTree" )
264 * Animation a = builder.CreateAnimation( "wobble", myInstance );
266 * @pre The Builder has been initialized.
267 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
268 * @pre The animationName exists in the animations section of the data representation
269 * @param animationName The animation name to create
270 * @param sourceActor The starting point in an actor tree, from which to look for property owners
271 * @returns The base handle of the created object
273 Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
276 * @brief Creates an animation from the set of known animations with user defined constants
278 * The animation is applied to a specific actor.
281 * map["ACTOR"] = actor.GetName(); // replaces '{ACTOR} in the template
282 * Actor myInstance = builder.Create( "templateActorTree" )
283 * Animation a = builder.CreateAnimation( "wobble", myInstance);
285 * @pre The Builder has been initialized.
286 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
287 * @pre The animationName exists in the animations section of the data representation
288 * @pre The map contains all the constant expansions in the style template
289 * @param animationName The animation name to create
290 * @param map The user defined constants used in style template expansion.
291 * @param sourceActor The starting point in an actor tree, from which to look for property owners
292 * @returns The base handle of the created object
294 Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
297 * @brief Creates an object (e.g. an actor) from the set of known style templates
300 * mActor.Add( Actor::DownCast(builder.Create( "defaultText")) );
302 * @pre The Builder has been initialized.
303 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
304 * @pre The templateName exists in the templates section of the data representation
305 * and contains 'type' property used to create the object.
306 * @param templateName The template to apply in creation.
307 * @returns The base handle of the created object
309 BaseHandle Create( const std::string& templateName );
312 * @brief Creates an object from the style templates with user defined constants
316 * map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
317 * mActor.Add( Actor::DownCast(builder.Create( "defaultImage", map) ) );
319 * @pre The Builder has been initialized.
320 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
321 * @pre The templateName exists in the templates section of the data representation
322 * and contains 'type' property used to create the object.
323 * @param templateName The template used to create the object.
324 * @param map The user defined constants used in template expansion.
325 * @returns The base handle of the created object
327 BaseHandle Create( const std::string& templateName, const Property::Map& map );
330 * @brief Creates an object (e.g. an actor) from given json snippet
333 * Actor a = Actor::DownCast(builder.CreateFromJson( "{\"type\":\"TextActor\"}"));
335 * @pre The Builder has been initialized.
336 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
337 * @param json The json snippet used to create the object.
338 * @returns The base handle of the created object if any
340 BaseHandle CreateFromJson( const std::string& json );
343 * Apply a style (a collection of properties) to an actor.
344 * @pre The Builder has been initialized.
345 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
346 * @param styleName The name of the set of style properties to set on the handle object.
347 * @param handle Then handle of the object on which to set the properties.
349 * @return Return true if the style was found
351 bool ApplyStyle( const std::string& styleName, Handle& handle );
354 * Apply a style (a collection of properties) to an actor from the given json snippet
355 * @pre The Builder has been initialized.
356 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
357 * @param handle Then handle of the object on which to set the properties.
358 * @param json The json snippet used to create the object.
360 * @return Return true if the json snippet was parsed
362 bool ApplyFromJson( Handle& handle, const std::string& json );
366 * Add the actor tree in the "stage" section to the actor toActor.
367 * ie if the representation has a 'stage' section that contains a tree of
369 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
370 * will create and add the actors to the stage root layer.
371 * @param toActor The actor to add the created actors to
373 void AddActors( Actor toActor );
376 * Adds actors in the sectionName to the actor toActor.
377 * ie if the representation has a sectionName section that contains a tree of
379 * builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
380 * will create and add the actors to the stage root layer.
381 * @param sectionName The section name to search for the actor tree
382 * @param toActor The actor to add the created actors to
384 void AddActors( const std::string §ionName, Actor toActor );
387 * Create a render task set.
388 * @pre The Builder has been initialized.
389 * @param name The library name of the render task set.
391 void CreateRenderTask( 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 );
412 * Get or create a PathConstrainer from the set of known PathConstrainers
414 * PathConstrainer a = builder.GetPathConstrainer( "myPathConstrainer");
416 * @pre The Builder has been initialized.
417 * @pre The pathConstrainerName exists in the Constrainers section of the data representation
418 * @param pathConstrainerName The name of the PathConstrainer
419 * @returns A handle to a PathConstrainer if found, otherwise empty
421 PathConstrainer GetPathConstrainer( const std::string& pathConstrainerName );
424 * Get or create a LinearConstrainer from the set of known LinearConstrainers
426 * LinearConstrainer a = builder.GetLinearConstrainer( "myLinearConstrainer");
428 * @pre The Builder has been initialized.
429 * @pre The linearConstrainerName exists in the Constrainers section of the data representation
430 * @param linearConstrainerName The name of the LinearConstrainer
431 * @returns A handle to a LinearConstrainer if found, otherwise empty
433 LinearConstrainer GetLinearConstrainer( const std::string& linearConstrainerName );
438 * @brief Builder signal type
440 typedef Signal< void () > BuilderSignalType;
443 * @brief Signal emitted when a quit action is requested by the builder.
445 BuilderSignalType& QuitSignal();
448 explicit DALI_INTERNAL Builder(Internal::Builder *impl);
452 } // namespace Toolkit
456 #endif // __DALI_TOOLKIT_UIBUILDER_H__