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/controls/control-devel.h>
21 #include <dali-toolkit/devel-api/visual-factory/visual-factory.h>
22 #include <dali-toolkit/devel-api/visuals/visual-properties-devel.h>
24 using namespace Dali; // Needed for macros
25 using namespace Dali::Toolkit;
35 const int BOUNCE_ANIMATION_RUNNING(0x0001);
36 const int FADE_ANIMATION_RUNNING (0x0002);
37 const int X_ANIMATION_RUNNING (0x0004);
38 const int Y_ANIMATION_RUNNING (0x0008);
41 Dali::BaseHandle Create()
43 return Demo::BeatControl::New();
46 DALI_TYPE_REGISTRATION_BEGIN( BeatControl, Dali::Toolkit::Control, Create );
48 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "bounceTransition", STRING, BOUNCE_TRANSITION );
49 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "leftTransition", STRING, LEFT_TRANSITION );
50 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "upTransition", STRING, UP_TRANSITION );
51 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "fadeTransition", STRING, FADE_TRANSITION );
52 DALI_PROPERTY_REGISTRATION( Demo, BeatControl, "beatVisual", MAP, BEAT_VISUAL );
53 DALI_TYPE_REGISTRATION_END();
56 Toolkit::TransitionData ConvertPropertyToTransition( const Property::Value& value )
58 Toolkit::TransitionData transitionData;
60 const Property::Array* array = value.GetArray();
63 transitionData = Toolkit::TransitionData::New( *array );
67 const Property::Map* map = value.GetMap();
70 transitionData = Toolkit::TransitionData::New( *map );
73 return transitionData;
76 } // anonymous namespace
79 Internal::BeatControl::BeatControl()
80 : Control( ControlBehaviour( REQUIRES_STYLE_CHANGE_SIGNALS ) ),
81 mTransformSize(1.0f, 1.0f),
82 mTransformOrigin(Align::CENTER),
83 mTransformAnchorPoint(Align::CENTER),
88 Internal::BeatControl::~BeatControl()
92 Demo::BeatControl Internal::BeatControl::New()
94 IntrusivePtr<Internal::BeatControl> impl = new Internal::BeatControl();
95 Demo::BeatControl handle = Demo::BeatControl( *impl );
101 void BeatControl::StartBounceAnimation()
106 mAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnBounceAnimationFinished );
107 OnBounceAnimationFinished(mAnimation);
110 mAnimation = DevelControl::CreateTransition( *this, mBounceTransition );
111 mAnimation.FinishedSignal().Connect( this, &BeatControl::OnBounceAnimationFinished );
113 mAnimationPlaying |= BOUNCE_ANIMATION_RUNNING;
117 void BeatControl::StartXAnimation()
122 mXAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnXAnimationFinished );
123 OnXAnimationFinished(mXAnimation);
126 mXAnimation = DevelControl::CreateTransition( *this, mLeftTransition );
127 mXAnimation.FinishedSignal().Connect( this, &BeatControl::OnXAnimationFinished );
129 mAnimationPlaying |= X_ANIMATION_RUNNING;
132 void BeatControl::StartYAnimation()
137 mYAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnYAnimationFinished );
138 OnYAnimationFinished(mYAnimation);
141 mYAnimation = DevelControl::CreateTransition( *this, mUpTransition );
142 mYAnimation.FinishedSignal().Connect( this, &BeatControl::OnYAnimationFinished );
144 mAnimationPlaying |= Y_ANIMATION_RUNNING;
147 void BeatControl::StartFadeAnimation()
151 mFadeAnimation.Stop();
152 mFadeAnimation.FinishedSignal().Disconnect( this, &BeatControl::OnFadeAnimationFinished );
153 OnFadeAnimationFinished(mFadeAnimation);
156 mFadeAnimation = DevelControl::CreateTransition( *this, mFadeTransition );
157 mFadeAnimation.FinishedSignal().Connect( this, &BeatControl::OnFadeAnimationFinished );
158 mFadeAnimation.Play();
159 mAnimationPlaying |= FADE_ANIMATION_RUNNING;
162 void BeatControl::OnBounceAnimationFinished( Animation& src )
164 mAnimationPlaying &= ~BOUNCE_ANIMATION_RUNNING;
166 void BeatControl::OnXAnimationFinished( Animation& src )
168 mAnimationPlaying &= ~X_ANIMATION_RUNNING;
170 void BeatControl::OnYAnimationFinished( Animation& src )
172 mAnimationPlaying &= ~Y_ANIMATION_RUNNING;
174 void BeatControl::OnFadeAnimationFinished( Animation& src )
176 mAnimationPlaying &= ~FADE_ANIMATION_RUNNING;
179 void BeatControl::OnInitialize()
184 void BeatControl::OnStageConnection( int depth )
186 Control::OnStageConnection( depth );
189 void BeatControl::OnStageDisconnection()
191 Control::OnStageDisconnection();
194 void BeatControl::OnSizeSet( const Vector3& targetSize )
196 Control::OnSizeSet( targetSize );
197 RelayoutVisuals( Vector2( targetSize ) );
200 void BeatControl::OnRelayout( const Vector2& targetSize, RelayoutContainer& container )
202 RelayoutVisuals( targetSize );
205 void BeatControl::RelayoutVisuals( const Vector2& targetSize )
209 if( (mAnimationPlaying & (X_ANIMATION_RUNNING | Y_ANIMATION_RUNNING)) == 0)
211 Vector2 size( targetSize );
212 Property::Map transformMap;
213 // Make the visual half the size of the control, but leave
214 // origin and anchor point at center, position is relative, but Zer0
215 transformMap[ DevelVisual::Transform::Property::SIZE ] = mTransformSize;
216 transformMap[ DevelVisual::Transform::Property::ORIGIN ] = mTransformOrigin;
217 transformMap[ DevelVisual::Transform::Property::ANCHOR_POINT ] = mTransformAnchorPoint;
218 mVisual.SetTransformAndSize( transformMap, size );
223 Vector3 BeatControl::GetNaturalSize()
228 mVisual.GetNaturalSize(naturalSize);
229 return Vector3(naturalSize);
231 return Vector3::ZERO;
234 void BeatControl::OnStyleChange( Toolkit::StyleManager styleManager, StyleChange::Type change )
237 Control::OnStyleChange( styleManager, change );
241 ///////////////////////////////////////////////////////////
246 void BeatControl::SetProperty( BaseObject* object, Property::Index index, const Property::Value& value )
248 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
252 BeatControl& impl = GetImpl( beatControl );
253 Actor self = impl.Self();
256 case Demo::BeatControl::Property::BEAT_VISUAL:
258 bool sizeAndPositionOnly = false;
260 // Determine if a transform.size property exists in the map, and
262 Property::Map* map = value.GetMap();
265 Property::Value* value = map->Find( DevelVisual::Property::TRANSFORM, "transform" );
268 Property::Map* transformMap = value->GetMap();
271 // 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
272 // If there are more properties in the transform map, then we need to create a new visual
273 unsigned int sizeAndPositionPropertyCount = 0;
275 Property::Value* sizeValue = transformMap->Find( DevelVisual::Transform::Property::SIZE, "size" );
278 sizeValue->Get( impl.mTransformSize );
279 ++sizeAndPositionPropertyCount;
282 Property::Value* originValue = transformMap->Find( DevelVisual::Transform::Property::ORIGIN, "origin" );
286 if( originValue->Get( intValue ) )
288 impl.mTransformOrigin = static_cast< Toolkit::Align::Type >( intValue );
289 ++sizeAndPositionPropertyCount;
293 Property::Value* anchorPointValue = transformMap->Find( DevelVisual::Transform::Property::ANCHOR_POINT, "anchorPoint" );
294 if( anchorPointValue )
297 if( anchorPointValue->Get( intValue ) )
299 impl.mTransformAnchorPoint = static_cast< Toolkit::Align::Type >( intValue );
300 ++sizeAndPositionPropertyCount;
304 // 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.
305 if( map->Count() == 1 && transformMap->Count() == sizeAndPositionPropertyCount )
307 sizeAndPositionOnly = true;
311 if( ! sizeAndPositionOnly )
313 // Only register a visual if there is more than just a size setting
314 impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual( *map );
315 DevelControl::RegisterVisual( impl, Demo::BeatControl::Property::BEAT_VISUAL, impl.mVisual );
317 // We have registered a new visual: must trigger size negotiation
318 // in order to call SetTransformAndSize on the visual with the right size:
319 impl.RelayoutRequest();
324 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
326 impl.mBounceTransition = ConvertPropertyToTransition( value );
329 case Demo::BeatControl::Property::LEFT_TRANSITION:
331 impl.mLeftTransition = ConvertPropertyToTransition( value );
334 case Demo::BeatControl::Property::UP_TRANSITION:
336 impl.mUpTransition = ConvertPropertyToTransition( value );
339 case Demo::BeatControl::Property::FADE_TRANSITION:
341 impl.mFadeTransition = ConvertPropertyToTransition( value );
348 Property::Value BeatControl::GetProperty( BaseObject* object, Property::Index propertyIndex )
350 Property::Value value;
352 Demo::BeatControl beatControl = Demo::BeatControl::DownCast( Dali::BaseHandle( object ) );
356 BeatControl& impl = GetImpl( beatControl );
357 switch ( propertyIndex )
359 case Demo::BeatControl::Property::BEAT_VISUAL:
364 impl.mVisual.CreatePropertyMap(map);
369 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
370 case Demo::BeatControl::Property::LEFT_TRANSITION:
371 case Demo::BeatControl::Property::UP_TRANSITION:
372 case Demo::BeatControl::Property::FADE_TRANSITION: