Support animatable property registration during type registry
[platform/core/uifw/dali-core.git] / dali / internal / event / common / object-impl.cpp
1 /*
2  * Copyright (c) 2014 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/object-impl.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/integration-api/debug.h>
26 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
27 #include <dali/internal/update/common/animatable-property.h>
28 #include <dali/internal/update/common/property-owner-messages.h>
29 #include <dali/internal/event/animation/active-constraint-base.h>
30 #include <dali/internal/event/animation/constraint-impl.h>
31 #include <dali/internal/event/common/stage-impl.h>
32 #include <dali/internal/event/common/property-notification-impl.h>
33 #include <dali/internal/event/common/type-registry-impl.h>
34
35 using Dali::Internal::SceneGraph::AnimatableProperty;
36 using Dali::Internal::SceneGraph::PropertyBase;
37
38 namespace Dali
39 {
40
41 namespace Internal
42 {
43
44 namespace // unnamed namespace
45 {
46 const int SUPPORTED_CAPABILITIES = Dali::Handle::DYNAMIC_PROPERTIES;  // Object provides this capability
47 typedef Dali::Vector<Object::Observer*>::Iterator ObserverIter;
48 typedef Dali::Vector<Object::Observer*>::ConstIterator ConstObserverIter;
49
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_OBJECT" );
52 #endif
53 } // unnamed namespace
54
55 Object::Object()
56 : mEventThreadServices( *Stage::GetCurrent() ),
57   mTypeInfo( NULL ),
58   mConstraints( NULL ),
59   mPropertyNotifications( NULL )
60 {
61 }
62
63 void Object::AddObserver(Observer& observer)
64 {
65   // make sure an observer doesn't observe the same object twice
66   // otherwise it will get multiple calls to OnSceneObjectAdd(), OnSceneObjectRemove() and ObjectDestroyed()
67   DALI_ASSERT_DEBUG( mObservers.End() == std::find( mObservers.Begin(), mObservers.End(), &observer));
68
69   mObservers.PushBack( &observer );
70 }
71
72 void Object::RemoveObserver(Observer& observer)
73 {
74   // Find the observer...
75   const ConstObserverIter endIter =  mObservers.End();
76   for( ObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
77   {
78     if( (*iter) == &observer)
79     {
80       mObservers.Erase( iter );
81       break;
82     }
83   }
84   DALI_ASSERT_DEBUG(endIter != mObservers.End());
85 }
86
87 void Object::OnSceneObjectAdd()
88 {
89   // Notification for this object's constraints
90   if( mConstraints )
91   {
92     const ActiveConstraintConstIter endIter = mConstraints->end();
93     for ( ActiveConstraintIter iter = mConstraints->begin(); endIter != iter; ++iter )
94     {
95       ActiveConstraintBase& baseConstraint = GetImplementation( *iter );
96       baseConstraint.OnParentSceneObjectAdded();
97     }
98   }
99
100   // Notification for observers
101   for( ConstObserverIter iter = mObservers.Begin(),  endIter =  mObservers.End(); iter != endIter; ++iter)
102   {
103     (*iter)->SceneObjectAdded(*this);
104   }
105
106   // enable property notifications in scene graph
107   EnablePropertyNotifications();
108 }
109
110 void Object::OnSceneObjectRemove()
111 {
112   // Notification for this object's constraints
113   if( mConstraints )
114   {
115     const ActiveConstraintConstIter endIter = mConstraints->end();
116     for ( ActiveConstraintIter iter = mConstraints->begin(); endIter != iter; ++iter )
117     {
118       ActiveConstraintBase& baseConstraint = GetImplementation( *iter );
119       baseConstraint.OnParentSceneObjectRemoved();
120     }
121   }
122
123   // Notification for observers
124   for( ConstObserverIter iter = mObservers.Begin(), endIter = mObservers.End(); iter != endIter; ++iter )
125   {
126     (*iter)->SceneObjectRemoved(*this);
127   }
128
129   // disable property notifications in scene graph
130   DisablePropertyNotifications();
131 }
132
133 int Object::GetPropertyComponentIndex( Property::Index index ) const
134 {
135   return Property::INVALID_COMPONENT_INDEX;
136 }
137
138 bool Object::Supports( Capability capability ) const
139 {
140   return (capability & SUPPORTED_CAPABILITIES);
141 }
142
143 unsigned int Object::GetPropertyCount() const
144 {
145   unsigned int count = GetDefaultPropertyCount();
146
147   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Default Properties: %d\n", count );
148
149   const TypeInfo* typeInfo( GetTypeInfo() );
150   if ( typeInfo )
151   {
152     unsigned int manual( typeInfo->GetPropertyCount() );
153     count += manual;
154
155     DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Manual Properties:  %d\n", manual );
156   }
157
158   unsigned int custom( mCustomProperties.Count() );
159   count += custom;
160   DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Custom Properties:  %d\n", custom );
161
162   DALI_LOG_INFO( gLogFilter, Debug::Concise, "Total Properties:   %d\n", count );
163
164   return count;
165 }
166
167 std::string Object::GetPropertyName( Property::Index index ) const
168 {
169   DALI_ASSERT_ALWAYS( index > Property::INVALID_INDEX && "Property index out of bounds" );
170
171   if ( index < DEFAULT_PROPERTY_MAX_COUNT )
172   {
173     return GetDefaultPropertyName( index );
174   }
175
176   if ( ( ( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX ) )
177     || ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) ) )
178   {
179     const TypeInfo* typeInfo( GetTypeInfo() );
180     if ( typeInfo )
181     {
182       return typeInfo->GetPropertyName( index );
183     }
184     else
185     {
186       DALI_ASSERT_ALWAYS( ! "Property index is invalid" );
187     }
188   }
189
190   CustomPropertyMetadata* custom = FindCustomProperty( index );
191   if( custom )
192   {
193     return custom->name;
194   }
195   return "";
196 }
197
198 Property::Index Object::GetPropertyIndex(const std::string& name) const
199 {
200   Property::Index index = GetDefaultPropertyIndex( name );
201
202   if(index == Property::INVALID_INDEX)
203   {
204     const TypeInfo* typeInfo( GetTypeInfo() );
205     if ( typeInfo )
206     {
207       index = typeInfo->GetPropertyIndex( name );
208       if ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
209       {
210         // check whether the animatable property is registered already, if not then register one.
211         AnimatablePropertyMetadata* animatableProperty = FindAnimatableProperty( index );
212         if(!animatableProperty)
213         {
214           const TypeInfo* typeInfo( GetTypeInfo() );
215           if (typeInfo)
216           {
217             index = RegisterSceneGraphProperty(typeInfo->GetPropertyName(index), index, Property::Value(typeInfo->GetPropertyType(index)));
218           }
219         }
220       }
221     }
222   }
223
224   if( (index == Property::INVALID_INDEX)&&( mCustomProperties.Count() > 0 ) )
225   {
226     Property::Index count = PROPERTY_CUSTOM_START_INDEX;
227     const PropertyMetadataLookup::ConstIterator end = mCustomProperties.End();
228     for( PropertyMetadataLookup::ConstIterator iter = mCustomProperties.Begin(); iter != end; ++iter, ++count )
229     {
230       CustomPropertyMetadata* custom = static_cast<CustomPropertyMetadata*>(*iter);
231       if ( custom->name == name )
232       {
233         index = count;
234         break;
235       }
236     }
237   }
238
239   return index;
240 }
241
242 bool Object::IsPropertyWritable( Property::Index index ) const
243 {
244   DALI_ASSERT_ALWAYS(index > Property::INVALID_INDEX && "Property index is out of bounds");
245
246   bool writable = false;
247
248   if ( index < DEFAULT_PROPERTY_MAX_COUNT )
249   {
250     writable = IsDefaultPropertyWritable( index );
251   }
252   else if ( ( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX ) )
253   {
254     const TypeInfo* typeInfo( GetTypeInfo() );
255     if ( typeInfo )
256     {
257       writable = typeInfo->IsPropertyWritable( index );
258     }
259     else
260     {
261       DALI_ASSERT_ALWAYS( ! "Invalid property index" );
262     }
263   }
264   else if ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
265   {
266     // Type Registry scene-graph properties are writable.
267     writable = true;
268   }
269   else
270   {
271     CustomPropertyMetadata* custom = FindCustomProperty( index );
272     if( custom )
273     {
274       writable = custom->IsWritable();
275     }
276   }
277
278   return writable;
279 }
280
281 bool Object::IsPropertyAnimatable( Property::Index index ) const
282 {
283   DALI_ASSERT_ALWAYS(index > Property::INVALID_INDEX && "Property index is out of bounds");
284
285   bool animatable = false;
286
287   if ( index < DEFAULT_PROPERTY_MAX_COUNT )
288   {
289     animatable = IsDefaultPropertyAnimatable( index );
290   }
291   else if ( ( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX ) )
292   {
293     // Type Registry event-thread only properties are not animatable.
294     animatable = false;
295   }
296   else if ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
297   {
298     // Type Registry scene-graph properties are animatable.
299     animatable = true;
300   }
301   else
302   {
303     CustomPropertyMetadata* custom = FindCustomProperty( index );
304     if( custom )
305     {
306       animatable = custom->IsAnimatable();
307     }
308   }
309
310   return animatable;
311 }
312
313 bool Object::IsPropertyAConstraintInput( Property::Index index ) const
314 {
315   DALI_ASSERT_ALWAYS(index > Property::INVALID_INDEX && "Property index is out of bounds");
316
317   bool isConstraintInput = false;
318
319   if ( index < DEFAULT_PROPERTY_MAX_COUNT )
320   {
321     isConstraintInput = IsDefaultPropertyAConstraintInput( index );
322   }
323   else if ( ( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX ) )
324   {
325     // Type Registry event-thread only properties cannot be used as an input to a constraint.
326     isConstraintInput = false;
327   }
328   else if ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
329   {
330     // scene graph properties can be used as input to a constraint.
331     isConstraintInput = true;
332   }
333   else
334   {
335     CustomPropertyMetadata* custom = FindCustomProperty( index );
336     if( custom )
337     {
338       // ... custom properties can be used as input to a constraint.
339       isConstraintInput = true;
340     }
341   }
342
343   return isConstraintInput;
344 }
345
346 Property::Type Object::GetPropertyType( Property::Index index ) const
347 {
348   DALI_ASSERT_ALWAYS(index > Property::INVALID_INDEX && "Property index is out of bounds" );
349
350   if ( index < DEFAULT_PROPERTY_MAX_COUNT )
351   {
352     return GetDefaultPropertyType( index );
353   }
354
355   if ( ( ( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX ) )
356     || ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) ) )
357   {
358     const TypeInfo* typeInfo( GetTypeInfo() );
359     if ( typeInfo )
360     {
361       return typeInfo->GetPropertyType( index );
362     }
363     else
364     {
365       DALI_ASSERT_ALWAYS( ! "Cannot find property index" );
366     }
367   }
368
369   CustomPropertyMetadata* custom = FindCustomProperty( index );
370   if( custom )
371   {
372     return custom->type;
373   }
374   return Property::NONE;
375 }
376
377 void Object::SetProperty( Property::Index index, const Property::Value& propertyValue )
378 {
379   DALI_ASSERT_ALWAYS(index > Property::INVALID_INDEX && "Property index is out of bounds" );
380
381   if ( index < DEFAULT_PROPERTY_MAX_COUNT )
382   {
383     SetDefaultProperty( index, propertyValue );
384   }
385   else if ( ( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX ) )
386   {
387     const TypeInfo* typeInfo( GetTypeInfo() );
388     if ( typeInfo )
389     {
390       typeInfo->SetProperty( this, index, propertyValue );
391     }
392     else
393     {
394       DALI_LOG_ERROR("Cannot find property index\n");
395     }
396   }
397   else if ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
398   {
399     // check whether the animatable property is registered already, if not then register one.
400     AnimatablePropertyMetadata* animatableProperty = FindAnimatableProperty( index );
401     if(!animatableProperty)
402     {
403       const TypeInfo* typeInfo( GetTypeInfo() );
404       if (typeInfo && Property::INVALID_INDEX == RegisterSceneGraphProperty(typeInfo->GetPropertyName(index), index, propertyValue))
405       {
406         DALI_LOG_ERROR("Cannot register property\n");
407       }
408       else
409       {
410         DALI_LOG_ERROR("Cannot find property index\n");
411       }
412     }
413     else
414     {
415       // set the scene graph property value
416       SetSceneGraphProperty( index, *animatableProperty, propertyValue );
417     }
418   }
419   else
420   {
421     CustomPropertyMetadata* custom = FindCustomProperty( index );
422     if( custom )
423     {
424       if( custom->IsAnimatable() )
425       {
426         // set the scene graph property value
427         SetSceneGraphProperty( index, *custom, propertyValue );
428       }
429       else if( custom->IsWritable() )
430       {
431         custom->value = propertyValue;
432         OnPropertySet(index, propertyValue);
433       }
434       // trying to set value on read only property is no-op
435     }
436     else
437     {
438       DALI_LOG_ERROR("Invalid property index\n");
439     }
440   }
441 }
442
443 Property::Value Object::GetProperty(Property::Index index) const
444 {
445   DALI_ASSERT_ALWAYS( index > Property::INVALID_INDEX && "Property index is out of bounds" );
446
447   Property::Value value;
448
449   if ( index < DEFAULT_PROPERTY_MAX_COUNT )
450   {
451     value = GetDefaultProperty( index );
452   }
453   else if ( ( index >= PROPERTY_REGISTRATION_START_INDEX ) && ( index <= PROPERTY_REGISTRATION_MAX_INDEX ) )
454   {
455     const TypeInfo* typeInfo( GetTypeInfo() );
456     if ( typeInfo )
457     {
458       value = typeInfo->GetProperty( this, index );
459     }
460     else
461     {
462       DALI_LOG_ERROR("Cannot find property index\n");
463     }
464   }
465   else if ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
466   {
467     // check whether the animatable property is registered already, if not then register one.
468     AnimatablePropertyMetadata* animatableProperty = FindAnimatableProperty( index );
469     if(!animatableProperty)
470     {
471       const TypeInfo* typeInfo( GetTypeInfo() );
472       if (typeInfo)
473       {
474         if(Property::INVALID_INDEX != RegisterSceneGraphProperty(typeInfo->GetPropertyName(index), index, Property::Value(typeInfo->GetPropertyType(index))))
475         {
476           value = Property::Value(typeInfo->GetPropertyType(index)); // Return an initialized property value according to the type
477         }
478         else
479         {
480           DALI_LOG_ERROR("Cannot register property\n");
481         }
482       }
483       else
484       {
485         DALI_LOG_ERROR("Cannot find property index\n");
486       }
487     }
488     else
489     {
490       // get the animatable property value
491       value = GetPropertyValue( animatableProperty );
492     }
493   }
494   else if(mCustomProperties.Count() > 0)
495   {
496     CustomPropertyMetadata* custom = FindCustomProperty( index );
497     if(custom)
498     {
499       // get the custom property value
500       value = GetPropertyValue( custom );
501     }
502     else
503     {
504       DALI_LOG_ERROR("Invalid property index\n");
505     }
506   } // if custom
507
508   return value;
509 }
510
511 void Object::GetPropertyIndices( Property::IndexContainer& indices ) const
512 {
513   indices.clear();
514
515   // Default Properties
516   GetDefaultPropertyIndices( indices );
517
518   // Manual Properties
519   const TypeInfo* typeInfo( GetTypeInfo() );
520   if ( typeInfo )
521   {
522     typeInfo->GetPropertyIndices( indices );
523   }
524
525   // Custom Properties
526   if ( mCustomProperties.Count() > 0 )
527   {
528     indices.reserve( indices.size() + mCustomProperties.Count() );
529
530     PropertyMetadataLookup::ConstIterator iter = mCustomProperties.Begin();
531     const PropertyMetadataLookup::ConstIterator endIter = mCustomProperties.End();
532     int i=0;
533     for ( ; iter != endIter; ++iter, ++i )
534     {
535       indices.push_back( PROPERTY_CUSTOM_START_INDEX + i );
536     }
537   }
538 }
539
540 Property::Index Object::RegisterSceneGraphProperty(const std::string& name, Property::Index index, const Property::Value& propertyValue) const
541 {
542   // Create a new property
543   Dali::Internal::OwnerPointer<PropertyBase> newProperty;
544
545   switch ( propertyValue.GetType() )
546   {
547     case Property::BOOLEAN:
548     {
549       newProperty = new AnimatableProperty<bool>( propertyValue.Get<bool>() );
550       break;
551     }
552
553     case Property::FLOAT:
554     {
555       newProperty = new AnimatableProperty<float>( propertyValue.Get<float>() );
556       break;
557     }
558
559     case Property::INTEGER:
560     {
561       newProperty = new AnimatableProperty<int>( propertyValue.Get<int>() );
562       break;
563     }
564
565     case Property::VECTOR2:
566     {
567       newProperty = new AnimatableProperty<Vector2>( propertyValue.Get<Vector2>() );
568       break;
569     }
570
571     case Property::VECTOR3:
572     {
573       newProperty = new AnimatableProperty<Vector3>( propertyValue.Get<Vector3>() );
574       break;
575     }
576
577     case Property::VECTOR4:
578     {
579       newProperty = new AnimatableProperty<Vector4>( propertyValue.Get<Vector4>() );
580       break;
581     }
582
583     case Property::MATRIX:
584     {
585       newProperty = new AnimatableProperty<Matrix>( propertyValue.Get<Matrix>() );
586       break;
587     }
588
589     case Property::MATRIX3:
590     {
591       newProperty = new AnimatableProperty<Matrix3>( propertyValue.Get<Matrix3>() );
592       break;
593     }
594
595     case Property::ROTATION:
596     {
597       newProperty = new AnimatableProperty<Quaternion>( propertyValue.Get<Quaternion>() );
598       break;
599     }
600
601     case Property::UNSIGNED_INTEGER:
602     case Property::RECTANGLE:
603     case Property::STRING:
604     case Property::ARRAY:
605     case Property::MAP:
606     {
607       DALI_LOG_WARNING( "Property Type %d\n", propertyValue.GetType() );
608       DALI_ASSERT_ALWAYS( !"PropertyType is not animatable" );
609       break;
610     }
611
612     default:
613     {
614       DALI_LOG_WARNING( "Property Type %d\n", propertyValue.GetType() );
615       DALI_ASSERT_ALWAYS( !"PropertyType enumeration is out of bounds" );
616       break;
617     }
618   }
619
620   // get the scene property owner from derived class
621   const SceneGraph::PropertyOwner* scenePropertyOwner = GetPropertyOwner();
622   // we can only pass properties to scene graph side if there is a scene object
623   if( scenePropertyOwner )
624   {
625     // keep a local pointer to the property as the OwnerPointer will pass its copy to the message
626     const PropertyBase* property = newProperty.Get();
627     if(index >= PROPERTY_CUSTOM_START_INDEX)
628     {
629       mCustomProperties.PushBack( new CustomPropertyMetadata( name, propertyValue.GetType(), property ) );
630     }
631     else
632     {
633       mAnimatableProperties.PushBack( new AnimatablePropertyMetadata( propertyValue.GetType(), property ) );
634     }
635
636     // queue a message to add the property
637     InstallCustomPropertyMessage( const_cast<EventThreadServices&>(GetEventThreadServices()), *scenePropertyOwner, newProperty.Release() ); // Message takes ownership
638
639     // notify the derived class (optional) method in case it needs to do some more work on the new property
640     // note! have to use the local pointer as OwnerPointer now points to NULL as it handed over its ownership
641     NotifyScenePropertyInstalled( *property, name, index );
642
643     return index;
644   }
645   else
646   {
647     // property was orphaned and killed so return invalid index
648     return Property::INVALID_INDEX;
649   }
650 }
651
652 Property::Index Object::RegisterProperty( const std::string& name, const Property::Value& propertyValue)
653 {
654   return RegisterSceneGraphProperty(name, PROPERTY_CUSTOM_START_INDEX + mCustomProperties.Count(), propertyValue);
655 }
656
657 Property::Index Object::RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode)
658 {
659   Property::Index index = Property::INVALID_INDEX;
660
661   if(Property::ANIMATABLE == accessMode)
662   {
663     index = RegisterProperty(name, propertyValue);
664   }
665   else
666   {
667     // Add entry to the property lookup
668     index = PROPERTY_CUSTOM_START_INDEX + mCustomProperties.Count();
669     mCustomProperties.PushBack( new CustomPropertyMetadata( name, propertyValue, accessMode ) );
670   }
671
672   return index;
673 }
674
675 Dali::PropertyNotification Object::AddPropertyNotification(Property::Index index,
676                                                                 int componentIndex,
677                                                                 const Dali::PropertyCondition& condition)
678 {
679   if ( index >= DEFAULT_PROPERTY_MAX_COUNT )
680   {
681     if ( index <= PROPERTY_REGISTRATION_MAX_INDEX )
682     {
683       DALI_ASSERT_ALWAYS( false && "Property notification added to event side only property." );
684     }
685     else if ( mCustomProperties.Count() > 0 )
686     {
687       CustomPropertyMetadata* custom = FindCustomProperty( index );
688       DALI_ASSERT_ALWAYS( custom && "Invalid property index" );
689       DALI_ASSERT_ALWAYS( custom->IsAnimatable() && "Property notification added to event side only property." );
690     }
691   }
692
693   Dali::Handle self(this);
694   Property target( self, index );
695
696   PropertyNotificationPtr internal = PropertyNotification::New( target, componentIndex, condition );
697   Dali::PropertyNotification propertyNotification(internal.Get());
698
699   if( !mPropertyNotifications )
700   {
701     mPropertyNotifications = new PropertyNotificationContainer;
702   }
703   mPropertyNotifications->push_back(propertyNotification);
704
705   return propertyNotification;
706 }
707
708 void Object::RemovePropertyNotification(Dali::PropertyNotification propertyNotification)
709 {
710   if( mPropertyNotifications )
711   {
712     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
713     while(iter != mPropertyNotifications->end() )
714     {
715       if(*iter == propertyNotification)
716       {
717         mPropertyNotifications->erase(iter);
718         // As we can't ensure all references are removed, we can just disable
719         // the notification.
720         GetImplementation(propertyNotification).Disable();
721         return;
722       }
723       ++iter;
724     }
725   }
726 }
727
728 void Object::RemovePropertyNotifications()
729 {
730   if( mPropertyNotifications )
731   {
732     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
733     while(iter != mPropertyNotifications->end() )
734     {
735       // As we can't ensure all references are removed, we can just disable
736       // the notification.
737       GetImplementation(*iter).Disable();
738       ++iter;
739     }
740
741     mPropertyNotifications->clear();
742   }
743 }
744
745 void Object::EnablePropertyNotifications()
746 {
747   if( mPropertyNotifications )
748   {
749     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
750     PropertyNotificationContainerIter endIter = mPropertyNotifications->end();
751
752     for( ; iter != endIter; ++iter )
753     {
754       GetImplementation(*iter).Enable();
755     }
756   }
757 }
758
759 void Object::DisablePropertyNotifications()
760 {
761   if( mPropertyNotifications )
762   {
763     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
764     PropertyNotificationContainerIter endIter = mPropertyNotifications->end();
765
766     for( ; iter != endIter; ++iter )
767     {
768       GetImplementation(*iter).Disable();
769     }
770   }
771 }
772
773 Dali::ActiveConstraint Object::ApplyConstraint( Constraint& constraint )
774 {
775   return Dali::ActiveConstraint( DoApplyConstraint( constraint, Dali::Handle() ) );
776 }
777
778 Dali::ActiveConstraint Object::ApplyConstraint( Constraint& constraint, Dali::Handle weightObject )
779 {
780   return Dali::ActiveConstraint( DoApplyConstraint( constraint, weightObject ) );
781 }
782
783 ActiveConstraintBase* Object::DoApplyConstraint( Constraint& constraint, Dali::Handle weightObject )
784 {
785   ActiveConstraintBase* activeConstraintImpl = constraint.CreateActiveConstraint();
786   DALI_ASSERT_DEBUG( NULL != activeConstraintImpl );
787
788   Dali::ActiveConstraint activeConstraint( activeConstraintImpl );
789
790   if( weightObject )
791   {
792     Object& weightObjectImpl = GetImplementation( weightObject );
793     Property::Index weightIndex = weightObjectImpl.GetPropertyIndex( "weight" );
794
795     if( Property::INVALID_INDEX != weightIndex )
796     {
797       activeConstraintImpl->SetCustomWeightObject( weightObjectImpl, weightIndex );
798     }
799   }
800
801   if( !mConstraints )
802   {
803     mConstraints = new ActiveConstraintContainer;
804   }
805   mConstraints->push_back( activeConstraint );
806
807   activeConstraintImpl->FirstApply( *this, constraint.GetApplyTime() );
808
809   return activeConstraintImpl;
810 }
811
812 Property::Value Object::GetPropertyValue( const PropertyMetadata* entry ) const
813 {
814   Property::Value value;
815
816   DALI_ASSERT_ALWAYS( entry && "Invalid property metadata" );
817
818   if( !entry->IsAnimatable() )
819   {
820     value = entry->value;
821   }
822   else
823   {
824     BufferIndex bufferIndex( GetEventThreadServices().GetEventBufferIndex() );
825
826     switch ( entry->type )
827     {
828       case Property::BOOLEAN:
829       {
830         const AnimatableProperty<bool>* property = dynamic_cast< const AnimatableProperty<bool>* >( entry->GetSceneGraphProperty() );
831         DALI_ASSERT_DEBUG( NULL != property );
832
833         value = (*property)[ bufferIndex ];
834         break;
835       }
836
837       case Property::FLOAT:
838       {
839         const AnimatableProperty<float>* property = dynamic_cast< const AnimatableProperty<float>* >( entry->GetSceneGraphProperty() );
840         DALI_ASSERT_DEBUG( NULL != property );
841
842         value = (*property)[ bufferIndex ];
843         break;
844       }
845
846       case Property::INTEGER:
847       {
848         const AnimatableProperty<int>* property = dynamic_cast< const AnimatableProperty<int>* >( entry->GetSceneGraphProperty() );
849         DALI_ASSERT_DEBUG( NULL != property );
850
851         value = (*property)[ bufferIndex ];
852         break;
853       }
854
855       case Property::VECTOR2:
856       {
857         const AnimatableProperty<Vector2>* property = dynamic_cast< const AnimatableProperty<Vector2>* >( entry->GetSceneGraphProperty() );
858         DALI_ASSERT_DEBUG( NULL != property );
859
860         value = (*property)[ bufferIndex ];
861         break;
862       }
863
864       case Property::VECTOR3:
865       {
866         const AnimatableProperty<Vector3>* property = dynamic_cast< const AnimatableProperty<Vector3>* >( entry->GetSceneGraphProperty() );
867         DALI_ASSERT_DEBUG( NULL != property );
868
869         value = (*property)[ bufferIndex ];
870         break;
871       }
872
873       case Property::VECTOR4:
874       {
875         const AnimatableProperty<Vector4>* property = dynamic_cast< const AnimatableProperty<Vector4>* >( entry->GetSceneGraphProperty() );
876         DALI_ASSERT_DEBUG( NULL != property );
877
878         value = (*property)[ bufferIndex ];
879         break;
880       }
881
882       case Property::MATRIX:
883       {
884         const AnimatableProperty<Matrix>* property = dynamic_cast< const AnimatableProperty<Matrix>* >( entry->GetSceneGraphProperty() );
885         DALI_ASSERT_DEBUG( NULL != property );
886
887         value = (*property)[ bufferIndex ];
888         break;
889       }
890
891       case Property::MATRIX3:
892       {
893         const AnimatableProperty<Matrix3>* property = dynamic_cast< const AnimatableProperty<Matrix3>* >( entry->GetSceneGraphProperty() );
894         DALI_ASSERT_DEBUG( NULL != property );
895
896         value = (*property)[ bufferIndex ];
897         break;
898       }
899
900       case Property::ROTATION:
901       {
902         const AnimatableProperty<Quaternion>* property = dynamic_cast< const AnimatableProperty<Quaternion>* >( entry->GetSceneGraphProperty() );
903         DALI_ASSERT_DEBUG( NULL != property );
904
905         value = (*property)[ bufferIndex ];
906         break;
907       }
908
909       default:
910       {
911         DALI_ASSERT_ALWAYS( false && "PropertyType enumeration is out of bounds" );
912         break;
913       }
914     } // switch(type)
915   } // if animatable
916
917   return value;
918 }
919
920 void Object::SetSceneGraphProperty( Property::Index index, const PropertyMetadata& entry, const Property::Value& value )
921 {
922   switch ( entry.type )
923   {
924     case Property::BOOLEAN:
925     {
926       const AnimatableProperty<bool>* property = dynamic_cast< const AnimatableProperty<bool>* >( entry.GetSceneGraphProperty() );
927       DALI_ASSERT_DEBUG( NULL != property );
928
929       // property is being used in a separate thread; queue a message to set the property
930       BakeMessage<bool>( GetEventThreadServices(), *property, value.Get<bool>() );
931       break;
932     }
933
934     case Property::FLOAT:
935     {
936       const AnimatableProperty<float>* property = dynamic_cast< const AnimatableProperty<float>* >( entry.GetSceneGraphProperty() );
937       DALI_ASSERT_DEBUG( NULL != property );
938
939       // property is being used in a separate thread; queue a message to set the property
940       BakeMessage<float>( GetEventThreadServices(), *property, value.Get<float>() );
941       break;
942     }
943
944     case Property::INTEGER:
945     {
946       const AnimatableProperty<int>* property = dynamic_cast< const AnimatableProperty<int>* >( entry.GetSceneGraphProperty() );
947       DALI_ASSERT_DEBUG( NULL != property );
948
949       // property is being used in a separate thread; queue a message to set the property
950       BakeMessage<int>( GetEventThreadServices(), *property, value.Get<int>() );
951       break;
952     }
953
954     case Property::VECTOR2:
955     {
956       const AnimatableProperty<Vector2>* property = dynamic_cast< const AnimatableProperty<Vector2>* >( entry.GetSceneGraphProperty() );
957       DALI_ASSERT_DEBUG( NULL != property );
958
959       // property is being used in a separate thread; queue a message to set the property
960       BakeMessage<Vector2>( GetEventThreadServices(), *property, value.Get<Vector2>() );
961       break;
962     }
963
964     case Property::VECTOR3:
965     {
966       const AnimatableProperty<Vector3>* property = dynamic_cast< const AnimatableProperty<Vector3>* >( entry.GetSceneGraphProperty() );
967       DALI_ASSERT_DEBUG( NULL != property );
968
969       // property is being used in a separate thread; queue a message to set the property
970       BakeMessage<Vector3>( GetEventThreadServices(), *property, value.Get<Vector3>() );
971       break;
972     }
973
974     case Property::VECTOR4:
975     {
976       const AnimatableProperty<Vector4>* property = dynamic_cast< const AnimatableProperty<Vector4>* >( entry.GetSceneGraphProperty() );
977       DALI_ASSERT_DEBUG( NULL != property );
978
979       // property is being used in a separate thread; queue a message to set the property
980       BakeMessage<Vector4>( GetEventThreadServices(), *property, value.Get<Vector4>() );
981       break;
982     }
983
984     case Property::ROTATION:
985     {
986       const AnimatableProperty<Quaternion>* property = dynamic_cast< const AnimatableProperty<Quaternion>* >( entry.GetSceneGraphProperty() );
987       DALI_ASSERT_DEBUG( NULL != property );
988
989       // property is being used in a separate thread; queue a message to set the property
990       BakeMessage<Quaternion>( GetEventThreadServices(), *property, value.Get<Quaternion>() );
991       break;
992     }
993
994     case Property::MATRIX:
995     {
996       const AnimatableProperty<Matrix>* property = dynamic_cast< const AnimatableProperty<Matrix>* >( entry.GetSceneGraphProperty() );
997       DALI_ASSERT_DEBUG( NULL != property );
998
999       // property is being used in a separate thread; queue a message to set the property
1000       BakeMessage<Matrix>( GetEventThreadServices(), *property, value.Get<Matrix>() );
1001       break;
1002     }
1003
1004     case Property::MATRIX3:
1005     {
1006       const AnimatableProperty<Matrix3>* property = dynamic_cast< const AnimatableProperty<Matrix3>* >( entry.GetSceneGraphProperty() );
1007       DALI_ASSERT_DEBUG( NULL != property );
1008
1009       // property is being used in a separate thread; queue a message to set the property
1010       BakeMessage<Matrix3>( GetEventThreadServices(), *property, value.Get<Matrix3>() );
1011       break;
1012     }
1013
1014     default:
1015     {
1016       // non-animatable scene graph property, do nothing
1017     }
1018   }
1019 }
1020
1021 const TypeInfo* Object::GetTypeInfo() const
1022 {
1023   if ( !mTypeInfo )
1024   {
1025     // This uses a dynamic_cast so can be quite expensive so we only really want to do it once
1026     // especially as the type-info does not change during the life-time of an application
1027
1028     Dali::TypeInfo typeInfoHandle = TypeRegistry::Get()->GetTypeInfo( this );
1029     if ( typeInfoHandle )
1030     {
1031       mTypeInfo = &GetImplementation( typeInfoHandle );
1032     }
1033   }
1034
1035   return mTypeInfo;
1036 }
1037
1038 void Object::RemoveConstraint( ActiveConstraint& constraint, bool isInScenegraph )
1039 {
1040   // guard against constraint sending messages during core destruction
1041   if ( Stage::IsInstalled() )
1042   {
1043     if( isInScenegraph )
1044     {
1045       ActiveConstraintBase& baseConstraint = GetImplementation( constraint );
1046       baseConstraint.BeginRemove();
1047     }
1048   }
1049 }
1050
1051 void Object::RemoveConstraint( Dali::ActiveConstraint activeConstraint )
1052 {
1053   // guard against constraint sending messages during core destruction
1054   if( mConstraints && Stage::IsInstalled() )
1055   {
1056     bool isInSceneGraph( NULL != GetSceneObject() );
1057
1058     ActiveConstraintIter it( std::find( mConstraints->begin(), mConstraints->end(), activeConstraint ) );
1059     if( it !=  mConstraints->end() )
1060     {
1061       RemoveConstraint( *it, isInSceneGraph );
1062       mConstraints->erase( it );
1063     }
1064   }
1065 }
1066
1067 void Object::RemoveConstraints( unsigned int tag )
1068 {
1069   // guard against constraint sending messages during core destruction
1070   if( mConstraints && Stage::IsInstalled() )
1071   {
1072     bool isInSceneGraph( NULL != GetSceneObject() );
1073
1074     ActiveConstraintIter iter( mConstraints->begin() );
1075     while(iter != mConstraints->end() )
1076     {
1077       ActiveConstraintBase& constraint = GetImplementation( *iter );
1078       if( constraint.GetTag() == tag )
1079       {
1080         RemoveConstraint( *iter, isInSceneGraph );
1081         iter = mConstraints->erase( iter );
1082       }
1083       else
1084       {
1085         ++iter;
1086       }
1087     }
1088   }
1089 }
1090
1091 void Object::RemoveConstraints()
1092 {
1093   // guard against constraint sending messages during core destruction
1094   if( mConstraints && Stage::IsInstalled() )
1095   {
1096     // If we have nothing in the scene-graph, just clear constraint containers
1097     const SceneGraph::PropertyOwner* propertyOwner = GetSceneObject();
1098     if ( NULL != propertyOwner )
1099     {
1100       const ActiveConstraintConstIter endIter = mConstraints->end();
1101       for ( ActiveConstraintIter iter = mConstraints->begin(); endIter != iter; ++iter )
1102       {
1103         RemoveConstraint( *iter, true );
1104       }
1105     }
1106
1107     delete mConstraints;
1108     mConstraints = NULL;
1109   }
1110 }
1111
1112 void Object::SetTypeInfo( const TypeInfo* typeInfo )
1113 {
1114   mTypeInfo = typeInfo;
1115 }
1116
1117 Object::~Object()
1118 {
1119   // Notification for this object's constraints
1120   // (note that the ActiveConstraint handles may outlive the Object)
1121   if( mConstraints )
1122   {
1123     const ActiveConstraintConstIter endIter = mConstraints->end();
1124     for ( ActiveConstraintIter iter = mConstraints->begin(); endIter != iter; ++iter )
1125     {
1126       ActiveConstraintBase& baseConstraint = GetImplementation( *iter );
1127       baseConstraint.OnParentDestroyed();
1128     }
1129   }
1130
1131   // Notification for observers
1132   for( ConstObserverIter iter = mObservers.Begin(), endIter =  mObservers.End(); iter != endIter; ++iter)
1133   {
1134     (*iter)->ObjectDestroyed(*this);
1135   }
1136
1137   delete mConstraints;
1138   delete mPropertyNotifications;
1139 }
1140
1141 CustomPropertyMetadata* Object::FindCustomProperty( Property::Index index ) const
1142 {
1143   CustomPropertyMetadata* property( NULL );
1144   int arrayIndex = index - PROPERTY_CUSTOM_START_INDEX;
1145   if( arrayIndex >= 0 )
1146   {
1147     if( arrayIndex < (int)mCustomProperties.Count() ) // we can only access the first 2 billion custom properties
1148     {
1149       property = static_cast<CustomPropertyMetadata*>(mCustomProperties[ arrayIndex ]);
1150     }
1151   }
1152   return property;
1153 }
1154
1155 AnimatablePropertyMetadata* Object::FindAnimatableProperty( Property::Index index ) const
1156 {
1157   AnimatablePropertyMetadata* property( NULL );
1158   int arrayIndex = index - ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX;
1159   if( arrayIndex >= 0 )
1160   {
1161     if( arrayIndex < (int)mAnimatableProperties.Count() )
1162     {
1163       property = static_cast<AnimatablePropertyMetadata*>(mAnimatableProperties[ arrayIndex ]);
1164     }
1165   }
1166   return property;
1167 }
1168
1169 } // namespace Internal
1170
1171 } // namespace Dali