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