Commit 13fb0bb3 authored by hybrid's avatar hybrid

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
parent 9d2dec96
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. - 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. - 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.
......
...@@ -67,7 +67,7 @@ namespace gui ...@@ -67,7 +67,7 @@ namespace gui
//! Sets an image which should be displayed on the button when it is in normal state. //! Sets an image which should be displayed on the button when it is in normal state.
/** \param image: Image to be displayed */ /** \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. //! Sets a background image for the button when it is in normal state.
/** \param image: Texture containing the image to be displayed /** \param image: Texture containing the image to be displayed
...@@ -78,7 +78,7 @@ namespace gui ...@@ -78,7 +78,7 @@ namespace gui
/** If no images is specified for the pressed state via /** If no images is specified for the pressed state via
setPressedImage(), this image is also drawn in pressed state. setPressedImage(), this image is also drawn in pressed state.
\param image: Image to be displayed */ \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. //! 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 /** \param image: Texture containing the image to be displayed
...@@ -86,7 +86,7 @@ namespace gui ...@@ -86,7 +86,7 @@ namespace gui
virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) = 0; virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) = 0;
//! Sets the sprite bank used by the button //! 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 //! Sets the animated sprite for a specific button state
/** \param index: Number of the sprite within the sprite bank, use -1 for no sprite /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite
...@@ -101,16 +101,16 @@ namespace gui ...@@ -101,16 +101,16 @@ namespace gui
//! Sets if the button should behave like a push button. //! 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, /** 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. */ 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 //! 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 //! Returns if the button is currently pressed
virtual bool isPressed() const = 0; virtual bool isPressed() const = 0;
//! Sets if the alpha channel should be used for drawing background images on the button (default is false) //! 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 //! Returns if the alpha channel should be used for drawing background images on the button
virtual bool isAlphaChannelUsed() const = 0; virtual bool isAlphaChannelUsed() const = 0;
...@@ -119,10 +119,16 @@ namespace gui ...@@ -119,10 +119,16 @@ namespace gui
virtual bool isPushButton() const = 0; virtual bool isPushButton() const = 0;
//! Sets if the button should use the skin to draw its border and button face (default is true) //! 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 //! Returns if the border and button face are being drawn using the skin
virtual bool isDrawingBorder() const = 0; 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;
}; };
......
...@@ -19,9 +19,10 @@ namespace gui ...@@ -19,9 +19,10 @@ namespace gui
//! constructor //! constructor
CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent, CGUIButton::CGUIButton(IGUIEnvironment* environment, IGUIElement* parent,
s32 id, core::rect<s32> rectangle, bool noclip) s32 id, core::rect<s32> rectangle, bool noclip)
: IGUIButton(environment, parent, id, rectangle), Pressed(false), : IGUIButton(environment, parent, id, rectangle),
IsPushButton(false), UseAlphaChannel(false), Border(true), SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0),
ClickTime(0), SpriteBank(0), OverrideFont(0), Image(0), PressedImage(0) ClickTime(0), IsPushButton(false), Pressed(false),
UseAlphaChannel(false), DrawBorder(true), ScaleImage(false)
{ {
#ifdef _DEBUG #ifdef _DEBUG
setDebugName("CGUIButton"); setDebugName("CGUIButton");
...@@ -55,10 +56,25 @@ CGUIButton::~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 //! Sets if the button should use the skin to draw its border
void CGUIButton::setDrawBorder(bool border) void CGUIButton::setDrawBorder(bool border)
{ {
Border = border; DrawBorder = border;
} }
...@@ -208,29 +224,26 @@ void CGUIButton::draw() ...@@ -208,29 +224,26 @@ void CGUIButton::draw()
IGUISkin* skin = Environment->getSkin(); IGUISkin* skin = Environment->getSkin();
video::IVideoDriver* driver = Environment->getVideoDriver(); video::IVideoDriver* driver = Environment->getVideoDriver();
IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont(EGDF_BUTTON);
core::rect<s32> rect = AbsoluteRect;
// todo: move sprite up and text down if the pressed state has a sprite // todo: move sprite up and text down if the pressed state has a sprite
// draw sprites for focused and mouse-over // draw sprites for focused and mouse-over
core::position2di spritePos = AbsoluteRect.getCenter(); const core::position2di spritePos = AbsoluteRect.getCenter();
if (!Pressed) if (!Pressed)
{ {
if (Border) if (DrawBorder)
skin->draw3DButtonPaneStandard(this, rect, &AbsoluteClippingRect); skin->draw3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect);
if (Image) if (Image)
{ {
core::position2d<s32> pos = AbsoluteRect.getCenter(); core::position2d<s32> pos = spritePos;
pos.X -= ImageRect.getWidth() / 2; pos.X -= ImageRect.getWidth() / 2;
pos.Y -= ImageRect.getHeight() / 2; pos.Y -= ImageRect.getHeight() / 2;
driver->draw2DImage(Image, pos, ImageRect, &AbsoluteClippingRect, driver->draw2DImage(Image,
video::SColor(255,255,255,255), UseAlphaChannel); ScaleImage? AbsoluteRect :
core::recti(pos, ImageRect.getSize()),
ImageRect, &AbsoluteClippingRect,
0, UseAlphaChannel);
} }
if (SpriteBank && ButtonSprites[EGBS_BUTTON_UP].Index != -1) if (SpriteBank && ButtonSprites[EGBS_BUTTON_UP].Index != -1)
{ {
...@@ -242,12 +255,12 @@ void CGUIButton::draw() ...@@ -242,12 +255,12 @@ void CGUIButton::draw()
} }
else else
{ {
if (Border) if (DrawBorder)
skin->draw3DButtonPanePressed(this, rect, &AbsoluteClippingRect); skin->draw3DButtonPanePressed(this, AbsoluteRect, &AbsoluteClippingRect);
if (PressedImage) if (PressedImage)
{ {
core::position2d<s32> pos = AbsoluteRect.getCenter(); core::position2d<s32> pos = spritePos;
pos.X -= PressedImageRect.getWidth() / 2; pos.X -= PressedImageRect.getWidth() / 2;
pos.Y -= PressedImageRect.getHeight() / 2; pos.Y -= PressedImageRect.getHeight() / 2;
// patch by Alan Tyndall/Jonas Petersen // patch by Alan Tyndall/Jonas Petersen
...@@ -256,8 +269,11 @@ void CGUIButton::draw() ...@@ -256,8 +269,11 @@ void CGUIButton::draw()
pos.X += 1; pos.X += 1;
pos.Y += 1; pos.Y += 1;
} }
driver->draw2DImage(PressedImage, pos, PressedImageRect, &AbsoluteClippingRect, driver->draw2DImage(PressedImage,
video::SColor(255,255,255,255), UseAlphaChannel); ScaleImage? AbsoluteRect :
core::recti(pos, PressedImageRect.getSize()),
PressedImageRect, &AbsoluteClippingRect,
0, UseAlphaChannel);
} }
if (SpriteBank && ButtonSprites[EGBS_BUTTON_DOWN].Index != -1) if (SpriteBank && ButtonSprites[EGBS_BUTTON_DOWN].Index != -1)
...@@ -267,12 +283,15 @@ void CGUIButton::draw() ...@@ -267,12 +283,15 @@ void CGUIButton::draw()
&AbsoluteClippingRect, ButtonSprites[EGBS_BUTTON_DOWN].Color, ClickTime, os::Timer::getTime(), &AbsoluteClippingRect, ButtonSprites[EGBS_BUTTON_DOWN].Color, ClickTime, os::Timer::getTime(),
ButtonSprites[EGBS_BUTTON_DOWN].Loop, true); ButtonSprites[EGBS_BUTTON_DOWN].Loop, true);
} }
} }
if (Text.size()) if (Text.size())
{ {
rect = AbsoluteRect; IGUIFont* font = OverrideFont;
if (!OverrideFont)
font = skin->getFont(EGDF_BUTTON);
core::rect<s32> rect = AbsoluteRect;
if (Pressed) if (Pressed)
rect.UpperLeftCorner.Y += 2; rect.UpperLeftCorner.Y += 2;
...@@ -404,7 +423,7 @@ bool CGUIButton::isAlphaChannelUsed() const ...@@ -404,7 +423,7 @@ bool CGUIButton::isAlphaChannelUsed() const
bool CGUIButton::isDrawingBorder() const bool CGUIButton::isDrawingBorder() const
{ {
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX; _IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return Border; return DrawBorder;
} }
...@@ -422,8 +441,9 @@ void CGUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWri ...@@ -422,8 +441,9 @@ void CGUIButton::serializeAttributes(io::IAttributes* out, io::SAttributeReadWri
out->addTexture ("PressedImage", PressedImage); out->addTexture ("PressedImage", PressedImage);
out->addRect ("PressedImageRect", PressedImageRect); out->addRect ("PressedImageRect", PressedImageRect);
out->addBool ("Border", Border); out->addBool ("UseAlphaChannel", isAlphaChannelUsed());
out->addBool ("UseAlphaChannel", UseAlphaChannel); out->addBool ("Border", isDrawingBorder());
out->addBool ("ScaleImage", isScalingImage());
// out->addString ("OverrideFont", OverrideFont); // out->addString ("OverrideFont", OverrideFont);
} }
...@@ -450,7 +470,8 @@ void CGUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWr ...@@ -450,7 +470,8 @@ void CGUIButton::deserializeAttributes(io::IAttributes* in, io::SAttributeReadWr
setPressedImage( in->getAttributeAsTexture("PressedImage") ); setPressedImage( in->getAttributeAsTexture("PressedImage") );
setDrawBorder(in->getAttributeAsBool("Border")); setDrawBorder(in->getAttributeAsBool("Border"));
UseAlphaChannel = in->getAttributeAsBool("UseAlphaChannel"); setUseAlphaChannel(in->getAttributeAsBool("UseAlphaChannel"));
setScaleImage(in->getAttributeAsBool("ScaleImage"));
// setOverrideFont(in->getAttributeAsString("OverrideFont")); // setOverrideFont(in->getAttributeAsString("OverrideFont"));
......
...@@ -38,19 +38,19 @@ namespace gui ...@@ -38,19 +38,19 @@ namespace gui
virtual void setOverrideFont(IGUIFont* font=0); virtual void setOverrideFont(IGUIFont* font=0);
//! Sets an image which should be displayed on the button when it is in normal state. //! 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. //! 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<s32>& pos); virtual void setImage(video::ITexture* image, const core::rect<s32>& pos);
//! Sets an image which should be displayed on the button when it is in pressed state. //! 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. //! 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<s32>& pos); virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos);
//! Sets the sprite bank used by the button //! 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 //! Sets the animated sprite for a specific button state
/** \param index: Number of the sprite within the sprite bank, use -1 for no sprite /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite
...@@ -64,28 +64,34 @@ namespace gui ...@@ -64,28 +64,34 @@ namespace gui
//! Sets if the button should behave like a push button. Which means it //! 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, //! can be in two states: Normal or Pressed. With a click on the button,
//! the user can change the state of the button. //! the user can change the state of the button.
virtual void setIsPushButton(bool isPushButton); virtual void setIsPushButton(bool isPushButton=true);
//! Returns if the button is currently pressed //! Checks whether the button is a push button
virtual bool isPressed() const; virtual bool isPushButton() const;
//! Sets the pressed state of the button if this is a pushbutton //! Sets the pressed state of the button if this is a pushbutton
virtual void setPressed(bool pressed); virtual void setPressed(bool pressed=true);
//! Returns if the button is currently pressed
virtual bool isPressed() const;
//! Sets if the button should use the skin to draw its border //! Sets if the button should use the skin to draw its border
virtual void setDrawBorder(bool border); virtual void setDrawBorder(bool border=true);
//! Checks if the button face and border are being drawn
virtual bool isDrawingBorder() const;
//! Sets if the alpha channel should be used for drawing images on the button (default is false) //! Sets if the alpha channel should be used for drawing images on the button (default is false)
virtual void setUseAlphaChannel(bool useAlphaChannel); virtual void setUseAlphaChannel(bool useAlphaChannel=true);
//! Returns if the alpha channel should be used for drawing images on the button //! Checks if the alpha channel should be used for drawing images on the button
virtual bool isAlphaChannelUsed() const; virtual bool isAlphaChannelUsed() const;
//! Returns if the button face and border are being drawn //! Sets if the button should scale the button images to fit
virtual bool isDrawingBorder() const; virtual void setScaleImage(bool scaleImage=true);
//! Returns whether the button is a push button //! Checks whether the button scales the used images
virtual bool isPushButton() const; virtual bool isScalingImage() const;
//! Writes attributes of the element. //! Writes attributes of the element.
virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const; virtual void serializeAttributes(io::IAttributes* out, io::SAttributeReadWriteOptions* options) const;
...@@ -102,23 +108,24 @@ namespace gui ...@@ -102,23 +108,24 @@ namespace gui
bool Loop; bool Loop;
}; };
bool Pressed; ButtonSprite ButtonSprites[EGBS_COUNT];
bool IsPushButton;
bool UseAlphaChannel;
bool Border;
u32 ClickTime;
IGUISpriteBank* SpriteBank; IGUISpriteBank* SpriteBank;
IGUIFont* OverrideFont; IGUIFont* OverrideFont;
ButtonSprite ButtonSprites[EGBS_COUNT];
video::ITexture* Image; video::ITexture* Image;
video::ITexture* PressedImage; video::ITexture* PressedImage;
core::rect<s32> ImageRect; core::rect<s32> ImageRect;
core::rect<s32> PressedImageRect; core::rect<s32> PressedImageRect;
u32 ClickTime;
bool IsPushButton;
bool Pressed;
bool UseAlphaChannel;
bool DrawBorder;
bool ScaleImage;
}; };
} // end namespace gui } // end namespace gui
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment