Visual bases checks if visual already staged before adding to stage
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / public-api / controls / control-impl.cpp
index 58f7d4f..90b3bb6 100644 (file)
@@ -292,7 +292,6 @@ public:
   Impl(Control& controlImpl)
   : mControlImpl( controlImpl ),
     mStyleName(""),
-    mBackgroundVisual(),
     mBackgroundColor(Color::TRANSPARENT),
     mStartingPinchScale( NULL ),
     mKeyEventSignal(),
@@ -375,7 +374,7 @@ public:
           }
           else
           {
-            // An empty map means the background is no longer required
+            // An empty image means the background is no longer required
             controlImpl.ClearBackground();
           }
           break;
@@ -397,13 +396,13 @@ public:
         case Toolkit::Control::Property::BACKGROUND:
         {
           const Property::Map* map = value.GetMap();
-          if( map )
+          if( map && !map->Empty() )
           {
             controlImpl.SetBackground( *map );
           }
           else
           {
-            // The background is not a property map, so we should clear the background
+            // The background is an empty property map, so we should clear the background
             controlImpl.ClearBackground();
           }
           break;
@@ -447,9 +446,10 @@ public:
         {
           DALI_LOG_WARNING( "BACKGROUND_IMAGE property is deprecated. Use BACKGROUND property instead\n" );
           Property::Map map;
-          if( controlImpl.mImpl->mBackgroundVisual )
+          Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
+          if( visual )
           {
-            controlImpl.mImpl->mBackgroundVisual.CreatePropertyMap( map );
+            visual.CreatePropertyMap( map );
           }
           value = map;
           break;
@@ -464,9 +464,10 @@ public:
         case Toolkit::Control::Property::BACKGROUND:
         {
           Property::Map map;
-          if( controlImpl.mImpl->mBackgroundVisual )
+          Toolkit::Visual::Base visual = controlImpl.GetVisual( Toolkit::Control::Property::BACKGROUND );
+          if( visual )
           {
-            (controlImpl.mImpl->mBackgroundVisual).CreatePropertyMap( map );
+            visual.CreatePropertyMap( map );
           }
 
           value = map;
@@ -484,7 +485,6 @@ public:
   Control& mControlImpl;
   RegisteredVisualContainer mVisuals; // Stores visuals needed by the control, non trivial type so std::vector used.
   std::string mStyleName;
-  Toolkit::Visual::Base mBackgroundVisual;   ///< The visual to render the background
   Vector4 mBackgroundColor;                       ///< The color of the background visual
   Vector3* mStartingPinchScale;      ///< The scale when a pinch gesture starts, TODO: consider removing this
   Toolkit::Control::KeyEventSignalType mKeyEventSignal;
@@ -562,12 +562,8 @@ void Control::SetBackgroundColor( const Vector4& color )
   Property::Map map;
   map[ Toolkit::VisualProperty::TYPE ] = Toolkit::Visual::COLOR;
   map[ Toolkit::ColorVisual::Property::MIX_COLOR ] = color;
-  mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
-  RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
-  if( mImpl->mBackgroundVisual )
-  {
-    mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
-  }
+
+  SetBackground( map );
 }
 
 Vector4 Control::GetBackgroundColor() const
@@ -577,29 +573,29 @@ Vector4 Control::GetBackgroundColor() const
 
 void Control::SetBackground( const Property::Map& map )
 {
-  mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( map );
-  RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
-  if( mImpl->mBackgroundVisual )
+  Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( map );
+  if( visual )
   {
-    mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
+    RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
+    visual.SetDepthIndex( DepthIndex::BACKGROUND );
   }
 }
 
 void Control::SetBackgroundImage( Image image )
 {
-  mImpl->mBackgroundVisual = Toolkit::VisualFactory::Get().CreateVisual( image );
-  RegisterVisual( Toolkit::Control::Property::BACKGROUND, mImpl->mBackgroundVisual );
-  if( mImpl->mBackgroundVisual )
+  DALI_LOG_WARNING( "SetBackgroundImage is for the depreciated Property::BACKGROUND_IMAGE use SetBackground( const Property::Map& map )\n" );
+  Toolkit::Visual::Base visual = Toolkit::VisualFactory::Get().CreateVisual( image );
+  if( visual )
   {
-    mImpl->mBackgroundVisual.SetDepthIndex( DepthIndex::BACKGROUND );
+    RegisterVisual( Toolkit::Control::Property::BACKGROUND, visual );
+    visual.SetDepthIndex( DepthIndex::BACKGROUND );
   }
 }
 
 void Control::ClearBackground()
 {
-  Actor self( Self() );
-  mImpl->mBackgroundVisual.RemoveAndReset( self );
-  mImpl->mBackgroundColor = Color::TRANSPARENT;
+   UnregisterVisual( Toolkit::Control::Property::BACKGROUND );
+   mImpl->mBackgroundColor = Color::TRANSPARENT;
 }
 
 void Control::EnableGestureDetection(Gesture::Type type)
@@ -759,7 +755,7 @@ void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visu
       {
         if( (*iter)->visual && self.OnStage() )
         {
-          (*iter)->visual.SetOffStage( self );
+          Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
         }
         (*iter)->visual = visual;
         visualReplaced = true;
@@ -773,7 +769,7 @@ void Control::RegisterVisual( Property::Index index, Toolkit::Visual::Base& visu
 
   if( visual && self.OnStage() && enabled )
   {
-    visual.SetOnStage( self );
+    Toolkit::GetImplementation(visual).SetOnStage( self );
   }
 }
 
@@ -782,6 +778,9 @@ void Control::UnregisterVisual( Property::Index index )
    RegisteredVisualContainer::Iterator iter;
    if ( FindVisual( index, mImpl->mVisuals, iter ) )
    {
+     Actor self( Self() );
+     Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
+     (*iter)->visual.Reset();
      mImpl->mVisuals.Erase( iter );
    }
 }
@@ -804,6 +803,7 @@ void Control::EnableVisual( Property::Index index, bool enable )
   {
     if (  (*iter)->enabled == enable )
     {
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Visual Already enabled set (%s) \n", enable?"enabled":"disabled");
       return;
     }
 
@@ -813,12 +813,13 @@ void Control::EnableVisual( Property::Index index, bool enable )
     {
       if ( enable )
       {
-
-        (*iter)->visual.SetOnStage( parentActor );
+        DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) on stage \n", index );
+        Toolkit::GetImplementation((*iter)->visual).SetOnStage( parentActor );
       }
       else
       {
-        (*iter)->visual.SetOffStage( parentActor );  // No need to call if control not staged.
+        DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::EnableVisual Setting Visual(%d) off stage \n", index );
+        Toolkit::GetImplementation((*iter)->visual).SetOffStage( parentActor );  // No need to call if control not staged.
       }
     }
   }
@@ -1085,13 +1086,16 @@ void Control::EmitKeyInputFocusSignal( bool focusGained )
 
 void Control::OnStageConnection( int depth )
 {
+  DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection number of registered visuals(%d)\n",  mImpl->mVisuals.Size() );
+
   for(RegisteredVisualContainer::Iterator iter = mImpl->mVisuals.Begin(); iter!= mImpl->mVisuals.End(); iter++)
   {
     // Check whether the visual is empty and enabled
     if( (*iter)->visual && (*iter)->enabled )
     {
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageConnection Setting visual(%d) on stage\n", (*iter)->index );
       Actor self( Self() );
-      (*iter)->visual.SetOnStage( self );
+      Toolkit::GetImplementation((*iter)->visual).SetOnStage( self );
     }
   }
 }
@@ -1103,8 +1107,9 @@ void Control::OnStageDisconnection()
     // Check whether the visual is empty
     if( (*iter)->visual )
     {
+      DALI_LOG_INFO( gLogFilter, Debug::Verbose, "Control::OnStageDisconnection Setting visual(%d) off stage\n", (*iter)->index );
       Actor self( Self() );
-      (*iter)->visual.SetOffStage( self );
+      Toolkit::GetImplementation((*iter)->visual).SetOffStage( self );
     }
   }
 }
@@ -1133,10 +1138,11 @@ void Control::OnChildRemove(Actor& child)
 
 void Control::OnSizeSet(const Vector3& targetSize)
 {
-  if( mImpl->mBackgroundVisual )
+  Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
+  if( visual )
   {
     Vector2 size( targetSize );
-    mImpl->mBackgroundVisual.SetSize( size );
+    visual.SetSize( size );
   }
 }
 
@@ -1179,11 +1185,12 @@ void Control::OnSetResizePolicy( ResizePolicy::Type policy, Dimension::Type dime
 
 Vector3 Control::GetNaturalSize()
 {
-  if( mImpl->mBackgroundVisual )
+  Toolkit::Visual::Base visual = GetVisual( Toolkit::Control::Property::BACKGROUND );
+  if( visual )
   {
     Vector2 naturalSize;
-    mImpl->mBackgroundVisual.GetNaturalSize(naturalSize);
-    return Vector3(naturalSize);
+    visual.GetNaturalSize( naturalSize );
+    return Vector3( naturalSize );
   }
   return Vector3::ZERO;
 }