Button to use Color Renderer when setting (un)selected color 47/79547/4
authorAgnelo Vaz <agnelo.vaz@samsung.com>
Mon, 11 Jul 2016 17:14:05 +0000 (18:14 +0100)
committerAgnelo Vaz <agnelo.vaz@samsung.com>
Thu, 14 Jul 2016 14:04:48 +0000 (15:04 +0100)
Previously would create another Toolkit::Control and set background.

Change-Id: I8377098a506b0a9b895e1ee77df646970af64a10

automated-tests/src/dali-toolkit/utc-Dali-Button.cpp
automated-tests/src/dali-toolkit/utc-Dali-PushButton.cpp
dali-toolkit/internal/controls/buttons/button-impl.cpp
dali-toolkit/internal/controls/buttons/button-impl.h

index b5ec1f1..5aff146 100644 (file)
@@ -64,28 +64,6 @@ struct CallbackFunctor
   bool* mCallbackFlag;
 };
 
-
-Image CreateSolidColorImage( const Vector4& color, unsigned int width, unsigned int height )
-{
-  BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
-
-  // Create the image
-  PixelBuffer* pixbuf = imageData.GetBuffer();
-  unsigned int size = width * height;
-
-  for( size_t i = 0; i < size; i++ )
-    {
-      pixbuf[i*4+0] = 0xFF * color.r;
-      pixbuf[i*4+1] = 0xFF * color.g;
-      pixbuf[i*4+2] = 0xFF * color.b;
-      pixbuf[i*4+3] = 0xFF * color.a;
-    }
-
-  imageData.Update();
-
-  return imageData;
-}
-
 Dali::Integration::Point GetPointDownInside()
 {
   Dali::Integration::Point point;
@@ -941,3 +919,56 @@ int UtcDaliButtonSetSelectedImageWithImageN(void)
 
   END_TEST;
 }
+
+int UtcDaliButtonSetSelectedColorP(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliButtonSetSelectedColorP");
+
+  PushButton pushButton = PushButton::New();
+  Stage::GetCurrent().Add( pushButton );
+
+  application.SendNotification();
+  application.Render();
+
+  const Vector4 SET_COLOR = Color::BLUE;
+
+  pushButton.SetSize( Vector2( 20.0f, 20.0f ) );
+  pushButton.SetProperty( Button::Property::SELECTED_COLOR, SET_COLOR );
+
+  application.SendNotification();
+  application.Render();
+
+  Vector4 color = pushButton.GetProperty<Vector4>( Button::Property::SELECTED_COLOR );
+
+  DALI_TEST_EQUALS( color, SET_COLOR, TEST_LOCATION );
+
+  END_TEST;
+}
+
+int UtcDaliButtonSetUnSelectedColorP(void)
+{
+  ToolkitTestApplication application;
+  tet_infoline(" UtcDaliButtonSetUnSelectedColorP");
+
+  PushButton pushButton = PushButton::New();
+  Stage::GetCurrent().Add( pushButton );
+
+  application.SendNotification();
+  application.Render();
+
+  const Vector4 SET_COLOR = Color::BLUE;
+
+  pushButton.SetSize( Vector2( 20.0f, 20.0f ) );
+  pushButton.SetProperty( Button::Property::UNSELECTED_COLOR, SET_COLOR );
+
+  application.SendNotification();
+  application.Render();
+
+  Vector4 color = pushButton.GetProperty<Vector4>( Button::Property::UNSELECTED_COLOR );
+
+  DALI_TEST_EQUALS( color, SET_COLOR, TEST_LOCATION );
+
+  END_TEST;
+}
+
index 82d6a40..a191f43 100644 (file)
@@ -113,27 +113,6 @@ Dali::Integration::Point GetPointUpOutside()
   return point;
 }
 
-Image CreateSolidColorImage( const Vector4& color, unsigned int width, unsigned int height )
-{
-  BufferImage imageData = BufferImage::New( width, height, Pixel::RGBA8888 );
-
-  // Create the image
-  PixelBuffer* pixbuf = imageData.GetBuffer();
-  unsigned int size = width * height;
-
-  for( size_t i = 0; i < size; i++ )
-    {
-      pixbuf[i*4+0] = 0xFF * color.r;
-      pixbuf[i*4+1] = 0xFF * color.g;
-      pixbuf[i*4+2] = 0xFF * color.b;
-      pixbuf[i*4+3] = 0xFF * color.a;
-    }
-
-  imageData.Update();
-
-  return imageData;
-}
-
 } //namespace
 
 int UtcDaliPushButtonConstructorP(void)
index dc00a3c..fcd0468 100644 (file)
@@ -29,6 +29,7 @@
 // INTERNAL INCLUDES
 #include <dali-toolkit/public-api/controls/text-controls/text-label.h>
 #include <dali-toolkit/public-api/controls/image-view/image-view.h>
+#include <dali-toolkit/devel-api/controls/renderer-factory/renderer-factory.h>
 
 /**
  * Button states and contents
@@ -527,46 +528,48 @@ void Button::SetupContent( Actor& actorToModify, Actor newActor )
   }
 }
 
-void Button::SetUnselectedColor( const Vector4& color )
+const Vector4 Button::GetUnselectedColor() const
+{
+  return mUnselectedColor;
+}
+
+void Button::SetColor( const Vector4& color, Button::PaintState selectedState )
 {
-  mUnselectedColor = color;
+  Actor& contentActor = mSelectedContent;
+  bool imageFileExists = false;
 
-  if( mUnselectedContent && !GetUnselectedImageFilename().empty() )
+  if ( selectedState == SelectedState || selectedState == DisabledSelectedState )
   {
-    // If there is existing unselected content, change the color on it directly.
-    mUnselectedContent.SetColor( mUnselectedColor );
+    mSelectedColor = color;
+    imageFileExists = !GetSelectedImageFilename().empty();
   }
   else
   {
-    // If there is no existing content, create a new actor to use for flat color.
-    Toolkit::Control unselectedContentActor = Toolkit::Control::New();
-    unselectedContentActor.SetBackgroundColor( mUnselectedColor );
-    SetupContent( mUnselectedContent, unselectedContentActor );
-    mUnselectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+    mUnselectedColor = color;
+    contentActor = mUnselectedContent;
+    imageFileExists = !GetUnselectedImageFilename().empty();
   }
-}
 
-const Vector4 Button::GetUnselectedColor() const
-{
-  return mUnselectedColor;
-}
-
-void Button::SetSelectedColor( const Vector4& color )
-{
-  mSelectedColor = color;
-
-  if( mSelectedContent && !GetSelectedImageFilename().empty() )
+  if( contentActor &&  imageFileExists )
   {
     // If there is existing unselected content, change the color on it directly.
-    mSelectedContent.SetColor( mSelectedColor );
+    contentActor.SetColor( color );
   }
   else
   {
     // If there is no existing content, create a new actor to use for flat color.
-    Toolkit::Control selectedContentActor = Toolkit::Control::New();
-    selectedContentActor.SetBackgroundColor( mSelectedColor );
-    SetupContent( mSelectedContent, selectedContentActor );
-    mSelectedContent.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
+    Actor placementActor = Actor::New();
+    Toolkit::RendererFactory rendererFactory = Toolkit::RendererFactory::Get();
+    Toolkit::ControlRenderer colorRenderer;
+
+    Property::Map map;
+    map["rendererType"] = "color";
+    map["mixColor"] = color;
+
+    colorRenderer = rendererFactory.CreateControlRenderer( map );
+    colorRenderer.SetOnStage( placementActor );
+    SetupContent( contentActor, placementActor );
+    contentActor.SetResizePolicy( ResizePolicy::FILL_TO_PARENT, Dimension::ALL_DIMENSIONS );
   }
 }
 
@@ -1386,13 +1389,13 @@ void Button::SetProperty( BaseObject* object, Property::Index index, const Prope
 
       case Toolkit::Button::Property::UNSELECTED_COLOR:
       {
-        GetImplementation( button ).SetUnselectedColor( value.Get< Vector4 >() );
+        GetImplementation( button ).SetColor( value.Get< Vector4 >(), UnselectedState );
         break;
       }
 
       case Toolkit::Button::Property::SELECTED_COLOR:
       {
-        GetImplementation( button ).SetSelectedColor( value.Get< Vector4 >() );
+        GetImplementation( button ).SetColor( value.Get< Vector4 >(), SelectedState );
         break;
       }
 
index 71a42fe..da2557f 100644 (file)
@@ -43,18 +43,6 @@ namespace Internal
 class Button : public Control
 {
 
-protected:
-
-  /**
-   * Construct a new Button.
-   */
-  Button();
-
-  /**
-   * A reference counted object may only be deleted by calling Unreference()
-   */
-  virtual ~Button();
-
 public:
 
   /**
@@ -285,6 +273,42 @@ public: // Deprecated API
 
 protected:
 
+  enum ButtonState
+  {
+    ButtonUp,                                  ///< The button is up.
+    ButtonDown,                                ///< The button is down.
+  };
+
+  /**
+   * Button paint states.
+   */
+  enum PaintState
+  {
+    UnselectedState,              ///< The button is unselected.
+    SelectedState,                ///< The button is selected.
+    DisabledUnselectedState,      ///< The button is disabled and unselected.
+    DisabledSelectedState,        ///< The button is disabled and selected.
+  };
+
+  /**
+   * Enum to specify which decoration when getting and setting decorations.
+   */
+  enum DecorationState
+  {
+    UNSELECTED_DECORATION = 0,
+    SELECTED_DECORATION,
+    DECORATION_STATES
+  };
+
+  /**
+   * Construct a new Button.
+   */
+  Button();
+
+  /**
+   * A reference counted object may only be deleted by calling Unreference()
+   */
+  virtual ~Button();
   /**
    * @return A reference to the label actor.
    */
@@ -494,6 +518,7 @@ protected: // From Control
    */
   void OnStageDisconnection();
 
+
 private:
 
   /**
@@ -551,24 +576,18 @@ private:
   void SetupContent( Actor& actorToModify, Actor newActor );
 
   /**
-   * Sets the color of the unselected image.
-   * If no image exists, it is created.
-   * @param[in]  color The color to use.
-   */
-  void SetUnselectedColor( const Vector4& color );
-
-  /**
    * Gets the unselected content color.
    * @return     The currently used unselected color.
    */
   const Vector4 GetUnselectedColor() const;
 
   /**
-   * Sets the color of the selected image.
-   * If no image exists, it is created.
+   * Sets the color of button in selected or unselected state, if image also supplied this color will be appplied to it.
+   * If no visual exists, it is created.
    * @param[in]  color The color to use.
+   * @param[in]  selectedState The state to apply the color to, SelectedState or DisabledUnselectedState.
    */
-  void SetSelectedColor( const Vector4& color );
+  void SetColor( const Vector4& color, PaintState selectedState );
 
   /**
    * Gets the selected content color.
@@ -578,33 +597,6 @@ private:
 
 protected:
 
-  enum ButtonState
-  {
-    ButtonUp,                                  ///< The button is up.
-    ButtonDown,                                ///< The button is down.
-  };
-
-  /**
-   * Button paint states.
-   */
-  enum PaintState
-  {
-    UnselectedState,              ///< The button is unselected.
-    SelectedState,                ///< The button is selected.
-    DisabledUnselectedState,      ///< The button is disabled and unselected.
-    DisabledSelectedState,        ///< The button is disabled and selected.
-  };
-
-  /**
-   * Enum to specify which decoration when getting and setting decorations.
-   */
-  enum DecorationState
-  {
-    UNSELECTED_DECORATION = 0,
-    SELECTED_DECORATION,
-    DECORATION_STATES
-  };
-
   ButtonState GetState();
   PaintState GetPaintState();
   void SetDecoration( DecorationState state, Actor actor );