DALi Version 2.2.11
[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 private:
233   std::string                    mModelUrl;
234   std::string                    mResourceDirectoryUrl;
235   Dali::Actor                    mModelRoot;
236   std::vector<AnimationData>     mAnimations;
237   std::vector<WeakHandle<Actor>> mRenderableActors;
238   WeakHandle<Scene3D::SceneView> mParentSceneView;
239
240   // Asynchronous loading variable
241   ModelLoadTaskPtr          mModelLoadTask;
242   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
243   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
244
245   std::string mDiffuseIblUrl;
246   std::string mSpecularIblUrl;
247
248   // TODO: This default texture can be removed after 3D Resource Cache is added.
249   Dali::Texture mDefaultSpecularTexture;
250   Dali::Texture mDefaultDiffuseTexture;
251   Dali::Texture mSceneSpecularTexture;
252   Dali::Texture mSceneDiffuseTexture;
253   Dali::Texture mSpecularTexture;
254   Dali::Texture mDiffuseTexture;
255   Vector3       mNaturalSize;
256   Vector3       mModelPivot;
257   float         mSceneIblScaleFactor;
258   float         mIblScaleFactor;
259   bool          mModelChildrenSensitive;
260   bool          mModelChildrenFocusable;
261   bool          mModelResourceReady;
262   bool          mIblDiffuseResourceReady;
263   bool          mIblSpecularResourceReady;
264   bool          mIblDiffuseDirty;
265   bool          mIblSpecularDirty;
266 };
267
268 } // namespace Internal
269
270 // Helpers for public-api forwarding methods
271 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
272 {
273   DALI_ASSERT_ALWAYS(obj);
274   Dali::RefObject& handle = obj.GetImplementation();
275   return static_cast<Dali::Scene3D::Internal::Model&>(handle);
276 }
277
278 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
279 {
280   DALI_ASSERT_ALWAYS(obj);
281   const Dali::RefObject& handle = obj.GetImplementation();
282   return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
283 }
284
285 } // namespace Scene3D
286
287 } // namespace Dali
288
289 #endif // DALI_SCENE3D_INTERNAL_MODEL_H