2 * Copyright (c) 2017 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/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>
27 using namespace Dali; // Needed for macros
28 using namespace Dali::Toolkit;
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);
44 Dali::BaseHandle Create()
46 return Demo::BeatControl::New();
49 DALI_TYPE_REGISTRATION_BEGIN( BeatControl, Dali::Toolkit::Control, Create );
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();
59 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
61 Toolkit::TransitionData transitionData;
63 if( value.GetType() == Property::ARRAY )
65 transitionData = Toolkit::TransitionData::New( *value.GetArray());
67 else if( value.GetType() == Property::MAP )
69 transitionData = Toolkit::TransitionData::New( *value.GetMap() );
71 return transitionData;
74 } // anonymous namespace
77 Internal::BeatControl::BeatControl()
78 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
79 mTransformSize(1.0f, 1.0f),
84 Internal::BeatControl::~BeatControl()
88 Demo::BeatControl Internal::BeatControl::New()
90 IntrusivePtr<Internal::BeatControl> impl = new Internal::BeatControl();
91 Demo::BeatControl handle = Demo::BeatControl( *impl );
97 void BeatControl::StartBounceAnimation()
102 mAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnBounceAnimationFinished );
103 OnBounceAnimationFinished(mAnimation);
106 mAnimation = DevelControl::CreateTransition( *this, mBounceTransition );
107 mAnimation.FinishedSignal().Connect( this, &BeatControl::OnBounceAnimationFinished );
109 mAnimationPlaying |= BOUNCE_ANIMATION_RUNNING;
113 void BeatControl::StartXAnimation()
118 mXAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnXAnimationFinished );
119 OnXAnimationFinished(mXAnimation);
122 mXAnimation = DevelControl::CreateTransition( *this, mLeftTransition );
123 mXAnimation.FinishedSignal().Connect( this, &BeatControl::OnXAnimationFinished );
125 mAnimationPlaying |= X_ANIMATION_RUNNING;
128 void BeatControl::StartYAnimation()
133 mYAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnYAnimationFinished );
134 OnYAnimationFinished(mYAnimation);
137 mYAnimation = DevelControl::CreateTransition( *this, mUpTransition );
138 mYAnimation.FinishedSignal().Connect( this, &BeatControl::OnYAnimationFinished );
140 mAnimationPlaying |= Y_ANIMATION_RUNNING;
143 void BeatControl::StartFadeAnimation()
147 mFadeAnimation.Stop();
148 mFadeAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnFadeAnimationFinished );
149 OnFadeAnimationFinished(mFadeAnimation);
152 mFadeAnimation = DevelControl::CreateTransition( *this, mFadeTransition );
153 mFadeAnimation.FinishedSignal().Connect( this, &BeatControl::OnFadeAnimationFinished );
154 mFadeAnimation.Play();
155 mAnimationPlaying |= FADE_ANIMATION_RUNNING;
158 void BeatControl::OnBounceAnimationFinished( Animation& src )
160 mAnimationPlaying &= ~BOUNCE_ANIMATION_RUNNING;
162 void BeatControl::OnXAnimationFinished( Animation& src )
164 mAnimationPlaying &= ~X_ANIMATION_RUNNING;
166 void BeatControl::OnYAnimationFinished( Animation& src )
168 mAnimationPlaying &= ~Y_ANIMATION_RUNNING;
170 void BeatControl::OnFadeAnimationFinished( Animation& src )
172 mAnimationPlaying &= ~FADE_ANIMATION_RUNNING;
175 void BeatControl::OnInitialize()
180 void BeatControl::OnStageConnection( int depth )
182 Control::OnStageConnection( depth );
185 void BeatControl::OnStageDisconnection()
187 Control::OnStageDisconnection();
190 void BeatControl::OnSizeSet( const Vector3& targetSize )
192 Control::OnSizeSet( targetSize );
193 RelayoutVisuals( Vector2( targetSize ) );
196 void BeatControl::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
198 RelayoutVisuals( targetSize );
201 void BeatControl::RelayoutVisuals( const Vector2& targetSize )
205 if( (mAnimationPlaying & (X_ANIMATION_RUNNING | Y_ANIMATION_RUNNING)) == 0)
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 );
217 Vector3 BeatControl::GetNaturalSize()
222 mVisual.GetNaturalSize(naturalSize);
223 return Vector3(naturalSize);
225 return Vector3::ZERO;
228 void BeatControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
231 Control::OnStyleChange( styleManager, change );
235 ///////////////////////////////////////////////////////////
240 void BeatControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
242 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
246 BeatControl& impl = GetImpl( beatControl );
247 Actor self = impl.Self();
250 case Demo::BeatControl::Property::BEAT_VISUAL:
252 bool sizeOnly = false;
254 // Determine if a transform.size property exists in the map, and
256 Property::Map* map = value.GetMap();
259 Property::Value* value = map->Find( DevelVisual::Property::TRANSFORM, "transform" );
262 Property::Map* transformMap = value->GetMap();
265 Property::Value* sizeValue = transformMap->Find( DevelVisual::Transform::Property::SIZE, "size" );
268 sizeValue->Get( impl.mTransformSize );
269 if( map->Count() == 1 && transformMap->Count() == 1 )
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 );
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();
289 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
291 impl.mBounceTransition = ConvertPropertyToTransition( value );
294 case Demo::BeatControl::Property::LEFT_TRANSITION:
296 impl.mLeftTransition = ConvertPropertyToTransition( value );
299 case Demo::BeatControl::Property::UP_TRANSITION:
301 impl.mUpTransition = ConvertPropertyToTransition( value );
304 case Demo::BeatControl::Property::FADE_TRANSITION:
306 impl.mFadeTransition = ConvertPropertyToTransition( value );
313 Property::Value BeatControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
315 Property::Value value;
317 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
321 BeatControl& impl = GetImpl( beatControl );
322 switch ( propertyIndex )
324 case Demo::BeatControl::Property::BEAT_VISUAL:
329 impl.mVisual.CreatePropertyMap(map);
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: