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