2 * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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>
26 using namespace Dali; // Needed for macros
27 using namespace Dali::Toolkit;
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);
43 Dali::BaseHandle Create()
45 return Demo::BeatControl::New();
48 DALI_TYPE_REGISTRATION_BEGIN( BeatControl, Dali::Toolkit::Control, Create );
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();
58 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
60 Toolkit::TransitionData transitionData;
62 if( value.GetType() == Property::ARRAY )
64 transitionData = Toolkit::TransitionData::New( *value.GetArray());
66 else if( value.GetType() == Property::MAP )
68 transitionData = Toolkit::TransitionData::New( *value.GetMap() );
70 return transitionData;
73 } // anonymous namespace
76 Internal::BeatControl::BeatControl()
77 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
78 mTransformSize(1.0f, 1.0f),
83 Internal::BeatControl::~BeatControl()
87 Demo::BeatControl Internal::BeatControl::New()
89 IntrusivePtr<Internal::BeatControl> impl = new Internal::BeatControl();
90 Demo::BeatControl handle = Demo::BeatControl( *impl );
96 void BeatControl::StartBounceAnimation()
101 mAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnBounceAnimationFinished );
102 OnBounceAnimationFinished(mAnimation);
105 mAnimation = CreateTransition( mBounceTransition );
106 mAnimation.FinishedSignal().Connect( this, &BeatControl::OnBounceAnimationFinished );
108 mAnimationPlaying |= BOUNCE_ANIMATION_RUNNING;
112 void BeatControl::StartXAnimation()
117 mXAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnXAnimationFinished );
118 OnXAnimationFinished(mXAnimation);
121 mXAnimation = CreateTransition( mLeftTransition );
122 mXAnimation.FinishedSignal().Connect( this, &BeatControl::OnXAnimationFinished );
124 mAnimationPlaying |= X_ANIMATION_RUNNING;
127 void BeatControl::StartYAnimation()
132 mYAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnYAnimationFinished );
133 OnYAnimationFinished(mYAnimation);
136 mYAnimation = CreateTransition( mUpTransition );
137 mYAnimation.FinishedSignal().Connect( this, &BeatControl::OnYAnimationFinished );
139 mAnimationPlaying |= Y_ANIMATION_RUNNING;
142 void BeatControl::StartFadeAnimation()
146 mFadeAnimation.Stop();
147 mFadeAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnFadeAnimationFinished );
148 OnFadeAnimationFinished(mFadeAnimation);
151 mFadeAnimation = CreateTransition( mFadeTransition );
152 mFadeAnimation.FinishedSignal().Connect( this, &BeatControl::OnFadeAnimationFinished );
153 mFadeAnimation.Play();
154 mAnimationPlaying |= FADE_ANIMATION_RUNNING;
157 void BeatControl::OnBounceAnimationFinished( Animation& src )
159 mAnimationPlaying &= ~BOUNCE_ANIMATION_RUNNING;
161 void BeatControl::OnXAnimationFinished( Animation& src )
163 mAnimationPlaying &= ~X_ANIMATION_RUNNING;
165 void BeatControl::OnYAnimationFinished( Animation& src )
167 mAnimationPlaying &= ~Y_ANIMATION_RUNNING;
169 void BeatControl::OnFadeAnimationFinished( Animation& src )
171 mAnimationPlaying &= ~FADE_ANIMATION_RUNNING;
174 void BeatControl::OnInitialize()
179 void BeatControl::OnStageConnection( int depth )
181 Control::OnStageConnection( depth );
184 void BeatControl::OnStageDisconnection()
186 Control::OnStageDisconnection();
189 void BeatControl::OnSizeSet( const Vector3& targetSize )
191 Control::OnSizeSet( targetSize );
192 RelayoutVisuals( Vector2( targetSize ) );
195 void BeatControl::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
197 RelayoutVisuals( targetSize );
200 void BeatControl::RelayoutVisuals( const Vector2& targetSize )
204 if( (mAnimationPlaying & (X_ANIMATION_RUNNING | Y_ANIMATION_RUNNING)) == 0)
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 );
216 Vector3 BeatControl::GetNaturalSize()
221 mVisual.GetNaturalSize(naturalSize);
222 return Vector3(naturalSize);
224 return Vector3::ZERO;
227 void BeatControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
230 Control::OnStyleChange( styleManager, change );
234 ///////////////////////////////////////////////////////////
239 void BeatControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
241 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
245 BeatControl& impl = GetImpl( beatControl );
246 Actor self = impl.Self();
249 case Demo::BeatControl::Property::BEAT_VISUAL:
251 bool sizeOnly = false;
253 // Determine if a transform.size property exists in the map, and
255 Property::Map* map = value.GetMap();
258 Property::Value* value = map->Find( DevelVisual::Property::TRANSFORM, "transform" );
261 Property::Map* transformMap = value->GetMap();
264 Property::Value* sizeValue = transformMap->Find( DevelVisual::Transform::Property::SIZE, "size" );
267 sizeValue->Get( impl.mTransformSize );
268 if( map->Count() == 1 && transformMap->Count() == 1 )
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 );
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();
288 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
290 impl.mBounceTransition = ConvertPropertyToTransition( value );
293 case Demo::BeatControl::Property::LEFT_TRANSITION:
295 impl.mLeftTransition = ConvertPropertyToTransition( value );
298 case Demo::BeatControl::Property::UP_TRANSITION:
300 impl.mUpTransition = ConvertPropertyToTransition( value );
303 case Demo::BeatControl::Property::FADE_TRANSITION:
305 impl.mFadeTransition = ConvertPropertyToTransition( value );
312 Property::Value BeatControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
314 Property::Value value;
316 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
320 BeatControl& impl = GetImpl( beatControl );
321 switch ( propertyIndex )
323 case Demo::BeatControl::Property::BEAT_VISUAL:
328 impl.mVisual.CreatePropertyMap(map);
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: