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