Merge "[dali-core] add all X11 colors to Color namespace" into devel/master
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-owner.cpp
1 /*
2  * Copyright (c) 2020 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/update/common/property-owner.h>
20
21 // EXTERNAL INCLUDES
22 #include <algorithm>
23
24 // INTERNAL INCLUDES
25 #include <dali/public-api/common/dali-common.h>
26 #include <dali/internal/update/animation/scene-graph-constraint-base.h>
27 #include <dali/internal/common/const-string.h>
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace SceneGraph
36 {
37
38 PropertyOwner* PropertyOwner::New()
39 {
40   return new PropertyOwner();
41 }
42
43 PropertyOwner::~PropertyOwner()
44 {
45   Destroy();
46 }
47
48 void PropertyOwner::AddObserver(Observer& observer)
49 {
50   //Check for duplicates in debug builds
51   DALI_ASSERT_DEBUG( mObservers.End() == std::find( mObservers.Begin(), mObservers.End(), &observer ) );
52
53   mObservers.PushBack( &observer );
54 }
55
56 void PropertyOwner::RemoveObserver(Observer& observer)
57 {
58   // Find the observer...
59   const ConstObserverIter endIter =  mObservers.End();
60   for( ObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
61   {
62     if( (*iter) == &observer)
63     {
64       // erase it
65       mObservers.Erase(iter);
66       break;
67     }
68   }
69 }
70
71 bool PropertyOwner::IsObserved()
72 {
73   return mObservers.Count() != 0u;
74 }
75
76 void PropertyOwner::Destroy()
77 {
78   // Notification for observers
79   const ConstObserverIter endIter = mObservers.End();
80   for( ConstObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
81   {
82     (*iter)->PropertyOwnerDestroyed(*this);
83   }
84
85   mObservers.Clear();
86
87   // Remove all constraints when disconnected from scene-graph
88   mConstraints.Clear();
89 }
90
91 void PropertyOwner::ConnectToSceneGraph()
92 {
93   mIsConnectedToSceneGraph = true;
94
95   // Notification for observers
96   const ConstObserverIter endIter = mObservers.End();
97   for( ConstObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
98   {
99     (*iter)->PropertyOwnerConnected( *this );
100   }
101 }
102
103 void PropertyOwner::DisconnectFromSceneGraph( BufferIndex updateBufferIndex )
104 {
105   mIsConnectedToSceneGraph = false;
106
107   // Notification for observers
108   const ConstObserverIter endIter = mObservers.End();
109   for( ConstObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
110   {
111     (*iter)->PropertyOwnerDisconnected( updateBufferIndex, *this );
112   }
113
114   // Remove all constraints when disconnected from scene-graph
115   mConstraints.Clear();
116 }
117
118 void PropertyOwner::InstallCustomProperty( OwnerPointer<PropertyBase>& property )
119 {
120   mCustomProperties.PushBack( property.Release() );
121 }
122
123
124 ConstraintOwnerContainer& PropertyOwner::GetConstraints()
125 {
126   return mConstraints;
127 }
128
129 void PropertyOwner::ApplyConstraint( OwnerPointer<ConstraintBase>& constraint )
130 {
131   constraint->OnConnect();
132   mConstraints.PushBack( constraint.Release() );
133 }
134
135 void PropertyOwner::RemoveConstraint( ConstraintBase* constraint )
136 {
137   const ConstraintIter constraintEndIter = mConstraints.End();
138   for( ConstraintIter iter = mConstraints.Begin(); constraintEndIter != iter; ++iter )
139   {
140     if ( *iter == constraint )
141     {
142       mConstraints.Erase( iter );
143       return; // We're finished
144     }
145   }
146
147   //it may be that the constraint has already been removed e.g. from disconnection from scene graph, so nothing needs to be done
148 }
149
150 PropertyOwner::PropertyOwner()
151 : mUpdated(false),
152   mIsConnectedToSceneGraph(false)
153 {
154 }
155
156 void PropertyOwner::AddUniformMapping(const UniformPropertyMapping& map)
157 {
158   mUniformMaps.Add(map);
159 }
160
161 void PropertyOwner::RemoveUniformMapping( const ConstString& uniformName )
162 {
163   mUniformMaps.Remove( uniformName );
164 }
165
166 const UniformMap& PropertyOwner::GetUniformMap() const
167 {
168   return mUniformMaps;
169 }
170
171 void PropertyOwner::AddUniformMapObserver( UniformMap::Observer& observer )
172 {
173   mUniformMaps.AddObserver( observer );
174 }
175
176 void PropertyOwner::RemoveUniformMapObserver( UniformMap::Observer& observer )
177 {
178   mUniformMaps.RemoveObserver( observer );
179 }
180
181
182
183 } // namespace SceneGraph
184
185 } // namespace Internal
186
187 } // namespace Dali