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