Updated all code to new format
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-notification-impl.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/event/common/property-notification-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/event/actors/actor-impl.h>
23 #include <dali/internal/event/common/object-impl.h>
24 #include <dali/internal/event/common/property-notification-manager.h>
25 #include <dali/internal/event/common/stage-impl.h>
26 #include <dali/internal/event/common/thread-local-storage.h>
27 #include <dali/internal/update/common/scene-graph-property-notification.h>
28 #include <dali/internal/update/manager/update-manager.h>
29 #include <dali/public-api/actors/actor.h>
30 #include <dali/public-api/common/dali-common.h>
31 #include <dali/public-api/math/radian.h>
32 #include <dali/public-api/math/vector2.h>
33
34 using Dali::Internal::SceneGraph::UpdateManager;
35
36 namespace Dali
37 {
38 namespace Internal
39 {
40 PropertyNotificationPtr PropertyNotification::New(Property&                      target,
41                                                   int                            componentIndex,
42                                                   const Dali::PropertyCondition& condition)
43 {
44   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
45
46   UpdateManager& updateManager = tls.GetUpdateManager();
47
48   PropertyNotificationManager& propertyNotificationManager = tls.GetPropertyNotificationManager();
49   PropertyNotificationPtr      propertyNotification        = new PropertyNotification(updateManager,
50                                                                           propertyNotificationManager,
51                                                                           target,
52                                                                           componentIndex,
53                                                                           condition);
54   return propertyNotification;
55 }
56
57 PropertyNotification::PropertyNotification(UpdateManager&                 updateManager,
58                                            PropertyNotificationManager&   propertyNotificationManager,
59                                            Property&                      target,
60                                            int                            componentIndex,
61                                            const Dali::PropertyCondition& condition)
62 : mUpdateManager(updateManager),
63   mPropertyNotification(nullptr),
64   mPropertyNotificationManager(propertyNotificationManager),
65   mObjectPropertyIndex(target.propertyIndex),
66   mPropertyType(Property::NONE),
67   mComponentIndex(componentIndex),
68   mCondition(condition),
69   mNotifyMode(Dali::PropertyNotification::NOTIFY_ON_TRUE),
70   mNotifyResult(false),
71   mCompare(false)
72 {
73   const Internal::PropertyCondition& conditionImpl = GetImplementation(condition);
74
75   Dali::Vector<float>::SizeType count = conditionImpl.arguments.Count();
76   for(Dali::Vector<float>::SizeType index = 0; index < count; ++index)
77   {
78     mRawConditionArgs.PushBack(conditionImpl.arguments[index]);
79   }
80
81   // Observe target object and create/destroy notification scene object accordingly.
82   mObject = dynamic_cast<Object*>(&GetImplementation(target.object));
83   if(mObject)
84   {
85     mPropertyType = mObject->GetPropertyType(mObjectPropertyIndex);
86
87     int internalComponentIndex = mObject->GetPropertyComponentIndex(mObjectPropertyIndex);
88     if(internalComponentIndex != Property::INVALID_COMPONENT_INDEX)
89     {
90       // override the one passed in
91       mComponentIndex = internalComponentIndex;
92     }
93     if(mComponentIndex != Property::INVALID_COMPONENT_INDEX)
94     {
95       Property::Type type = mObject->GetPropertyType(mObjectPropertyIndex);
96       if(type == Property::VECTOR2 || type == Property::VECTOR3 || type == Property::VECTOR4)
97       {
98         mPropertyType = Property::FLOAT;
99       }
100     }
101
102     // In Size Property case, swapping components occurs sometimes.
103     // To cover swapping components, previous and current components should be compared.
104     if(mObjectPropertyIndex == Dali::Actor::Property::SIZE && mObject->GetPropertyType(mObjectPropertyIndex) == Property::VECTOR3)
105     {
106       mCompare = true;
107       for(int i = 0; i < 3; ++i)
108       {
109         mRawConditionArgs.PushBack(0.0f);
110       }
111     }
112
113     // all objects always have scene object
114     CreateSceneObject();
115   }
116
117   // Connect to the property notification manager
118   mPropertyNotificationManager.PropertyNotificationCreated(*this);
119 }
120
121 PropertyNotification::~PropertyNotification()
122 {
123   Disable();
124
125   // Guard to disallow use of PropertyNotificationManager after Core has been destroyed
126   if(Stage::IsInstalled())
127   {
128     // Disconnect from the property notification manager
129     mPropertyNotificationManager.PropertyNotificationDestroyed(*this);
130   }
131 }
132
133 Dali::PropertyNotifySignalType& PropertyNotification::NotifySignal()
134 {
135   return mNotifySignal;
136 }
137
138 void PropertyNotification::EmitSignalNotify()
139 {
140   Dali::PropertyNotification source(this);
141
142   mNotifySignal.Emit(source);
143 }
144
145 void PropertyNotification::Enable()
146 {
147   CreateSceneObject();
148 }
149
150 void PropertyNotification::Disable()
151 {
152   // Guard to allow handle destruction after Core has been destroyed
153   if(Stage::IsInstalled())
154   {
155     // Stop scene-graph from monitoring the target's properties.
156     DestroySceneObject();
157   }
158 }
159
160 void PropertyNotification::SetNotifyResult(bool result)
161 {
162   mNotifyResult = result;
163 }
164
165 const Dali::PropertyCondition& PropertyNotification::GetCondition() const
166 {
167   return mCondition;
168 }
169
170 Dali::Handle PropertyNotification::GetTarget() const
171 {
172   Dali::Handle handle(mObject);
173
174   return handle;
175 }
176
177 Property::Index PropertyNotification::GetTargetProperty() const
178 {
179   return mObjectPropertyIndex;
180 }
181
182 void PropertyNotification::SetNotifyMode(NotifyMode mode)
183 {
184   mNotifyMode = mode;
185   if(mPropertyNotification)
186   {
187     PropertyNotificationSetNotifyModeMessage(mUpdateManager, mPropertyNotification, mode);
188   }
189 }
190
191 PropertyNotification::NotifyMode PropertyNotification::GetNotifyMode()
192 {
193   return mNotifyMode;
194 }
195
196 bool PropertyNotification::GetNotifyResult() const
197 {
198   return mNotifyResult;
199 }
200
201 bool PropertyNotification::CompareSceneObject(const SceneGraph::PropertyNotification* sceneObject)
202 {
203   return sceneObject && sceneObject == mPropertyNotification;
204 }
205
206 void PropertyNotification::CreateSceneObject()
207 {
208   // this method can be called from constructor and on stage connection
209   if(!mPropertyNotification)
210   {
211     // Create a new PropertyNotification, keep a const pointer to it
212     mPropertyNotification = SceneGraph::PropertyNotification::New(*mObject,
213                                                                   mObjectPropertyIndex,
214                                                                   mPropertyType,
215                                                                   mComponentIndex,
216                                                                   GetImplementation(mCondition).type,
217                                                                   mRawConditionArgs,
218                                                                   mNotifyMode,
219                                                                   mCompare);
220     OwnerPointer<SceneGraph::PropertyNotification> transferOwnership(const_cast<SceneGraph::PropertyNotification*>(mPropertyNotification));
221     AddPropertyNotificationMessage(mUpdateManager, transferOwnership);
222   }
223 }
224
225 void PropertyNotification::DestroySceneObject()
226 {
227   if(mPropertyNotification != nullptr)
228   {
229     // Remove PropertyNotification using a message to the update manager
230     RemovePropertyNotificationMessage(mUpdateManager, *mPropertyNotification);
231     mPropertyNotification = nullptr;
232   }
233 }
234
235 } // namespace Internal
236
237 } // namespace Dali