Revert "License conversion from Flora to Apache 2.0"
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-notification-impl.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/event/common/property-notification-impl.h>
19
20 // INTERNAL INCLUDES
21 #include <dali/public-api/common/dali-common.h>
22 #include <dali/public-api/math/vector2.h>
23 #include <dali/public-api/math/radian.h>
24 #include <dali/internal/event/actors/actor-impl.h>
25 #include <dali/internal/event/common/property-notification-manager.h>
26 #include <dali/internal/event/common/proxy-object.h>
27 #include <dali/internal/event/common/stage-impl.h>
28 #include <dali/internal/update/manager/update-manager.h>
29 #include <dali/internal/update/common/scene-graph-property-notification.h>
30 #include <dali/internal/event/common/thread-local-storage.h>
31
32 using namespace std;
33
34 using Dali::Internal::SceneGraph::UpdateManager;
35
36 namespace Dali
37 {
38
39 namespace Internal
40 {
41
42 PropertyNotificationPtr PropertyNotification::New(Property& target,
43                                                   int componentIndex,
44                                                   const Dali::PropertyCondition& condition)
45 {
46   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
47
48   UpdateManager& updateManager = tls.GetUpdateManager();
49
50   PropertyNotificationManager& propertyNotificationManager = Stage::GetCurrent()->GetPropertyNotificationManager();
51
52   PropertyNotificationPtr propertyNotification = new PropertyNotification(updateManager,
53                                                                           propertyNotificationManager,
54                                                                           target,
55                                                                           componentIndex,
56                                                                           condition);
57
58   return propertyNotification;
59 }
60
61 PropertyNotification::PropertyNotification(UpdateManager& updateManager,
62                                            PropertyNotificationManager& propertyNotificationManager,
63                                            Property& target,
64                                            int componentIndex,
65                                            const Dali::PropertyCondition& condition)
66 : mUpdateManager(updateManager),
67   mPropertyNotification(NULL),
68   mPropertyNotificationManager(propertyNotificationManager),
69   mProxyPropertyIndex(target.propertyIndex),
70   mPropertyType(Property::NONE),
71   mComponentIndex(componentIndex),
72   mCondition(condition),
73   mNotifyMode(Dali::PropertyNotification::NotifyOnTrue),
74   mNotifyResult(false)
75 {
76   // Set condition arguments (as simple vector of floats)
77   PropertyCondition::ArgumentContainer arguments = GetImplementation(condition).arguments;
78   PropertyCondition::ArgumentConstIter iter = arguments.begin();
79
80   while( iter != arguments.end() )
81   {
82     const Property::Value& value = *iter;
83     float floatValue;
84     value.Get(floatValue);
85
86     mRawConditionArgs.PushBack( floatValue );
87     ++iter;
88   }
89
90   // Observe target proxy and create/destroy notification scene object accordingly.
91   mProxy = dynamic_cast<ProxyObject*>( &GetImplementation(target.object) );
92   if ( mProxy )
93   {
94     mPropertyType = mProxy->GetPropertyType(mProxyPropertyIndex);
95
96     int internalComponentIndex = mProxy->GetPropertyComponentIndex(mProxyPropertyIndex);
97     if( internalComponentIndex != Property::INVALID_COMPONENT_INDEX )
98     {
99       // override the one passed in
100       mComponentIndex = internalComponentIndex;
101     }
102     if(mComponentIndex != Property::INVALID_COMPONENT_INDEX)
103     {
104       Property::Type type = mProxy->GetPropertyType(mProxyPropertyIndex);
105       if( type == Property::VECTOR2
106           || type == Property::VECTOR3
107           || type == Property::VECTOR4 )
108       {
109         mPropertyType = Property::FLOAT;
110       }
111     }
112
113     // Check if target scene-object already present, and if so create our notification
114     // scene-object
115     const SceneGraph::PropertyOwner* object = mProxy->GetSceneObject();
116     if (object)
117     {
118       CreateSceneObject();
119     }
120   }
121
122   // Connect to the property notification manager
123   mPropertyNotificationManager.PropertyNotificationCreated( *this );
124 }
125
126 PropertyNotification::~PropertyNotification()
127 {
128   Disable();
129
130   // Guard to disallow use of PropertyNotificationManager after Core has been destroyed
131   if ( Stage::IsInstalled() )
132   {
133     // Disconnect from the property notification manager
134     mPropertyNotificationManager.PropertyNotificationDestroyed( *this );
135   }
136 }
137
138 Dali::PropertyNotifySignalV2& PropertyNotification::NotifySignal()
139 {
140   return mNotifySignal;
141 }
142
143 void PropertyNotification::EmitSignalNotify()
144 {
145   Dali::PropertyNotification source(this);
146
147   mNotifySignal.Emit( source );
148 }
149
150 void PropertyNotification::Enable()
151 {
152   CreateSceneObject();
153 }
154
155 void PropertyNotification::Disable()
156 {
157   // Guard to allow handle destruction after Core has been destroyed
158   if ( Stage::IsInstalled() )
159   {
160     // Stop scene-graph from monitoring the target's properties.
161     DestroySceneObject();
162   }
163 }
164
165 void PropertyNotification::SetNotifyResult( bool result )
166 {
167   mNotifyResult = result;
168 }
169
170 const Dali::PropertyCondition& PropertyNotification::GetCondition() const
171 {
172   return mCondition;
173 }
174
175 Dali::Handle PropertyNotification::GetTarget() const
176 {
177   Dali::Handle handle(mProxy);
178
179   return handle;
180 }
181
182 Property::Index PropertyNotification::GetTargetProperty() const
183 {
184   return mProxyPropertyIndex;
185 }
186
187 void PropertyNotification::SetNotifyMode( NotifyMode mode )
188 {
189   mNotifyMode = mode;
190   if( mPropertyNotification )
191   {
192     PropertyNotificationSetNotifyModeMessage( mUpdateManager, mPropertyNotification, mode );
193   }
194 }
195
196 PropertyNotification::NotifyMode PropertyNotification::GetNotifyMode()
197 {
198   return mNotifyMode;
199 }
200
201 bool PropertyNotification::GetNotifyResult() const
202 {
203   return mNotifyResult;
204 }
205
206 bool PropertyNotification::CompareSceneObject( const SceneGraph::PropertyNotification* sceneObject )
207 {
208   return sceneObject && sceneObject == mPropertyNotification;
209 }
210
211 void PropertyNotification::CreateSceneObject()
212 {
213   DALI_ASSERT_DEBUG( mPropertyNotification == NULL );
214
215   // Create a new PropertyNotification, temporarily owned
216   SceneGraph::PropertyNotification* propertyNotification = SceneGraph::PropertyNotification::New( *mProxy,
217                                                                                                   mProxyPropertyIndex,
218                                                                                                   mPropertyType,
219                                                                                                   mComponentIndex,
220                                                                                                   GetImplementation(mCondition).type,
221                                                                                                   mRawConditionArgs,
222                                                                                                   mNotifyMode );
223   // Keep a const pointer to the PropertyNotification.
224   mPropertyNotification = propertyNotification;
225
226   // Transfer scene object ownership to the update manager through a message
227   AddPropertyNotificationMessage( mUpdateManager, propertyNotification );
228 }
229
230 void PropertyNotification::DestroySceneObject()
231 {
232   if ( mPropertyNotification != NULL )
233   {
234     // Remove PropertyNotification using a message to the update manager
235     RemovePropertyNotificationMessage( mUpdateManager, *mPropertyNotification );
236     mPropertyNotification = NULL;
237   }
238 }
239
240 } // namespace Internal
241
242 } // namespace Dali