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