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