(PropertyNotification) Added conditions to allow checking of a specific vector components
[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   mPropertyType = mProxy->GetPropertyType(mProxyPropertyIndex);
93
94   int internalComponentIndex = mProxy->GetPropertyComponentIndex(mProxyPropertyIndex);
95   if( internalComponentIndex != INVALID_PROPERTY_COMPONENT_INDEX )
96   {
97     // override the one passed in
98     mComponentIndex = internalComponentIndex;
99   }
100   if(mComponentIndex != INVALID_PROPERTY_COMPONENT_INDEX)
101   {
102     Property::Type type = mProxy->GetPropertyType(mProxyPropertyIndex);
103     if( type == Property::VECTOR2
104         || type == Property::VECTOR3
105         || type == Property::VECTOR4 )
106     {
107       mPropertyType = Property::FLOAT;
108     }
109   }
110
111   // Check if target scene-object already present, and if so create our notification
112   // scene-object
113   if ( mProxy )
114   {
115     const SceneGraph::PropertyOwner* object = mProxy->GetSceneObject();
116
117     if (object)
118     {
119       CreateSceneObject();
120     }
121   }
122
123   // Connect to the property notification manager
124   mPropertyNotificationManager.PropertyNotificationCreated( *this );
125 }
126
127 PropertyNotification::~PropertyNotification()
128 {
129   Disable();
130
131   // Guard to disallow use of PropertyNotificationManager after Core has been destroyed
132   if ( Stage::IsInstalled() )
133   {
134     // Disconnect from the property notification manager
135     mPropertyNotificationManager.PropertyNotificationDestroyed( *this );
136   }
137 }
138
139 Dali::PropertyNotifySignalV2& PropertyNotification::NotifySignal()
140 {
141   return mNotifySignal;
142 }
143
144 void PropertyNotification::EmitSignalNotify()
145 {
146   Dali::PropertyNotification source(this);
147
148   mNotifySignal.Emit( source );
149 }
150
151 void PropertyNotification::Enable()
152 {
153   CreateSceneObject();
154 }
155
156 void PropertyNotification::Disable()
157 {
158   // Guard to allow handle destruction after Core has been destroyed
159   if ( Stage::IsInstalled() )
160   {
161     // Stop scene-graph from monitoring the target's properties.
162     DestroySceneObject();
163   }
164 }
165
166 void PropertyNotification::SetNotifyResult( bool result )
167 {
168   mNotifyResult = result;
169 }
170
171 const Dali::PropertyCondition& PropertyNotification::GetCondition() const
172 {
173   return mCondition;
174 }
175
176 Dali::Handle PropertyNotification::GetTarget() const
177 {
178   Dali::Handle handle(mProxy);
179
180   return handle;
181 }
182
183 Property::Index PropertyNotification::GetTargetProperty() const
184 {
185   return mProxyPropertyIndex;
186 }
187
188 void PropertyNotification::SetNotifyMode( NotifyMode mode )
189 {
190   mNotifyMode = mode;
191   PropertyNotificationSetNotifyModeMessage( mUpdateManager, mPropertyNotification, mode );
192 }
193
194 PropertyNotification::NotifyMode PropertyNotification::GetNotifyMode()
195 {
196   return mNotifyMode;
197 }
198
199 bool PropertyNotification::GetNotifyResult() const
200 {
201   return mNotifyResult;
202 }
203
204 bool PropertyNotification::CompareSceneObject( const SceneGraph::PropertyNotification* sceneObject )
205 {
206   return sceneObject && sceneObject == mPropertyNotification;
207 }
208
209 void PropertyNotification::CreateSceneObject()
210 {
211   DALI_ASSERT_DEBUG( mPropertyNotification == NULL );
212
213   // Create a new PropertyNotification, temporarily owned
214   SceneGraph::PropertyNotification* propertyNotification = SceneGraph::PropertyNotification::New( *mProxy,
215                                                                                                   mProxyPropertyIndex,
216                                                                                                   mPropertyType,
217                                                                                                   mComponentIndex,
218                                                                                                   GetImplementation(mCondition).type,
219                                                                                                   mRawConditionArgs,
220                                                                                                   mNotifyMode );
221   // Keep a const pointer to the PropertyNotification.
222   mPropertyNotification = propertyNotification;
223
224   // Transfer scene object ownership to the update manager through a message
225   AddPropertyNotificationMessage( mUpdateManager, propertyNotification );
226 }
227
228 void PropertyNotification::DestroySceneObject()
229 {
230   if ( mPropertyNotification != NULL )
231   {
232     // Remove PropertyNotification using a message to the update manager
233     RemovePropertyNotificationMessage( mUpdateManager, *mPropertyNotification );
234     mPropertyNotification = NULL;
235   }
236 }
237
238 } // namespace Internal
239
240 } // namespace Dali