Merge "Allow registering property components for animatable properties" into tizen
[platform/core/uifw/dali-core.git] / dali / internal / event / common / type-info-impl.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/event/common/type-info-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm> // std::find_if
23 #include <string>
24
25 // INTERNAL INCLUDES
26 #include <dali/integration-api/debug.h>
27 #include <dali/internal/event/common/type-registry-impl.h>
28 #include <dali/internal/event/common/object-impl.h>
29
30 using std::find_if;
31
32 namespace
33 {
34
35 /*
36  * Functor to find by given type for vector of pairs
37  */
38 template <typename S, typename T>
39 struct PairFinder
40 {
41   PairFinder(const S& find)
42   : mFind(find)
43   {
44   }
45
46   bool operator()(const T& p) const
47   {
48     return p.first == mFind;
49   }
50
51 private:
52
53   const S& mFind;
54 };
55
56 /**
57  * Functor to find a matching property name
58  */
59 template <typename T>
60 struct PropertyNameFinder
61 {
62   PropertyNameFinder( const std::string& find )
63   : mFind( find )
64   {
65   }
66
67   bool operator()(const T &p) const
68   {
69     return p.second.name == mFind;
70   }
71
72 private:
73
74   const std::string& mFind;
75 };
76
77 /**
78  * Functor to find a matching property component index
79  */
80 template <typename T>
81 struct PropertyComponentFinder
82 {
83   PropertyComponentFinder( Dali::Property::Index basePropertyIndex, const int find )
84   : mBasePropertyIndex( basePropertyIndex ),
85     mFind( find )
86   {
87   }
88
89   bool operator()(const T &p) const
90   {
91     return ( p.second.basePropertyIndex == mBasePropertyIndex && p.second.componentIndex == mFind );
92   }
93
94 private:
95
96   Dali::Property::Index mBasePropertyIndex;
97   const int mFind;
98 };
99
100 } // namespace anon
101
102 namespace Dali
103 {
104
105 namespace Internal
106 {
107
108 TypeInfo::TypeInfo(const std::string &name, const std::string &baseTypeName, Dali::TypeInfo::CreateFunction creator)
109   : mTypeName(name), mBaseTypeName(baseTypeName), mCreate(creator)
110 {
111   DALI_ASSERT_ALWAYS(!name.empty() && "Type info construction must have a name");
112   DALI_ASSERT_ALWAYS(!baseTypeName.empty() && "Type info construction must have a base type name");
113 }
114
115 TypeInfo::~TypeInfo()
116 {
117 }
118
119 BaseHandle TypeInfo::CreateInstance() const
120 {
121   BaseHandle ret;
122
123   if(mCreate)
124   {
125     ret = mCreate();
126
127     if ( ret )
128     {
129       BaseObject& handle = ret.GetBaseObject();
130       Object *object = dynamic_cast<Internal::Object*>(&handle);
131
132       if ( object )
133       {
134         object->SetTypeInfo( this );
135       }
136     }
137   }
138   return ret;
139 }
140
141 bool TypeInfo::DoActionTo(BaseObject *object, const std::string &actionName, const std::vector<Property::Value> &properties)
142 {
143   bool done = false;
144
145   ActionContainer::iterator iter = find_if(mActions.begin(), mActions.end(), PairFinder<std::string, ActionPair>(actionName));
146
147   if( iter != mActions.end() )
148   {
149     done = (iter->second)(object, actionName, properties);
150   }
151   else
152   {
153     DALI_LOG_WARNING("Type '%s' cannot do action '%s'\n", mTypeName.c_str(), actionName.c_str());
154   }
155
156   if(!done)
157   {
158     Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
159     while( base )
160     {
161       done = GetImplementation(base).DoActionTo(object, actionName, properties);
162       if( done )
163       {
164         break;
165       }
166       base =  Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
167     }
168   }
169
170   return done;
171 }
172
173 bool TypeInfo::ConnectSignal( BaseObject* object, ConnectionTrackerInterface* connectionTracker, const std::string& signalName, FunctorDelegate* functor )
174 {
175   bool connected( false );
176
177   ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
178                                                  PairFinder<std::string, ConnectionPair>(signalName) );
179
180   if( iter != mSignalConnectors.end() )
181   {
182     connected = (iter->second)( object, connectionTracker, signalName, functor );
183   }
184
185   return connected;
186 }
187
188 const std::string& TypeInfo::GetName() const
189 {
190   return mTypeName;
191 }
192
193 const std::string& TypeInfo::GetBaseName() const
194 {
195   return mBaseTypeName;
196 }
197
198 Dali::TypeInfo::CreateFunction TypeInfo::GetCreator() const
199 {
200   return mCreate;
201 }
202
203 void TypeInfo::GetActions( Dali::TypeInfo::NameContainer& ret ) const
204 {
205   for(ActionContainer::const_iterator iter = mActions.begin(); iter != mActions.end(); ++iter)
206   {
207     ret.push_back(iter->first);
208   }
209
210   Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
211   while( base )
212   {
213     for(ActionContainer::const_iterator iter = GetImplementation(base).mActions.begin();
214         iter != GetImplementation(base).mActions.end(); ++iter)
215     {
216       ret.push_back(iter->first);
217     }
218
219     base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
220   }
221 }
222
223 void TypeInfo::GetSignals(Dali::TypeInfo::NameContainer& ret) const
224 {
225   for(ConnectorContainer::const_iterator iter = mSignalConnectors.begin(); iter != mSignalConnectors.end(); ++iter)
226   {
227     ret.push_back(iter->first);
228   }
229
230   Dali::TypeInfo base = Dali::TypeRegistry::Get().GetTypeInfo( mBaseTypeName );
231   while( base )
232   {
233     for(ConnectorContainer::const_iterator iter = GetImplementation(base).mSignalConnectors.begin();
234         iter != GetImplementation(base).mSignalConnectors.end(); ++iter)
235     {
236       ret.push_back(iter->first);
237     }
238
239     base = Dali::TypeRegistry::Get().GetTypeInfo( base.GetBaseName() );
240   }
241 }
242
243 void TypeInfo::GetProperties( Dali::TypeInfo::NameContainer& ret ) const
244 {
245   Property::IndexContainer indices;
246
247   GetPropertyIndices(indices);
248
249   ret.reserve(indices.size());
250
251   for(Property::IndexContainer::iterator iter = indices.begin(); iter != indices.end(); ++iter)
252   {
253     const std::string& name = GetPropertyName( *iter );
254     if(name.size())
255     {
256       ret.push_back( name );
257     }
258     else
259     {
260       DALI_LOG_WARNING("Property had no name\n");
261     }
262   }
263 }
264
265 void TypeInfo::GetPropertyIndices( Property::IndexContainer& indices ) const
266 {
267   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
268   if ( base )
269   {
270     const TypeInfo& baseImpl( GetImplementation( base ) );
271     baseImpl.GetPropertyIndices( indices );
272   }
273
274   if ( ! mRegisteredProperties.empty() )
275   {
276     indices.reserve( indices.size() + mRegisteredProperties.size() );
277
278     const RegisteredPropertyContainer::const_iterator endIter = mRegisteredProperties.end();
279     for ( RegisteredPropertyContainer::const_iterator iter = mRegisteredProperties.begin(); iter != endIter; ++iter )
280     {
281       indices.push_back( iter->first );
282     }
283   }
284 }
285
286 const std::string& TypeInfo::GetPropertyName( Property::Index index ) const
287 {
288   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
289                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
290
291   if ( iter != mRegisteredProperties.end() )
292   {
293     return iter->second.name;
294   }
295
296   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
297   if ( base )
298   {
299     return GetImplementation(base).GetPropertyName( index );
300   }
301
302   DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
303 }
304
305 void TypeInfo::AddActionFunction( const std::string &actionName, Dali::TypeInfo::ActionFunction function )
306 {
307   if( NULL == function)
308   {
309     DALI_LOG_WARNING("Action function is empty\n");
310   }
311   else
312   {
313     ActionContainer::iterator iter = std::find_if(mActions.begin(), mActions.end(),
314                                                   PairFinder<std::string, ActionPair>(actionName));
315
316     if( iter == mActions.end() )
317     {
318       mActions.push_back( ActionPair( actionName, function ) );
319     }
320     else
321     {
322       DALI_LOG_WARNING("Action already exists in TypeRegistry Type", actionName.c_str());
323     }
324   }
325 }
326
327 void TypeInfo::AddConnectorFunction( const std::string& signalName, Dali::TypeInfo::SignalConnectorFunction function )
328 {
329   if( NULL == function)
330   {
331     DALI_LOG_WARNING("Connector function is empty\n");
332   }
333   else
334   {
335     ConnectorContainer::iterator iter = find_if( mSignalConnectors.begin(), mSignalConnectors.end(),
336                                                    PairFinder<std::string, ConnectionPair>(signalName) );
337
338     if( iter == mSignalConnectors.end() )
339     {
340       mSignalConnectors.push_back( ConnectionPair( signalName, function ) );
341     }
342     else
343     {
344       DALI_LOG_WARNING("Signal name already exists in TypeRegistry Type for signal connector function", signalName.c_str());
345     }
346   }
347 }
348
349 void TypeInfo::AddProperty( const std::string& name, Property::Index index, Property::Type type, Dali::TypeInfo::SetPropertyFunction setFunc, Dali::TypeInfo::GetPropertyFunction getFunc )
350 {
351   // The setter can be empty as a property can be read-only.
352
353   if ( NULL == getFunc )
354   {
355     DALI_ASSERT_ALWAYS( ! "GetProperty Function is empty" );
356   }
357   else
358   {
359     RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
360                                                           PairFinder< Property::Index, RegisteredPropertyPair>(index) );
361
362     if ( iter == mRegisteredProperties.end() )
363     {
364       mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, setFunc, getFunc, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
365     }
366     else
367     {
368       DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
369     }
370   }
371 }
372
373 void TypeInfo::AddAnimatableProperty( const std::string& name, Property::Index index, Property::Type type )
374 {
375   RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
376                                                         PairFinder< Property::Index, RegisteredPropertyPair>(index) );
377
378   if ( iter == mRegisteredProperties.end() )
379   {
380     mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, NULL, NULL, name, Property::INVALID_INDEX, Property::INVALID_COMPONENT_INDEX ) ) );
381   }
382   else
383   {
384     DALI_ASSERT_ALWAYS( ! "Property index already added to Type" );
385   }
386 }
387
388 void TypeInfo::AddAnimatablePropertyComponent( const std::string& name, Property::Index index, Property::Index baseIndex, unsigned int componentIndex )
389 {
390   Property::Type type = GetPropertyType( baseIndex );
391   DALI_ASSERT_ALWAYS( ( type == Property::VECTOR2 || type == Property::VECTOR3 || type == Property::VECTOR4 ) && "Base property does not support component" );
392
393   bool success = false;
394
395   RegisteredPropertyContainer::iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
396                                                         PairFinder< Property::Index, RegisteredPropertyPair>(index) );
397
398   if ( iter == mRegisteredProperties.end() )
399   {
400     iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
401                     PropertyComponentFinder< RegisteredPropertyPair >( baseIndex, componentIndex ) );
402
403     if ( iter == mRegisteredProperties.end() )
404     {
405       mRegisteredProperties.push_back( RegisteredPropertyPair( index, RegisteredProperty( type, NULL, NULL, name, baseIndex, componentIndex ) ) );
406       success = true;
407     }
408   }
409
410   DALI_ASSERT_ALWAYS( success && "Property component already registered" );
411 }
412
413 unsigned int TypeInfo::GetPropertyCount() const
414 {
415   unsigned int count( mRegisteredProperties.size() );
416
417   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
418   while ( base )
419   {
420     const TypeInfo& baseImpl( GetImplementation(base) );
421     count += baseImpl.mRegisteredProperties.size();
422     base = TypeRegistry::Get()->GetTypeInfo( baseImpl.mBaseTypeName );
423   }
424
425   return count;
426 }
427
428 Property::Index TypeInfo::GetPropertyIndex( const std::string& name ) const
429 {
430   Property::Index index = Property::INVALID_INDEX;
431
432   // Slow but should not be done that often
433   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
434                                                           PropertyNameFinder< RegisteredPropertyPair >( name ) );
435
436   if ( iter != mRegisteredProperties.end() )
437   {
438     index = iter->first;
439   }
440   else
441   {
442     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
443     if ( base )
444     {
445       index = GetImplementation(base).GetPropertyIndex( name );
446     }
447   }
448
449   return index;
450 }
451
452 Property::Index TypeInfo::GetBasePropertyIndex( Property::Index index ) const
453 {
454   Property::Index basePropertyIndex = Property::INVALID_INDEX;
455
456   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
457                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
458
459   if ( iter != mRegisteredProperties.end() )
460   {
461     basePropertyIndex = iter->second.basePropertyIndex;
462   }
463   else
464   {
465     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
466     if ( base )
467     {
468       basePropertyIndex = GetImplementation(base).GetBasePropertyIndex( index );
469     }
470   }
471
472   return basePropertyIndex;
473 }
474
475 int TypeInfo::GetComponentIndex( Property::Index index ) const
476 {
477   int componentIndex = Property::INVALID_COMPONENT_INDEX;
478
479   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
480                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
481
482   if ( iter != mRegisteredProperties.end() )
483   {
484     componentIndex = iter->second.componentIndex;
485   }
486   else
487   {
488     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
489     if ( base )
490     {
491       componentIndex = GetImplementation(base).GetComponentIndex( index );
492     }
493   }
494
495   return componentIndex;
496 }
497
498 bool TypeInfo::IsPropertyWritable( Property::Index index ) const
499 {
500   bool writable( false );
501
502   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
503                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
504
505   if ( iter != mRegisteredProperties.end() )
506   {
507     if( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
508     {
509       writable = true; // animatable property is writable
510     }
511     else
512     {
513       writable = iter->second.setFunc ? true : false;
514     }
515   }
516   else
517   {
518     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
519     if ( base )
520     {
521       writable = GetImplementation(base).IsPropertyWritable( index );
522     }
523     else
524     {
525       DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
526     }
527   }
528
529   return writable;
530 }
531
532 Property::Type TypeInfo::GetPropertyType( Property::Index index ) const
533 {
534   Property::Type type( Property::NONE );
535
536   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
537                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
538
539   if ( iter != mRegisteredProperties.end() )
540   {
541     type = iter->second.type;
542   }
543   else
544   {
545     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
546     if ( base )
547     {
548       type = GetImplementation(base).GetPropertyType( index );
549     }
550     else
551     {
552       DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
553     }
554   }
555
556   return type;
557 }
558
559 void TypeInfo::SetProperty( BaseObject *object, Property::Index index, const Property::Value& value ) const
560 {
561   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
562                                                               PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
563   if ( iter != mRegisteredProperties.end() )
564   {
565     if( iter->second.setFunc )
566     {
567       iter->second.setFunc( object, index, value );
568     }
569   }
570   else
571   {
572     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
573     if ( base )
574     {
575       GetImplementation(base).SetProperty( object, index, value );
576     }
577     else
578     {
579       DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
580     }
581   }
582 }
583
584 void TypeInfo::SetProperty( BaseObject *object, const std::string& name, const Property::Value& value ) const
585 {
586   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
587                                                               PropertyNameFinder< RegisteredPropertyPair >( name ) );
588   if ( iter != mRegisteredProperties.end() )
589   {
590     DALI_ASSERT_ALWAYS( iter->second.setFunc && "Trying to write to a read-only property" );
591     iter->second.setFunc( object, iter->first, value );
592   }
593   else
594   {
595     Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
596     if ( base )
597     {
598       GetImplementation(base).SetProperty( object, name, value );
599     }
600     else
601     {
602       DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
603     }
604   }
605 }
606
607 Property::Value TypeInfo::GetProperty( const BaseObject *object, Property::Index index ) const
608 {
609   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
610                                                           PairFinder< Property::Index, RegisteredPropertyPair >( index ) );
611   if( iter != mRegisteredProperties.end() )
612   {
613     // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
614     return iter->second.getFunc( const_cast< BaseObject* >( object ), index );
615   }
616
617   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
618   if ( base )
619   {
620     return GetImplementation( base ).GetProperty( object, index );
621   }
622
623   DALI_ASSERT_ALWAYS( ! "Cannot find property index" ); // use the same assert as Object
624 }
625
626 Property::Value TypeInfo::GetProperty( const BaseObject *object, const std::string& name ) const
627 {
628   RegisteredPropertyContainer::const_iterator iter = find_if( mRegisteredProperties.begin(), mRegisteredProperties.end(),
629                                                           PropertyNameFinder< RegisteredPropertyPair >( name ) );
630   if( iter != mRegisteredProperties.end() )
631   {
632     // Need to remove the constness here as CustomActor will not be able to call Downcast with a const pointer to the object
633     return iter->second.getFunc( const_cast< BaseObject* >( object ), iter->first );
634   }
635
636   Dali::TypeInfo base = TypeRegistry::Get()->GetTypeInfo( mBaseTypeName );
637   if ( base )
638   {
639     return GetImplementation( base ).GetProperty( object, name );
640   }
641
642   DALI_ASSERT_ALWAYS( ! "Cannot find property name" );
643 }
644
645 } // namespace Internal
646
647 } // namespace Dali