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-toolkit/devel-api/controls/control-devel.h>
20 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
22 using namespace Dali; // Needed for macros
23 using namespace Dali::Toolkit;
33 const int BOUNCE_ANIMATION_RUNNING(0x0001);
34 const int FADE_ANIMATION_RUNNING (0x0002);
35 const int X_ANIMATION_RUNNING (0x0004);
36 const int Y_ANIMATION_RUNNING (0x0008);
39 Dali::BaseHandle Create()
41 return Demo::BeatControl::New();
44 DALI_TYPE_REGISTRATION_BEGIN( BeatControl, Dali::Toolkit::Control, Create );
46 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "bounceTransition", STRING, BOUNCE_TRANSITION );
47 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "leftTransition", STRING, LEFT_TRANSITION );
48 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "upTransition", STRING, UP_TRANSITION );
49 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "fadeTransition", STRING, FADE_TRANSITION );
50 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "beatVisual", MAP, BEAT_VISUAL );
51 DALI_TYPE_REGISTRATION_END();
54 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
56 Toolkit::TransitionData transitionData;
58 const Property::Array* array = value.GetArray();
61 transitionData = Toolkit::TransitionData::New( *array );
65 const Property::Map* map = value.GetMap();
68 transitionData = Toolkit::TransitionData::New( *map );
71 return transitionData;
74 } // anonymous namespace
77 Internal::BeatControl::BeatControl()
78 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
79 mTransformSize(1.0f, 1.0f),
80 mTransformOrigin(Align::CENTER),
81 mTransformAnchorPoint(Align::CENTER),
86 Internal::BeatControl::~BeatControl()
90 Demo::BeatControl Internal::BeatControl::New()
92 IntrusivePtr<Internal::BeatControl> impl = new Internal::BeatControl();
93 Demo::BeatControl handle = Demo::BeatControl( *impl );
99 void BeatControl::StartBounceAnimation()
104 mAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnBounceAnimationFinished );
105 OnBounceAnimationFinished(mAnimation);
108 mAnimation = DevelControl::CreateTransition( *this, mBounceTransition );
109 mAnimation.FinishedSignal().Connect( this, &BeatControl::OnBounceAnimationFinished );
111 mAnimationPlaying |= BOUNCE_ANIMATION_RUNNING;
115 void BeatControl::StartXAnimation()
120 mXAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnXAnimationFinished );
121 OnXAnimationFinished(mXAnimation);
124 mXAnimation = DevelControl::CreateTransition( *this, mLeftTransition );
125 mXAnimation.FinishedSignal().Connect( this, &BeatControl::OnXAnimationFinished );
127 mAnimationPlaying |= X_ANIMATION_RUNNING;
130 void BeatControl::StartYAnimation()
135 mYAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnYAnimationFinished );
136 OnYAnimationFinished(mYAnimation);
139 mYAnimation = DevelControl::CreateTransition( *this, mUpTransition );
140 mYAnimation.FinishedSignal().Connect( this, &BeatControl::OnYAnimationFinished );
142 mAnimationPlaying |= Y_ANIMATION_RUNNING;
145 void BeatControl::StartFadeAnimation()
149 mFadeAnimation.Stop();
150 mFadeAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnFadeAnimationFinished );
151 OnFadeAnimationFinished(mFadeAnimation);
154 mFadeAnimation = DevelControl::CreateTransition( *this, mFadeTransition );
155 mFadeAnimation.FinishedSignal().Connect( this, &BeatControl::OnFadeAnimationFinished );
156 mFadeAnimation.Play();
157 mAnimationPlaying |= FADE_ANIMATION_RUNNING;
160 void BeatControl::OnBounceAnimationFinished( Animation& src )
162 mAnimationPlaying &= ~BOUNCE_ANIMATION_RUNNING;
164 void BeatControl::OnXAnimationFinished( Animation& src )
166 mAnimationPlaying &= ~X_ANIMATION_RUNNING;
168 void BeatControl::OnYAnimationFinished( Animation& src )
170 mAnimationPlaying &= ~Y_ANIMATION_RUNNING;
172 void BeatControl::OnFadeAnimationFinished( Animation& src )
174 mAnimationPlaying &= ~FADE_ANIMATION_RUNNING;
177 void BeatControl::OnInitialize()
182 void BeatControl::OnStageConnection( int depth )
184 Control::OnStageConnection( depth );
187 void BeatControl::OnStageDisconnection()
189 Control::OnStageDisconnection();
192 void BeatControl::OnSizeSet( const Vector3& targetSize )
194 Control::OnSizeSet( targetSize );
195 RelayoutVisuals( Vector2( targetSize ) );
198 void BeatControl::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
200 RelayoutVisuals( targetSize );
203 void BeatControl::RelayoutVisuals( const Vector2& targetSize )
207 if( (mAnimationPlaying & (X_ANIMATION_RUNNING | Y_ANIMATION_RUNNING)) == 0)
209 Vector2 size( targetSize );
210 Property::Map transformMap;
211 // Make the visual half the size of the control, but leave
212 // origin and anchor point at center, position is relative, but Zer0
213 transformMap[ Visual::Transform::Property::SIZE ] = mTransformSize;
214 transformMap[ Visual::Transform::Property::ORIGIN ] = mTransformOrigin;
215 transformMap[ Visual::Transform::Property::ANCHOR_POINT ] = mTransformAnchorPoint;
216 mVisual.SetTransformAndSize( transformMap, size );
221 Vector3 BeatControl::GetNaturalSize()
226 mVisual.GetNaturalSize(naturalSize);
227 return Vector3(naturalSize);
229 return Vector3::ZERO;
232 void BeatControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
235 Control::OnStyleChange( styleManager, change );
239 ///////////////////////////////////////////////////////////
244 void BeatControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
246 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
250 BeatControl& impl = GetImpl( beatControl );
251 Actor self = impl.Self();
254 case Demo::BeatControl::Property::BEAT_VISUAL:
256 bool sizeAndPositionOnly = false;
258 // Determine if a transform.size property exists in the map, and
260 Property::Map* map = value.GetMap();
263 Property::Value* value = map->Find( Visual::Property::TRANSFORM, "transform" );
266 Property::Map* transformMap = value->GetMap();
269 // We'll increment this whenever SIZE, ORIGIN or ANCHOR_POINT's are modified as we won't need to create a new visual if only these properties are used
270 // If there are more properties in the transform map, then we need to create a new visual
271 unsigned int sizeAndPositionPropertyCount = 0;
273 Property::Value* sizeValue = transformMap->Find( Visual::Transform::Property::SIZE, "size" );
276 sizeValue->Get( impl.mTransformSize );
277 ++sizeAndPositionPropertyCount;
280 Property::Value* originValue = transformMap->Find( Visual::Transform::Property::ORIGIN, "origin" );
284 if( originValue->Get( intValue ) )
286 impl.mTransformOrigin = static_cast< Toolkit::Align::Type >( intValue );
287 ++sizeAndPositionPropertyCount;
291 Property::Value* anchorPointValue = transformMap->Find( Visual::Transform::Property::ANCHOR_POINT, "anchorPoint" );
292 if( anchorPointValue )
295 if( anchorPointValue->Get( intValue ) )
297 impl.mTransformAnchorPoint = static_cast< Toolkit::Align::Type >( intValue );
298 ++sizeAndPositionPropertyCount;
302 // If the only properties that the application is overriding are the size and the position properties, then we do not need to create another visual.
303 if( map->Count() == 1 && transformMap->Count() == sizeAndPositionPropertyCount )
305 sizeAndPositionOnly = true;
309 if( ! sizeAndPositionOnly )
311 // Only register a visual if there is more than just a size setting
312 impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
313 DevelControl::RegisterVisual( impl, Demo::BeatControl::Property::BEAT_VISUAL, impl.mVisual );
315 // We have registered a new visual: must trigger size negotiation
316 // in order to call SetTransformAndSize on the visual with the right size:
317 impl.RelayoutRequest();
322 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
324 impl.mBounceTransition = ConvertPropertyToTransition( value );
327 case Demo::BeatControl::Property::LEFT_TRANSITION:
329 impl.mLeftTransition = ConvertPropertyToTransition( value );
332 case Demo::BeatControl::Property::UP_TRANSITION:
334 impl.mUpTransition = ConvertPropertyToTransition( value );
337 case Demo::BeatControl::Property::FADE_TRANSITION:
339 impl.mFadeTransition = ConvertPropertyToTransition( value );
346 Property::Value BeatControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
348 Property::Value value;
350 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
354 BeatControl& impl = GetImpl( beatControl );
355 switch ( propertyIndex )
357 case Demo::BeatControl::Property::BEAT_VISUAL:
362 impl.mVisual.CreatePropertyMap(map);
367 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
368 case Demo::BeatControl::Property::LEFT_TRANSITION:
369 case Demo::BeatControl::Property::UP_TRANSITION:
370 case Demo::BeatControl::Property::FADE_TRANSITION: