2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 #include <dali-toolkit/dali-toolkit.h>
\r
19 #include <dali/dali.h>
\r
22 class SceneLoaderExtension : public Dali::ConnectionTracker
\r
25 SceneLoaderExtension()
\r
26 : mSceneLoader(nullptr),
\r
27 mCurrentAnimationIndex(ANIMATION_IDLE)
\r
31 ~SceneLoaderExtension() = default; // Nothing to do in destructor
\r
33 void SetSceneLoader(SceneLoaderExample* sceneLoader)
\r
35 mSceneLoader = sceneLoader;
\r
38 void ConnectTouchSignals()
\r
40 // This is a temporary hack for now to manually connect these signals.
\r
41 // We should connect these signals automatically when loading the scene.
\r
45 ConnectTouchSignal(ICON_IDLE);
\r
46 ConnectTouchSignal(ICON_SQUAT);
\r
47 ConnectTouchSignal(ICON_JUMPING_JACK);
\r
48 ConnectTouchSignal(ICON_LUNGE);
\r
52 void OnKey(const Dali::KeyEvent& e)
\r
54 // This is a temporary hack for now to manually handle these key events.
\r
55 // We should links them to the animations automatically when loading the scene.
\r
57 switch(e.GetKeyCode())
\r
61 PlaySceneAnimation(ANIMATION_IDLE);
\r
66 PlaySceneAnimation(ANIMATION_IDLE_TO_SQUAT);
\r
71 PlaySceneAnimation(ANIMATION_IDLE_TO_JUMPING_JACK);
\r
76 PlaySceneAnimation(ANIMATION_IDLE_TO_LUNGE);
\r
85 bool PlaySceneAnimation(unsigned int animationIndex)
\r
87 if(mSceneLoader && mSceneLoader->mScene)
\r
89 if(mSceneLoader->mCurrentAnimation &&
\r
90 mCurrentAnimationIndex != ANIMATION_IDLE &&
\r
91 mSceneLoader->mCurrentAnimation.GetState() != Dali::Animation::STOPPED)
\r
96 auto root = mSceneLoader->mScene;
\r
97 auto getActor = [&root](const std::string& name) {
\r
98 return root.FindChildByName(name);
\r
101 if(mSceneLoader->mSceneAnimations.size() > animationIndex)
\r
103 mCurrentAnimationIndex = animationIndex;
\r
104 mSceneLoader->mCurrentAnimation = mSceneLoader->mSceneAnimations[animationIndex].ReAnimate(getActor);
\r
105 mSceneLoader->mCurrentAnimation.FinishedSignal().Connect(this, &SceneLoaderExtension::OnAnimationFinished);
\r
106 mSceneLoader->mCurrentAnimation.Play();
\r
113 void ConnectTouchSignal(const std::string actorName)
\r
115 if(mSceneLoader && mSceneLoader->mScene)
\r
117 auto actor = mSceneLoader->mScene.FindChildByName(actorName);
\r
120 actor.TouchedSignal().Connect(this, &SceneLoaderExtension::OnTouch);
\r
125 void OnAnimationFinished(Dali::Animation& source)
\r
127 switch(mCurrentAnimationIndex)
\r
129 case ANIMATION_IDLE_TO_SQUAT:
\r
131 PlaySceneAnimation(ANIMATION_SQUAT_TO_IDLE);
\r
134 case ANIMATION_IDLE_TO_JUMPING_JACK:
\r
136 PlaySceneAnimation(ANIMATION_JUMPING_JACK);
\r
139 case ANIMATION_JUMPING_JACK:
\r
141 PlaySceneAnimation(ANIMATION_JUMPING_JACK_TO_IDLE);
\r
144 case ANIMATION_IDLE_TO_LUNGE:
\r
146 PlaySceneAnimation(ANIMATION_LUNGE);
\r
149 case ANIMATION_LUNGE:
\r
151 PlaySceneAnimation(ANIMATION_LUNGE_TO_IDLE);
\r
156 mCurrentAnimationIndex = ANIMATION_IDLE;
\r
163 bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& touch)
\r
165 bool processed = false;
\r
167 if(touch.GetPointCount() > 0)
\r
169 if(touch.GetState(0) == Dali::PointState::DOWN)
\r
171 auto actorName = actor.GetProperty<std::string>(Dali::Actor::Property::NAME);
\r
173 if(ICON_IDLE == actorName)
\r
175 processed = PlaySceneAnimation(ANIMATION_IDLE);
\r
177 else if(ICON_SQUAT == actorName)
\r
179 processed = PlaySceneAnimation(ANIMATION_IDLE_TO_SQUAT);
\r
181 else if(ICON_JUMPING_JACK == actorName)
\r
183 processed = PlaySceneAnimation(ANIMATION_IDLE_TO_JUMPING_JACK);
\r
185 else if(ICON_LUNGE == actorName)
\r
187 processed = PlaySceneAnimation(ANIMATION_IDLE_TO_LUNGE);
\r
195 static constexpr unsigned int KEY_ONE = 10;
\r
196 static constexpr unsigned int KEY_TWO = 11;
\r
197 static constexpr unsigned int KEY_THREE = 12;
\r
198 static constexpr unsigned int KEY_FOUR = 13;
\r
201 static constexpr unsigned int ANIMATION_IDLE = 0;
\r
203 static constexpr unsigned int ANIMATION_IDLE_TO_SQUAT = 3;
\r
204 static constexpr unsigned int ANIMATION_SQUAT_TO_IDLE = 15;
\r
205 // JumpingJack animation
\r
206 static constexpr unsigned int ANIMATION_IDLE_TO_JUMPING_JACK = 1;
\r
207 static constexpr unsigned int ANIMATION_JUMPING_JACK = 5;
\r
208 static constexpr unsigned int ANIMATION_JUMPING_JACK_TO_IDLE = 6;
\r
210 static constexpr unsigned int ANIMATION_IDLE_TO_LUNGE = 2;
\r
211 static constexpr unsigned int ANIMATION_LUNGE = 9;
\r
212 static constexpr unsigned int ANIMATION_LUNGE_TO_IDLE = 10;
\r
214 inline static const std::string ICON_IDLE = "Idle";
\r
215 inline static const std::string ICON_SQUAT = "Squat";
\r
216 inline static const std::string ICON_JUMPING_JACK = "JumpingJack";
\r
217 inline static const std::string ICON_LUNGE = "Lunge";
\r
219 SceneLoaderExample* mSceneLoader;
\r
220 unsigned int mCurrentAnimationIndex;
\r