7bb4889b14f7760b3d09c5d14ceb0c609652c180
[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/image-based-light-observer.h>
30 #include <dali-scene3d/public-api/controls/model/model.h>
31 #include <dali-scene3d/public-api/controls/scene-view/scene-view.h>
32
33 namespace Dali
34 {
35 namespace Scene3D
36 {
37 class Model;
38
39 namespace Internal
40 {
41 /**
42  * @brief Impl class for Model.
43  */
44 class Model : public Dali::Toolkit::Internal::Control, public ImageBasedLightObserver
45 {
46 public:
47   using AnimationData = std::pair<std::string, Dali::Animation>;
48
49   /**
50    * @brief Creates a new Model.
51    *
52    * @return A public handle to the newly allocated Model.
53    */
54   static Dali::Scene3D::Model New(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
55
56   /**
57    * @copydoc Model::GetModelRoot()
58    */
59   const Actor GetModelRoot() const;
60
61   /**
62    * @copydoc Model::SetChildrenSensitive()
63    */
64   void SetChildrenSensitive(bool enable);
65
66   /**
67    * @copydoc Model::GetChildrenSensitive()
68    */
69   bool GetChildrenSensitive() const;
70
71   /**
72    * @copydoc Model::SetChildrenFocusable()
73    */
74   void SetChildrenFocusable(bool enable);
75
76   /**
77    * @copydoc Model::GetChildrenFocusable()
78    */
79   bool GetChildrenFocusable() const;
80
81   /**
82    * @copydoc Model::SetImageBasedLightSource()
83    */
84   void SetImageBasedLightSource(const std::string& diffuseUrl, const std::string& specularUrl, float scaleFactor);
85
86   /**
87    * @copydoc Model::SetImageBasedLightTexture()
88    */
89   void SetImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor);
90
91   /**
92    * @copydoc Model::SetImageBasedLightScaleFactor()
93    */
94   void SetImageBasedLightScaleFactor(float scaleFactor);
95
96   /**
97    * @copydoc Model::GetImageBasedLightScaleFactor()
98    */
99   float GetImageBasedLightScaleFactor() const;
100
101   /**
102    * @copydoc Model::GetAnimationCount()
103    */
104   uint32_t GetAnimationCount() const;
105
106   /**
107    * @copydoc Model::GetAnimation()
108    */
109   Dali::Animation GetAnimation(uint32_t index) const;
110
111   /**
112    * @copydoc Model::GetAnimation()
113    */
114   Dali::Animation GetAnimation(const std::string& name) const;
115
116 protected:
117   /**
118    * @brief Constructs a new Model.
119    */
120   Model(const std::string& modelUrl, const std::string& resourceDirectoryUrl);
121
122   /**
123    * A reference counted object may only be deleted by calling Unreference()
124    */
125   virtual ~Model();
126
127 private:
128   /**
129    * @copydoc Toolkit::Control::OnInitialize
130    */
131   void OnInitialize();
132
133   /**
134    * @copydoc CustomActorImpl::OnSceneConnection()
135    */
136   void OnSceneConnection(int depth) override;
137
138   /**
139    * @copydoc CustomActorImpl::OnSceneDisconnection()
140    */
141   void OnSceneDisconnection() override;
142
143   /**
144    * @copydoc Toolkit::Control::GetNaturalSize
145    */
146   Vector3 GetNaturalSize() override;
147
148   /**
149    * @copydoc Toolkit::Control::GetHeightForWidth()
150    */
151   float GetHeightForWidth(float width) override;
152
153   /**
154    * @copydoc Toolkit::Control::GetWidthForHeight()
155    */
156   float GetWidthForHeight(float height) override;
157
158   /**
159    * @copydoc Toolkit::Control::OnRelayout()
160    */
161   void OnRelayout(const Vector2& size, RelayoutContainer& container) override;
162
163   /**
164    * @copydoc Toolkit::Control::IsResourceReady()
165    */
166   bool IsResourceReady() const override;
167
168   /**
169    * @brief Loads a model from file
170    */
171   void LoadModel();
172
173   /**
174    * @brief Scales the model to fit the control or to return to original size.
175    */
176   void ScaleModel();
177
178   /**
179    * @brief Changes model anchor point to set the model at center or returns to the original model pivot.
180    */
181   void FitModelPosition();
182
183   /**
184    * @brief Changes IBL information of the input node.
185    */
186   void CollectRenderableActor(Actor actor);
187
188   /**
189    * @brief Changes IBL textures of the input node.
190    */
191   void UpdateImageBasedLightTexture();
192
193   /**
194    * @brief Changes IBL scale factor of the input node.
195    */
196   void UpdateImageBasedLightScaleFactor();
197
198 public: // Overrides ImageBasedLightObserver Methods.
199   /**
200    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightTexture()
201    */
202   void NotifyImageBasedLightTexture(Dali::Texture diffuseTexture, Dali::Texture specularTexture, float scaleFactor) override;
203
204   /**
205    * @copydoc Dali::Scene3D::Internal::ImageBasedLightObserver::NotifyImageBasedLightScaleFactor()
206    */
207   void NotifyImageBasedLightScaleFactor(float scaleFactor) override;
208
209 private:
210   std::string                    mModelUrl;
211   std::string                    mResourceDirectoryUrl;
212   Dali::Actor                    mModelRoot;
213   std::vector<AnimationData>     mAnimations;
214   std::vector<WeakHandle<Actor>> mRenderableActors;
215   WeakHandle<Scene3D::SceneView> mParentSceneView;
216
217   // TODO: This default texture can be removed after 3D Resource Cache is added.
218   Dali::Texture mDefaultSpecularTexture;
219   Dali::Texture mDefaultDiffuseTexture;
220   Dali::Texture mSceneSpecularTexture;
221   Dali::Texture mSceneDiffuseTexture;
222   Dali::Texture mSpecularTexture;
223   Dali::Texture mDiffuseTexture;
224   Vector3       mNaturalSize;
225   Vector3       mModelPivot;
226   float         mSceneIblScaleFactor;
227   float         mIblScaleFactor;
228   bool          mModelChildrenSensitive;
229   bool          mModelChildrenFocusable;
230   bool          mModelResourceReady;
231   bool          mIBLResourceReady;
232 };
233
234 } // namespace Internal
235
236 // Helpers for public-api forwarding methods
237 inline Dali::Scene3D::Internal::Model& GetImpl(Dali::Scene3D::Model& obj)
238 {
239   DALI_ASSERT_ALWAYS(obj);
240   Dali::RefObject& handle = obj.GetImplementation();
241   return static_cast<Dali::Scene3D::Internal::Model&>(handle);
242 }
243
244 inline const Dali::Scene3D::Internal::Model& GetImpl(const Dali::Scene3D::Model& obj)
245 {
246   DALI_ASSERT_ALWAYS(obj);
247   const Dali::RefObject& handle = obj.GetImplementation();
248   return static_cast<const Dali::Scene3D::Internal::Model&>(handle);
249 }
250
251 } // namespace Scene3D
252
253 } // namespace Dali
254
255 #endif // DALI_SCENE3D_INTERNAL_MODEL_H