[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / internal / builder / builder-impl.h
1 #ifndef DALI_TOOLKIT_INTERNAL_BUILDER_H
2 #define DALI_TOOLKIT_INTERNAL_BUILDER_H
3
4 /*
5  * Copyright (c) 2019 Samsung Electronics Co., Ltd.
6  *
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <string>
23 #include <list>
24 #include <map>
25 #include <dali/public-api/common/stage.h>
26 #include <dali/public-api/common/vector-wrapper.h>
27 #include <dali/public-api/actors/actor.h>
28 #include <dali/public-api/object/base-object.h>
29 #include <dali/public-api/object/property-map.h>
30 #include <dali/public-api/render-tasks/render-task.h>
31 #include <dali/integration-api/debug.h>
32
33 // INTERNAL INCLUDES
34 #include <dali-toolkit/devel-api/builder/json-parser.h>
35 #include <dali-toolkit/devel-api/builder/builder.h>
36 #include <dali-toolkit/internal/builder/builder-declarations.h>
37 #include <dali-toolkit/internal/builder/style.h>
38
39 // Warning messages usually displayed
40 #define DALI_SCRIPT_WARNING(format, ...) \
41   DALI_LOG_WARNING("Script:" format, ## __VA_ARGS__)
42
43 // Info messages are usually debug build
44 #define DALI_SCRIPT_INFO(format, ...) \
45   DALI_LOG_INFO(Dali::Toolkit::Internal::gFilterScript, Debug::General, "Script:" format, ## __VA_ARGS__)
46
47 // Info Verbose need to be swiched on in gFilterScript filter constructor (by default set to General)
48 #define DALI_SCRIPT_VERBOSE(format, ...) \
49   DALI_LOG_INFO(Dali::Toolkit::Internal::gFilterScript, Debug::Verbose, "Script:" format, ## __VA_ARGS__)
50
51 namespace Dali
52 {
53
54 namespace Toolkit
55 {
56   class TreeNode;
57 }
58
59 namespace Toolkit
60 {
61
62 namespace Internal
63 {
64
65 #if defined(DEBUG_ENABLED)
66 extern Dali::Integration::Log::Filter* gFilterScript;
67 #endif
68
69 class Builder;
70 class Replacement;
71
72 /**
73  * @copydoc Toolkit::Builder
74  */
75 class Builder : public Dali::BaseObject
76 {
77 public:
78
79   Builder();
80
81   /**
82    * @copydoc Toolkit::Builder::LoadFromString
83    */
84   void LoadFromString( const std::string &data,
85                        Dali::Toolkit::Builder::UIFormat rep = Dali::Toolkit::Builder::JSON );
86
87   /**
88    * @copydoc Toolkit::Builder::AddConstants
89    */
90   void AddConstants( const Property::Map& map );
91
92   /**
93    * @copydoc Toolkit::Builder::AddConstant
94    */
95   void AddConstant( const std::string& key, const Property::Value& value );
96
97   /**
98    * @copydoc Toolkit::Builder::GetConfigurations
99    */
100   const Property::Map& GetConfigurations() const;
101
102   /**
103    * @copydoc Toolkit::Builder::GetConstants
104    */
105   const Property::Map& GetConstants() const;
106
107   /**
108    * @copydoc Toolkit::Builder::GetConstant
109    */
110   const Property::Value& GetConstant( const std::string& key ) const;
111
112   /**
113    * @copydoc Toolkit::Builder::CreateAnimation( const std::string& animationName );
114    */
115   Animation CreateAnimation( const std::string& animationName );
116
117   /**
118    * @copydoc Toolkit::Builder::CreateAnimation( const std::string& animationName, const Property::Map& map );
119    */
120   Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
121
122   /**
123    * @copydoc Toolkit::Builder::CreateAnimation( const std::string&,Dali::Actor);
124    */
125   Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
126
127   /**
128    * @copydoc Toolkit::Builder::CreateAnimation( const std::string&,const Property::Map&, Dali::Actor);
129    */
130   Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
131
132   /**
133    * @copydoc Toolkit::Builder::Create( const std::string& templateName );
134    */
135   BaseHandle Create( const std::string& templateName );
136
137   /**
138    * @copydoc Toolkit::Builder::Create( const std::string& templateName, const Property::Map& map );
139    */
140   BaseHandle Create( const std::string& templateName, const Property::Map& map );
141
142   /**
143    * @copydoc Toolkit::Builder::CreateFromJson( const std::string& json );
144    */
145   BaseHandle CreateFromJson( const std::string& json );
146
147   /**
148    * @copydoc Toolkit::Builder::ApplyFromJson( Handle& handle, const std::string& json );
149    */
150   bool ApplyFromJson(  Handle& handle, const std::string& json );
151
152   /**
153    * @copydoc Toolkit::Builder::ApplyStyle
154    */
155   bool ApplyStyle( const std::string& styleName, Handle& handle );
156
157   /**
158    * Lookup the stylename in builder. If it's found in the parse tree,
159    * then return true.
160    * @param[in] styleName The style name to search for
161    * @return true if the stylename exists
162    */
163   bool LookupStyleName( const std::string& styleName );
164
165   /**
166    * Lookup the stylename in the recorded Styles - if it exists,
167    * performs a shallow copy to the passed in style and returns true.
168    * Otherwise it returns false.
169
170    * @param[in] styleName The stylename to search for
171    * @return A const pointer to the style object
172    */
173   const StylePtr GetStyle( const std::string& styleName );
174
175   /**
176    * @copydoc Toolkit::Builder::AddActors
177    */
178   void AddActors( Actor toActor );
179
180   /**
181    * @copydoc Toolkit::Builder::AddActors
182    */
183   void AddActors( const std::string &sectionName, Actor toActor );
184
185   /**
186    * @copydoc Toolkit::Builder::CreateRenderTask
187    */
188   void CreateRenderTask( const std::string &name );
189
190   /**
191    * @copydoc Toolkit::Builder::GetPath
192    */
193   Path GetPath( const std::string &name );
194
195   /**
196    * @copydoc Toolkit::Builder::GetPathConstrainer
197    */
198   Dali::PathConstrainer GetPathConstrainer( const std::string& name );
199
200   /*
201    * Check if a given constrainer is of type PathConstrainer
202    * @param[in] name The name of the constrainer
203    * @return True if constainer is of type PathConstrainer, False otherwise
204    *
205    */
206   bool IsPathConstrainer( const std::string& name );
207
208   /**
209    * @copydoc Toolkit::Builder::GetLinearConstrainer
210    */
211   Dali::LinearConstrainer GetLinearConstrainer( const std::string& name );
212
213   /*
214    * Check if a given constrainer is of type LinearConstrainer
215    * @param[in] name The name of the constrainer
216    * @return True if constainer is of type LinearConstrainer, False otherwise
217    *
218    */
219   bool IsLinearConstrainer( const std::string& name );
220
221   /**
222    * @copydoc Toolkit::Builder::QuitSignal
223    */
224   Toolkit::Builder::BuilderSignalType& QuitSignal();
225
226   /**
227    * Emits the quit signal
228    */
229   void EmitQuitSignal();
230
231
232 protected:
233
234   virtual ~Builder();
235
236 private:
237   typedef std::vector<const char*> KeyStack;
238   typedef std::vector< TreeNode::KeyNodePair > MappingsLut;
239   typedef struct{ std::string name; Dali::LinearConstrainer linearConstrainer; } LinearConstrainerEntry;
240   typedef std::vector<LinearConstrainerEntry> LinearConstrainerLut;
241   typedef struct{ std::string name; Dali::PathConstrainer pathConstrainer; } PathConstrainerEntry;
242   typedef std::vector<PathConstrainerEntry> PathConstrainerLut;
243   typedef std::map<const std::string, Path> PathLut;
244
245 private:
246   // Undefined
247   Builder(const Builder&);
248
249   // Undefined
250   Builder& operator=(const Builder& rhs);
251
252   void LoadConstants( const TreeNode& root, Property::Map& intoMap );
253
254   void LoadConfiguration( const TreeNode& root, Property::Map& intoMap );
255
256   Animation CreateAnimation( const std::string& animationName,
257                              const Replacement& replacement,
258                              Dali::Actor        sourceActor );
259
260   BaseHandle Create( const std::string& templateName,
261                      const Replacement& constant );
262
263   BaseHandle DoCreate( const TreeNode&    root,
264                        const TreeNode&    node,
265                        Actor              parent,
266                        const Replacement& replacements );
267
268   void SetupTask( RenderTask&              task,
269                   const Toolkit::TreeNode& node,
270                   const Replacement&       replacement );
271
272   bool ApplyStyle( const std::string& styleName,
273                    Handle&            handle,
274                    const Replacement& replacement);
275
276   void ApplyAllStyleProperties( const TreeNode&    root,
277                                 const TreeNode&    node,
278                                 Dali::Handle&      handle,
279                                 const Replacement& constant );
280
281   void RecordStyles( const char*        styleName,
282                      const TreeNode&    node,
283                      Dali::Handle&      handle,
284                      const Replacement& replacements );
285
286   void RecordStyle( StylePtr           style,
287                     const TreeNode&    node,
288                     Dali::Handle&      handle,
289                     const Replacement& replacements );
290
291   void RecordTransitions( const TreeNode::KeyNodePair& keyValue,
292                           Property::Array& transitions,
293                           const Replacement& replacements );
294
295   void RecordTransitionData( const TreeNode::KeyNodePair& keyNode,
296                              Toolkit::TransitionData& transitionData,
297                              const Replacement& replacements );
298
299   void ApplyProperties( const TreeNode&    root,
300                         const TreeNode&    node,
301                         Dali::Handle&      handle,
302                         const Replacement& constant );
303
304   void ApplySignals( const TreeNode& root,
305                      const TreeNode& node,
306                      Dali::Handle& handle );
307
308   void ApplyStylesByActor( const TreeNode&    root,
309                            const TreeNode&    node,
310                            Dali::Handle&      handle,
311                            const Replacement& constant );
312
313   void SetProperties( const TreeNode&    node,
314                       Handle&            handle,
315                       const Replacement& constant );
316
317   bool MapToTargetProperty( Handle&            propertyObject,
318                             const std::string& key,
319                             const TreeNode&    node,
320                             const Replacement& constant,
321                             Property::Index&   index,
322                             Property::Value&   value );
323
324   /**
325    * Find the key in the mapping table, if it's present, then generate
326    * a property value for it (of the given type if available),
327    * recursing as necessary, and stopping if any cycles are detected.
328    *
329    * @param[in] mappingRoot The JSON node containing the mappings
330    * @param[in] theKey The key to search for
331    * @param[in] propertyType The property type if known, or NONE
332    * @param[in,out] value The string value to test and write back to.
333    */
334   bool GetPropertyMap( const TreeNode&  mappingRoot,
335                        const char*      theKey,
336                        Property::Type   propertyType,
337                        Property::Value& value );
338
339   void SetCustomProperties( const TreeNode&      node,
340                             Handle&              handle,
341                             const Replacement&   constant,
342                             const std::string&   childName,
343                             Property::AccessMode accessMode );
344
345   /**
346    * Find the key in the mapping table, if it's present, then generate
347    * a property value for it (of the given type if available),
348    * recursing as necessary, and stopping if any cycles are detected.
349    *
350    * @param[in] mappingRoot The JSON node containing the mappings
351    * @param[in] theKey The key to search for
352    * @param[in,out] keyStack the stack of visited keys
353    * @param[in] propertyType The property type if known, or NONE
354    * @param[in,out] value The string value to test and write back to.
355    */
356   bool RecursePropertyMap( const TreeNode&  mappingRoot,
357                            KeyStack&        keyStack,
358                            const char*      theKey,
359                            Property::Type   propertyType,
360                            Property::Value& value );
361
362
363   /**
364    * Tests if the value is a string delimited by <>. If it is, then it attempts to
365    * change the value to the mapping from a matching key in the mappings table.
366    * @param[in] mappingRoot The JSON node containing the mappings
367    * @param[in,out] keyStack the stack of visited keys
368    * @param[in,out] value The string value to test and write back to.
369    * @return true if the value was converted, false otherwise.
370    */
371   bool ConvertChildValue( const TreeNode& mappingRoot,
372                           KeyStack& keyStack,
373                           Property::Value& value );
374
375 private:
376   Toolkit::JsonParser                 mParser;
377   PathLut                             mPathLut;
378   PathConstrainerLut                  mPathConstrainerLut;
379   LinearConstrainerLut                mLinearConstrainerLut;
380   SlotDelegate<Builder>               mSlotDelegate;
381   Property::Map                       mReplacementMap;
382   Property::Map                       mConfigurationMap;
383   MappingsLut                         mCompleteMappings;
384   Dictionary<StylePtr>                mStyles; // State based styles
385   Toolkit::Builder::BuilderSignalType mQuitSignal;
386 };
387
388 } // namespace Internal
389
390 inline Internal::Builder& GetImpl(Dali::Toolkit::Builder& obj)
391 {
392   DALI_ASSERT_ALWAYS(obj);
393
394   Dali::BaseObject& handle = obj.GetBaseObject();
395
396   return static_cast<Internal::Builder&>(handle);
397 }
398
399 inline const Internal::Builder& GetImpl(const Dali::Toolkit::Builder& obj)
400 {
401   DALI_ASSERT_ALWAYS(obj);
402
403   const Dali::BaseObject& handle = obj.GetBaseObject();
404
405   return static_cast<const Internal::Builder&>(handle);
406 }
407
408 } // namespace Toolkit
409
410 } // namespace Dali
411
412 #endif // DALI_TOOLKIT_INTERNAL_BUILDER_H