3051917ceab5ea6e45457af14b6e1d9522c4c127
[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) 2014 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-toolkit/devel-api/visual-factory/transition-data.h>
32 #include <dali/integration-api/debug.h>
33
34 // INTERNAL INCLUDES
35 #include <dali-toolkit/devel-api/builder/json-parser.h>
36 #include <dali-toolkit/devel-api/builder/builder.h>
37 #include <dali-toolkit/internal/builder/builder-declarations.h>
38
39 // Warning messages usually displayed
40 #define DALI_SCRIPT_WARNING(format, args...) \
41   DALI_LOG_WARNING("Script:" format, ## args)
42
43 // Info messages are usually debug build
44 #define DALI_SCRIPT_INFO(format, args...) \
45   DALI_LOG_INFO(Dali::Toolkit::Internal::gFilterScript, Debug::General, "Script:" format, ## 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, args...) \
49   DALI_LOG_INFO(Dali::Toolkit::Internal::gFilterScript, Debug::Verbose, "Script:" format, ## 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::GetConstants
99    */
100   const Property::Map& GetConstants() const;
101
102   /**
103    * @copydoc Toolkit::Builder::GetConstant
104    */
105   const Property::Value& GetConstant( const std::string& key ) const;
106
107   /**
108    * @copydoc Toolkit::Builder::CreateAnimation( const std::string& animationName );
109    */
110   Animation CreateAnimation( const std::string& animationName );
111
112   /**
113    * @copydoc Toolkit::Builder::CreateAnimation( const std::string& animationName, const Property::Map& map );
114    */
115   Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
116
117   /**
118    * @copydoc Toolkit::Builder::CreateAnimation( const std::string&,Dali::Actor);
119    */
120   Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
121
122   /**
123    * @copydoc Toolkit::Builder::CreateAnimation( const std::string&,const Property::Map&, Dali::Actor);
124    */
125   Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
126
127   /**
128    * @copydoc Toolkit::Builder::Create( const std::string& templateName );
129    */
130   BaseHandle Create( const std::string& templateName );
131
132   /**
133    * @copydoc Toolkit::Builder::Create( const std::string& templateName, const Property::Map& map );
134    */
135   BaseHandle Create( const std::string& templateName, const Property::Map& map );
136
137   /**
138    * @copydoc Toolkit::Builder::CreateFromJson( const std::string& json );
139    */
140   BaseHandle CreateFromJson( const std::string& json );
141
142   /**
143    * @copydoc Toolkit::Builder::ApplyFromJson( Handle& handle, const std::string& json );
144    */
145   bool ApplyFromJson(  Handle& handle, const std::string& json );
146
147   /**
148    * @copydoc Toolkit::Builder::ApplyStyle
149    */
150   bool ApplyStyle( const std::string& styleName, Handle& handle );
151
152   /**
153    * @copydoc Toolkit::Builder::AddActors
154    */
155   void AddActors( Actor toActor );
156
157   /**
158    * @copydoc Toolkit::Builder::AddActors
159    */
160   void AddActors( const std::string &sectionName, Actor toActor );
161
162   /**
163    * @copydoc Toolkit::Builder::CreateRenderTask
164    */
165   void CreateRenderTask( const std::string &name );
166
167   /**
168    * @copydoc Toolkit::Builder::GetFrameBufferImage
169    */
170   FrameBufferImage GetFrameBufferImage( const std::string &name );
171
172   /**
173    * @copydoc Toolkit::Builder::GetFrameBufferImage
174    */
175   FrameBufferImage GetFrameBufferImage( const std::string &name, const Replacement& constant );
176
177   /**
178    * @copydoc Toolkit::Builder::GetPath
179    */
180   Path GetPath( const std::string &name );
181
182   /**
183    * @copydoc Toolkit::Builder::GetPathConstrainer
184    */
185   Dali::PathConstrainer GetPathConstrainer( const std::string& name );
186
187   /*
188    * Check if a given constrainer is of type PathConstrainer
189    * @param[in] name The name of the constrainer
190    * @return True if constainer is of type PathConstrainer, False otherwise
191    *
192    */
193   bool IsPathConstrainer( const std::string& name );
194
195   /**
196    * @copydoc Toolkit::Builder::GetLinearConstrainer
197    */
198   Dali::LinearConstrainer GetLinearConstrainer( const std::string& name );
199
200   /*
201    * Check if a given constrainer is of type LinearConstrainer
202    * @param[in] name The name of the constrainer
203    * @return True if constainer is of type LinearConstrainer, False otherwise
204    *
205    */
206   bool IsLinearConstrainer( const std::string& name );
207
208   /**
209    * @copydoc Toolkit::Builder::QuitSignal
210    */
211   Toolkit::Builder::BuilderSignalType& QuitSignal();
212
213   /**
214    * Emits the quit signal
215    */
216   void EmitQuitSignal();
217
218 protected:
219
220   virtual ~Builder();
221
222 private:
223   typedef std::vector<const char*> KeyStack;
224   typedef std::vector< TreeNode::KeyNodePair > MappingsLut;
225   typedef struct{ std::string name; Dali::LinearConstrainer linearConstrainer; } LinearConstrainerEntry;
226   typedef std::vector<LinearConstrainerEntry> LinearConstrainerLut;
227   typedef struct{ std::string name; Dali::PathConstrainer pathConstrainer; } PathConstrainerEntry;
228   typedef std::vector<PathConstrainerEntry> PathConstrainerLut;
229   typedef std::map<const std::string, Path> PathLut;
230   typedef std::map<const std::string, FrameBufferImage> ImageLut;
231
232 private:
233   // Undefined
234   Builder(const Builder&);
235
236   // Undefined
237   Builder& operator=(const Builder& rhs);
238
239   void LoadConstants( const TreeNode& root, Property::Map& intoMap );
240
241   Animation CreateAnimation( const std::string& animationName,
242                              const Replacement& replacement,
243                              Dali::Actor        sourceActor );
244
245   BaseHandle Create( const std::string& templateName,
246                      const Replacement& constant );
247
248   BaseHandle DoCreate( const TreeNode&    root,
249                        const TreeNode&    node,
250                        Actor              parent,
251                        const Replacement& replacements );
252
253   void SetupTask( RenderTask&              task,
254                   const Toolkit::TreeNode& node,
255                   const Replacement&       replacement );
256
257   bool ApplyStyle( const std::string& styleName,
258                    Handle&            handle,
259                    const Replacement& replacement);
260
261   void ApplyAllStyleProperties( const TreeNode&    root,
262                                 const TreeNode&    node,
263                                 Dali::Handle&      handle,
264                                 const Replacement& constant );
265
266   void ApplyProperties( const TreeNode&    root,
267                         const TreeNode&    node,
268                         Dali::Handle&      handle,
269                         const Replacement& constant );
270
271   void ApplyStylesByActor( const TreeNode&    root,
272                            const TreeNode&    node,
273                            Dali::Handle&      handle,
274                            const Replacement& constant );
275
276   void SetProperties( const TreeNode&    node,
277                       Handle&            handle,
278                       const Replacement& constant );
279
280   /**
281    * Find the key in the mapping table, if it's present, then generate
282    * a property value for it (of the given type if available),
283    * recursing as necessary, and stopping if any cycles are detected.
284    *
285    * @param[in] mappingRoot The JSON node containing the mappings
286    * @param[in] theKey The key to search for
287    * @param[in] propertyType The property type if known, or NONE
288    * @param[in,out] value The string value to test and write back to.
289    */
290   bool GetPropertyMap( const TreeNode&  mappingRoot,
291                        const char*      theKey,
292                        Property::Type   propertyType,
293                        Property::Value& value );
294
295   void SetCustomProperties( const TreeNode&      node,
296                             Handle&              handle,
297                             const Replacement&   constant,
298                             const std::string&   childName,
299                             Property::AccessMode accessMode );
300
301   /**
302    * Find the key in the mapping table, if it's present, then generate
303    * a property value for it (of the given type if available),
304    * recursing as necessary, and stopping if any cycles are detected.
305    *
306    * @param[in] mappingRoot The JSON node containing the mappings
307    * @param[in] theKey The key to search for
308    * @param[in,out] keyStack the stack of visited keys
309    * @param[in] propertyType The property type if known, or NONE
310    * @param[in,out] value The string value to test and write back to.
311    */
312   bool RecursePropertyMap( const TreeNode&  mappingRoot,
313                            KeyStack&        keyStack,
314                            const char*      theKey,
315                            Property::Type   propertyType,
316                            Property::Value& value );
317
318
319   /**
320    * Tests if the value is a string delimited by <>. If it is, then it attempts to
321    * change the value to the mapping from a matching key in the mappings table.
322    * @param[in] mappingRoot The JSON node containing the mappings
323    * @param[in,out] keyStack the stack of visited keys
324    * @param[in,out] value The string value to test and write back to.
325    * @return true if the value was converted, false otherwise.
326    */
327   bool ConvertChildValue( const TreeNode& mappingRoot,
328                           KeyStack& keyStack,
329                           Property::Value& value );
330
331 private:
332   Toolkit::JsonParser                 mParser;
333   ImageLut                            mFrameBufferImageLut;
334   PathLut                             mPathLut;
335   PathConstrainerLut                  mPathConstrainerLut;
336   LinearConstrainerLut                mLinearConstrainerLut;
337   SlotDelegate<Builder>               mSlotDelegate;
338   Property::Map                       mReplacementMap;
339   MappingsLut                         mCompleteMappings;
340   Toolkit::Builder::BuilderSignalType mQuitSignal;
341 };
342
343 } // namespace Internal
344
345 inline Internal::Builder& GetImpl(Dali::Toolkit::Builder& obj)
346 {
347   DALI_ASSERT_ALWAYS(obj);
348
349   Dali::BaseObject& handle = obj.GetBaseObject();
350
351   return static_cast<Internal::Builder&>(handle);
352 }
353
354 inline const Internal::Builder& GetImpl(const Dali::Toolkit::Builder& obj)
355 {
356   DALI_ASSERT_ALWAYS(obj);
357
358   const Dali::BaseObject& handle = obj.GetBaseObject();
359
360   return static_cast<const Internal::Builder&>(handle);
361 }
362
363 } // namespace Toolkit
364
365 } // namespace Dali
366
367 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_H__