Use Property:LABEL instead of SetLabelText for buttons
[platform/core/uifw/dali-demo.git] / examples / 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
38 Dali::BaseHandle Create()
39 {
40   return Demo::BeatControl::New();
41 }
42
43 DALI_TYPE_REGISTRATION_BEGIN( BeatControl, Dali::Toolkit::Control, Create );
44
45 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "bounceTransition", STRING, BOUNCE_TRANSITION );
46 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "leftTransition", STRING, LEFT_TRANSITION );
47 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "upTransition", STRING, UP_TRANSITION );
48 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "beatVisual", MAP, BEAT_VISUAL );
49 DALI_TYPE_REGISTRATION_END();
50
51
52 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
53 {
54   Toolkit::TransitionData transitionData;
55
56   if( value.GetType() == Property::ARRAY )
57   {
58     transitionData = Toolkit::TransitionData::New( *value.GetArray());
59   }
60   else if( value.GetType() == Property::MAP )
61   {
62     transitionData = Toolkit::TransitionData::New( *value.GetMap() );
63   }
64   return transitionData;
65 }
66
67 } // anonymous namespace
68
69
70 Internal::BeatControl::BeatControl()
71 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) )
72 {
73 }
74
75 Internal::BeatControl::~BeatControl()
76 {
77 }
78
79 Demo::BeatControl Internal::BeatControl::New()
80 {
81   IntrusivePtr<Internal::BeatControl> impl = new Internal::BeatControl();
82   Demo::BeatControl handle = Demo::BeatControl( *impl );
83   impl->Initialize();
84   return handle;
85 }
86
87
88 void BeatControl::StartBounceAnimation()
89 {
90   if( mAnimation )
91   {
92     mAnimation.Stop();
93     mAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnBounceAnimationFinished );
94     OnBounceAnimationFinished(mAnimation);
95   }
96
97   mAnimation = CreateTransition( mBounceTransition );
98   mAnimation.FinishedSignal().Connect( this, &BeatControl::OnBounceAnimationFinished );
99   mAnimation.Play();
100 }
101
102
103 void BeatControl::StartXAnimation()
104 {
105   if( mXAnimation )
106   {
107     mXAnimation.Stop();
108     mXAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnXAnimationFinished );
109     OnXAnimationFinished(mXAnimation);
110   }
111
112   mXAnimation = CreateTransition( mLeftTransition );
113   mXAnimation.FinishedSignal().Connect( this, &BeatControl::OnXAnimationFinished );
114   mXAnimation.Play();
115 }
116
117 void BeatControl::StartYAnimation()
118 {
119   if( mYAnimation )
120   {
121     mYAnimation.Stop();
122     mYAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnYAnimationFinished );
123     OnYAnimationFinished(mYAnimation);
124   }
125
126   mYAnimation = CreateTransition( mUpTransition );
127   mYAnimation.FinishedSignal().Connect( this, &BeatControl::OnYAnimationFinished );
128   mYAnimation.Play();
129 }
130
131
132 void BeatControl::OnBounceAnimationFinished( Animation& src )
133 {
134   // Do stuff
135 }
136 void BeatControl::OnXAnimationFinished( Animation& src )
137 {
138   // Do stuff
139 }
140 void BeatControl::OnYAnimationFinished( Animation& src )
141 {
142   // Do stuff
143 }
144
145 void BeatControl::OnInitialize()
146 {
147   Actor self = Self();
148 }
149
150 void BeatControl::OnStageConnection( int depth )
151 {
152   Control::OnStageConnection( depth );
153 }
154
155 void BeatControl::OnStageDisconnection()
156 {
157   Control::OnStageDisconnection();
158 }
159
160 void BeatControl::OnSizeSet( const Vector3& targetSize )
161 {
162   Control::OnSizeSet( targetSize );
163   RelayoutVisuals( Vector2( targetSize ) );
164 }
165
166 void BeatControl::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
167 {
168   RelayoutVisuals( targetSize );
169 }
170
171 void BeatControl::RelayoutVisuals( const Vector2& targetSize )
172 {
173   if( mVisual )
174   {
175     Vector2 size( targetSize );
176     Property::Map transformMap;
177     // Make the visual half the size of the control, but leave
178     // origin and anchor point at center, position is relative, but Zer0
179     transformMap[ DevelVisual::Transform::Property::SIZE ] = Vector2(0.5, 0.5);
180     mVisual.SetTransformAndSize( transformMap, size );
181
182     // @todo We must stop this clashing with a transform animation
183   }
184 }
185
186 Vector3 BeatControl::GetNaturalSize()
187 {
188   if( mVisual )
189   {
190     Vector2 naturalSize;
191     mVisual.GetNaturalSize(naturalSize);
192     return Vector3(naturalSize);
193   }
194   return Vector3::ZERO;
195 }
196
197 void BeatControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
198 {
199   // Chain up.
200   Control::OnStyleChange( styleManager, change );
201 }
202
203
204 ///////////////////////////////////////////////////////////
205 //
206 // Properties
207 //
208
209 void BeatControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
210 {
211   Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
212
213   if( beatControl )
214   {
215     BeatControl& impl = GetImpl( beatControl );
216     Actor self = impl.Self();
217     switch ( index )
218     {
219       case Demo::BeatControl::Property::BEAT_VISUAL:
220       {
221         Property::Map* map = value.GetMap();
222         if( map )
223         {
224           impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
225           impl.RegisterVisual( Demo::BeatControl::Property::BEAT_VISUAL, impl.mVisual );
226         }
227         break;
228       }
229       case Demo::BeatControl::Property::BOUNCE_TRANSITION:
230       {
231         impl.mBounceTransition = ConvertPropertyToTransition( value );
232         break;
233       }
234       case Demo::BeatControl::Property::LEFT_TRANSITION:
235       {
236         impl.mLeftTransition = ConvertPropertyToTransition( value );
237         break;
238       }
239       case Demo::BeatControl::Property::UP_TRANSITION:
240       {
241         impl.mUpTransition = ConvertPropertyToTransition( value );
242         break;
243       }
244     }
245   }
246 }
247
248 Property::Value BeatControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
249 {
250   Property::Value value;
251
252   Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
253
254   if ( beatControl )
255   {
256     BeatControl& impl = GetImpl( beatControl );
257     switch ( propertyIndex )
258     {
259       case Demo::BeatControl::Property::BEAT_VISUAL:
260       {
261         if( impl.mVisual )
262         {
263           Property::Map map;
264           impl.mVisual.CreatePropertyMap(map);
265           value = map;
266         }
267         break;
268       }
269       case Demo::BeatControl::Property::BOUNCE_TRANSITION:
270       default:
271         break;
272     }
273   }
274
275   return value;
276 }
277
278
279 } // Internal
280 } // Demo