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 Scene3DExtension : public Dali::ConnectionTracker
\r
26 : mSceneLoader(nullptr),
\r
27 mCurrentAnimationIndex(ANIMATION_IDLE)
\r
31 ~Scene3DExtension() = default; // Nothing to do in destructor
\r
33 void SetSceneLoader(Scene3DExample* scene3D)
\r
35 mSceneLoader = scene3D;
\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 if(mSceneLoader->mSceneAnimations.size() > animationIndex)
\r
98 mCurrentAnimationIndex = animationIndex;
\r
99 mSceneLoader->mCurrentAnimation = mSceneLoader->mSceneAnimations[animationIndex];
\r
100 if(mSceneLoader->mCurrentAnimation.FinishedSignal().GetConnectionCount() == 0)
\r
102 mSceneLoader->mCurrentAnimation.FinishedSignal().Connect(this, &Scene3DExtension::OnAnimationFinished);
\r
104 mSceneLoader->mCurrentAnimation.Play();
\r
111 void ConnectTouchSignal(const std::string actorName)
\r
113 if(mSceneLoader && mSceneLoader->mScene)
\r
115 auto actor = mSceneLoader->mScene.FindChildByName(actorName);
\r
118 actor.TouchedSignal().Connect(this, &Scene3DExtension::OnTouch);
\r
123 void OnAnimationFinished(Dali::Animation& source)
\r
125 switch(mCurrentAnimationIndex)
\r
127 case ANIMATION_IDLE_TO_SQUAT:
\r
129 PlaySceneAnimation(ANIMATION_SQUAT_TO_IDLE);
\r
132 case ANIMATION_IDLE_TO_JUMPING_JACK:
\r
134 PlaySceneAnimation(ANIMATION_JUMPING_JACK);
\r
137 case ANIMATION_JUMPING_JACK:
\r
139 PlaySceneAnimation(ANIMATION_JUMPING_JACK_TO_IDLE);
\r
142 case ANIMATION_IDLE_TO_LUNGE:
\r
144 PlaySceneAnimation(ANIMATION_LUNGE);
\r
147 case ANIMATION_LUNGE:
\r
149 PlaySceneAnimation(ANIMATION_LUNGE_TO_IDLE);
\r
154 mCurrentAnimationIndex = ANIMATION_IDLE;
\r
161 bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& touch)
\r
163 bool processed = false;
\r
165 if(touch.GetPointCount() > 0)
\r
167 if(touch.GetState(0) == Dali::PointState::DOWN)
\r
169 auto actorName = actor.GetProperty<std::string>(Dali::Actor::Property::NAME);
\r
171 if(ICON_IDLE == actorName)
\r
173 processed = PlaySceneAnimation(ANIMATION_IDLE);
\r
175 else if(ICON_SQUAT == actorName)
\r
177 processed = PlaySceneAnimation(ANIMATION_IDLE_TO_SQUAT);
\r
179 else if(ICON_JUMPING_JACK == actorName)
\r
181 processed = PlaySceneAnimation(ANIMATION_IDLE_TO_JUMPING_JACK);
\r
183 else if(ICON_LUNGE == actorName)
\r
185 processed = PlaySceneAnimation(ANIMATION_IDLE_TO_LUNGE);
\r
193 static constexpr unsigned int KEY_ONE = 10;
\r
194 static constexpr unsigned int KEY_TWO = 11;
\r
195 static constexpr unsigned int KEY_THREE = 12;
\r
196 static constexpr unsigned int KEY_FOUR = 13;
\r
199 static constexpr unsigned int ANIMATION_IDLE = 0;
\r
201 static constexpr unsigned int ANIMATION_IDLE_TO_SQUAT = 3;
\r
202 static constexpr unsigned int ANIMATION_SQUAT_TO_IDLE = 15;
\r
203 // JumpingJack animation
\r
204 static constexpr unsigned int ANIMATION_IDLE_TO_JUMPING_JACK = 1;
\r
205 static constexpr unsigned int ANIMATION_JUMPING_JACK = 5;
\r
206 static constexpr unsigned int ANIMATION_JUMPING_JACK_TO_IDLE = 6;
\r
208 static constexpr unsigned int ANIMATION_IDLE_TO_LUNGE = 2;
\r
209 static constexpr unsigned int ANIMATION_LUNGE = 9;
\r
210 static constexpr unsigned int ANIMATION_LUNGE_TO_IDLE = 10;
\r
212 inline static const std::string ICON_IDLE = "Idle";
\r
213 inline static const std::string ICON_SQUAT = "Squat";
\r
214 inline static const std::string ICON_JUMPING_JACK = "JumpingJack";
\r
215 inline static const std::string ICON_LUNGE = "Lunge";
\r
217 Scene3DExample* mSceneLoader;
\r
218 unsigned int mCurrentAnimationIndex;
\r