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