Remove Geometry::QUAD() usage in Toolkit
[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
38 // Warning messages usually displayed
39 #define DALI_SCRIPT_WARNING(format, args...) \
40   DALI_LOG_WARNING("Script:" format, ## args)
41
42 // Info messages are usually debug build
43 #define DALI_SCRIPT_INFO(format, args...) \
44   DALI_LOG_INFO(Dali::Toolkit::Internal::gFilterScript, Debug::General, "Script:" format, ## args)
45
46 // Info Verbose need to be swiched on in gFilterScript filter constructor (by default set to General)
47 #define DALI_SCRIPT_VERBOSE(format, args...) \
48   DALI_LOG_INFO(Dali::Toolkit::Internal::gFilterScript, Debug::Verbose, "Script:" format, ## args)
49
50 namespace Dali
51 {
52
53 namespace Toolkit
54 {
55   class TreeNode;
56 }
57
58 namespace Toolkit
59 {
60
61 namespace Internal
62 {
63
64 #if defined(DEBUG_ENABLED)
65 extern Dali::Integration::Log::Filter* gFilterScript;
66 #endif
67
68 class Builder;
69 class Replacement;
70
71 /**
72  * @copydoc Toolkit::Builder
73  */
74 class Builder : public Dali::BaseObject
75 {
76 public:
77
78   Builder();
79
80   /**
81    * @copydoc Toolkit::Builder::LoadFromString
82    */
83   void LoadFromString( const std::string &data,
84                        Dali::Toolkit::Builder::UIFormat rep = Dali::Toolkit::Builder::JSON );
85
86   /**
87    * @copydoc Toolkit::Builder::AddConstants
88    */
89   void AddConstants( const Property::Map& map );
90
91   /**
92    * @copydoc Toolkit::Builder::AddConstant
93    */
94   void AddConstant( const std::string& key, const Property::Value& value );
95
96   /**
97    * @copydoc Toolkit::Builder::GetConstants
98    */
99   const Property::Map& GetConstants() const;
100
101   /**
102    * @copydoc Toolkit::Builder::GetConstant
103    */
104   const Property::Value& GetConstant( const std::string& key ) const;
105
106   /**
107    * @copydoc Toolkit::Builder::CreateAnimation( const std::string& animationName );
108    */
109   Animation CreateAnimation( const std::string& animationName );
110
111   /**
112    * @copydoc Toolkit::Builder::CreateAnimation( const std::string& animationName, const Property::Map& map );
113    */
114   Animation CreateAnimation( const std::string& animationName, const Property::Map& map );
115
116   /**
117    * @copydoc Toolkit::Builder::CreateAnimation( const std::string&,Dali::Actor);
118    */
119   Animation CreateAnimation( const std::string& animationName, Dali::Actor sourceActor );
120
121   /**
122    * @copydoc Toolkit::Builder::CreateAnimation( const std::string&,const Property::Map&, Dali::Actor);
123    */
124   Animation CreateAnimation( const std::string& animationName, const Property::Map& map, Dali::Actor sourceActor );
125
126   /**
127    * @copydoc Toolkit::Builder::Create( const std::string& templateName );
128    */
129   BaseHandle Create( const std::string& templateName );
130
131   /**
132    * @copydoc Toolkit::Builder::Create( const std::string& templateName, const Property::Map& map );
133    */
134   BaseHandle Create( const std::string& templateName, const Property::Map& map );
135
136   /**
137    * @copydoc Toolkit::Builder::CreateFromJson( const std::string& json );
138    */
139   BaseHandle CreateFromJson( const std::string& json );
140
141   /**
142    * @copydoc Toolkit::Builder::ApplyFromJson( Handle& handle, const std::string& json );
143    */
144   bool ApplyFromJson(  Handle& handle, const std::string& json );
145
146   /**
147    * @copydoc Toolkit::Builder::ApplyStyle
148    */
149   bool ApplyStyle( const std::string& styleName, Handle& handle );
150
151   /**
152    * @copydoc Toolkit::Builder::AddActors
153    */
154   void AddActors( Actor toActor );
155
156   /**
157    * @copydoc Toolkit::Builder::AddActors
158    */
159   void AddActors( const std::string &sectionName, Actor toActor );
160
161   /**
162    * @copydoc Toolkit::Builder::CreateRenderTask
163    */
164   void CreateRenderTask( const std::string &name );
165
166   /**
167    * @copydoc Toolkit::Builder::GetFrameBufferImage
168    */
169   FrameBufferImage GetFrameBufferImage( const std::string &name );
170
171   /**
172    * @copydoc Toolkit::Builder::GetFrameBufferImage
173    */
174   FrameBufferImage GetFrameBufferImage( const std::string &name, const Replacement& constant );
175
176   /**
177    * @copydoc Toolkit::Builder::GetPath
178    */
179   Path GetPath( const std::string &name );
180
181   /**
182    * @copydoc Toolkit::Builder::GetPathConstrainer
183    */
184   Dali::PathConstrainer GetPathConstrainer( const std::string& name );
185
186   /*
187    * Check if a given constrainer is of type PathConstrainer
188    * @param[in] name The name of the constrainer
189    * @return True if constainer is of type PathConstrainer, False otherwise
190    *
191    */
192   bool IsPathConstrainer( const std::string& name );
193
194   /**
195    * @copydoc Toolkit::Builder::GetLinearConstrainer
196    */
197   Dali::LinearConstrainer GetLinearConstrainer( const std::string& name );
198
199   /*
200    * Check if a given constrainer is of type LinearConstrainer
201    * @param[in] name The name of the constrainer
202    * @return True if constainer is of type LinearConstrainer, False otherwise
203    *
204    */
205   bool IsLinearConstrainer( const std::string& name );
206
207   /**
208    * @copydoc Toolkit::Builder::QuitSignal
209    */
210   Toolkit::Builder::BuilderSignalType& QuitSignal();
211
212   /**
213    * Emits the quit signal
214    */
215   void EmitQuitSignal();
216
217 protected:
218
219   virtual ~Builder();
220
221 private:
222   // Undefined
223   Builder(const Builder&);
224   Builder& operator=(const Builder& rhs);
225
226   void SetupTask( RenderTask& task, const Toolkit::TreeNode& node, const Replacement& replacement );
227
228   void SetCustomProperties( const TreeNode& node, Handle& handle, const Replacement& constant, const std::string& childName, Property::AccessMode accessMode );
229
230 private:
231   Toolkit::JsonParser mParser;
232
233   typedef std::map<const std::string, FrameBufferImage> ImageLut;
234   ImageLut mFrameBufferImageLut;
235
236   typedef std::map<const std::string, Path> PathLut;
237   PathLut mPathLut;
238
239   typedef struct{ std::string name; Dali::PathConstrainer pathConstrainer; } PathConstrainerEntry;
240   typedef std::vector<PathConstrainerEntry> PathConstrainerLut;
241   PathConstrainerLut mPathConstrainerLut;
242
243   typedef struct{ std::string name; Dali::LinearConstrainer linearConstrainer; } LinearConstrainerEntry;
244   typedef std::vector<LinearConstrainerEntry> LinearConstrainerLut;
245   LinearConstrainerLut mLinearConstrainerLut;
246
247   SlotDelegate<Builder> mSlotDelegate;
248
249   Property::Map mReplacementMap;
250
251   typedef std::vector< TreeNode::KeyNodePair > MappingsLut;
252   MappingsLut mCompleteMappings;
253
254   BaseHandle Create( const std::string& templateName, const Replacement& constant );
255
256   BaseHandle DoCreate( const TreeNode& root, const TreeNode& node, Actor parent, const Replacement& replacements );
257
258   void LoadConstants( const TreeNode& root, Property::Map& intoMap );
259
260   void LoadIncludes( const std::string& data );
261
262   bool ApplyStyle( const std::string& styleName, Handle& handle, const Replacement& replacement);
263
264   Animation CreateAnimation( const std::string& animationName, const Replacement& replacement, Dali::Actor sourceActor );
265
266   typedef std::vector<const char*> KeyStack;
267
268   /**
269    * Tests if the value is a string delimited by <>. If it is, then it attempts to
270    * change the value to the mapping from a matching key in the mappings table.
271    * @param[in] mappingRoot The JSON node containing the mappings
272    * @param[in,out] keyStack the stack of visited keys
273    * @param[in,out] value The string value to test and write back to.
274    * @return true if the value was converted, false otherwise.
275    */
276   bool ConvertChildValue( const TreeNode& mappingRoot, KeyStack& keyStack, Property::Value& value );
277
278   /**
279    * Find the key in the mapping table, if it's present, then generate a property value for it (of the given type if available), recursing as necessary, and stopping if any cycles
280    * are detected.
281    * @param[in] mappingRoot The JSON node containing the mappings
282    * @param[in] theKey The key to search for
283    * @param[in,out] keyStack the stack of visited keys
284    * @param[in] propertyType The property type if known, or NONE
285    * @param[in,out] value The string value to test and write back to.
286    */
287   bool RecursePropertyMap( const TreeNode& mappingRoot, KeyStack& keyStack, const char* theKey, Property::Type propertyType, Property::Value& value );
288
289   /**
290    * Find the key in the mapping table, if it's present, then generate a property value for it (of the given type if available), recursing as necessary, and stopping if any cycles
291    * are detected.
292    * @param[in] mappingRoot The JSON node containing the mappings
293    * @param[in] theKey The key to search for
294    * @param[in] propertyType The property type if known, or NONE
295    * @param[in,out] value The string value to test and write back to.
296    */
297   bool GetPropertyMap( const TreeNode& mappingRoot, const char* theKey, Property::Type propertyType, Property::Value& value );
298
299   void ApplyProperties( const TreeNode& root, const TreeNode& node,
300                         Dali::Handle& handle, const Replacement& constant );
301
302   void ApplyStylesByActor( const TreeNode& root, const TreeNode& node,
303                            Dali::Handle& handle, const Replacement& constant );
304
305   void ApplyAllStyleProperties( const TreeNode& root, const TreeNode& node,
306                                 Dali::Handle& handle, const Replacement& constant );
307
308   void SetProperties( const TreeNode& node, Handle& handle, const Replacement& constant );
309
310   Toolkit::Builder::BuilderSignalType mQuitSignal;
311 };
312
313 } // namespace Internal
314
315 inline Internal::Builder& GetImpl(Dali::Toolkit::Builder& obj)
316 {
317   DALI_ASSERT_ALWAYS(obj);
318
319   Dali::BaseObject& handle = obj.GetBaseObject();
320
321   return static_cast<Internal::Builder&>(handle);
322 }
323
324 inline const Internal::Builder& GetImpl(const Dali::Toolkit::Builder& obj)
325 {
326   DALI_ASSERT_ALWAYS(obj);
327
328   const Dali::BaseObject& handle = obj.GetBaseObject();
329
330   return static_cast<const Internal::Builder&>(handle);
331 }
332
333 } // namespace Toolkit
334
335 } // namespace Dali
336
337 #endif // __DALI_TOOLKIT_INTERNAL_BUILDER_H__