Merge branch 'devel/master(1.1.39)' into tizen
[platform/core/uifw/dali-core.git] / dali / devel-api / scripting / scripting.h
1 #ifndef __DALI_SCRIPTING_H__
2 #define __DALI_SCRIPTING_H__
3
4 /*
5  * Copyright (c) 2015 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 // INTERNAL INCLUDES
22 #include <dali/public-api/actors/actor-enumerations.h>
23 #include <dali/public-api/actors/draw-mode.h>
24 #include <dali/devel-api/animation/animation-data.h>
25 #include <dali/public-api/images/image.h>
26 #include <dali/public-api/shader-effects/shader-effect.h>
27 #include <dali/public-api/object/property-map.h>
28 #include <dali/public-api/object/property-value.h>
29
30 namespace Dali
31 {
32
33 class Actor;
34
35 /**
36  * @brief Utilities for scripting support.
37  */
38 namespace Scripting
39 {
40
41 /**
42  * @brief Structure which stores an enumeration and its string equivalent.
43  */
44 struct StringEnum
45 {
46   const char* string; ///< The string representation
47   const int value;    ///< The enumeration value wrapped in int
48 };
49
50 /**
51  * @brief Find the given enum index from the table
52  *
53  * @param[in]  value       The string equivalent (case-insensitive).
54  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
55  * @param[in]  tableCount  Number of items in the array.
56  * @return     The index of the enumeration. If enumeration is not found, logs an error and returns tableCount.
57  */
58 DALI_IMPORT_API unsigned int FindEnumIndex( const char* value, const StringEnum* table, unsigned int tableCount );
59
60 /**
61  * @brief Find the enum as an integer from the table
62  *
63  * @SINCE_1_1.12
64  *
65  * @param[in]  value       The string equivalent (case-insensitive, comma separate to OR values).
66  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
67  * @param[in]  tableCount  Number of items in the array.
68  * @param[out] integerEnum The value of the enum.
69  * @return     true if one or more enums in value.
70  */
71 DALI_IMPORT_API bool EnumStringToInteger( const char* const value, const StringEnum* const table, unsigned int tableCount, unsigned int& integerEnum );
72
73 /**
74  * @brief Chooses the appropriate enumeration for the provided string from the given table.
75  *
76  * @param[in]  value       The string equivalent (case-insensitive, comma separate to OR values).
77  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
78  * @param[in]  tableCount  Number of items in the array.
79  * @param[out] result      The enum value
80  *
81  * @return     True if the value was found from the table
82  */
83 template< typename T >
84 bool GetEnumeration( const char* value, const StringEnum* table, unsigned int tableCount, T& result )
85 {
86   bool retVal( false );
87   if( table )
88   {
89     unsigned int integerEnum = 0;
90     // check to avoid crash, not asserting on purpose, error is logged instead
91     if( EnumStringToInteger( value, table, tableCount, integerEnum ) )
92     {
93       result = static_cast<T>( integerEnum );
94       retVal = true;
95     }
96   }
97   return retVal;
98 }
99
100 /**
101  * @brief Chooses the appropriate string for the provided enumeration from the given table.
102  *
103  * @param[in]  value       The enumeration.
104  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
105  * @param[in]  tableCount  Number of items in the array.
106  *
107  * @return     The equivalent enumeration for the given string. Will return NULL if the value does not exist
108  *
109  * @note The caller is NOT responsible for cleaning up the returned pointer as it is statically allocated.
110  */
111 template< typename T >
112 const char* GetEnumerationName( T value, const StringEnum* table, unsigned int tableCount )
113 {
114   if( table )
115   {
116     for ( unsigned int i = 0; i < tableCount; ++i )
117     {
118       if ( value == T(table[ i ].value) )
119       {
120         return table[ i ].string;
121       }
122     }
123   }
124   return NULL;
125 }
126
127 /**
128  * @brief Chooses the appropriate string for the provided enumeration from the given table.
129  * This is an optimised version that handles enumerations that start at 0 and are linear only.
130  *
131  * @param[in]  value       The enumeration.
132  * @param[in]  table       A pointer to an array with the enumeration to string equivalents.
133  * @param[in]  tableCount  Number of items in the array.
134  *
135  * @return     The equivalent enumeration for the given string. Will return NULL if the value does not exist
136  *
137  * @note The caller is NOT responsible for cleaning up the returned pointer as it is statically allocated.
138  */
139 template< typename T >
140 const char * GetLinearEnumerationName( T value, const StringEnum* table, unsigned int tableCount )
141 {
142   if ( table && ( value > 0 || value <= (int)tableCount ) )
143   {
144     return table[value].string;
145   }
146   return NULL;
147 }
148
149 /**
150  * @brief Creates object with data from the property value map.
151  *
152  * @param[in] property The property value map with the following valid fields:
153  * @code
154  * "filename":      type std::string
155  * "loadPolicy"     type std::string (enum)
156  * "releasePolicy"  type std::string (enum)
157  * "width"          type float
158  * "height"         type float
159  * "pixelFormat"    type std::string (enum)
160  * "fittingMode"    type std::string (enum)
161  * "samplingMode"   type std::string (enum)
162  * "orientation"    type bool
163  * "type"           type std::string (FrameBufferImage|BufferImage|ResourceImage(default))
164  * @endcode
165  * Some fields are optional and some only pertain to a specific type.
166  *
167  * @return A pointer to a newly created object.
168  */
169 DALI_IMPORT_API Image NewImage( const Property::Value& property );
170
171 /**
172  * @brief Creates an actor with the date from the property value map.
173  *
174  * @param[in] map The property value map with the properties (and hierarchy) of the actor required
175  *                 For example:
176  * @code
177  * {
178  *   "type": "ImageActor",
179  *   "image":
180  *   {
181  *     "filename":"my-image-path.png"
182  *   },
183  *   "actors":
184  *   [
185  *     {
186  *       "type":"Actor",
187  *       "position":[0,0,0]
188  *     }
189  *   ]
190  * }
191  * @endcode
192  *
193  * @return A handle to the newly created actor.
194  */
195 DALI_IMPORT_API Actor NewActor( const Property::Map& map );
196
197 /**
198  * @brief Creates a Property::Map from the actor provided.
199  *
200  * @param[in]  actor The base-actor from which a Property::Map should be created
201  * @param[out] map This map is cleared and a property map of actor and its children is filled in
202  */
203 DALI_IMPORT_API void CreatePropertyMap( Actor actor, Property::Map& map );
204
205 /**
206  * @brief Creates a Property::Map from the image provided.
207  *
208  * @param[in]  image The image from which a Property::Map should be created
209  * @param[out] map This map is cleared and a property map of the image is filled in
210  */
211 DALI_IMPORT_API void CreatePropertyMap( Image image, Property::Map& map );
212
213 /**
214  * @brief Creates description data required to create an Animation object from a property map.
215  *
216  * @param[in]  map The property value map containing the animation description
217  * @param[out] outputAnimationData Resultant data retrieved from the property map is written here
218  */
219 DALI_IMPORT_API void NewAnimation( const Property::Map& map, Dali::AnimationData& outputAnimationData );
220
221 } // namespace Scripting
222
223 } // namespace Dali
224
225 #endif // __DALI_SCRIPTING_H__