1 #ifndef DALI_TOOLKIT_UIBUILDER_H
2 #define DALI_TOOLKIT_UIBUILDER_H
5 * Copyright (c) 2020 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>
28 #include <dali-toolkit/public-api/dali-toolkit-common.h>
36 namespace Internal DALI_INTERNAL
42 * This class provides the ability to load and style an actor tree from a string representation.
44 * The following is an example in JSON.
49 * "templates": // are named instantiable actor trees
55 * "parentOrigin":[0.5,0.5,0],
59 * "styles": // are named property sets applied to actor trees
63 * "size": [10,10,1] // root properties applied to a given root actor
64 * "actors": // properties applied to actors found by name from root
66 * "ok": // properties for an actor named "ok"
80 * "type":"defaultText",
81 * "text":"Hello World",
89 * The following shows a method to load the json file.
91 * Builder builder = Builder::New();
92 * std::string json_data(ReadFile("layout.json"));
93 * builder.LoadFromString(json_data);
96 * - Load all actors in the "stage" section to the root layer
98 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
101 * - Create an actor tree from the "templates" section
103 * TextActor actor = TextActor::DownCast( builder.Create( "defaultText" ) );
106 * - Style an actor tree from the "styles" section
108 * builder.ApplyStyle( "myStyle", actor );
111 * - Create an actor tree from json
113 * TextActor actor = TextActor::DownCast( builder.CreateFromJson("{\"type\":\"TextActor\",\"font\":\"\",\"scale\":[50,50,1]}") );
116 * - Apply a style to an actor tree from json
118 * builder.ApplyFromJson( textActor, ("{\"scale\":[5,5,1]}") );
123 class DALI_TOOLKIT_API Builder : public BaseHandle
127 * Create an Builder handle; this can be initialised with Builder::New()
128 * Calling member functions with an uninitialised handle is not allowed.
133 * Creates an Builder object.
134 * @return A handle to the Builder control.
136 static Builder New();
141 * This is non-virtual since derived Handle types must not contain data or virtual methods.
146 * UI string data format
150 JSON, ///< String is JSON
154 * Loads a string representation of an actor tree into memory.
155 * The Actor is not automatically added to the stage.
156 * This function will raise an exception for parse and logical structure errors.
157 * @pre The Builder has been initialized.
158 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
159 * @param data A string represenation of an Actor tree
160 * @param format The string representation format ie JSON
162 void LoadFromString( const std::string& data, UIFormat format = JSON );
165 * @brief Adds user defined constants to all future style template or animation expansions
169 * map["IMAGE_DIRECTORY"] = "/usr/share/images";
170 * builder.AddConstants( map );
172 * @pre The Builder has been initialized.
173 * @param map The user defined constants used in template expansions.
175 void AddConstants( const Property::Map& map );
178 * @brief Adds or modifies a user defined constant to all future style template or animation expansions
182 * builder.AddConstant( "IMAGE_DIRECTORY", "/usr/share/images" );
185 * @pre The Builder has been initialized.
186 * @param key The constant name to add or update
187 * @param value The new value for the constant.
189 void AddConstant( const std::string& key, const Property::Value& value );
192 * @brief Gets all currently defined configurations.
194 * @pre The Builder has been initialized.
195 * @return A reference to the currently defined configurations.
197 const Property::Map& GetConfigurations() const;
200 * @brief Gets all currently defined constants.
204 * Property::Map map = builder.GetConstants(); // get copy of current constants
205 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
206 * builder.AddConstants( map ); // write back changes
209 * @pre The Builder has been initialized.
210 * @return A reference to the currently defined constants.
212 const Property::Map& GetConstants() const;
215 * @brief Gets a currently defined constant, or returns Property::INVALID
219 * Property::Map map = builder.GetConstants(); // get copy of current constants
220 * map["IMAGE_DIRECTORY"] = "/usr/share/images"; // make modification
221 * builder.AddConstants( map ); // write back changes
224 * @pre The Builder has been initialized.
225 * @param key The constant name to search for.
227 const Property::Value& GetConstant( const std::string& key ) const;
230 * Creates an animation from the set of known animations
232 * Animation a = builder.CreateAnimation( "wobble");
234 * @pre The Builder has been initialized.
235 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
236 * @pre The animationName exists in the animations section of the data representation
237 * @param animationName The animation name to create
238 * @returns The base handle of the created object
240 Animation CreateAnimation( const std::string& animationName );
243 * @brief Creates an animation from the set of known animations with user defined constants
247 * map["ACTOR"] = actor.GetProperty< std::string >( Dali::Actor::Property::NAME ); // replaces '{ACTOR} in the template
248 * Animation a = builder.CreateAnimation( "wobble");
250 * @pre The Builder has been initialized.
251 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
252 * @pre The animationName exists in the animations section of the data representation
253 * @pre The map contains all the constant expansions in the style template
254 * @param animationName The animation name to create
255 * @param map The user defined constants used in style template expansion.
256 * @returns The base handle of the created object
258 Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
261 * @brief Creates an animation from the set of known animations.
263 * The animation is applied to a specific actor.
265 * Actor myInstance = builder.Create( "templateActorTree" )
266 * Animation a = builder.CreateAnimation( "wobble", myInstance );
268 * @pre The Builder has been initialized.
269 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
270 * @pre The animationName exists in the animations section of the data representation
271 * @param animationName The animation name to create
272 * @param sourceActor The starting point in an actor tree, from which to look for property owners
273 * @returns The base handle of the created object
275 Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
278 * @brief Creates an animation from the set of known animations with user defined constants
280 * The animation is applied to a specific actor.
283 * map["ACTOR"] = actor.GetProperty< std::string >( Dali::Actor::Property::NAME ); // replaces '{ACTOR} in the template
284 * Actor myInstance = builder.Create( "templateActorTree" )
285 * Animation a = builder.CreateAnimation( "wobble", myInstance);
287 * @pre The Builder has been initialized.
288 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
289 * @pre The animationName exists in the animations section of the data representation
290 * @pre The map contains all the constant expansions in the style template
291 * @param animationName The animation name to create
292 * @param map The user defined constants used in style template expansion.
293 * @param sourceActor The starting point in an actor tree, from which to look for property owners
294 * @returns The base handle of the created object
296 Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
299 * @brief Creates an object (e.g. an actor) from the set of known style templates
302 * mActor.Add( Actor::DownCast(builder.Create( "defaultText")) );
304 * @pre The Builder has been initialized.
305 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
306 * @pre The templateName exists in the templates section of the data representation
307 * and contains 'type' property used to create the object.
308 * @param templateName The template to apply in creation.
309 * @returns The base handle of the created object
311 BaseHandle Create( const std::string& templateName );
314 * @brief Creates an object from the style templates with user defined constants
318 * map["IMAGE_DIR"] = "/usr/share/images"; // replaces '{IMAGE_DIR} in the template
319 * mActor.Add( Actor::DownCast(builder.Create( "defaultImage", map) ) );
321 * @pre The Builder has been initialized.
322 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
323 * @pre The templateName exists in the templates section of the data representation
324 * and contains 'type' property used to create the object.
325 * @param templateName The template used to create the object.
326 * @param map The user defined constants used in template expansion.
327 * @returns The base handle of the created object
329 BaseHandle Create( const std::string& templateName, const Property::Map& map );
332 * @brief Creates an object (e.g. an actor) from given json snippet
335 * Actor a = Actor::DownCast(builder.CreateFromJson( "{\"type\":\"TextActor\"}"));
337 * @pre The Builder has been initialized.
338 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
339 * @param json The json snippet used to create the object.
340 * @returns The base handle of the created object if any
342 BaseHandle CreateFromJson( const std::string& json );
345 * Apply a style (a collection of properties) to an actor.
346 * @pre The Builder has been initialized.
347 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
348 * @param styleName The name of the set of style properties to set on the handle object.
349 * @param handle Then handle of the object on which to set the properties.
351 * @return Return true if the style was found
353 bool ApplyStyle( const std::string& styleName, Handle& handle );
356 * Apply a style (a collection of properties) to an actor from the given json snippet
357 * @pre The Builder has been initialized.
358 * @pre Preconditions have been met for creating dali objects ie Images, Actors etc
359 * @param handle Then handle of the object on which to set the properties.
360 * @param json The json snippet used to create the object.
362 * @return Return true if the json snippet was parsed
364 bool ApplyFromJson( Handle& handle, const std::string& json );
368 * Add the actor tree in the "stage" section to the actor toActor.
369 * ie if the representation has a 'stage' section that contains a tree of
371 * builder.AddActors( Stage::GetCurrent().GetRootLayer() );
372 * will create and add the actors to the stage root layer.
373 * @param toActor The actor to add the created actors to
375 void AddActors( Actor toActor );
378 * Adds actors in the sectionName to the actor toActor.
379 * ie if the representation has a sectionName section that contains a tree of
381 * builder.AddActors( sectionName, Stage::GetCurrent().GetRootLayer() );
382 * will create and add the actors to the stage root layer.
383 * @param sectionName The section name to search for the actor tree
384 * @param toActor The actor to add the created actors to
386 void AddActors( const std::string §ionName, Actor toActor );
389 * Create a render task set.
390 * @pre The Builder has been initialized.
391 * @param name The library name of the render task set.
393 void CreateRenderTask( const std::string &name );
396 * Get or create Path from the Path instance library.
397 * An empty handle is returned otherwise.
398 * @pre The Builder has been initialized.
399 * @param name The name of a Path in the loaded representation
400 * @return A handle to a Path if found, otherwise empty
402 Path GetPath( const std::string &name );
405 * Get or create a PathConstrainer from the set of known PathConstrainers
407 * PathConstrainer a = builder.GetPathConstrainer( "myPathConstrainer");
409 * @pre The Builder has been initialized.
410 * @pre The pathConstrainerName exists in the Constrainers section of the data representation
411 * @param pathConstrainerName The name of the PathConstrainer
412 * @returns A handle to a PathConstrainer if found, otherwise empty
414 PathConstrainer GetPathConstrainer( const std::string& pathConstrainerName );
417 * Get or create a LinearConstrainer from the set of known LinearConstrainers
419 * LinearConstrainer a = builder.GetLinearConstrainer( "myLinearConstrainer");
421 * @pre The Builder has been initialized.
422 * @pre The linearConstrainerName exists in the Constrainers section of the data representation
423 * @param linearConstrainerName The name of the LinearConstrainer
424 * @returns A handle to a LinearConstrainer if found, otherwise empty
426 LinearConstrainer GetLinearConstrainer( const std::string& linearConstrainerName );
431 * @brief Builder signal type
433 typedef Signal< void () > BuilderSignalType;
436 * @brief Signal emitted when a quit action is requested by the builder.
438 BuilderSignalType& QuitSignal();
441 explicit DALI_INTERNAL Builder(Internal::Builder *impl);
445 } // namespace Toolkit
449 #endif // DALI_TOOLKIT_UIBUILDER_H