[dali_1.2.42] Merge branch 'devel/master'
[platform/core/uifw/dali-demo.git] / examples / visual-transitions / beat-control-impl.cpp
1 /*
2  * Copyright (c) 2017 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 #include "beat-control-impl.h"
18 #include <dali-toolkit/dali-toolkit.h>
19 #include <dali/public-api/object/type-registry-helper.h>
20 #include <dali-toolkit/devel-api/align-enums.h>
21 #include <dali-toolkit/devel-api/controls/control-devel.h>
22 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
23 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
24
25 #include <cstdio>
26
27 using namespace Dali; // Needed for macros
28 using namespace Dali::Toolkit;
29
30 namespace Demo
31 {
32 namespace Internal
33 {
34
35 namespace
36 {
37
38 const int BOUNCE_ANIMATION_RUNNING(0x0001);
39 const int FADE_ANIMATION_RUNNING  (0x0002);
40 const int X_ANIMATION_RUNNING     (0x0004);
41 const int Y_ANIMATION_RUNNING     (0x0008);
42
43
44 Dali::BaseHandle Create()
45 {
46   return Demo::BeatControl::New();
47 }
48
49 DALI_TYPE_REGISTRATION_BEGIN( BeatControl, Dali::Toolkit::Control, Create );
50
51 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "bounceTransition", STRING, BOUNCE_TRANSITION );
52 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "leftTransition", STRING, LEFT_TRANSITION );
53 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "upTransition", STRING, UP_TRANSITION );
54 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "fadeTransition", STRING, FADE_TRANSITION );
55 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "beatVisual", MAP, BEAT_VISUAL );
56 DALI_TYPE_REGISTRATION_END();
57
58
59 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
60 {
61   Toolkit::TransitionData transitionData;
62
63   if( value.GetType() == Property::ARRAY )
64   {
65     transitionData = Toolkit::TransitionData::New( *value.GetArray());
66   }
67   else if( value.GetType() == Property::MAP )
68   {
69     transitionData = Toolkit::TransitionData::New( *value.GetMap() );
70   }
71   return transitionData;
72 }
73
74 } // anonymous namespace
75
76
77 Internal::BeatControl::BeatControl()
78 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
79   mTransformSize(1.0f, 1.0f),
80   mAnimationPlaying(0)
81 {
82 }
83
84 Internal::BeatControl::~BeatControl()
85 {
86 }
87
88 Demo::BeatControl Internal::BeatControl::New()
89 {
90   IntrusivePtr<Internal::BeatControl> impl = new Internal::BeatControl();
91   Demo::BeatControl handle = Demo::BeatControl( *impl );
92   impl->Initialize();
93   return handle;
94 }
95
96
97 void BeatControl::StartBounceAnimation()
98 {
99   if( mAnimation )
100   {
101     mAnimation.Stop();
102     mAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnBounceAnimationFinished );
103     OnBounceAnimationFinished(mAnimation);
104   }
105
106   mAnimation = DevelControl::CreateTransition( *this, mBounceTransition );
107   mAnimation.FinishedSignal().Connect( this, &BeatControl::OnBounceAnimationFinished );
108   mAnimation.Play();
109   mAnimationPlaying |= BOUNCE_ANIMATION_RUNNING;
110 }
111
112
113 void BeatControl::StartXAnimation()
114 {
115   if( mXAnimation )
116   {
117     mXAnimation.Stop();
118     mXAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnXAnimationFinished );
119     OnXAnimationFinished(mXAnimation);
120   }
121
122   mXAnimation = DevelControl::CreateTransition( *this, mLeftTransition );
123   mXAnimation.FinishedSignal().Connect( this, &BeatControl::OnXAnimationFinished );
124   mXAnimation.Play();
125   mAnimationPlaying |= X_ANIMATION_RUNNING;
126 }
127
128 void BeatControl::StartYAnimation()
129 {
130   if( mYAnimation )
131   {
132     mYAnimation.Stop();
133     mYAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnYAnimationFinished );
134     OnYAnimationFinished(mYAnimation);
135   }
136
137   mYAnimation = DevelControl::CreateTransition( *this, mUpTransition );
138   mYAnimation.FinishedSignal().Connect( this, &BeatControl::OnYAnimationFinished );
139   mYAnimation.Play();
140   mAnimationPlaying |= Y_ANIMATION_RUNNING;
141 }
142
143 void BeatControl::StartFadeAnimation()
144 {
145   if( mFadeAnimation )
146   {
147     mFadeAnimation.Stop();
148     mFadeAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnFadeAnimationFinished );
149     OnFadeAnimationFinished(mFadeAnimation);
150   }
151
152   mFadeAnimation = DevelControl::CreateTransition( *this, mFadeTransition );
153   mFadeAnimation.FinishedSignal().Connect( this, &BeatControl::OnFadeAnimationFinished );
154   mFadeAnimation.Play();
155   mAnimationPlaying |= FADE_ANIMATION_RUNNING;
156 }
157
158 void BeatControl::OnBounceAnimationFinished( Animation& src )
159 {
160   mAnimationPlaying &= ~BOUNCE_ANIMATION_RUNNING;
161 }
162 void BeatControl::OnXAnimationFinished( Animation& src )
163 {
164   mAnimationPlaying &= ~X_ANIMATION_RUNNING;
165 }
166 void BeatControl::OnYAnimationFinished( Animation& src )
167 {
168   mAnimationPlaying &= ~Y_ANIMATION_RUNNING;
169 }
170 void BeatControl::OnFadeAnimationFinished( Animation& src )
171 {
172   mAnimationPlaying &= ~FADE_ANIMATION_RUNNING;
173 }
174
175 void BeatControl::OnInitialize()
176 {
177   Actor self = Self();
178 }
179
180 void BeatControl::OnStageConnection( int depth )
181 {
182   Control::OnStageConnection( depth );
183 }
184
185 void BeatControl::OnStageDisconnection()
186 {
187   Control::OnStageDisconnection();
188 }
189
190 void BeatControl::OnSizeSet( const Vector3& targetSize )
191 {
192   Control::OnSizeSet( targetSize );
193   RelayoutVisuals( Vector2( targetSize ) );
194 }
195
196 void BeatControl::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
197 {
198   RelayoutVisuals( targetSize );
199 }
200
201 void BeatControl::RelayoutVisuals( const Vector2& targetSize )
202 {
203   if( mVisual )
204   {
205     if( (mAnimationPlaying & (X_ANIMATION_RUNNING | Y_ANIMATION_RUNNING)) == 0)
206     {
207       Vector2 size( targetSize );
208       Property::Map transformMap;
209       // Make the visual half the size of the control, but leave
210       // origin and anchor point at center, position is relative, but Zer0
211       transformMap[ DevelVisual::Transform::Property::SIZE ] = mTransformSize;
212       mVisual.SetTransformAndSize( transformMap, size );
213     }
214   }
215 }
216
217 Vector3 BeatControl::GetNaturalSize()
218 {
219   if( mVisual )
220   {
221     Vector2 naturalSize;
222     mVisual.GetNaturalSize(naturalSize);
223     return Vector3(naturalSize);
224   }
225   return Vector3::ZERO;
226 }
227
228 void BeatControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
229 {
230   // Chain up.
231   Control::OnStyleChange( styleManager, change );
232 }
233
234
235 ///////////////////////////////////////////////////////////
236 //
237 // Properties
238 //
239
240 void BeatControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
241 {
242   Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
243
244   if( beatControl )
245   {
246     BeatControl& impl = GetImpl( beatControl );
247     Actor self = impl.Self();
248     switch ( index )
249     {
250       case Demo::BeatControl::Property::BEAT_VISUAL:
251       {
252         bool sizeOnly = false;
253
254         // Determine if a transform.size property exists in the map, and
255         // save it.
256         Property::Map* map = value.GetMap();
257         if( map )
258         {
259           Property::Value* value = map->Find( DevelVisual::Property::TRANSFORM, "transform" );
260           if( value )
261           {
262             Property::Map* transformMap = value->GetMap();
263             if( transformMap )
264             {
265               Property::Value* sizeValue = transformMap->Find( DevelVisual::Transform::Property::SIZE, "size" );
266               if( sizeValue )
267               {
268                 sizeValue->Get( impl.mTransformSize );
269                 if( map->Count() == 1 && transformMap->Count() == 1 )
270                 {
271                   sizeOnly = true;
272                 }
273               }
274             }
275           }
276           if( ! sizeOnly )
277           {
278             // Only register a visual if there is more than just a size setting
279             impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
280             DevelControl::RegisterVisual( impl, Demo::BeatControl::Property::BEAT_VISUAL, impl.mVisual );
281
282             // We have registered a new visual: must trigger size negotiation
283             // in order to call SetTransformAndSize on the visual with the right size:
284             impl.RelayoutRequest();
285           }
286         }
287         break;
288       }
289       case Demo::BeatControl::Property::BOUNCE_TRANSITION:
290       {
291         impl.mBounceTransition = ConvertPropertyToTransition( value );
292         break;
293       }
294       case Demo::BeatControl::Property::LEFT_TRANSITION:
295       {
296         impl.mLeftTransition = ConvertPropertyToTransition( value );
297         break;
298       }
299       case Demo::BeatControl::Property::UP_TRANSITION:
300       {
301         impl.mUpTransition = ConvertPropertyToTransition( value );
302         break;
303       }
304       case Demo::BeatControl::Property::FADE_TRANSITION:
305       {
306         impl.mFadeTransition = ConvertPropertyToTransition( value );
307         break;
308       }
309     }
310   }
311 }
312
313 Property::Value BeatControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
314 {
315   Property::Value value;
316
317   Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
318
319   if ( beatControl )
320   {
321     BeatControl& impl = GetImpl( beatControl );
322     switch ( propertyIndex )
323     {
324       case Demo::BeatControl::Property::BEAT_VISUAL:
325       {
326         if( impl.mVisual )
327         {
328           Property::Map map;
329           impl.mVisual.CreatePropertyMap(map);
330           value = map;
331         }
332         break;
333       }
334       case Demo::BeatControl::Property::BOUNCE_TRANSITION:
335       case Demo::BeatControl::Property::LEFT_TRANSITION:
336       case Demo::BeatControl::Property::UP_TRANSITION:
337       case Demo::BeatControl::Property::FADE_TRANSITION:
338       default:
339         break;
340     }
341   }
342
343   return value;
344 }
345
346
347 } // Internal
348 } // Demo