Change to use ModelLoader instead of Gltf2Loader and DliLoader
[platform/core/uifw/dali-demo.git] / examples / scene3d / scene3d-extension.h
1 /*\r
2  * Copyright (c) 2022 Samsung Electronics Co., Ltd.\r
3  *\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
7  *\r
8  * http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  *\r
16  */\r
17 \r
18 #include <dali-toolkit/dali-toolkit.h>\r
19 #include <dali/dali.h>\r
20 #include <cstring>\r
21 \r
22 class Scene3DExtension : public Dali::ConnectionTracker\r
23 {\r
24 public:\r
25   Scene3DExtension()\r
26   : mSceneLoader(nullptr),\r
27     mCurrentAnimationIndex(ANIMATION_IDLE)\r
28   {\r
29   }\r
30 \r
31   ~Scene3DExtension() = default; // Nothing to do in destructor\r
32 \r
33   void SetSceneLoader(Scene3DExample* scene3D)\r
34   {\r
35     mSceneLoader = scene3D;\r
36   }\r
37 \r
38   void ConnectTouchSignals()\r
39   {\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
42 \r
43     if(mSceneLoader)\r
44     {\r
45       ConnectTouchSignal(ICON_IDLE);\r
46       ConnectTouchSignal(ICON_SQUAT);\r
47       ConnectTouchSignal(ICON_JUMPING_JACK);\r
48       ConnectTouchSignal(ICON_LUNGE);\r
49     }\r
50   }\r
51 \r
52   void OnKey(const Dali::KeyEvent& e)\r
53   {\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
56 \r
57     switch(e.GetKeyCode())\r
58     {\r
59       case KEY_ONE:\r
60       {\r
61         PlaySceneAnimation(ANIMATION_IDLE);\r
62         break;\r
63       }\r
64       case KEY_TWO:\r
65       {\r
66         PlaySceneAnimation(ANIMATION_IDLE_TO_SQUAT);\r
67         break;\r
68       }\r
69       case KEY_THREE:\r
70       {\r
71         PlaySceneAnimation(ANIMATION_IDLE_TO_JUMPING_JACK);\r
72         break;\r
73       }\r
74       case KEY_FOUR:\r
75       {\r
76         PlaySceneAnimation(ANIMATION_IDLE_TO_LUNGE);\r
77         break;\r
78       }\r
79       default:\r
80         break;\r
81     }\r
82   }\r
83 \r
84 private:\r
85   bool PlaySceneAnimation(unsigned int animationIndex)\r
86   {\r
87     if(mSceneLoader && mSceneLoader->mScene)\r
88     {\r
89       if(mSceneLoader->mCurrentAnimation &&\r
90          mCurrentAnimationIndex != ANIMATION_IDLE &&\r
91          mSceneLoader->mCurrentAnimation.GetState() != Dali::Animation::STOPPED)\r
92       {\r
93         return false;\r
94       }\r
95 \r
96       if(mSceneLoader->mSceneAnimations.size() > animationIndex)\r
97       {\r
98         mCurrentAnimationIndex          = animationIndex;\r
99         mSceneLoader->mCurrentAnimation = mSceneLoader->mSceneAnimations[animationIndex];\r
100         if(mSceneLoader->mCurrentAnimation.FinishedSignal().GetConnectionCount() == 0)\r
101         {\r
102           mSceneLoader->mCurrentAnimation.FinishedSignal().Connect(this, &Scene3DExtension::OnAnimationFinished);\r
103         }\r
104         mSceneLoader->mCurrentAnimation.Play();\r
105       }\r
106     }\r
107 \r
108     return true;\r
109   }\r
110 \r
111   void ConnectTouchSignal(const std::string actorName)\r
112   {\r
113     if(mSceneLoader && mSceneLoader->mScene)\r
114     {\r
115       auto actor = mSceneLoader->mScene.FindChildByName(actorName);\r
116       if(actor)\r
117       {\r
118         actor.TouchedSignal().Connect(this, &Scene3DExtension::OnTouch);\r
119       }\r
120     }\r
121   }\r
122 \r
123   void OnAnimationFinished(Dali::Animation& source)\r
124   {\r
125     switch(mCurrentAnimationIndex)\r
126     {\r
127       case ANIMATION_IDLE_TO_SQUAT:\r
128       {\r
129         PlaySceneAnimation(ANIMATION_SQUAT_TO_IDLE);\r
130         break;\r
131       }\r
132       case ANIMATION_IDLE_TO_JUMPING_JACK:\r
133       {\r
134         PlaySceneAnimation(ANIMATION_JUMPING_JACK);\r
135         break;\r
136       }\r
137       case ANIMATION_JUMPING_JACK:\r
138       {\r
139         PlaySceneAnimation(ANIMATION_JUMPING_JACK_TO_IDLE);\r
140         break;\r
141       }\r
142       case ANIMATION_IDLE_TO_LUNGE:\r
143       {\r
144         PlaySceneAnimation(ANIMATION_LUNGE);\r
145         break;\r
146       }\r
147       case ANIMATION_LUNGE:\r
148       {\r
149         PlaySceneAnimation(ANIMATION_LUNGE_TO_IDLE);\r
150         break;\r
151       }\r
152       default:\r
153       {\r
154         mCurrentAnimationIndex = ANIMATION_IDLE;\r
155         break;\r
156       }\r
157       break;\r
158     }\r
159   }\r
160 \r
161   bool OnTouch(Dali::Actor actor, const Dali::TouchEvent& touch)\r
162   {\r
163     bool processed = false;\r
164 \r
165     if(touch.GetPointCount() > 0)\r
166     {\r
167       if(touch.GetState(0) == Dali::PointState::DOWN)\r
168       {\r
169         auto actorName = actor.GetProperty<std::string>(Dali::Actor::Property::NAME);\r
170 \r
171         if(ICON_IDLE == actorName)\r
172         {\r
173           processed = PlaySceneAnimation(ANIMATION_IDLE);\r
174         }\r
175         else if(ICON_SQUAT == actorName)\r
176         {\r
177           processed = PlaySceneAnimation(ANIMATION_IDLE_TO_SQUAT);\r
178         }\r
179         else if(ICON_JUMPING_JACK == actorName)\r
180         {\r
181           processed = PlaySceneAnimation(ANIMATION_IDLE_TO_JUMPING_JACK);\r
182         }\r
183         else if(ICON_LUNGE == actorName)\r
184         {\r
185           processed = PlaySceneAnimation(ANIMATION_IDLE_TO_LUNGE);\r
186         }\r
187       }\r
188     }\r
189     return processed;\r
190   }\r
191 \r
192 private:\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
197 \r
198   // Idle animation\r
199   static constexpr unsigned int ANIMATION_IDLE = 0;\r
200   // Squat animation\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
207   // Lunge animation\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
211 \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
216 \r
217   Scene3DExample* mSceneLoader;\r
218   unsigned int        mCurrentAnimationIndex;\r
219 };\r