Updated all code to new format
[platform/core/uifw/dali-core.git] / dali / internal / update / common / property-owner.cpp
1 /*
2  * Copyright (c) 2021 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
92   // Notification for observers
93   const ConstObserverIter endIter = mObservers.End();
94   for(ConstObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
95   {
96     (*iter)->PropertyOwnerConnected(*this);
97   }
98 }
99
100 void PropertyOwner::DisconnectFromSceneGraph(BufferIndex updateBufferIndex)
101 {
102   mIsConnectedToSceneGraph = false;
103
104   // Notification for observers
105   const ConstObserverIter endIter = mObservers.End();
106   for(ConstObserverIter iter = mObservers.Begin(); iter != endIter; ++iter)
107   {
108     (*iter)->PropertyOwnerDisconnected(updateBufferIndex, *this);
109   }
110
111   // Remove all constraints when disconnected from scene-graph
112   mConstraints.Clear();
113 }
114
115 void PropertyOwner::InstallCustomProperty(OwnerPointer<PropertyBase>& property)
116 {
117   mCustomProperties.PushBack(property.Release());
118 }
119
120 ConstraintOwnerContainer& PropertyOwner::GetConstraints()
121 {
122   return mConstraints;
123 }
124
125 void PropertyOwner::ApplyConstraint(OwnerPointer<ConstraintBase>& constraint)
126 {
127   constraint->OnConnect();
128   mConstraints.PushBack(constraint.Release());
129 }
130
131 void PropertyOwner::RemoveConstraint(ConstraintBase* constraint)
132 {
133   const ConstraintIter constraintEndIter = mConstraints.End();
134   for(ConstraintIter iter = mConstraints.Begin(); constraintEndIter != iter; ++iter)
135   {
136     if(*iter == constraint)
137     {
138       mConstraints.Erase(iter);
139       return; // We're finished
140     }
141   }
142
143   //it may be that the constraint has already been removed e.g. from disconnection from scene graph, so nothing needs to be done
144 }
145
146 PropertyOwner::PropertyOwner()
147 : mUpdated(false),
148   mIsConnectedToSceneGraph(false)
149 {
150 }
151
152 void PropertyOwner::AddUniformMapping(const UniformPropertyMapping& map)
153 {
154   mUniformMaps.Add(map);
155 }
156
157 void PropertyOwner::RemoveUniformMapping(const ConstString& uniformName)
158 {
159   mUniformMaps.Remove(uniformName);
160 }
161
162 const UniformMap& PropertyOwner::GetUniformMap() const
163 {
164   return mUniformMaps;
165 }
166
167 void PropertyOwner::AddUniformMapObserver(UniformMap::Observer& observer)
168 {
169   mUniformMaps.AddObserver(observer);
170 }
171
172 void PropertyOwner::RemoveUniformMapObserver(UniformMap::Observer& observer)
173 {
174   mUniformMaps.RemoveObserver(observer);
175 }
176
177 } // namespace SceneGraph
178
179 } // namespace Internal
180
181 } // namespace Dali