Refactor SceneGraphProperty handling code in event side to make RegisterProperty...
[platform/core/uifw/dali-core.git] / dali / internal / event / common / property-notification-impl.cpp
1 /*
2  * Copyright (c) 2018 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/object-impl.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   StagePtr stage = Stage::GetCurrent();
50   if( stage )
51   {
52     PropertyNotificationManager& propertyNotificationManager = stage->GetPropertyNotificationManager();
53     PropertyNotificationPtr propertyNotification = new PropertyNotification(updateManager,
54                                                                             propertyNotificationManager,
55                                                                             target,
56                                                                             componentIndex,
57                                                                             condition);
58     return propertyNotification;
59   }
60   else
61   {
62     return NULL;
63   }
64 }
65
66 PropertyNotification::PropertyNotification( UpdateManager& updateManager,
67                                             PropertyNotificationManager& propertyNotificationManager,
68                                             Property& target,
69                                             int componentIndex,
70                                             const Dali::PropertyCondition& condition )
71 : mUpdateManager( updateManager ),
72   mPropertyNotification( NULL ),
73   mPropertyNotificationManager( propertyNotificationManager ),
74   mObjectPropertyIndex( target.propertyIndex ),
75   mPropertyType( Property::NONE ),
76   mComponentIndex( componentIndex ),
77   mCondition( condition ),
78   mNotifyMode( Dali::PropertyNotification::NotifyOnTrue ),
79   mNotifyResult( false )
80 {
81   const Internal::PropertyCondition& conditionImpl = GetImplementation( condition );
82
83   Dali::Vector<float>::SizeType count = conditionImpl.arguments.Count();
84   for( Dali::Vector<float>::SizeType index = 0; index < count; ++index )
85   {
86     mRawConditionArgs.PushBack( conditionImpl.arguments[ index ] );
87   }
88
89   // Observe target object and create/destroy notification scene object accordingly.
90   mObject = dynamic_cast<Object*>( &GetImplementation(target.object) );
91   if ( mObject )
92   {
93     mPropertyType = mObject->GetPropertyType(mObjectPropertyIndex);
94
95     int internalComponentIndex = mObject->GetPropertyComponentIndex(mObjectPropertyIndex);
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 = mObject->GetPropertyType(mObjectPropertyIndex);
104       if( type == Property::VECTOR2
105           || type == Property::VECTOR3
106           || type == Property::VECTOR4 )
107       {
108         mPropertyType = Property::FLOAT;
109       }
110     }
111
112     // all objects always have scene object
113     CreateSceneObject();
114   }
115
116   // Connect to the property notification manager
117   mPropertyNotificationManager.PropertyNotificationCreated( *this );
118 }
119
120 PropertyNotification::~PropertyNotification()
121 {
122   Disable();
123
124   // Guard to disallow use of PropertyNotificationManager after Core has been destroyed
125   if ( Stage::IsInstalled() )
126   {
127     // Disconnect from the property notification manager
128     mPropertyNotificationManager.PropertyNotificationDestroyed( *this );
129   }
130 }
131
132 Dali::PropertyNotifySignalType& PropertyNotification::NotifySignal()
133 {
134   return mNotifySignal;
135 }
136
137 void PropertyNotification::EmitSignalNotify()
138 {
139   Dali::PropertyNotification source(this);
140
141   mNotifySignal.Emit( source );
142 }
143
144 void PropertyNotification::Enable()
145 {
146   CreateSceneObject();
147 }
148
149 void PropertyNotification::Disable()
150 {
151   // Guard to allow handle destruction after Core has been destroyed
152   if ( Stage::IsInstalled() )
153   {
154     // Stop scene-graph from monitoring the target's properties.
155     DestroySceneObject();
156   }
157 }
158
159 void PropertyNotification::SetNotifyResult( bool result )
160 {
161   mNotifyResult = result;
162 }
163
164 const Dali::PropertyCondition& PropertyNotification::GetCondition() const
165 {
166   return mCondition;
167 }
168
169 Dali::Handle PropertyNotification::GetTarget() const
170 {
171   Dali::Handle handle(mObject);
172
173   return handle;
174 }
175
176 Property::Index PropertyNotification::GetTargetProperty() const
177 {
178   return mObjectPropertyIndex;
179 }
180
181 void PropertyNotification::SetNotifyMode( NotifyMode mode )
182 {
183   mNotifyMode = mode;
184   if( mPropertyNotification )
185   {
186     PropertyNotificationSetNotifyModeMessage( mUpdateManager, mPropertyNotification, mode );
187   }
188 }
189
190 PropertyNotification::NotifyMode PropertyNotification::GetNotifyMode()
191 {
192   return mNotifyMode;
193 }
194
195 bool PropertyNotification::GetNotifyResult() const
196 {
197   return mNotifyResult;
198 }
199
200 bool PropertyNotification::CompareSceneObject( const SceneGraph::PropertyNotification* sceneObject )
201 {
202   return sceneObject && sceneObject == mPropertyNotification;
203 }
204
205 void PropertyNotification::CreateSceneObject()
206 {
207   // this method can be called from constructor and on stage connection
208   if( !mPropertyNotification )
209   {
210     // Create a new PropertyNotification, keep a const pointer to it
211     mPropertyNotification = SceneGraph::PropertyNotification::New( *mObject,
212                                                                    mObjectPropertyIndex,
213                                                                    mPropertyType,
214                                                                    mComponentIndex,
215                                                                    GetImplementation( mCondition ).type,
216                                                                    mRawConditionArgs,
217                                                                    mNotifyMode );
218     OwnerPointer< SceneGraph::PropertyNotification > transferOwnership( const_cast<SceneGraph::PropertyNotification*>( mPropertyNotification ) );
219     AddPropertyNotificationMessage( mUpdateManager, transferOwnership );
220   }
221 }
222
223 void PropertyNotification::DestroySceneObject()
224 {
225   if ( mPropertyNotification != NULL )
226   {
227     // Remove PropertyNotification using a message to the update manager
228     RemovePropertyNotificationMessage( mUpdateManager, *mPropertyNotification );
229     mPropertyNotification = NULL;
230   }
231 }
232
233 } // namespace Internal
234
235 } // namespace Dali