a67e390ab86cdcfd15050f67b29444577c25a620
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-owner.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include <dali/internal/update/common/property-owner.h>
19
20 // EXTERNAL INCLUDES
21 #include <algorithm>
22
23 // INTERNAL INCLUDES
24 #include <dali/public-api/common/dali-common.h>
25 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace SceneGraph
34 {
35
36 PropertyOwner::~PropertyOwner()
37 {
38   DisconnectFromSceneGraph();
39 }
40
41 void PropertyOwner::AddObserver(Observer& observer)
42 {
43   //Check for duplicates in debug builds
44   DALI_ASSERT_DEBUG( mObservers.End() == std::find( mObservers.Begin(), mObservers.End(), &observer ) );
45
46   mObservers.PushBack( &observer );
47 }
48
49 void PropertyOwner::RemoveObserver(Observer& observer)
50 {
51   // Find the observer...
52   const ConstObserverIter endIter =  mObservers.End();
53   for( ObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
54   {
55     if( (*iter) == &observer)
56     {
57       // erase it
58       mObservers.Erase(iter);
59       break;
60     }
61   }
62 }
63
64 bool PropertyOwner::IsObserved()
65 {
66   return mObservers.Count() != 0u;
67 }
68
69 void PropertyOwner::DisconnectFromSceneGraph()
70 {
71   // Notification for observers
72   const ConstObserverIter endIter = mObservers.End();
73   for( ConstObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
74   {
75     (*iter)->PropertyOwnerDestroyed(*this);
76   }
77
78   mObservers.Clear();
79
80   // Remove all constraints when disconnected from scene-graph
81   mConstraints.Clear();
82 }
83
84 void PropertyOwner::InstallCustomProperty(PropertyBase* property)
85 {
86   DALI_ASSERT_DEBUG( NULL != property );
87
88   mCustomProperties.PushBack( property );
89 }
90
91 void PropertyOwner::ResetToBaseValues( BufferIndex updateBufferIndex )
92 {
93   // Reset custom properties
94   const OwnedPropertyIter endIter = mCustomProperties.End();
95   for ( OwnedPropertyIter iter = mCustomProperties.Begin(); endIter != iter; ++iter )
96   {
97     (*iter)->ResetToBaseValue( updateBufferIndex );
98   }
99
100   // Reset constraint weights
101   const ConstraintIter constraintEndIter = mConstraints.End();
102   for( ConstraintIter iter = mConstraints.Begin(); constraintEndIter != iter; ++iter )
103   {
104     (*iter)->mWeight.ResetToBaseValue( updateBufferIndex );
105   }
106
107   // Notification for derived classes, to reset default properties
108   ResetDefaultProperties( updateBufferIndex );
109 }
110
111 ConstraintOwnerContainer& PropertyOwner::GetConstraints()
112 {
113   return mConstraints;
114 }
115
116 void PropertyOwner::ApplyConstraint( ConstraintBase* constraint )
117 {
118   mConstraints.PushBack( constraint );
119
120   constraint->OnConnect();
121 }
122
123 void PropertyOwner::RemoveConstraint( ConstraintBase* constraint )
124 {
125   // Reset constraint weights
126   const ConstraintIter constraintEndIter = mConstraints.End();
127   for( ConstraintIter iter = mConstraints.Begin(); constraintEndIter != iter; ++iter )
128   {
129     if ( *iter == constraint )
130     {
131       mConstraints.Erase( iter );
132       return; // We're finished
133     }
134   }
135
136   // Should not come here
137   DALI_ASSERT_DEBUG( false && "Constraint missing in RemoveConstraint" );
138 }
139
140 PropertyOwner::PropertyOwner()
141 {
142 }
143
144 } // namespace SceneGraph
145
146 } // namespace Internal
147
148 } // namespace Dali