Merge "Refactoring model-impl.cpp" into devel/master
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / model / model-impl.h
1 #ifndef DALI_SCENE3D_INTERNAL_MODEL_H
2 #define DALI_SCENE3D_INTERNAL_MODEL_H
3
4 /*
5  * Copyright (c) 2022 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali-toolkit/public-api/controls/control-impl.h>
23 #include <dali/public-api/actors/layer.h>
24 #include <dali/public-api/animation/animation.h>
25 #include <dali/public-api/object/weak-handle.h>
26 #include <dali/public-api/rendering/texture.h>
27
28 // INTERNAL INCLUDES
29 #include <dali-scene3d/internal/common/environment-map-load-task.h>
30 #include <dali-scene3d/internal/common/image-based-light-observer.h>
31 #include <dali-scene3d/internal/common/model-load-task.h>
32 #include <dali-scene3d/public-api/controls/model/model.h>
33 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
34
35 namespace Dali
36 {
37 namespace Scene3D
38 {
39 class Model;
40
41 namespace Internal
42 {
43 /**
44  * @brief Impl class for Model.
45  */
46 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
47 {
48 public:
49   using AnimationData = std::pair<std::string, Dali::Animation>;
50
51   /**
52    * @brief Creates a new Model.
53    *
54    * @return A public handle to the newly allocated Model.
55    */
56   static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
57
58   /**
59    * @copydoc Model::GetModelRoot()
60    */
61   const Actor GetModelRoot() const;
62
63   /**
64    * @copydoc Model::SetChildrenSensitive()
65    */
66   void SetChildrenSensitive(bool enable);
67
68   /**
69    * @copydoc Model::GetChildrenSensitive()
70    */
71   bool GetChildrenSensitive() const;
72
73   /**
74    * @copydoc Model::SetChildrenFocusable()
75    */
76   void SetChildrenFocusable(bool enable);
77
78   /**
79    * @copydoc Model::GetChildrenFocusable()
80    */
81   bool GetChildrenFocusable() const;
82
83   /**
84    * @copydoc Model::SetImageBasedLightSource()
85    */
86   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
87
88   /**
89    * @copydoc Model::SetImageBasedLightTexture()
90    */
91   void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor);
92
93   /**
94    * @copydoc Model::SetImageBasedLightScaleFactor()
95    */
96   void SetImageBasedLightScaleFactor(float scaleFactor);
97
98   /**
99    * @copydoc Model::GetImageBasedLightScaleFactor()
100    */
101   float GetImageBasedLightScaleFactor() const;
102
103   /**
104    * @copydoc Model::GetAnimationCount()
105    */
106   uint32_t GetAnimationCount() const;
107
108   /**
109    * @copydoc Model::GetAnimation()
110    */
111   Dali::Animation GetAnimation(uint32_t index) const;
112
113   /**
114    * @copydoc Model::GetAnimation()
115    */
116   Dali::Animation GetAnimation(const std::string& name) const;
117
118 protected:
119   /**
120    * @brief Constructs a new Model.
121    */
122   Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
123
124   /**
125    * A reference counted object may only be deleted by calling Unreference()
126    */
127   virtual ~Model();
128
129 private:
130   /**
131    * @copydoc Toolkit::Control::OnInitialize
132    */
133   void OnInitialize();
134
135   /**
136    * @copydoc CustomActorImpl::OnSceneConnection()
137    */
138   void OnSceneConnection(int depth) override;
139
140   /**
141    * @copydoc CustomActorImpl::OnSceneDisconnection()
142    */
143   void OnSceneDisconnection() override;
144
145   /**
146    * @copydoc Toolkit::Control::GetNaturalSize
147    */
148   Vector3 GetNaturalSize() override;
149
150   /**
151    * @copydoc Toolkit::Control::GetHeightForWidth()
152    */
153   float GetHeightForWidth(float width) override;
154
155   /**
156    * @copydoc Toolkit::Control::GetWidthForHeight()
157    */
158   float GetWidthForHeight(float height) override;
159
160   /**
161    * @copydoc Toolkit::Control::OnRelayout()
162    */
163   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
164
165   /**
166    * @copydoc Toolkit::Control::IsResourceReady()
167    */
168   bool IsResourceReady() const override;
169
170   /**
171    * @brief Scales the model to fit the control or to return to original size.
172    */
173   void ScaleModel();
174
175   /**
176    * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
177    */
178   void FitModelPosition();
179
180   /**
181    * @brief Changes IBL information of the input node.
182    */
183   void CollectRenderableActor(Actor actor);
184
185   /**
186    * @brief Changes IBL textures of the input node.
187    */
188   void UpdateImageBasedLightTexture();
189
190   /**
191    * @brief Changes IBL scale factor of the input node.
192    */
193   void UpdateImageBasedLightScaleFactor();
194
195 public: // Overrides ImageBasedLightObserver Methods.
196   /**
197    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
198    */
199   void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor) override;
200
201   /**
202    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
203    */
204   void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
205
206 private:
207   /**
208    * @brief Asynchronously model loading finished.
209    */
210   void OnModelLoadComplete();
211
212   /**
213    * @brief Asynchronously ibl diffusel image loading finished.
214    */
215   void OnIblDiffuseLoadComplete();
216
217   /**
218    * @brief Asynchronously ibl specular image loading finished.
219    */
220   void OnIblSpecularLoadComplete();
221
222   /**
223    * @brief Asynchronously ibl loading finished.
224    */
225   void OnIblLoadComplete();
226
227   /**
228    * @brief Reset Resource loading tasks.
229    */
230   void ResetResourceTasks();
231
232   /**
233    * @brief Reset a Resource loading task.
234    */
235   void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
236
237   /**
238    * @brief Request to load a Ibl texture asynchronously
239    */
240   void RequestLoadIblTexture(EnvironmentMapLoadTaskPtr asyncLoadTask, const std::string& url);
241
242   /**
243    * @brief Notify Resource Ready signal.
244    */
245   void NotifyResourceReady();
246
247   /**
248    * @brief Create Model from loaded SceneDefinition.
249    */
250   void CreateModel();
251
252   /**
253    * @brief Create Dali::Animation from loaded AnimationDefinitions.
254    */
255   void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
256
257 private:
258   std::string                    mModelUrl;
259   std::string                    mResourceDirectoryUrl;
260   Dali::Actor                    mModelRoot;
261   std::vector<AnimationData>     mAnimations;
262   std::vector<WeakHandle<Actor>> mRenderableActors;
263   WeakHandle<Scene3D::SceneView> mParentSceneView;
264
265   // Asynchronous loading variable
266   ModelLoadTaskPtr          mModelLoadTask;
267   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
268   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
269
270   std::string mDiffuseIblUrl;
271   std::string mSpecularIblUrl;
272
273   // TODO: This default texture can be removed after 3D Resource Cache is added.
274   Dali::Texture mDefaultSpecularTexture;
275   Dali::Texture mDefaultDiffuseTexture;
276   Dali::Texture mSceneSpecularTexture;
277   Dali::Texture mSceneDiffuseTexture;
278   Dali::Texture mSpecularTexture;
279   Dali::Texture mDiffuseTexture;
280   Vector3       mNaturalSize;
281   Vector3       mModelPivot;
282   float         mSceneIblScaleFactor;
283   float         mIblScaleFactor;
284   bool          mModelChildrenSensitive;
285   bool          mModelChildrenFocusable;
286   bool          mModelResourceReady;
287   bool          mIblDiffuseResourceReady;
288   bool          mIblSpecularResourceReady;
289   bool          mIblDiffuseDirty;
290   bool          mIblSpecularDirty;
291 };
292
293 } // namespace Internal
294
295 // Helpers for public-api forwarding methods
296 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
297 {
298   DALI_ASSERT_ALWAYS(obj);
299   Dali::RefObject& handle = obj.GetImplementation();
300   return static_cast<Dali::Scene3D::Internal::Model&>(handle);
301 }
302
303 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
304 {
305   DALI_ASSERT_ALWAYS(obj);
306   const Dali::RefObject& handle = obj.GetImplementation();
307   return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
308 }
309
310 } // namespace Scene3D
311
312 } // namespace Dali
313
314 #endif // DALI_SCENE3D_INTERNAL_MODEL_H