Patch for some TLButton-Properties

Discussion regarding the development of CEGUI itself - as opposed to questions about CEGUI usage that should be in the help forums.

Moderators: CEGUI MVP, CEGUI Team

User avatar
rincewind
Just popping in
Just popping in
Posts: 14
Joined: Wed Jan 12, 2005 12:06

Patch for some TLButton-Properties

Postby rincewind » Mon Sep 20, 2004 12:15

Hi,

this small patch adds some properties to the Taharez Look Button.
Namely:
NormalImage, PushedImage, HoverImage, UseStandardImagery.
this allows for using custom images on a per button basis.

TextXOffset allows you to specify an offset to shift the text on the button in the x-direction. This is useful if your button-images shouldn't have the text centered around the middle of the image but rather a little to the left (if your area for text doesn't cover the whole button-image).
The value is relative to the button-width.

Greetings,

Rincewind

P.S: Patch is against CVS-head

Code: Select all

? WidgetSets/TaharezLook/include/TLButtonProperties.h
? WidgetSets/TaharezLook/src/TLButtonProperties.cpp
Index: WidgetSets/TaharezLook/include/TLButton.h
===================================================================
RCS file: /cvsroot/crayzedsgui/cegui_mk2/WidgetSets/TaharezLook/include/TLButton.h,v
retrieving revision 1.1
diff -u -r1.1 TLButton.h
--- WidgetSets/TaharezLook/include/TLButton.h   13 Jun 2004 09:43:50 -0000   1.1
+++ WidgetSets/TaharezLook/include/TLButton.h   20 Sep 2004 12:05:21 -0000
@@ -31,6 +31,8 @@
 #include "CEGUIWindowFactory.h"
 #include "CEGUIRenderableImage.h"
 
+#include "TLButtonProperties.h"
+
 
 // Start of CEGUI namespace section
 namespace CEGUI
@@ -191,6 +193,31 @@
    */
    void   setDisabledImage(const RenderableImage* image);
 
+   /*!
+   \brief
+      get the offset that is used to shift of the text in the x-direction
+      this is useful if the button-images are not symetrical and the
+      text shouldn't be completly centered
+
+   \return
+      the offset relative to the button-size
+   */
+   float   getTextXOffset() const;
+
+   /*!
+   \brief
+      set the offset to use for a shift of the text in the x-direction
+      this is useful if the button-images are not symetrical and the
+      text shouldn't be completly centered
+
+   \param offset
+      The offset to use. This is relative to the button-width
+
+   \return
+      Nothing.
+   */
+   void   setTextXOffset(float offset);
+
 
 protected:
    /*************************************************************************
@@ -254,6 +281,18 @@
    const Image*   d_leftSectionPushed;         //!< Image to use when rendering the button left section (pushed state).
    const Image*   d_middleSectionPushed;         //!< Image to use when rendering the button middle section (pushed state).
    const Image*   d_rightSectionPushed;         //!< Image to use when rendering the button right section (pushed state).
+
+   //text-offset
+   float d_textXOffset;
+
+private:
+   static TLButtonProperties::NormalImage d_normalImageProperty;
+   static TLButtonProperties::PushedImage d_pushedImageProperty;
+   static TLButtonProperties::HoverImage  d_hoverImageProperty;
+   static TLButtonProperties::UseStandardImagery d_useStandardImageryProperty;
+   static TLButtonProperties::TextXOffset d_textXOffsetProperty;
+
+   void addTLButtonProperties(void);
 };
 
 
Index: WidgetSets/TaharezLook/src/TLButton.cpp
===================================================================
RCS file: /cvsroot/crayzedsgui/cegui_mk2/WidgetSets/TaharezLook/src/TLButton.cpp,v
retrieving revision 1.2
diff -u -r1.2 TLButton.cpp
--- WidgetSets/TaharezLook/src/TLButton.cpp   19 Jul 2004 20:01:03 -0000   1.2
+++ WidgetSets/TaharezLook/src/TLButton.cpp   19 Sep 2004 21:16:22 -0000
@@ -46,6 +46,12 @@
 const utf8   TLButton::RightPushedImageName[]      = "ButtonRightPushed";
 const utf8  TLButton::MouseCursorImageName[]      = "MouseArrow";
 
+TLButtonProperties::NormalImage TLButton::d_normalImageProperty;
+TLButtonProperties::PushedImage TLButton::d_pushedImageProperty;
+TLButtonProperties::HoverImage  TLButton::d_hoverImageProperty;
+TLButtonProperties::UseStandardImagery TLButton::d_useStandardImageryProperty;
+TLButtonProperties::TextXOffset TLButton::d_textXOffsetProperty;
+
 
 /*************************************************************************
    Constructor
@@ -76,7 +82,11 @@
    d_middleSectionPushed   = &iset->getImage(MiddlePushedImageName);
    d_rightSectionPushed   = &iset->getImage(RightPushedImageName);
 
+   d_textXOffset = 0.0f;
+
    setMouseCursor(&iset->getImage(MouseCursorImageName));
+
+   addTLButtonProperties();
 }
 
 
@@ -186,6 +196,16 @@
    requestRedraw();
 }
 
+float TLButton::getTextXOffset() const
+{
+   return d_textXOffset;
+}
+
+void TLButton::setTextXOffset(float offset)
+{
+   d_textXOffset = offset;
+}
+
 
 /*************************************************************************
    render Widget in normal state   
@@ -246,6 +266,7 @@
    // Draw label text
    //
    absrect.d_top += (absrect.getHeight() - getFont()->getLineSpacing()) / 2;
+   absrect.d_left += d_textXOffset * absrect.getWidth();
    colours.d_top_left = colours.d_top_right = colours.d_bottom_left = colours.d_bottom_right = (d_normalColour | alpha_comp);
    getFont()->drawText(getText(), absrect, System::getSingleton().getRenderer()->getZLayer(2), clipper, Centred, colours);
 }
@@ -310,6 +331,7 @@
    // Draw label text
    //
    absrect.d_top += (absrect.getHeight() - getFont()->getLineSpacing()) / 2;
+   absrect.d_left += d_textXOffset * absrect.getWidth();
    colours.d_top_left = colours.d_top_right = colours.d_bottom_left = colours.d_bottom_right = (d_hoverColour | alpha_comp);
    getFont()->drawText(getText(), absrect, System::getSingleton().getRenderer()->getZLayer(2), clipper, Centred, colours);
 }
@@ -374,6 +396,7 @@
    // Draw label text
    //
    absrect.d_top += (absrect.getHeight() - getFont()->getLineSpacing()) / 2;
+   absrect.d_left += d_textXOffset * absrect.getWidth();
    colours.d_top_left = colours.d_top_right = colours.d_bottom_left = colours.d_bottom_right = (d_pushedColour | alpha_comp);
    getFont()->drawText(getText(), absrect, System::getSingleton().getRenderer()->getZLayer(2), clipper, Centred, colours);
 }
@@ -435,6 +458,7 @@
    // Draw label text
    //
    absrect.d_top += (absrect.getHeight() - getFont()->getLineSpacing()) / 2;
+   absrect.d_left += d_textXOffset * absrect.getWidth();
    colours.d_top_left = colours.d_top_right = colours.d_bottom_left = colours.d_bottom_right = (d_disabledColour | alpha_comp);
    getFont()->drawText(getText(), absrect, System::getSingleton().getRenderer()->getZLayer(2), clipper, Centred, colours);
 }
@@ -486,6 +510,15 @@
 
 }
 
+void TLButton::addTLButtonProperties(void)
+{
+   addProperty(&d_normalImageProperty);
+   addProperty(&d_pushedImageProperty);
+   addProperty(&d_hoverImageProperty);
+   addProperty(&d_useStandardImageryProperty);
+   addProperty(&d_textXOffsetProperty);
+}
+
 
 //////////////////////////////////////////////////////////////////////////
 /*************************************************************************


plus WidgetSets/TaharezLook/src/TLButtonProperties.h

Code: Select all

#ifndef _CEGUITLButtonProperties_h_
#define _CEGUITLButtonProperties_h_

#include "CEGUIProperty.h"


// Start of CEGUI namespace section
namespace CEGUI
{

// Start of TLButtonProperties namespace section
/*!
\brief
   Namespace containing all classes that make up the properties interface for the TLButton class
*/
namespace TLButtonProperties
{

/*!
\brief
   Property to access the normal image of the button

   \par Usage:
      - Name: NormalImage
      - Format: "set:<imageset> image:<imagename>".

*/
class NormalImage : public Property
{
public:
   NormalImage() : Property((utf8*)"NormalImage", (utf8*)"Property to get/set the normal image for the TLButton widget.  Value should be \"set:[imageset name] image:[image name]\".") {}

   String   get(const void* receiver) const;
   void   set(void* receiver, const String& value);
};

/*!
\brief
   Property to access the pushed image of the button

   \par Usage:
      - Name: PushedImage
      - Format: "set:<imageset> image:<imagename>".

*/
class PushedImage : public Property
{
public:
   PushedImage() : Property((utf8*)"PushedImage", (utf8*)"Property to get/set the pushed image for the TLButton widget.  Value should be \"set:[imageset name] image:[image name]\".") {}

   String   get(const void* receiver) const;
   void   set(void* receiver, const String& value);
};

/*!
\brief
   Property to access the hover image of the button

   \par Usage:
      - Name: HoverImage
      - Format: "set:<imageset> image:<imagename>".

*/
class HoverImage : public Property
{
public:
   HoverImage() : Property((utf8*)"HoverImage", (utf8*)"Property to get/set the hover image for the TLButton widget.  Value should be \"set:[imageset name] image:[image name]\".") {}

   String   get(const void* receiver) const;
   void   set(void* receiver, const String& value);
};

/*!
\brief
   Property to access whether to use the standard images or the supplied custom ones

   \par Usage:
      - Name: UseStandardImagery
      - Format: "True|False".

*/
class UseStandardImagery : public Property
{
public:
   UseStandardImagery() : Property((utf8*)"UseStandardImagery", (utf8*)"Property to get/set whether to use the standard imagery for the TLButton widget.  Value should be \"set:[imageset name] image:[image name]\".") {}

   String   get(const void* receiver) const;
   void   set(void* receiver, const String& value);
};

/*!
\brief
   Property to access an offset that is used to shift the text in the x-direction.

   \par Usage:
      - Name: UseStandardImagery
      - Format: "<xoffset>".

   \par
      where <xoffset> is a float value specifying the offset relative to the button-size

*/
class TextXOffset : public Property
{
public:
   TextXOffset() : Property((utf8*)"TextXOffset", (utf8*)"Property to get/set a relative x offset for the button's text") {}

   String get(const void* receiver) const;
   void set(void* receiver, const String& value);
};

}

}
#endif


plus WidgetSets/TaharezLook/src/TLButtonProperties.cpp

Code: Select all

#include "CEGUIImage.h"
#include "CEGUIImageset.h"
#include "CEGUIImagesetManager.h"

#include "CEGUIExceptions.h"

#include "TLButton.h"
#include "TLButtonProperties.h"

// Start of CEGUI namespace section
namespace CEGUI
{

// Start of StaticImageProperties namespace section
namespace TLButtonProperties
{

   //these are helper functions copied over from PropertyHelper,
   //somehow it won't find them even when linking to the proper lib...

float stringToFloat(const String& str)
{
   using namespace std;

   float val = 0;
   sscanf(str.c_str(), " %f", &val);

   return val;
}


bool stringToBool(const String& str)
{
   if ((str == (utf8*)"True") || (str == (utf8*)"true"))
   {
      return true;
   }
   else
   {
      return false;
   }
}

const Image* stringToImage(const String& str)
{
   using namespace std;

   char imageSet[128];
   char imageName[128];

   sscanf(str.c_str(), " set:%127s image:%127s", imageSet, imageName);

   const Image* image;

   try
   {
      image = &ImagesetManager::getSingleton().getImageset((utf8*)imageSet)->getImage((utf8*)imageName);
   }
   catch (UnknownObjectException)
   {
      image = NULL;
   }

   return image;
}

String boolToString(bool val)
{
   if (val)
   {
      return String((utf8*)"True");
   }
   else
   {
      return String ((utf8*)"False");
   }

}

String floatToString(float val)
{
   using namespace std;

   char buff[64];
   sprintf(buff, "%f", val);

   return String((utf8*)buff);
}

String NormalImage::get(const void *receiver) const
{
   //TLButton currently has no getter for this...
   return "";//PropertyHelper::imageToString(static_cast<const TLButton*>(receiver)->getNormal());
}

void NormalImage::set(void *receiver, const String &value)
{
   RenderableImage * image = new RenderableImage();
   image->setImage(stringToImage(value));
   image->setHorzFormatting(RenderableImage::HorzFormatting::HorzStretched);
   image->setVertFormatting(RenderableImage::VertFormatting::VertStretched);
   static_cast<TLButton*>(receiver)->setNormalImage(image);
}

String PushedImage::get(const void *receiver) const
{
   //TLButton currently has no getter for this...
   return "";//PropertyHelper::imageToString(static_cast<const TLButton*>(receiver)->getNormal());
}

void PushedImage::set(void *receiver, const String &value)
{
   RenderableImage * image = new RenderableImage();
   image->setImage(stringToImage(value));
   image->setHorzFormatting(RenderableImage::HorzFormatting::HorzStretched);
   image->setVertFormatting(RenderableImage::VertFormatting::VertStretched);
   static_cast<TLButton*>(receiver)->setPushedImage(image);
}

String HoverImage::get(const void *receiver) const
{
   //TLButton currently has no getter for this...
   return "";//PropertyHelper::imageToString(static_cast<const TLButton*>(receiver)->getNormal());
}

void HoverImage::set(void *receiver, const String &value)
{
   RenderableImage * image = new RenderableImage();
   image->setImage(stringToImage(value));
   image->setHorzFormatting(RenderableImage::HorzFormatting::HorzStretched);
   image->setVertFormatting(RenderableImage::VertFormatting::VertStretched);
   static_cast<TLButton*>(receiver)->setHoverImage(image);
}

String UseStandardImagery::get(const void *receiver) const
{
   return boolToString(static_cast<const TLButton*>(receiver)->isStandardImageryEnabled());
}

void UseStandardImagery::set(void *receiver, const String &value)
{
   static_cast<TLButton*>(receiver)->setStandardImageryEnabled(stringToBool(value));
}

String TextXOffset::get(const void *receiver) const
{
   return floatToString(static_cast<const TLButton*>(receiver)->getTextXOffset());
}

void TextXOffset::set(void *receiver, const String &value)
{
   static_cast<TLButton*>(receiver)->setTextXOffset(stringToFloat(value));
}

}

};
[/code]

User avatar
CrazyEddie
CEGUI Project Lead
Posts: 6760
Joined: Wed Jan 12, 2005 12:06
Location: England
Contact:

Patch for some TLButton-Properties

Postby CrazyEddie » Mon Sep 20, 2004 17:55

Thanks, I'll give this a look over and probably add it to CVS later in the week :)

CE.


Return to “CEGUI Library Development Discussion”

Who is online

Users browsing this forum: No registered users and 3 guests