Add ResourceReady for Control
[platform/core/uifw/dali-toolkit.git] / dali-scene3d / internal / controls / model-view / model-view-impl.cpp
index 35029f3..06ed187 100644 (file)
@@ -20,7 +20,6 @@
 
 // EXTERNAL INCLUDES
 #include <dali-toolkit/dali-toolkit.h>
-#include <dali-toolkit/devel-api/controls/control-devel.h>
 #include <dali-toolkit/internal/controls/control/control-data-impl.h>
 #include <dali-toolkit/internal/graphics/builtin-shader-extern-gen.h>
 #include <dali/integration-api/debug.h>
@@ -29,8 +28,8 @@
 #include <filesystem>
 
 // INTERNAL INCLUDES
-#include <dali-scene3d/public-api/controls/model-view/model-view.h>
 #include <dali-scene3d/internal/controls/scene-view/scene-view-impl.h>
+#include <dali-scene3d/public-api/controls/model-view/model-view.h>
 #include <dali-scene3d/public-api/loader/animation-definition.h>
 #include <dali-scene3d/public-api/loader/camera-parameters.h>
 #include <dali-scene3d/public-api/loader/cube-map-loader.h>
@@ -173,16 +172,17 @@ void AddModelTreeToAABB(BoundingVolume& AABB, const Dali::Scene3D::Loader::Scene
 } // anonymous namespace
 
 ModelView::ModelView(const std::string& modelPath, const std::string& resourcePath)
-: Control(ControlBehaviour(CONTROL_BEHAVIOUR_DEFAULT)),
+: Control(ControlBehaviour(DISABLE_SIZE_NEGOTIATION | DISABLE_STYLE_CHANGE_SIGNALS)),
   mModelPath(modelPath),
   mResourcePath(resourcePath),
-  mModelLayer(),
   mModelRoot(),
   mNaturalSize(Vector3::ZERO),
   mModelPivot(AnchorPoint::CENTER),
   mIblScaleFactor(1.0f),
-  mFitSize(false),
-  mFitCenter(false)
+  mFitSize(true),
+  mFitCenter(true),
+  mModelResourceReady(false),
+  mIBLResourceReady(true)
 {
 }
 
@@ -222,9 +222,18 @@ void ModelView::FitCenter(bool fit)
 
 void ModelView::SetImageBasedLightSource(const std::string& diffuse, const std::string& specular, float scaleFactor)
 {
+  mIBLResourceReady = false;
   Texture diffuseTexture  = Dali::Scene3D::Loader::LoadCubeMap(diffuse);
   Texture specularTexture = Dali::Scene3D::Loader::LoadCubeMap(specular);
   SetImageBasedLightTexture(diffuseTexture, specularTexture, scaleFactor);
+  mIBLResourceReady = true;
+
+  // If Model resource is already ready, then set resource ready.
+  // If Model resource is still not ready, wait for model resource ready.
+  if(IsResourceReady())
+  {
+    SetResourceReady(false);
+  }
 }
 
 void ModelView::SetImageBasedLightTexture(Dali::Texture diffuse, Dali::Texture specular, float scaleFactor)
@@ -310,23 +319,6 @@ void ModelView::OnSceneDisconnection()
   Control::OnSceneDisconnection();
 }
 
-void ModelView::OnInitialize()
-{
-  Actor self  = Self();
-  mModelLayer = Layer::New();
-  mModelLayer.SetProperty(Layer::Property::BEHAVIOR, Layer::LAYER_3D);
-  mModelLayer.SetProperty(Layer::Property::DEPTH_TEST, true);
-  mModelLayer.SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
-  mModelLayer.SetProperty(Dali::Actor::Property::ANCHOR_POINT, AnchorPoint::CENTER);
-  mModelLayer.SetResizePolicy(ResizePolicy::FILL_TO_PARENT,
-                              Dimension::ALL_DIMENSIONS);
-
-  // Models in glTF and dli are defined as right hand coordinate system.
-  // DALi uses left hand coordinate system. Scaling negative is for change winding order.
-  mModelLayer.SetProperty(Dali::Actor::Property::SCALE_Y, -1.0f);
-  self.Add(mModelLayer);
-}
-
 Vector3 ModelView::GetNaturalSize()
 {
   if(!mModelRoot)
@@ -357,6 +349,11 @@ void ModelView::OnRelayout(const Vector2& size, RelayoutContainer& container)
   ScaleModel();
 }
 
+bool ModelView::IsResourceReady() const
+{
+  return mModelResourceReady && mIBLResourceReady;
+}
+
 void ModelView::LoadModel()
 {
   std::filesystem::path modelPath(mModelPath);
@@ -469,35 +466,44 @@ void ModelView::LoadModel()
   mNaturalSize = AABB.CalculateSize();
   mModelPivot  = AABB.CalculatePivot();
   mModelRoot.SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
+  Vector3 controlSize = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
+  if(controlSize.x == 0.0f || controlSize.y == 0.0f)
+  {
+    Self().SetProperty(Dali::Actor::Property::SIZE, mNaturalSize);
+  }
 
   FitModelPosition();
   ScaleModel();
 
-  mModelLayer.Add(mModelRoot);
+  Self().Add(mModelRoot);
+
+  Self().SetProperty(Dali::Actor::Property::PARENT_ORIGIN, ParentOrigin::CENTER);
+  Self().SetProperty(Dali::Actor::Property::ANCHOR_POINT, Vector3(mModelPivot.x, 1.0f - mModelPivot.y, mModelPivot.z));
+
+  mModelResourceReady = true;
+
+  Control::SetResourceReady(false);
 }
 
 void ModelView::ScaleModel()
 {
   if(mModelRoot)
   {
-    if(mFitSize)
+    Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
+    if(mFitSize && size.x > 0.0f && size.y > 0.0f)
     {
-      Vector3 size = Self().GetProperty<Vector3>(Dali::Actor::Property::SIZE);
-      if(size.x > 0.0f && size.y > 0.0f)
-      {
-        float scaleFactor = MAXFLOAT;
-        scaleFactor       = std::min(size.x / mNaturalSize.x, scaleFactor);
-        scaleFactor       = std::min(size.y / mNaturalSize.y, scaleFactor);
-        mModelRoot.SetProperty(Dali::Actor::Property::SCALE, scaleFactor);
-      }
-      else
-      {
-        DALI_LOG_ERROR("ModelView size is wrong.");
-      }
+      float scaleFactor = MAXFLOAT;
+      scaleFactor       = std::min(size.x / mNaturalSize.x, scaleFactor);
+      scaleFactor       = std::min(size.y / mNaturalSize.y, scaleFactor);
+      // Models in glTF and dli are defined as right hand coordinate system.
+      // DALi uses left hand coordinate system. Scaling negative is for change winding order.
+      mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION * scaleFactor);
     }
     else
     {
-      mModelRoot.SetProperty(Dali::Actor::Property::SCALE, 1.0f);
+      // Models in glTF and dli are defined as right hand coordinate system.
+      // DALi uses left hand coordinate system. Scaling negative is for change winding order.
+      mModelRoot.SetProperty(Dali::Actor::Property::SCALE, Y_DIRECTION);
     }
   }
 }