Use c-style string when webview loads contents.
[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) 2023 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/camera-actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/animation/animation.h>
26 #include <dali/public-api/object/weak-handle.h>
27 #include <dali/public-api/rendering/texture.h>
28
29 // INTERNAL INCLUDES
30 #include <dali-scene3d/internal/common/environment-map-load-task.h>
31 #include <dali-scene3d/internal/common/image-based-light-observer.h>
32 #include <dali-scene3d/internal/common/model-load-task.h>
33 #include <dali-scene3d/public-api/controls/model/model.h>
34 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
35 #include <dali-scene3d/public-api/loader/load-result.h>
36
37 namespace Dali
38 {
39 namespace Scene3D
40 {
41 class Model;
42
43 namespace Internal
44 {
45 /**
46  * @brief Impl class for Model.
47  */
48 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
49 {
50 public:
51   using AnimationData = std::pair<std::string, Dali::Animation>;
52   using CameraData    = Loader::CameraParameters;
53
54   /**
55    * @copydoc Model::New()
56    */
57   static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
58
59   /**
60    * @copydoc Model::GetModelRoot()
61    */
62   const Actor GetModelRoot() const;
63
64   /**
65    * @copydoc Model::SetChildrenSensitive()
66    */
67   void SetChildrenSensitive(bool enable);
68
69   /**
70    * @copydoc Model::GetChildrenSensitive()
71    */
72   bool GetChildrenSensitive() const;
73
74   /**
75    * @copydoc Model::SetChildrenFocusable()
76    */
77   void SetChildrenFocusable(bool enable);
78
79   /**
80    * @copydoc Model::GetChildrenFocusable()
81    */
82   bool GetChildrenFocusable() const;
83
84   /**
85    * @copydoc Model::SetImageBasedLightSource()
86    */
87   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
88
89   /**
90    * @copydoc Model::SetImageBasedLightScaleFactor()
91    */
92   void SetImageBasedLightScaleFactor(float scaleFactor);
93
94   /**
95    * @copydoc Model::GetImageBasedLightScaleFactor()
96    */
97   float GetImageBasedLightScaleFactor() const;
98
99   /**
100    * @copydoc Model::GetAnimationCount()
101    */
102   uint32_t GetAnimationCount() const;
103
104   /**
105    * @copydoc Model::GetAnimation()
106    */
107   Dali::Animation GetAnimation(uint32_t index) const;
108
109   /**
110    * @copydoc Model::GetAnimation()
111    */
112   Dali::Animation GetAnimation(const std::string& name) const;
113
114   /**
115    * @copydoc Model::GetCameraCount()
116    */
117   uint32_t GetCameraCount() const;
118
119   /**
120    * @copydoc Model::GenerateCamera()
121    */
122   Dali::CameraActor GenerateCamera(uint32_t index) const;
123
124   /**
125    * @copydoc Model::ApplyCamera()
126    */
127   bool ApplyCamera(uint32_t index, Dali::CameraActor camera) const;
128
129 protected:
130   /**
131    * @brief Constructs a new Model.
132    * @param[in] modelUrl model file path.(e.g., glTF, and DLI).
133    * @param[in] resourceDirectoryUrl resource file path that includes binary, image etc.
134    */
135   Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
136
137   /**
138    * A reference counted object may only be deleted by calling Unreference()
139    */
140   virtual ~Model();
141
142 private:
143   /**
144    * @copydoc Toolkit::Control::OnInitialize
145    */
146   void OnInitialize();
147
148   /**
149    * @copydoc CustomActorImpl::OnSceneConnection()
150    */
151   void OnSceneConnection(int depth) override;
152
153   /**
154    * @copydoc CustomActorImpl::OnSceneDisconnection()
155    */
156   void OnSceneDisconnection() override;
157
158   /**
159    * @copydoc Toolkit::Control::GetNaturalSize
160    */
161   Vector3 GetNaturalSize() override;
162
163   /**
164    * @copydoc Toolkit::Control::GetHeightForWidth()
165    */
166   float GetHeightForWidth(float width) override;
167
168   /**
169    * @copydoc Toolkit::Control::GetWidthForHeight()
170    */
171   float GetWidthForHeight(float height) override;
172
173   /**
174    * @copydoc Toolkit::Control::OnRelayout()
175    */
176   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
177
178   /**
179    * @copydoc Toolkit::Control::IsResourceReady()
180    */
181   bool IsResourceReady() const override;
182
183 private:
184   /**
185    * @brief Scales the model to fit the control or to return to original size.
186    */
187   void ScaleModel();
188
189   /**
190    * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
191    */
192   void FitModelPosition();
193
194   /**
195    * @brief Changes IBL information of the input node.
196    */
197   void CollectRenderableActor(Actor actor);
198
199   /**
200    * @brief Changes IBL textures of the input node.
201    */
202   void UpdateImageBasedLightTexture();
203
204   /**
205    * @brief Changes IBL scale factor of the input node.
206    */
207   void UpdateImageBasedLightScaleFactor();
208
209   /**
210    * @brief Apply self transform into inputed camera.
211    * Inputed camera must be configured by CameraParameter. Mean, inputed camera coordinate depend on Model.
212    * After this API finished, CameraActor coordinate system converted as DALi coordinate system.
213    *
214    * @param[in,out] camera CameraActor who need to apply model itself's transform
215    */
216   void ApplyCameraTransform(Dali::CameraActor camera) const;
217
218 public: // Overrides ImageBasedLightObserver Methods.
219   /**
220    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
221    */
222   void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor, uint32_t specularMipmapLevels) override;
223
224   /**
225    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
226    */
227   void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
228
229 private:
230   /**
231    * @brief Asynchronously model loading finished.
232    */
233   void OnModelLoadComplete();
234
235   /**
236    * @brief Asynchronously ibl diffusel image loading finished.
237    */
238   void OnIblDiffuseLoadComplete();
239
240   /**
241    * @brief Asynchronously ibl specular image loading finished.
242    */
243   void OnIblSpecularLoadComplete();
244
245   /**
246    * @brief Asynchronously ibl loading finished.
247    */
248   void OnIblLoadComplete();
249
250   /**
251    * @brief Reset Resource loading tasks.
252    */
253   void ResetResourceTasks();
254
255   /**
256    * @brief Reset a Resource loading task.
257    */
258   void ResetResourceTask(IntrusivePtr<AsyncTask> asyncTask);
259
260   /**
261    * @brief Notify Resource Ready signal.
262    */
263   void NotifyResourceReady();
264
265   /**
266    * @brief Create Model from loaded SceneDefinition.
267    */
268   void CreateModel();
269
270   /**
271    * @brief Create Dali::Animation from loaded AnimationDefinitions.
272    */
273   void CreateAnimations(Dali::Scene3D::Loader::SceneDefinition& scene);
274
275   /**
276    * @brief Reset CameraData from loaded CameraParameters.
277    */
278   void ResetCameraParameters();
279
280 private:
281   std::string                    mModelUrl;
282   std::string                    mResourceDirectoryUrl;
283   Dali::Actor                    mModelRoot;
284   std::vector<AnimationData>     mAnimations;
285   std::vector<CameraData>        mCameraParameters;
286   std::vector<WeakHandle<Actor>> mRenderableActors;
287   WeakHandle<Scene3D::SceneView> mParentSceneView;
288
289   // Asynchronous loading variable
290   ModelLoadTaskPtr          mModelLoadTask;
291   EnvironmentMapLoadTaskPtr mIblDiffuseLoadTask;
292   EnvironmentMapLoadTaskPtr mIblSpecularLoadTask;
293
294   std::string mDiffuseIblUrl;
295   std::string mSpecularIblUrl;
296
297   // TODO: This default texture can be removed after 3D Resource Cache is added.
298   Dali::Texture mDefaultSpecularTexture;
299   Dali::Texture mDefaultDiffuseTexture;
300   Dali::Texture mSceneSpecularTexture;
301   Dali::Texture mSceneDiffuseTexture;
302   Dali::Texture mSpecularTexture;
303   Dali::Texture mDiffuseTexture;
304   Vector3       mNaturalSize;
305   Vector3       mModelPivot;
306   float         mSceneIblScaleFactor;
307   float         mIblScaleFactor;
308   uint32_t      mSceneSpecularMipmapLevels;
309   uint32_t      mSpecularMipmapLevels;
310   bool          mModelChildrenSensitive;
311   bool          mModelChildrenFocusable;
312   bool          mModelResourceReady;
313   bool          mIblDiffuseResourceReady;
314   bool          mIblSpecularResourceReady;
315   bool          mIblDiffuseDirty;
316   bool          mIblSpecularDirty;
317 };
318
319 } // namespace Internal
320
321 // Helpers for public-api forwarding methods
322 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
323 {
324   DALI_ASSERT_ALWAYS(obj);
325   Dali::RefObject& handle = obj.GetImplementation();
326   return static_cast<Dali::Scene3D::Internal::Model&>(handle);
327 }
328
329 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
330 {
331   DALI_ASSERT_ALWAYS(obj);
332   const Dali::RefObject& handle = obj.GetImplementation();
333   return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
334 }
335
336 } // namespace Scene3D
337
338 } // namespace Dali
339
340 #endif // DALI_SCENE3D_INTERNAL_MODEL_H