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