[SRUK] Initial copy from Tizen 2.2 version
[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                                                   const Dali::PropertyCondition& condition)
44 {
45   ThreadLocalStorage& tls = ThreadLocalStorage::Get();
46
47   UpdateManager& updateManager = tls.GetUpdateManager();
48
49   PropertyNotificationManager& propertyNotificationManager = Stage::GetCurrent()->GetPropertyNotificationManager();
50
51   PropertyNotificationPtr propertyNotification = new PropertyNotification(updateManager,
52                                                                           propertyNotificationManager,
53                                                                           target,
54                                                                           condition);
55
56   return propertyNotification;
57 }
58
59 PropertyNotification::PropertyNotification(UpdateManager& updateManager,
60                                            PropertyNotificationManager& propertyNotificationManager,
61                                            Property& target,
62                                            const Dali::PropertyCondition& condition)
63 : mUpdateManager(updateManager),
64   mPropertyNotification(NULL),
65   mPropertyNotificationManager(propertyNotificationManager),
66   mProxyPropertyIndex(target.propertyIndex),
67   mCondition(condition),
68   mNotifyMode(Dali::PropertyNotification::NotifyOnTrue),
69   mNotifyResult(false)
70 {
71   // Set condition arguments (as simple vector of floats)
72   PropertyCondition::ArgumentContainer arguments = GetImplementation(condition).arguments;
73   PropertyCondition::ArgumentConstIter iter = arguments.begin();
74
75   while( iter != arguments.end() )
76   {
77     const Property::Value& value = *iter;
78     float floatValue;
79     value.Get(floatValue);
80
81     mRawConditionArgs.PushBack( floatValue );
82     ++iter;
83   }
84
85   // Observe target proxy and create/destroy notification scene object accordingly.
86   mProxy = dynamic_cast<ProxyObject*>( &GetImplementation(target.object) );
87
88   // Check if target scene-object already present, and if so create our notification
89   // scene-object
90   if ( mProxy )
91   {
92     const SceneGraph::PropertyOwner* object = mProxy->GetSceneObject();
93
94     if (object)
95     {
96       CreateSceneObject();
97     }
98   }
99
100   // Connect to the property notification manager
101   mPropertyNotificationManager.PropertyNotificationCreated( *this );
102 }
103
104 PropertyNotification::~PropertyNotification()
105 {
106   Disable();
107
108   // Guard to disallow use of PropertyNotificationManager after Core has been destroyed
109   if ( Stage::IsInstalled() )
110   {
111     // Disconnect from the property notification manager
112     mPropertyNotificationManager.PropertyNotificationDestroyed( *this );
113   }
114 }
115
116 Dali::PropertyNotifySignalV2& PropertyNotification::NotifySignal()
117 {
118   return mNotifySignal;
119 }
120
121 void PropertyNotification::EmitSignalNotify()
122 {
123   Dali::PropertyNotification source(this);
124
125   mNotifySignal.Emit( source );
126 }
127
128 void PropertyNotification::Enable()
129 {
130   CreateSceneObject();
131 }
132
133 void PropertyNotification::Disable()
134 {
135   // Guard to allow handle destruction after Core has been destroyed
136   if ( Stage::IsInstalled() )
137   {
138     // Stop scene-graph from monitoring the target's properties.
139     DestroySceneObject();
140   }
141 }
142
143 void PropertyNotification::SetNotifyResult( bool result )
144 {
145   mNotifyResult = result;
146 }
147
148 const Dali::PropertyCondition& PropertyNotification::GetCondition() const
149 {
150   return mCondition;
151 }
152
153 Dali::Handle PropertyNotification::GetTarget() const
154 {
155   Dali::Handle handle(mProxy);
156
157   return handle;
158 }
159
160 Property::Index PropertyNotification::GetTargetProperty() const
161 {
162   return mProxyPropertyIndex;
163 }
164
165 void PropertyNotification::SetNotifyMode( NotifyMode mode )
166 {
167   mNotifyMode = mode;
168   PropertyNotificationSetNotifyModeMessage( mUpdateManager, mPropertyNotification, mode );
169 }
170
171 PropertyNotification::NotifyMode PropertyNotification::GetNotifyMode()
172 {
173   return mNotifyMode;
174 }
175
176 bool PropertyNotification::GetNotifyResult() const
177 {
178   return mNotifyResult;
179 }
180
181 bool PropertyNotification::CompareSceneObject( const SceneGraph::PropertyNotification* sceneObject )
182 {
183   return sceneObject && sceneObject == mPropertyNotification;
184 }
185
186 void PropertyNotification::CreateSceneObject()
187 {
188   DALI_ASSERT_DEBUG( mPropertyNotification == NULL );
189
190   Property::Type propertyType = mProxy->GetPropertyType(mProxyPropertyIndex);
191
192   // Create a new PropertyNotification, temporarily owned
193   SceneGraph::PropertyNotification* propertyNotification = SceneGraph::PropertyNotification::New( *mProxy,
194                                                                                                   mProxyPropertyIndex,
195                                                                                                   propertyType,
196                                                                                                   GetImplementation(mCondition).type,
197                                                                                                   mRawConditionArgs,
198                                                                                                   mNotifyMode );
199   // Keep a const pointer to the PropertyNotification.
200   mPropertyNotification = propertyNotification;
201
202   // Transfer scene object ownership to the update manager through a message
203   AddPropertyNotificationMessage( mUpdateManager, propertyNotification );
204 }
205
206 void PropertyNotification::DestroySceneObject()
207 {
208   if ( mPropertyNotification != NULL )
209   {
210     // Remove PropertyNotification using a message to the update manager
211     RemovePropertyNotificationMessage( mUpdateManager, *mPropertyNotification );
212     mPropertyNotification = NULL;
213   }
214 }
215
216 } // namespace Internal
217
218 } // namespace Dali