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