2 * Copyright (c) 2020 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;
31 const int BOUNCE_ANIMATION_RUNNING(0x0001);
32 const int FADE_ANIMATION_RUNNING(0x0002);
33 const int X_ANIMATION_RUNNING(0x0004);
34 const int Y_ANIMATION_RUNNING(0x0008);
36 Dali::BaseHandle Create()
38 return Demo::BeatControl::New();
41 DALI_TYPE_REGISTRATION_BEGIN(BeatControl, Dali::Toolkit::Control, Create);
43 DALI_PROPERTY_REGISTRATION(Demo, BeatControl, "bounceTransition", STRING, BOUNCE_TRANSITION);
44 DALI_PROPERTY_REGISTRATION(Demo, BeatControl, "leftTransition", STRING, LEFT_TRANSITION);
45 DALI_PROPERTY_REGISTRATION(Demo, BeatControl, "upTransition", STRING, UP_TRANSITION);
46 DALI_PROPERTY_REGISTRATION(Demo, BeatControl, "fadeTransition", STRING, FADE_TRANSITION);
47 DALI_PROPERTY_REGISTRATION(Demo, BeatControl, "beatVisual", MAP, BEAT_VISUAL);
48 DALI_TYPE_REGISTRATION_END();
50 Toolkit::TransitionData ConvertPropertyToTransition(const Property::Value& value)
52 Toolkit::TransitionData transitionData;
54 const Property::Array* array = value.GetArray();
57 transitionData = Toolkit::TransitionData::New(*array);
61 const Property::Map* map = value.GetMap();
64 transitionData = Toolkit::TransitionData::New(*map);
67 return transitionData;
70 } // anonymous namespace
72 Internal::BeatControl::BeatControl()
73 : Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
74 mTransformSize(1.0f, 1.0f),
75 mTransformOrigin(Align::CENTER),
76 mTransformAnchorPoint(Align::CENTER),
81 Internal::BeatControl::~BeatControl()
85 Demo::BeatControl Internal::BeatControl::New()
87 IntrusivePtr<Internal::BeatControl> impl = new Internal::BeatControl();
88 Demo::BeatControl handle = Demo::BeatControl(*impl);
93 void BeatControl::StartBounceAnimation()
98 mAnimation.FinishedSignal().Disconnect(this, &BeatControl::OnBounceAnimationFinished);
99 OnBounceAnimationFinished(mAnimation);
102 mAnimation = DevelControl::CreateTransition(*this, mBounceTransition);
103 mAnimation.FinishedSignal().Connect(this, &BeatControl::OnBounceAnimationFinished);
105 mAnimationPlaying |= BOUNCE_ANIMATION_RUNNING;
108 void BeatControl::StartXAnimation()
113 mXAnimation.FinishedSignal().Disconnect(this, &BeatControl::OnXAnimationFinished);
114 OnXAnimationFinished(mXAnimation);
117 mXAnimation = DevelControl::CreateTransition(*this, mLeftTransition);
118 mXAnimation.FinishedSignal().Connect(this, &BeatControl::OnXAnimationFinished);
120 mAnimationPlaying |= X_ANIMATION_RUNNING;
123 void BeatControl::StartYAnimation()
128 mYAnimation.FinishedSignal().Disconnect(this, &BeatControl::OnYAnimationFinished);
129 OnYAnimationFinished(mYAnimation);
132 mYAnimation = DevelControl::CreateTransition(*this, mUpTransition);
133 mYAnimation.FinishedSignal().Connect(this, &BeatControl::OnYAnimationFinished);
135 mAnimationPlaying |= Y_ANIMATION_RUNNING;
138 void BeatControl::StartFadeAnimation()
142 mFadeAnimation.Stop();
143 mFadeAnimation.FinishedSignal().Disconnect(this, &BeatControl::OnFadeAnimationFinished);
144 OnFadeAnimationFinished(mFadeAnimation);
147 mFadeAnimation = DevelControl::CreateTransition(*this, mFadeTransition);
148 mFadeAnimation.FinishedSignal().Connect(this, &BeatControl::OnFadeAnimationFinished);
149 mFadeAnimation.Play();
150 mAnimationPlaying |= FADE_ANIMATION_RUNNING;
153 void BeatControl::OnBounceAnimationFinished(Animation& src)
155 mAnimationPlaying &= ~BOUNCE_ANIMATION_RUNNING;
157 void BeatControl::OnXAnimationFinished(Animation& src)
159 mAnimationPlaying &= ~X_ANIMATION_RUNNING;
161 void BeatControl::OnYAnimationFinished(Animation& src)
163 mAnimationPlaying &= ~Y_ANIMATION_RUNNING;
165 void BeatControl::OnFadeAnimationFinished(Animation& src)
167 mAnimationPlaying &= ~FADE_ANIMATION_RUNNING;
170 void BeatControl::OnInitialize()
175 void BeatControl::OnSceneConnection(int depth)
177 Control::OnSceneConnection(depth);
180 void BeatControl::OnSceneDisconnection()
182 Control::OnSceneDisconnection();
185 void BeatControl::OnSizeSet(const Vector3& targetSize)
187 Control::OnSizeSet(targetSize);
188 RelayoutVisuals(Vector2(targetSize));
191 void BeatControl::OnRelayout(const Vector2& targetSize, RelayoutContainer& container)
193 RelayoutVisuals(targetSize);
196 void BeatControl::RelayoutVisuals(const Vector2& targetSize)
200 if((mAnimationPlaying & (X_ANIMATION_RUNNING | Y_ANIMATION_RUNNING)) == 0)
202 Vector2 size(targetSize);
203 Property::Map transformMap;
204 // Make the visual half the size of the control, but leave
205 // origin and anchor point at center, position is relative, but Zer0
206 transformMap[Visual::Transform::Property::SIZE] = mTransformSize;
207 transformMap[Visual::Transform::Property::ORIGIN] = mTransformOrigin;
208 transformMap[Visual::Transform::Property::ANCHOR_POINT] = mTransformAnchorPoint;
209 mVisual.SetTransformAndSize(transformMap, size);
214 Vector3 BeatControl::GetNaturalSize()
219 mVisual.GetNaturalSize(naturalSize);
220 return Vector3(naturalSize);
222 return Vector3::ZERO;
225 void BeatControl::OnStyleChange(Toolkit::StyleManager styleManager, StyleChange::Type change)
228 Control::OnStyleChange(styleManager, change);
231 ///////////////////////////////////////////////////////////
236 void BeatControl::SetProperty(BaseObject* object, Property::Index index, const Property::Value& value)
238 Demo::BeatControl beatControl = Demo::BeatControl::DownCast(Dali::BaseHandle(object));
242 BeatControl& impl = GetImpl(beatControl);
243 Actor self = impl.Self();
246 case Demo::BeatControl::Property::BEAT_VISUAL:
248 bool sizeAndPositionOnly = false;
250 // Determine if a transform.size property exists in the map, and
252 const Property::Map* map = value.GetMap();
255 Property::Value* value = map->Find(Visual::Property::TRANSFORM, "transform");
258 Property::Map* transformMap = value->GetMap();
261 // 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
262 // If there are more properties in the transform map, then we need to create a new visual
263 unsigned int sizeAndPositionPropertyCount = 0;
265 Property::Value* sizeValue = transformMap->Find(Visual::Transform::Property::SIZE, "size");
268 sizeValue->Get(impl.mTransformSize);
269 ++sizeAndPositionPropertyCount;
272 Property::Value* originValue = transformMap->Find(Visual::Transform::Property::ORIGIN, "origin");
276 if(originValue->Get(intValue))
278 impl.mTransformOrigin = static_cast<Toolkit::Align::Type>(intValue);
279 ++sizeAndPositionPropertyCount;
283 Property::Value* anchorPointValue = transformMap->Find(Visual::Transform::Property::ANCHOR_POINT, "anchorPoint");
287 if(anchorPointValue->Get(intValue))
289 impl.mTransformAnchorPoint = static_cast<Toolkit::Align::Type>(intValue);
290 ++sizeAndPositionPropertyCount;
294 // 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.
295 if(map->Count() == 1 && transformMap->Count() == sizeAndPositionPropertyCount)
297 sizeAndPositionOnly = true;
301 if(!sizeAndPositionOnly)
303 // Only register a visual if there is more than just a size setting
304 impl.mVisual = Toolkit::VisualFactory::Get().CreateVisual(*map);
305 DevelControl::RegisterVisual(impl, Demo::BeatControl::Property::BEAT_VISUAL, impl.mVisual);
307 // We have registered a new visual: must trigger size negotiation
308 // in order to call SetTransformAndSize on the visual with the right size:
309 impl.RelayoutRequest();
314 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
316 impl.mBounceTransition = ConvertPropertyToTransition(value);
319 case Demo::BeatControl::Property::LEFT_TRANSITION:
321 impl.mLeftTransition = ConvertPropertyToTransition(value);
324 case Demo::BeatControl::Property::UP_TRANSITION:
326 impl.mUpTransition = ConvertPropertyToTransition(value);
329 case Demo::BeatControl::Property::FADE_TRANSITION:
331 impl.mFadeTransition = ConvertPropertyToTransition(value);
338 Property::Value BeatControl::GetProperty(BaseObject* object, Property::Index propertyIndex)
340 Property::Value value;
342 Demo::BeatControl beatControl = Demo::BeatControl::DownCast(Dali::BaseHandle(object));
346 BeatControl& impl = GetImpl(beatControl);
347 switch(propertyIndex)
349 case Demo::BeatControl::Property::BEAT_VISUAL:
354 impl.mVisual.CreatePropertyMap(map);
359 case Demo::BeatControl::Property::BOUNCE_TRANSITION:
360 case Demo::BeatControl::Property::LEFT_TRANSITION:
361 case Demo::BeatControl::Property::UP_TRANSITION:
362 case Demo::BeatControl::Property::FADE_TRANSITION:
371 } // namespace Internal