From 13fb0bb3ac29b3808f0fb9e8190f2b4b24f7be13 Mon Sep 17 00:00:00 2001 From: hybrid Date: Mon, 3 Aug 2009 12:18:47 +0000 Subject: [PATCH] Add support for scaling button images to fit. git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@2548 dfc29bdd-3216-0410-991c-e03cc46cb475 --- changes.txt | 6 ++- include/IGUIButton.h | 20 +++++---- source/Irrlicht/CGUIButton.cpp | 75 ++++++++++++++++++++++------------ source/Irrlicht/CGUIButton.h | 57 ++++++++++++++------------ 4 files changed, 97 insertions(+), 61 deletions(-) diff --git a/changes.txt b/changes.txt index 7d0df4e8..65100997 100644 --- a/changes.txt +++ b/changes.txt @@ -1,5 +1,7 @@ -Changes in 1.6 (??.??.2009) - +Changes in 1.6 (??.??.2009) + + - Add support for scaling button images. + - Irrlicht can now come with multiple device types compiled in, the device to use is selected by SIrrlichtCreationParameters.DeviceType. This defaults to EIDT_BEST which automatically select the best device available starting with native, then X11, SDL and finally the console. - Added support for EXP2 fog distribution. This required a change in the setFog parameters where now an enum value instead of the bool linear is given. diff --git a/include/IGUIButton.h b/include/IGUIButton.h index 41127eb5..16d80505 100644 --- a/include/IGUIButton.h +++ b/include/IGUIButton.h @@ -67,7 +67,7 @@ namespace gui //! Sets an image which should be displayed on the button when it is in normal state. /** \param image: Image to be displayed */ - virtual void setImage(video::ITexture* image) = 0; + virtual void setImage(video::ITexture* image=0) = 0; //! Sets a background image for the button when it is in normal state. /** \param image: Texture containing the image to be displayed @@ -78,7 +78,7 @@ namespace gui /** If no images is specified for the pressed state via setPressedImage(), this image is also drawn in pressed state. \param image: Image to be displayed */ - virtual void setPressedImage(video::ITexture* image) = 0; + virtual void setPressedImage(video::ITexture* image=0) = 0; //! Sets an image which should be displayed on the button when it is in pressed state. /** \param image: Texture containing the image to be displayed @@ -86,7 +86,7 @@ namespace gui virtual void setPressedImage(video::ITexture* image, const core::rect& pos) = 0; //! Sets the sprite bank used by the button - virtual void setSpriteBank(IGUISpriteBank* bank) = 0; + virtual void setSpriteBank(IGUISpriteBank* bank=0) = 0; //! Sets the animated sprite for a specific button state /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite @@ -101,16 +101,16 @@ namespace gui //! Sets if the button should behave like a push button. /** Which means it can be in two states: Normal or Pressed. With a click on the button, the user can change the state of the button. */ - virtual void setIsPushButton(bool isPushButton) = 0; + virtual void setIsPushButton(bool isPushButton=true) = 0; //! Sets the pressed state of the button if this is a pushbutton - virtual void setPressed(bool pressed) = 0; + virtual void setPressed(bool pressed=true) = 0; //! Returns if the button is currently pressed virtual bool isPressed() const = 0; //! Sets if the alpha channel should be used for drawing background images on the button (default is false) - virtual void setUseAlphaChannel(bool useAlphaChannel) = 0; + virtual void setUseAlphaChannel(bool useAlphaChannel=true) = 0; //! Returns if the alpha channel should be used for drawing background images on the button virtual bool isAlphaChannelUsed() const = 0; @@ -119,10 +119,16 @@ namespace gui virtual bool isPushButton() const = 0; //! Sets if the button should use the skin to draw its border and button face (default is true) - virtual void setDrawBorder(bool border) = 0; + virtual void setDrawBorder(bool border=true) = 0; //! Returns if the border and button face are being drawn using the skin virtual bool isDrawingBorder() const = 0; + + //! Sets if the button should scale the button images to fit + virtual void setScaleImage(bool scaleImage=true) = 0; + + //! Checks whether the button scales the used images + virtual bool isScalingImage() const = 0; }; diff --git a/source/Irrlicht/CGUIButton.cpp b/source/Irrlicht/CGUIButton.cpp index 4b7af2eb..b1c0912f 100644 --- a/source/Irrlicht/CGUIButton.cpp +++ b/source/Irrlicht/CGUIButton.cpp @@ -19,9 +19,10 @@ namespace gui //! constructor CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect rectangle, bool noclip) -: IGUIButton(environment, parent, id, rectangle), Pressed(false), - IsPushButton(false), UseAlphaChannel(false), Border(true), - ClickTime(0), SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0) +: IGUIButton(environment, parent, id, rectangle), + SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0), + ClickTime(0), IsPushButton(false), Pressed(false), + UseAlphaChannel(false), DrawBorder(true), ScaleImage(false) { #ifdef _DEBUG setDebugName("CGUIButton"); @@ -55,10 +56,25 @@ CGUIButton::~CGUIButton() } +//! Sets if the images should be scaled to fit the button +void CGUIButton::setScaleImage(bool scaleImage) +{ + ScaleImage = scaleImage; +} + + +//! Returns whether the button scale the used images +bool CGUIButton::isScalingImage() const +{ + _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; + return ScaleImage; +} + + //! Sets if the button should use the skin to draw its border void CGUIButton::setDrawBorder(bool border) { - Border = border; + DrawBorder = border; } @@ -208,29 +224,26 @@ void CGUIButton::draw() IGUISkin* skin = Environment->getSkin(); video::IVideoDriver* driver = Environment->getVideoDriver(); - IGUIFont* font = OverrideFont; - if (!OverrideFont) - font = skin->getFont(EGDF_BUTTON); - - core::rect rect = AbsoluteRect; - // todo: move sprite up and text down if the pressed state has a sprite // draw sprites for focused and mouse-over - core::position2di spritePos = AbsoluteRect.getCenter(); + const core::position2di spritePos = AbsoluteRect.getCenter(); if (!Pressed) { - if (Border) - skin->draw3DButtonPaneStandard(this, rect, &AbsoluteClippingRect); + if (DrawBorder) + skin->draw3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect); if (Image) { - core::position2d pos = AbsoluteRect.getCenter(); + core::position2d pos = spritePos; pos.X -= ImageRect.getWidth() / 2; pos.Y -= ImageRect.getHeight() / 2; - driver->draw2DImage(Image, pos, ImageRect, &AbsoluteClippingRect, - video::SColor(255,255,255,255), UseAlphaChannel); + driver->draw2DImage(Image, + ScaleImage? AbsoluteRect : + core::recti(pos, ImageRect.getSize()), + ImageRect, &AbsoluteClippingRect, + 0, UseAlphaChannel); } if (SpriteBank && ButtonSprites[EGBS_BUTTON_UP].Index != -1) { @@ -242,12 +255,12 @@ void CGUIButton::draw() } else { - if (Border) - skin->draw3DButtonPanePressed(this, rect, &AbsoluteClippingRect); + if (DrawBorder) + skin->draw3DButtonPanePressed(this, AbsoluteRect, &AbsoluteClippingRect); if (PressedImage) { - core::position2d pos = AbsoluteRect.getCenter(); + core::position2d pos = spritePos; pos.X -= PressedImageRect.getWidth() / 2; pos.Y -= PressedImageRect.getHeight() / 2; // patch by Alan Tyndall/Jonas Petersen @@ -256,8 +269,11 @@ void CGUIButton::draw() pos.X += 1; pos.Y += 1; } - driver->draw2DImage(PressedImage, pos, PressedImageRect, &AbsoluteClippingRect, - video::SColor(255,255,255,255), UseAlphaChannel); + driver->draw2DImage(PressedImage, + ScaleImage? AbsoluteRect : + core::recti(pos, PressedImageRect.getSize()), + PressedImageRect, &AbsoluteClippingRect, + 0, UseAlphaChannel); } if (SpriteBank && ButtonSprites[EGBS_BUTTON_DOWN].Index != -1) @@ -267,12 +283,15 @@ void CGUIButton::draw() &AbsoluteClippingRect, ButtonSprites[EGBS_BUTTON_DOWN].Color, ClickTime, os::Timer::getTime(), ButtonSprites[EGBS_BUTTON_DOWN].Loop, true); } - } if (Text.size()) { - rect = AbsoluteRect; + IGUIFont* font = OverrideFont; + if (!OverrideFont) + font = skin->getFont(EGDF_BUTTON); + + core::rect rect = AbsoluteRect; if (Pressed) rect.UpperLeftCorner.Y += 2; @@ -404,7 +423,7 @@ bool CGUIButton::isAlphaChannelUsed() const bool CGUIButton::isDrawingBorder() const { _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; - return Border; + return DrawBorder; } @@ -422,8 +441,9 @@ void CGUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWri out->addTexture ("PressedImage", PressedImage); out->addRect ("PressedImageRect", PressedImageRect); - out->addBool ("Border", Border); - out->addBool ("UseAlphaChannel", UseAlphaChannel); + out->addBool ("UseAlphaChannel", isAlphaChannelUsed()); + out->addBool ("Border", isDrawingBorder()); + out->addBool ("ScaleImage", isScalingImage()); // out->addString ("OverrideFont", OverrideFont); } @@ -450,7 +470,8 @@ void CGUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWr setPressedImage( in->getAttributeAsTexture("PressedImage") ); setDrawBorder(in->getAttributeAsBool("Border")); - UseAlphaChannel = in->getAttributeAsBool("UseAlphaChannel"); + setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel")); + setScaleImage(in->getAttributeAsBool("ScaleImage")); // setOverrideFont(in->getAttributeAsString("OverrideFont")); diff --git a/source/Irrlicht/CGUIButton.h b/source/Irrlicht/CGUIButton.h index 5b7530e6..2a1119cf 100644 --- a/source/Irrlicht/CGUIButton.h +++ b/source/Irrlicht/CGUIButton.h @@ -38,19 +38,19 @@ namespace gui virtual void setOverrideFont(IGUIFont* font=0); //! Sets an image which should be displayed on the button when it is in normal state. - virtual void setImage(video::ITexture* image); + virtual void setImage(video::ITexture* image=0); //! Sets an image which should be displayed on the button when it is in normal state. virtual void setImage(video::ITexture* image, const core::rect& pos); //! Sets an image which should be displayed on the button when it is in pressed state. - virtual void setPressedImage(video::ITexture* image); + virtual void setPressedImage(video::ITexture* image=0); //! Sets an image which should be displayed on the button when it is in pressed state. virtual void setPressedImage(video::ITexture* image, const core::rect& pos); //! Sets the sprite bank used by the button - virtual void setSpriteBank(IGUISpriteBank* bank); + virtual void setSpriteBank(IGUISpriteBank* bank=0); //! Sets the animated sprite for a specific button state /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite @@ -64,28 +64,34 @@ namespace gui //! Sets if the button should behave like a push button. Which means it //! can be in two states: Normal or Pressed. With a click on the button, //! the user can change the state of the button. - virtual void setIsPushButton(bool isPushButton); + virtual void setIsPushButton(bool isPushButton=true); + + //! Checks whether the button is a push button + virtual bool isPushButton() const; + + //! Sets the pressed state of the button if this is a pushbutton + virtual void setPressed(bool pressed=true); //! Returns if the button is currently pressed virtual bool isPressed() const; - //! Sets the pressed state of the button if this is a pushbutton - virtual void setPressed(bool pressed); - //! Sets if the button should use the skin to draw its border - virtual void setDrawBorder(bool border); + virtual void setDrawBorder(bool border=true); - //! Sets if the alpha channel should be used for drawing images on the button (default is false) - virtual void setUseAlphaChannel(bool useAlphaChannel); - - //! Returns if the alpha channel should be used for drawing images on the button - virtual bool isAlphaChannelUsed() const; - - //! Returns if the button face and border are being drawn + //! Checks if the button face and border are being drawn virtual bool isDrawingBorder() const; - //! Returns whether the button is a push button - virtual bool isPushButton() const; + //! Sets if the alpha channel should be used for drawing images on the button (default is false) + virtual void setUseAlphaChannel(bool useAlphaChannel=true); + + //! Checks if the alpha channel should be used for drawing images on the button + virtual bool isAlphaChannelUsed() const; + + //! Sets if the button should scale the button images to fit + virtual void setScaleImage(bool scaleImage=true); + + //! Checks whether the button scales the used images + virtual bool isScalingImage() const; //! Writes attributes of the element. virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const; @@ -102,23 +108,24 @@ namespace gui bool Loop; }; - bool Pressed; - bool IsPushButton; - bool UseAlphaChannel; - bool Border; - - u32 ClickTime; + ButtonSprite ButtonSprites[EGBS_COUNT]; IGUISpriteBank* SpriteBank; IGUIFont* OverrideFont; - ButtonSprite ButtonSprites[EGBS_COUNT]; - video::ITexture* Image; video::ITexture* PressedImage; core::rect ImageRect; core::rect PressedImageRect; + + u32 ClickTime; + + bool IsPushButton; + bool Pressed; + bool UseAlphaChannel; + bool DrawBorder; + bool ScaleImage; }; } // end namespace gui