Merge "Fix prevent issue - Unsigned compared against 0" into devel/master
[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     {
591       DALI_ASSERT_ALWAYS( !"PropertyType is not animatable" );
592       break;
593     }
594   }
595
596   // get the scene property owner from derived class
597   const SceneGraph::PropertyOwner* scenePropertyOwner = GetPropertyOwner();
598   // we can only pass properties to scene graph side if there is a scene object
599   if( scenePropertyOwner )
600   {
601     // keep a local pointer to the property as the OwnerPointer will pass its copy to the message
602     const PropertyBase* property = newProperty.Get();
603     if(index >= PROPERTY_CUSTOM_START_INDEX)
604     {
605       mCustomProperties.PushBack( new CustomPropertyMetadata( name, propertyValue.GetType(), property ) );
606     }
607     else
608     {
609       mAnimatableProperties.PushBack( new AnimatablePropertyMetadata( index, Property::INVALID_COMPONENT_INDEX, propertyValue.GetType(), property ) ); // base property
610     }
611
612     // queue a message to add the property
613     InstallCustomPropertyMessage( const_cast<EventThreadServices&>(GetEventThreadServices()), *scenePropertyOwner, newProperty.Release() ); // Message takes ownership
614
615     // notify the derived class (optional) method in case it needs to do some more work on the new property
616     // note! have to use the local pointer as OwnerPointer now points to NULL as it handed over its ownership
617     NotifyScenePropertyInstalled( *property, name, index );
618
619     return index;
620   }
621   else
622   {
623     // property was orphaned and killed so return invalid index
624     return Property::INVALID_INDEX;
625   }
626 }
627
628 Property::Index Object::RegisterProperty( const std::string& name, const Property::Value& propertyValue)
629 {
630   return RegisterSceneGraphProperty(name, PROPERTY_CUSTOM_START_INDEX + mCustomProperties.Count(), propertyValue);
631 }
632
633 Property::Index Object::RegisterProperty( const std::string& name, const Property::Value& propertyValue, Property::AccessMode accessMode)
634 {
635   Property::Index index = Property::INVALID_INDEX;
636
637   if(Property::ANIMATABLE == accessMode)
638   {
639     index = RegisterProperty(name, propertyValue);
640   }
641   else
642   {
643     // Add entry to the property lookup
644     index = PROPERTY_CUSTOM_START_INDEX + mCustomProperties.Count();
645     mCustomProperties.PushBack( new CustomPropertyMetadata( name, propertyValue, accessMode ) );
646   }
647
648   return index;
649 }
650
651 Dali::PropertyNotification Object::AddPropertyNotification(Property::Index index,
652                                                                 int componentIndex,
653                                                                 const Dali::PropertyCondition& condition)
654 {
655   if ( index >= DEFAULT_PROPERTY_MAX_COUNT )
656   {
657     if ( index <= PROPERTY_REGISTRATION_MAX_INDEX )
658     {
659       DALI_ASSERT_ALWAYS( false && "Property notification added to event side only property." );
660     }
661     else if ( ( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ) )
662     {
663       // check whether the animatable property is registered already, if not then register one.
664       AnimatablePropertyMetadata* animatable = RegisterAnimatableProperty( index );
665       DALI_ASSERT_ALWAYS( animatable && "Property index is invalid" );
666     }
667     else if ( mCustomProperties.Count() > 0 )
668     {
669       CustomPropertyMetadata* custom = FindCustomProperty( index );
670       DALI_ASSERT_ALWAYS( custom && "Invalid property index" );
671       DALI_ASSERT_ALWAYS( custom->IsAnimatable() && "Property notification added to event side only property." );
672     }
673   }
674
675   Dali::Handle self(this);
676   Property target( self, index );
677
678   PropertyNotificationPtr internal = PropertyNotification::New( target, componentIndex, condition );
679   Dali::PropertyNotification propertyNotification(internal.Get());
680
681   if( !mPropertyNotifications )
682   {
683     mPropertyNotifications = new PropertyNotificationContainer;
684   }
685   mPropertyNotifications->push_back(propertyNotification);
686
687   return propertyNotification;
688 }
689
690 void Object::RemovePropertyNotification(Dali::PropertyNotification propertyNotification)
691 {
692   if( mPropertyNotifications )
693   {
694     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
695     while(iter != mPropertyNotifications->end() )
696     {
697       if(*iter == propertyNotification)
698       {
699         mPropertyNotifications->erase(iter);
700         // As we can't ensure all references are removed, we can just disable
701         // the notification.
702         GetImplementation(propertyNotification).Disable();
703         return;
704       }
705       ++iter;
706     }
707   }
708 }
709
710 void Object::RemovePropertyNotifications()
711 {
712   if( mPropertyNotifications )
713   {
714     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
715     while(iter != mPropertyNotifications->end() )
716     {
717       // As we can't ensure all references are removed, we can just disable
718       // the notification.
719       GetImplementation(*iter).Disable();
720       ++iter;
721     }
722
723     mPropertyNotifications->clear();
724   }
725 }
726
727 void Object::EnablePropertyNotifications()
728 {
729   if( mPropertyNotifications )
730   {
731     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
732     PropertyNotificationContainerIter endIter = mPropertyNotifications->end();
733
734     for( ; iter != endIter; ++iter )
735     {
736       GetImplementation(*iter).Enable();
737     }
738   }
739 }
740
741 void Object::DisablePropertyNotifications()
742 {
743   if( mPropertyNotifications )
744   {
745     PropertyNotificationContainerIter iter = mPropertyNotifications->begin();
746     PropertyNotificationContainerIter endIter = mPropertyNotifications->end();
747
748     for( ; iter != endIter; ++iter )
749     {
750       GetImplementation(*iter).Disable();
751     }
752   }
753 }
754
755 Property::Value Object::GetPropertyValue( const PropertyMetadata* entry ) const
756 {
757   Property::Value value;
758
759   DALI_ASSERT_ALWAYS( entry && "Invalid property metadata" );
760
761   if( !entry->IsAnimatable() )
762   {
763     value = entry->value;
764   }
765   else
766   {
767     BufferIndex bufferIndex( GetEventThreadServices().GetEventBufferIndex() );
768
769     switch ( entry->type )
770     {
771       case Property::BOOLEAN:
772       {
773         const AnimatableProperty<bool>* property = dynamic_cast< const AnimatableProperty<bool>* >( entry->GetSceneGraphProperty() );
774         DALI_ASSERT_DEBUG( NULL != property );
775
776         value = (*property)[ bufferIndex ];
777         break;
778       }
779
780       case Property::INTEGER:
781       {
782         const AnimatableProperty<int>* property = dynamic_cast< const AnimatableProperty<int>* >( entry->GetSceneGraphProperty() );
783         DALI_ASSERT_DEBUG( NULL != property );
784
785         value = (*property)[ bufferIndex ];
786         break;
787       }
788
789       case Property::UNSIGNED_INTEGER:
790       {
791         const AnimatableProperty<unsigned int>* property = dynamic_cast< const AnimatableProperty<unsigned int>* >( entry->GetSceneGraphProperty() );
792         DALI_ASSERT_DEBUG( NULL != property );
793
794         value = (*property)[ bufferIndex ];
795         break;
796       }
797
798       case Property::FLOAT:
799       {
800         const AnimatableProperty<float>* property = dynamic_cast< const AnimatableProperty<float>* >( entry->GetSceneGraphProperty() );
801         DALI_ASSERT_DEBUG( NULL != property );
802
803         value = (*property)[ bufferIndex ];
804         break;
805       }
806
807       case Property::VECTOR2:
808       {
809         const AnimatableProperty<Vector2>* property = dynamic_cast< const AnimatableProperty<Vector2>* >( entry->GetSceneGraphProperty() );
810         DALI_ASSERT_DEBUG( NULL != property );
811
812         if(entry->componentIndex == 0)
813         {
814           value = (*property)[ bufferIndex ].x;
815         }
816         else if(entry->componentIndex == 1)
817         {
818           value = (*property)[ bufferIndex ].y;
819         }
820         else
821         {
822           value = (*property)[ bufferIndex ];
823         }
824         break;
825       }
826
827       case Property::VECTOR3:
828       {
829         const AnimatableProperty<Vector3>* property = dynamic_cast< const AnimatableProperty<Vector3>* >( entry->GetSceneGraphProperty() );
830         DALI_ASSERT_DEBUG( NULL != property );
831
832         if(entry->componentIndex == 0)
833         {
834           value = (*property)[ bufferIndex ].x;
835         }
836         else if(entry->componentIndex == 1)
837         {
838           value = (*property)[ bufferIndex ].y;
839         }
840         else if(entry->componentIndex == 2)
841         {
842           value = (*property)[ bufferIndex ].z;
843         }
844         else
845         {
846           value = (*property)[ bufferIndex ];
847         }
848         break;
849       }
850
851       case Property::VECTOR4:
852       {
853         const AnimatableProperty<Vector4>* property = dynamic_cast< const AnimatableProperty<Vector4>* >( entry->GetSceneGraphProperty() );
854         DALI_ASSERT_DEBUG( NULL != property );
855
856         if(entry->componentIndex == 0)
857         {
858           value = (*property)[ bufferIndex ].x;
859         }
860         else if(entry->componentIndex == 1)
861         {
862           value = (*property)[ bufferIndex ].y;
863         }
864         else if(entry->componentIndex == 2)
865         {
866           value = (*property)[ bufferIndex ].z;
867         }
868         else if(entry->componentIndex == 3)
869         {
870           value = (*property)[ bufferIndex ].w;
871         }
872         else
873         {
874           value = (*property)[ bufferIndex ];
875         }
876         break;
877       }
878
879       case Property::MATRIX:
880       {
881         const AnimatableProperty<Matrix>* property = dynamic_cast< const AnimatableProperty<Matrix>* >( entry->GetSceneGraphProperty() );
882         DALI_ASSERT_DEBUG( NULL != property );
883
884         value = (*property)[ bufferIndex ];
885         break;
886       }
887
888       case Property::MATRIX3:
889       {
890         const AnimatableProperty<Matrix3>* property = dynamic_cast< const AnimatableProperty<Matrix3>* >( entry->GetSceneGraphProperty() );
891         DALI_ASSERT_DEBUG( NULL != property );
892
893         value = (*property)[ bufferIndex ];
894         break;
895       }
896
897       case Property::ROTATION:
898       {
899         const AnimatableProperty<Quaternion>* property = dynamic_cast< const AnimatableProperty<Quaternion>* >( entry->GetSceneGraphProperty() );
900         DALI_ASSERT_DEBUG( NULL != property );
901
902         value = (*property)[ bufferIndex ];
903         break;
904       }
905
906       default:
907       {
908         DALI_ASSERT_ALWAYS( false && "PropertyType enumeration is out of bounds" );
909         break;
910       }
911     } // switch(type)
912   } // if animatable
913
914   return value;
915 }
916
917 void Object::SetSceneGraphProperty( Property::Index index, const PropertyMetadata& entry, const Property::Value& value )
918 {
919   switch ( entry.type )
920   {
921     case Property::BOOLEAN:
922     {
923       const AnimatableProperty<bool>* property = dynamic_cast< const AnimatableProperty<bool>* >( entry.GetSceneGraphProperty() );
924       DALI_ASSERT_DEBUG( NULL != property );
925
926       // property is being used in a separate thread; queue a message to set the property
927       BakeMessage<bool>( GetEventThreadServices(), *property, value.Get<bool>() );
928       break;
929     }
930
931     case Property::INTEGER:
932     {
933       const AnimatableProperty<int>* property = dynamic_cast< const AnimatableProperty<int>* >( entry.GetSceneGraphProperty() );
934       DALI_ASSERT_DEBUG( NULL != property );
935
936       // property is being used in a separate thread; queue a message to set the property
937       BakeMessage<int>( GetEventThreadServices(), *property, value.Get<int>() );
938       break;
939     }
940
941     case Property::UNSIGNED_INTEGER:
942     {
943       const AnimatableProperty<unsigned int>* property = dynamic_cast< const AnimatableProperty<unsigned int>* >( entry.GetSceneGraphProperty() );
944       DALI_ASSERT_DEBUG( NULL != property );
945
946       // property is being used in a separate thread; queue a message to set the property
947       BakeMessage<unsigned int>( GetEventThreadServices(), *property, value.Get<unsigned int>() );
948       break;
949     }
950
951     case Property::FLOAT:
952     {
953       const AnimatableProperty<float>* property = dynamic_cast< const AnimatableProperty<float>* >( entry.GetSceneGraphProperty() );
954       DALI_ASSERT_DEBUG( NULL != property );
955
956       // property is being used in a separate thread; queue a message to set the property
957       BakeMessage<float>( GetEventThreadServices(), *property, value.Get<float>() );
958       break;
959     }
960
961     case Property::VECTOR2:
962     {
963       const AnimatableProperty<Vector2>* property = dynamic_cast< const AnimatableProperty<Vector2>* >( entry.GetSceneGraphProperty() );
964       DALI_ASSERT_DEBUG( NULL != property );
965
966       // property is being used in a separate thread; queue a message to set the property
967       if(entry.componentIndex == 0)
968       {
969         SetXComponentMessage<Vector2>( GetEventThreadServices(), *property, value.Get<float>() );
970       }
971       else if(entry.componentIndex == 1)
972       {
973         SetYComponentMessage<Vector2>( GetEventThreadServices(), *property, value.Get<float>() );
974       }
975       else
976       {
977         BakeMessage<Vector2>( GetEventThreadServices(), *property, value.Get<Vector2>() );
978       }
979       break;
980     }
981
982     case Property::VECTOR3:
983     {
984       const AnimatableProperty<Vector3>* property = dynamic_cast< const AnimatableProperty<Vector3>* >( entry.GetSceneGraphProperty() );
985       DALI_ASSERT_DEBUG( NULL != property );
986
987       // property is being used in a separate thread; queue a message to set the property
988       if(entry.componentIndex == 0)
989       {
990         SetXComponentMessage<Vector3>( GetEventThreadServices(), *property, value.Get<float>() );
991       }
992       else if(entry.componentIndex == 1)
993       {
994         SetYComponentMessage<Vector3>( GetEventThreadServices(), *property, value.Get<float>() );
995       }
996       else if(entry.componentIndex == 2)
997       {
998         SetZComponentMessage<Vector3>( GetEventThreadServices(), *property, value.Get<float>() );
999       }
1000       else
1001       {
1002         BakeMessage<Vector3>( GetEventThreadServices(), *property, value.Get<Vector3>() );
1003       }
1004
1005       break;
1006     }
1007
1008     case Property::VECTOR4:
1009     {
1010       const AnimatableProperty<Vector4>* property = dynamic_cast< const AnimatableProperty<Vector4>* >( entry.GetSceneGraphProperty() );
1011       DALI_ASSERT_DEBUG( NULL != property );
1012
1013       // property is being used in a separate thread; queue a message to set the property
1014       if(entry.componentIndex == 0)
1015       {
1016         SetXComponentMessage<Vector4>( GetEventThreadServices(), *property, value.Get<float>() );
1017       }
1018       else if(entry.componentIndex == 1)
1019       {
1020         SetYComponentMessage<Vector4>( GetEventThreadServices(), *property, value.Get<float>() );
1021       }
1022       else if(entry.componentIndex == 2)
1023       {
1024         SetZComponentMessage<Vector4>( GetEventThreadServices(), *property, value.Get<float>() );
1025       }
1026       else if(entry.componentIndex == 3)
1027       {
1028         SetWComponentMessage<Vector4>( GetEventThreadServices(), *property, value.Get<float>() );
1029       }
1030       else
1031       {
1032         BakeMessage<Vector4>( GetEventThreadServices(), *property, value.Get<Vector4>() );
1033       }
1034       break;
1035     }
1036
1037     case Property::ROTATION:
1038     {
1039       const AnimatableProperty<Quaternion>* property = dynamic_cast< const AnimatableProperty<Quaternion>* >( entry.GetSceneGraphProperty() );
1040       DALI_ASSERT_DEBUG( NULL != property );
1041
1042       // property is being used in a separate thread; queue a message to set the property
1043       BakeMessage<Quaternion>( GetEventThreadServices(), *property, value.Get<Quaternion>() );
1044       break;
1045     }
1046
1047     case Property::MATRIX:
1048     {
1049       const AnimatableProperty<Matrix>* property = dynamic_cast< const AnimatableProperty<Matrix>* >( entry.GetSceneGraphProperty() );
1050       DALI_ASSERT_DEBUG( NULL != property );
1051
1052       // property is being used in a separate thread; queue a message to set the property
1053       BakeMessage<Matrix>( GetEventThreadServices(), *property, value.Get<Matrix>() );
1054       break;
1055     }
1056
1057     case Property::MATRIX3:
1058     {
1059       const AnimatableProperty<Matrix3>* property = dynamic_cast< const AnimatableProperty<Matrix3>* >( entry.GetSceneGraphProperty() );
1060       DALI_ASSERT_DEBUG( NULL != property );
1061
1062       // property is being used in a separate thread; queue a message to set the property
1063       BakeMessage<Matrix3>( GetEventThreadServices(), *property, value.Get<Matrix3>() );
1064       break;
1065     }
1066
1067     default:
1068     {
1069       // non-animatable scene graph property, do nothing
1070     }
1071   }
1072 }
1073
1074 const TypeInfo* Object::GetTypeInfo() const
1075 {
1076   if ( !mTypeInfo )
1077   {
1078     // This uses a dynamic_cast so can be quite expensive so we only really want to do it once
1079     // especially as the type-info does not change during the life-time of an application
1080
1081     Dali::TypeInfo typeInfoHandle = TypeRegistry::Get()->GetTypeInfo( this );
1082     if ( typeInfoHandle )
1083     {
1084       mTypeInfo = &GetImplementation( typeInfoHandle );
1085     }
1086   }
1087
1088   return mTypeInfo;
1089 }
1090
1091 void Object::ApplyConstraint( ConstraintBase& constraint )
1092 {
1093   if( !mConstraints )
1094   {
1095     mConstraints = new ConstraintContainer;
1096   }
1097   mConstraints->push_back( Dali::Constraint( &constraint ) );
1098 }
1099
1100 void Object::RemoveConstraint( ConstraintBase& constraint )
1101 {
1102   // NULL if the Constraint sources are destroyed before Constraint::Apply()
1103   if( mConstraints )
1104   {
1105     ConstraintIter it( std::find( mConstraints->begin(), mConstraints->end(), Dali::Constraint( &constraint ) ) );
1106     if( it != mConstraints->end() )
1107     {
1108       mConstraints->erase( it );
1109     }
1110   }
1111 }
1112
1113 void Object::RemoveConstraints()
1114 {
1115   // guard against constraint sending messages during core destruction
1116   if( mConstraints && Stage::IsInstalled() )
1117   {
1118     // If we have nothing in the scene-graph, just clear constraint containers
1119     const SceneGraph::PropertyOwner* propertyOwner = GetSceneObject();
1120     if ( NULL != propertyOwner )
1121     {
1122       const ConstraintConstIter endIter = mConstraints->end();
1123       for ( ConstraintIter iter = mConstraints->begin(); endIter != iter; ++iter )
1124       {
1125         GetImplementation( *iter ).RemoveInternal();
1126       }
1127     }
1128
1129     delete mConstraints;
1130     mConstraints = NULL;
1131   }
1132 }
1133
1134 void Object::RemoveConstraints( unsigned int tag )
1135 {
1136   // guard against constraint sending messages during core destruction
1137   if( mConstraints && Stage::IsInstalled() )
1138   {
1139     ConstraintIter iter( mConstraints->begin() );
1140     while(iter != mConstraints->end() )
1141     {
1142       ConstraintBase& constraint = GetImplementation( *iter );
1143       if( constraint.GetTag() == tag )
1144       {
1145         GetImplementation( *iter ).RemoveInternal();
1146         iter = mConstraints->erase( iter );
1147       }
1148       else
1149       {
1150         ++iter;
1151       }
1152     }
1153
1154     if ( mConstraints->empty() )
1155     {
1156       delete mConstraints;
1157       mConstraints = NULL;
1158     }
1159   }
1160 }
1161
1162 void Object::SetTypeInfo( const TypeInfo* typeInfo )
1163 {
1164   mTypeInfo = typeInfo;
1165 }
1166
1167 Object::~Object()
1168 {
1169   // Notification for observers
1170   for( ConstObserverIter iter = mObservers.Begin(), endIter =  mObservers.End(); iter != endIter; ++iter)
1171   {
1172     (*iter)->ObjectDestroyed(*this);
1173   }
1174
1175   delete mConstraints;
1176   delete mPropertyNotifications;
1177 }
1178
1179 CustomPropertyMetadata* Object::FindCustomProperty( Property::Index index ) const
1180 {
1181   CustomPropertyMetadata* property( NULL );
1182   int arrayIndex = index - PROPERTY_CUSTOM_START_INDEX;
1183   if( arrayIndex >= 0 )
1184   {
1185     if( arrayIndex < (int)mCustomProperties.Count() ) // we can only access the first 2 billion custom properties
1186     {
1187       property = static_cast<CustomPropertyMetadata*>(mCustomProperties[ arrayIndex ]);
1188     }
1189   }
1190   return property;
1191 }
1192
1193 AnimatablePropertyMetadata* Object::FindAnimatableProperty( Property::Index index ) const
1194 {
1195   for ( int arrayIndex = 0; arrayIndex < (int)mAnimatableProperties.Count(); arrayIndex++ )
1196   {
1197     AnimatablePropertyMetadata* property = static_cast<AnimatablePropertyMetadata*>( mAnimatableProperties[ arrayIndex ] );
1198     if( property->index == index )
1199     {
1200       return property;
1201     }
1202   }
1203   return NULL;
1204 }
1205
1206 AnimatablePropertyMetadata* Object::RegisterAnimatableProperty(Property::Index index) const
1207 {
1208   DALI_ASSERT_ALWAYS( (( index >= ANIMATABLE_PROPERTY_REGISTRATION_START_INDEX ) && ( index <= ANIMATABLE_PROPERTY_REGISTRATION_MAX_INDEX ))
1209                       && "Property index is out of bounds" );
1210
1211   // check whether the animatable property is registered already, if not then register one.
1212   AnimatablePropertyMetadata* animatableProperty = FindAnimatableProperty( index );
1213   if(!animatableProperty)
1214   {
1215     const TypeInfo* typeInfo( GetTypeInfo() );
1216     if (typeInfo)
1217     {
1218       Property::Index basePropertyIndex = typeInfo->GetBasePropertyIndex(index);
1219       if(basePropertyIndex == Property::INVALID_INDEX)
1220       {
1221         // If the property is not a component of a base property, register the whole property itself.
1222         index = RegisterSceneGraphProperty(typeInfo->GetPropertyName(index), index, Property::Value(typeInfo->GetPropertyType(index)));
1223       }
1224       else
1225       {
1226         // Since the property is a component of a base property, check whether the base property is regsitered.
1227         animatableProperty = FindAnimatableProperty( basePropertyIndex );
1228         if(!animatableProperty)
1229         {
1230           // If the base property is not registered yet, register the base property first.
1231           if(Property::INVALID_INDEX != RegisterSceneGraphProperty(typeInfo->GetPropertyName(basePropertyIndex), basePropertyIndex, Property::Value(typeInfo->GetPropertyType(basePropertyIndex))))
1232           {
1233             animatableProperty = static_cast<AnimatablePropertyMetadata*>(mAnimatableProperties[mAnimatableProperties.Size()-1]);
1234           }
1235         }
1236
1237         if(animatableProperty)
1238         {
1239           // Create the metadata for the property component.
1240           mAnimatableProperties.PushBack( new AnimatablePropertyMetadata( index, typeInfo->GetComponentIndex(index), animatableProperty->type, animatableProperty->GetSceneGraphProperty() ) );
1241         }
1242       }
1243
1244       // The metadata has just been added and therefore should be in the end of the vector.
1245       animatableProperty = static_cast<AnimatablePropertyMetadata*>(mAnimatableProperties[mAnimatableProperties.Size()-1]);
1246     }
1247   }
1248
1249   return animatableProperty;
1250 }
1251
1252 } // namespace Internal
1253
1254 } // namespace Dali