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