[dali_1.4.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) 2018 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::GetFrameBufferImage
192    */
193   FrameBufferImage GetFrameBufferImage( const std::string &name );
194
195   /**
196    * @copydoc Toolkit::Builder::GetFrameBufferImage
197    */
198   FrameBufferImage GetFrameBufferImage( const std::string &name, const Replacement& constant );
199
200   /**
201    * @copydoc Toolkit::Builder::GetPath
202    */
203   Path GetPath( const std::string &name );
204
205   /**
206    * @copydoc Toolkit::Builder::GetPathConstrainer
207    */
208   Dali::PathConstrainer GetPathConstrainer( const std::string& name );
209
210   /*
211    * Check if a given constrainer is of type PathConstrainer
212    * @param[in] name The name of the constrainer
213    * @return True if constainer is of type PathConstrainer, False otherwise
214    *
215    */
216   bool IsPathConstrainer( const std::string& name );
217
218   /**
219    * @copydoc Toolkit::Builder::GetLinearConstrainer
220    */
221   Dali::LinearConstrainer GetLinearConstrainer( const std::string& name );
222
223   /*
224    * Check if a given constrainer is of type LinearConstrainer
225    * @param[in] name The name of the constrainer
226    * @return True if constainer is of type LinearConstrainer, False otherwise
227    *
228    */
229   bool IsLinearConstrainer( const std::string& name );
230
231   /**
232    * @copydoc Toolkit::Builder::QuitSignal
233    */
234   Toolkit::Builder::BuilderSignalType& QuitSignal();
235
236   /**
237    * Emits the quit signal
238    */
239   void EmitQuitSignal();
240
241
242 protected:
243
244   virtual ~Builder();
245
246 private:
247   typedef std::vector<const char*> KeyStack;
248   typedef std::vector< TreeNode::KeyNodePair > MappingsLut;
249   typedef struct{ std::string name; Dali::LinearConstrainer linearConstrainer; } LinearConstrainerEntry;
250   typedef std::vector<LinearConstrainerEntry> LinearConstrainerLut;
251   typedef struct{ std::string name; Dali::PathConstrainer pathConstrainer; } PathConstrainerEntry;
252   typedef std::vector<PathConstrainerEntry> PathConstrainerLut;
253   typedef std::map<const std::string, Path> PathLut;
254   typedef std::map<const std::string, FrameBufferImage> ImageLut;
255
256 private:
257   // Undefined
258   Builder(const Builder&);
259
260   // Undefined
261   Builder& operator=(const Builder& rhs);
262
263   void LoadConstants( const TreeNode& root, Property::Map& intoMap );
264
265   void LoadConfiguration( const TreeNode& root, Property::Map& intoMap );
266
267   Animation CreateAnimation( const std::string& animationName,
268                              const Replacement& replacement,
269                              Dali::Actor        sourceActor );
270
271   BaseHandle Create( const std::string& templateName,
272                      const Replacement& constant );
273
274   BaseHandle DoCreate( const TreeNode&    root,
275                        const TreeNode&    node,
276                        Actor              parent,
277                        const Replacement& replacements );
278
279   void SetupTask( RenderTask&              task,
280                   const Toolkit::TreeNode& node,
281                   const Replacement&       replacement );
282
283   bool ApplyStyle( const std::string& styleName,
284                    Handle&            handle,
285                    const Replacement& replacement);
286
287   void ApplyAllStyleProperties( const TreeNode&    root,
288                                 const TreeNode&    node,
289                                 Dali::Handle&      handle,
290                                 const Replacement& constant );
291
292   void RecordStyles( const char*        styleName,
293                      const TreeNode&    node,
294                      Dali::Handle&      handle,
295                      const Replacement& replacements );
296
297   void RecordStyle( StylePtr           style,
298                     const TreeNode&    node,
299                     Dali::Handle&      handle,
300                     const Replacement& replacements );
301
302   void RecordTransitions( const TreeNode::KeyNodePair& keyValue,
303                           Property::Array& transitions,
304                           const Replacement& replacements );
305
306   void RecordTransitionData( const TreeNode::KeyNodePair& keyNode,
307                              Toolkit::TransitionData& transitionData,
308                              const Replacement& replacements );
309
310   void ApplyProperties( const TreeNode&    root,
311                         const TreeNode&    node,
312                         Dali::Handle&      handle,
313                         const Replacement& constant );
314
315   void ApplySignals( const TreeNode& root,
316                      const TreeNode& node,
317                      Dali::Handle& handle );
318
319   void ApplyStylesByActor( const TreeNode&    root,
320                            const TreeNode&    node,
321                            Dali::Handle&      handle,
322                            const Replacement& constant );
323
324   void SetProperties( const TreeNode&    node,
325                       Handle&            handle,
326                       const Replacement& constant );
327
328   bool MapToTargetProperty( Handle&            propertyObject,
329                             const std::string& key,
330                             const TreeNode&    node,
331                             const Replacement& constant,
332                             Property::Index&   index,
333                             Property::Value&   value );
334
335   /**
336    * Find the key in the mapping table, if it's present, then generate
337    * a property value for it (of the given type if available),
338    * recursing as necessary, and stopping if any cycles are detected.
339    *
340    * @param[in] mappingRoot The JSON node containing the mappings
341    * @param[in] theKey The key to search for
342    * @param[in] propertyType The property type if known, or NONE
343    * @param[in,out] value The string value to test and write back to.
344    */
345   bool GetPropertyMap( const TreeNode&  mappingRoot,
346                        const char*      theKey,
347                        Property::Type   propertyType,
348                        Property::Value& value );
349
350   void SetCustomProperties( const TreeNode&      node,
351                             Handle&              handle,
352                             const Replacement&   constant,
353                             const std::string&   childName,
354                             Property::AccessMode accessMode );
355
356   /**
357    * Find the key in the mapping table, if it's present, then generate
358    * a property value for it (of the given type if available),
359    * recursing as necessary, and stopping if any cycles are detected.
360    *
361    * @param[in] mappingRoot The JSON node containing the mappings
362    * @param[in] theKey The key to search for
363    * @param[in,out] keyStack the stack of visited keys
364    * @param[in] propertyType The property type if known, or NONE
365    * @param[in,out] value The string value to test and write back to.
366    */
367   bool RecursePropertyMap( const TreeNode&  mappingRoot,
368                            KeyStack&        keyStack,
369                            const char*      theKey,
370                            Property::Type   propertyType,
371                            Property::Value& value );
372
373
374   /**
375    * Tests if the value is a string delimited by <>. If it is, then it attempts to
376    * change the value to the mapping from a matching key in the mappings table.
377    * @param[in] mappingRoot The JSON node containing the mappings
378    * @param[in,out] keyStack the stack of visited keys
379    * @param[in,out] value The string value to test and write back to.
380    * @return true if the value was converted, false otherwise.
381    */
382   bool ConvertChildValue( const TreeNode& mappingRoot,
383                           KeyStack& keyStack,
384                           Property::Value& value );
385
386 private:
387   Toolkit::JsonParser                 mParser;
388   ImageLut                            mFrameBufferImageLut;
389   PathLut                             mPathLut;
390   PathConstrainerLut                  mPathConstrainerLut;
391   LinearConstrainerLut                mLinearConstrainerLut;
392   SlotDelegate<Builder>               mSlotDelegate;
393   Property::Map                       mReplacementMap;
394   Property::Map                       mConfigurationMap;
395   MappingsLut                         mCompleteMappings;
396   Dictionary<StylePtr>                mStyles; // State based styles
397   Toolkit::Builder::BuilderSignalType mQuitSignal;
398 };
399
400 } // namespace Internal
401
402 inline Internal::Builder& GetImpl(Dali::Toolkit::Builder& obj)
403 {
404   DALI_ASSERT_ALWAYS(obj);
405
406   Dali::BaseObject& handle = obj.GetBaseObject();
407
408   return static_cast<Internal::Builder&>(handle);
409 }
410
411 inline const Internal::Builder& GetImpl(const Dali::Toolkit::Builder& obj)
412 {
413   DALI_ASSERT_ALWAYS(obj);
414
415   const Dali::BaseObject& handle = obj.GetBaseObject();
416
417   return static_cast<const Internal::Builder&>(handle);
418 }
419
420 } // namespace Toolkit
421
422 } // namespace Dali
423
424 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_H__