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