Merge "Allow registering property components for animatable properties" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-info-impl.h
1 #ifndef __DALI_INTERNAL_TYPE_INFO_H__
2 #define __DALI_INTERNAL_TYPE_INFO_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 // EXTERNAL INCLUDES
22 #include <string>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/object/base-handle.h>
26 #include <dali/public-api/object/base-object.h>
27 #include <dali/public-api/object/type-info.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 /**
36  * A TypeInfo class to support registered type creation, and introspection of available
37  * actions and signal connection.
38  * It also supports doing actions and connecting signal handlers. Note this is properly
39  * conducted through the BaseHandle interface which uses the TypeRegistry to walk
40  * all base classes.
41  */
42 class TypeInfo : public BaseObject
43 {
44 public:
45   /**
46    * Create TypeInfo
47    * @param [name] the registered name
48    * @param [baseName] the base type registered name
49    * @param [creator] the creator function for this type
50    */
51   TypeInfo(const std::string &name, const std::string &baseName, Dali::TypeInfo::CreateFunction creator);
52
53   /**
54    *
55    */
56   ~TypeInfo();
57
58   /**
59    * @copydoc Dali::TypeInfo::GetName
60    */
61   const std::string& GetName() const;
62
63   /**
64    * @copydoc Dali::TypeInfo::GetBaseName
65    */
66   const std::string& GetBaseName() const;
67
68   /**
69    * @copydoc TypeInfo::CreateFunction
70    */
71   BaseHandle CreateInstance() const;
72
73   /**
74    * @copydoc Dali::TypeInfo::GetCreator
75    */
76   Dali::TypeInfo::CreateFunction GetCreator() const;
77
78   /**
79    * @copydoc Dali::TypeInfo::GetActions
80    */
81   void GetActions( Dali::TypeInfo::NameContainer& container ) const;
82
83   /**
84    * @copydoc Dali::TypeInfo::GetSignals
85    */
86   void GetSignals( Dali::TypeInfo::NameContainer& container) const;
87
88   /**
89    * @copydoc Dali::TypeInfo::GetProperties
90    */
91   void GetProperties( Dali::TypeInfo::NameContainer& container) const;
92
93   /**
94    * Adds the property indices to the container specified.
95    * @param[in/out] indices The container where the property indices are added.
96    */
97   void GetPropertyIndices( Property::IndexContainer& indices ) const;
98
99   /**
100    * @copydoc Dali::TypeInfo::GetPropertyName() const
101    */
102   const std::string& GetPropertyName( Property::Index index ) const;
103
104   /*
105    * Add an action function
106    */
107   void AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function );
108
109   /*
110    * Add a function for connecting a signal.
111    * @param[in] signalName The name of the signal.
112    * @param[in] function The function used for connecting to the signal.
113    */
114   void AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunction function );
115
116   /**
117    * Adds an event-thread only property to the type.
118    * @param[in] name The name of the property.
119    * @param[in] index The index of the property.
120    * @param[in] type The Property::Type.
121    * @param[in] setFunc The function to call to set the property (Can be NULL).
122    * @param[in] getFunc The function to call to retrieve the value of the property.
123    */
124   void AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc );
125
126   /**
127    * Adds an animatable property to the type.
128    * @param[in] name The name of the property.
129    * @param[in] index The index of the property
130    * @param[in] type The Property::Type.
131    */
132   void AddAnimatableProperty( const std::string& name, Property::Index index, Property::Type type );
133
134   /**
135    * Adds a component of an animatable property to the type.
136    * The animatable property must have been type-registered and must be a Vector2, Vector3 or Vector4 type.
137    * @param[in] name The name of the component.
138    * @param[in] index The index of the property
139    * @param[in] baseIndex The index of the base animatable property
140    * @param[in] component The index The index of the component.
141    */
142   void AddAnimatablePropertyComponent( const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex );
143
144   /**
145    * Do an action on base object
146    * @param [in] object The base object to act upon
147    * @param [in] actionName The name of the desired action
148    * @param [in] properties The arguments of the action
149    * @return bool If the action could be executed
150    */
151   bool DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties);
152
153   /**
154    * Connects a callback function with the object's signals.
155    * @param[in] object The object providing the signal.
156    * @param[in] tracker Used to disconnect the signal.
157    * @param[in] signalName The signal to connect to.
158    * @param[in] functor A newly allocated FunctorDelegate.
159    * @return True if the signal was connected.
160    * @post If a signal was connected, ownership of functor was passed to CallbackBase. Otherwise the caller is responsible for deleting the unused functor.
161    */
162   bool ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor );
163
164   /**
165    * Retrieve the property count for this type.
166    * @return The total number of properties.
167    */
168   unsigned int GetPropertyCount() const;
169
170   /**
171    * Given a property name, retrieve the index.
172    * @param[in] name The name of the property.
173    * @return The index associated with that name.
174    */
175   Property::Index GetPropertyIndex( const std::string& name ) const;
176
177   /**
178    * Given a property index, retrieve the index of its base property.
179    * @param[in] index The index of the property.
180    * @return The index of the base property associated with the given property index.
181    */
182   Property::Index GetBasePropertyIndex( Property::Index index ) const;
183
184   /**
185    * Given a property index, retrieve its component index.
186    * @param[in] index The index of the property.
187    * @return The component index associated with that property index.
188    */
189   int GetComponentIndex( Property::Index index ) const;
190
191   /**
192    * Checks if there is a setter for the property. If there is then it is writable.
193    * @param[in] index The property index.
194    * @return True, if writable, false otherwise.
195    */
196   bool IsPropertyWritable( Property::Index index ) const;
197
198   /**
199    * Retrieve the Property::Type of the property at the given index.
200    * @param[in] index The property index.
201    * @return The Property::Type at that index.
202    */
203   Property::Type GetPropertyType( Property::Index index ) const;
204
205   /**
206    * Sets the value of a property at the index specified for the given object.
207    * @param[in] object The object whose property is to be set.
208    * @param[in] index The property index.
209    * @param[in] value The value to set.
210    */
211   void SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const;
212
213   /**
214    * Sets the value of a property with the name specified for the given object.
215    * @param[in] object The object whose property is to be set.
216    * @param[in] name The property name.
217    * @param[in] value The value to set.
218    */
219   void SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const;
220
221   /**
222    * Retrieves the value of a property at the index specified for the given object.
223    * @param[in] object The object whose property is to be queried.
224    * @param[in] index The property index.
225    * @return The current value of the property.
226    */
227   Property::Value GetProperty( const BaseObject *object, Property::Index index ) const;
228
229   /**
230    * Retrieves the value of a property with the name specified for the given object.
231    * @param[in] object The object whose property is to be queried.
232    * @param[in] name The property name.
233    * @return The current value of the property.
234    */
235   Property::Value GetProperty( const BaseObject *object, const std::string& name ) const;
236
237 private:
238
239   struct RegisteredProperty
240   {
241     RegisteredProperty()
242     : type( Property::NONE ),
243       setFunc( NULL ),
244       getFunc( NULL ),
245       name(),
246       basePropertyIndex(Property::INVALID_INDEX),
247       componentIndex(Property::INVALID_COMPONENT_INDEX)
248     {
249     }
250
251     RegisteredProperty( Property::Type propType, Dali::TypeInfo::SetPropertyFunction set, Dali::TypeInfo::GetPropertyFunction get, const std::string& propName, Property::Index basePropertyIndex, int componentIndex )
252     : type( propType ),
253       setFunc( set ),
254       getFunc( get ),
255       name( propName ),
256       basePropertyIndex(basePropertyIndex),
257       componentIndex(componentIndex)
258     {
259     }
260
261     Property::Type type;
262     Dali::TypeInfo::SetPropertyFunction setFunc;
263     Dali::TypeInfo::GetPropertyFunction getFunc;
264     std::string name;
265     Property::Index basePropertyIndex;
266     int componentIndex;
267   };
268
269   typedef std::pair<std::string, Dali::TypeInfo::SignalConnectorFunction > ConnectionPair;
270   typedef std::pair<std::string, Dali::TypeInfo::ActionFunction > ActionPair;
271   typedef std::pair<Property::Index, RegisteredProperty> RegisteredPropertyPair;
272
273   typedef std::vector< ActionPair > ActionContainer;
274   typedef std::vector< ConnectionPair > ConnectorContainer;
275   typedef std::vector< RegisteredPropertyPair > RegisteredPropertyContainer;
276
277   std::string mTypeName;
278   std::string mBaseTypeName;
279   Dali::TypeInfo::CreateFunction mCreate;
280   ActionContainer mActions;
281   ConnectorContainer mSignalConnectors;
282   RegisteredPropertyContainer mRegisteredProperties;
283 };
284
285 } // namespace Internal
286
287 // Helpers for public-api forwarding methods
288
289 inline Internal::TypeInfo& GetImplementation(Dali::TypeInfo& typeInfo)
290 {
291   DALI_ASSERT_ALWAYS(typeInfo);
292
293   BaseObject& handle = typeInfo.GetBaseObject();
294
295   return static_cast<Internal::TypeInfo&>(handle);
296 }
297
298 inline const Internal::TypeInfo& GetImplementation(const Dali::TypeInfo& typeInfo)
299 {
300   DALI_ASSERT_ALWAYS(typeInfo);
301
302   const BaseObject& handle = typeInfo.GetBaseObject();
303
304   return static_cast<const Internal::TypeInfo&>(handle);
305 }
306
307 } // namespace Dali
308
309 #endif // header