summaryrefslogtreecommitdiff
path: root/src/gui/guiButton.cpp
diff options
context:
space:
mode:
authorHugues Ross <hugues.ross@gmail.com>2019-12-09 15:06:51 -0500
committerrubenwardy <rw@rubenwardy.com>2019-12-09 20:06:51 +0000
commit9284313d17ba906de449c6d39ef3ddacb6c73791 (patch)
treedd1ce7e801fbd5ed9e8a7446560d3343ffb2f15a /src/gui/guiButton.cpp
parenta462181e5fe631b816cb67a4e2cb8a870ed71840 (diff)
downloadminetest-9284313d17ba906de449c6d39ef3ddacb6c73791.tar.gz
minetest-9284313d17ba906de449c6d39ef3ddacb6c73791.tar.bz2
minetest-9284313d17ba906de449c6d39ef3ddacb6c73791.zip
Refactor to centralize GUIButton styling/rendering code (#9090)
Diffstat (limited to 'src/gui/guiButton.cpp')
-rw-r--r--src/gui/guiButton.cpp115
1 files changed, 94 insertions, 21 deletions
diff --git a/src/gui/guiButton.cpp b/src/gui/guiButton.cpp
index 6ed5d3772..6518bbd1f 100644
--- a/src/gui/guiButton.cpp
+++ b/src/gui/guiButton.cpp
@@ -5,11 +5,15 @@
#include "guiButton.h"
+#include "client/guiscalingfilter.h"
+#include "client/tile.h"
#include "IGUISkin.h"
#include "IGUIEnvironment.h"
#include "IVideoDriver.h"
#include "IGUIFont.h"
+#include "irrlicht_changes/static_text.h"
#include "porting.h"
+#include "StyleSpec.h"
using namespace irr;
using namespace gui;
@@ -49,6 +53,9 @@ GUIButton::GUIButton(IGUIEnvironment* environment, IGUIElement* parent,
core::clamp<u32>(Colors[i].getGreen() * COLOR_PRESSED_MOD, 0, 255),
core::clamp<u32>(Colors[i].getBlue() * COLOR_PRESSED_MOD, 0, 255));
}
+
+ StaticText = gui::StaticText::add(Environment, Text.c_str(), core::rect<s32>(0,0,rectangle.getWidth(),rectangle.getHeight()), false, false, this, id);
+ StaticText->setTextAlignment(EGUIA_CENTER, EGUIA_CENTER);
// END PATCH
}
@@ -262,7 +269,7 @@ void GUIButton::draw()
{
// PATCH
skin->drawColored3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect,
- Environment->getHovered() == this ? HoveredColors : Colors);
+ isHovered() ? HoveredColors : Colors);
// END PATCH
}
else
@@ -318,7 +325,7 @@ void GUIButton::draw()
drawSprite(state, FocusTime, pos);
// mouse over / off animation
- state = Environment->getHovered() == this ? EGBS_BUTTON_MOUSE_OVER : EGBS_BUTTON_MOUSE_OFF;
+ state = isHovered() ? EGBS_BUTTON_MOUSE_OVER : EGBS_BUTTON_MOUSE_OFF;
drawSprite(state, HoverTime, pos);
}
else
@@ -328,23 +335,6 @@ void GUIButton::draw()
}
}
- if (Text.size())
- {
- IGUIFont* font = getActiveFont();
-
- core::rect<s32> rect = AbsoluteRect;
- if (Pressed)
- {
- rect.UpperLeftCorner.X += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_X);
- rect.UpperLeftCorner.Y += skin->getSize(EGDS_BUTTON_PRESSED_TEXT_OFFSET_Y);
- }
-
- if (font)
- font->draw(Text.c_str(), rect,
- OverrideColorEnabled ? OverrideColor : skin->getColor(isEnabled() ? EGDC_BUTTON_TEXT : EGDC_GRAY_TEXT),
- true, true, &AbsoluteClippingRect);
- }
-
IGUIElement::draw();
}
@@ -372,10 +362,17 @@ void GUIButton::drawSprite(EGUI_BUTTON_STATE state, u32 startTime, const core::p
EGUI_BUTTON_IMAGE_STATE GUIButton::getImageState(bool pressed) const
{
+ // PATCH
+ return getImageState(pressed, ButtonImages);
+ // END PATCH
+}
+
+EGUI_BUTTON_IMAGE_STATE GUIButton::getImageState(bool pressed, const ButtonImage* images) const
+{
// figure state we should have
EGUI_BUTTON_IMAGE_STATE state = EGBIS_IMAGE_DISABLED;
bool focused = Environment->hasFocus((IGUIElement*)this);
- bool mouseOver = static_cast<const IGUIElement*>(Environment->getHovered()) == this; // (static cast for Borland)
+ bool mouseOver = isHovered();
if (isEnabled())
{
if ( pressed )
@@ -403,7 +400,7 @@ EGUI_BUTTON_IMAGE_STATE GUIButton::getImageState(bool pressed) const
}
// find a compatible state that has images
- while ( state != EGBIS_IMAGE_UP && !ButtonImages[(u32)state].Texture )
+ while ( state != EGBIS_IMAGE_UP && !images[(u32)state].Texture )
{
// PATCH
switch ( state )
@@ -451,6 +448,8 @@ void GUIButton::setOverrideFont(IGUIFont* font)
if (OverrideFont)
OverrideFont->grab();
+
+ StaticText->setOverrideFont(font);
}
//! Gets the override font (if any)
@@ -475,6 +474,8 @@ void GUIButton::setOverrideColor(video::SColor color)
{
OverrideColor = color;
OverrideColorEnabled = true;
+
+ StaticText->setOverrideColor(color);
}
video::SColor GUIButton::getOverrideColor() const
@@ -540,6 +541,14 @@ void GUIButton::setHoveredImage(video::ITexture* image, const core::rect<s32>& p
setImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image, pos);
setImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image, pos);
}
+
+//! Sets the text displayed by the button
+void GUIButton::setText(const wchar_t* text)
+{
+ StaticText->setText(text);
+
+ IGUIButton::setText(text);
+}
// END PATCH
//! Sets if the button should behave like a push button. Which means it
@@ -557,6 +566,14 @@ bool GUIButton::isPressed() const
return Pressed;
}
+// PATCH
+//! Returns if this element (or one of its direct children) is hovered
+bool GUIButton::isHovered() const
+{
+ IGUIElement *hovered = Environment->getHovered();
+ return hovered == this || (hovered != nullptr && hovered->getParent() == this);
+}
+// END PATCH
//! Sets the pressed state of the button if this is a pushbutton
void GUIButton::setPressed(bool pressed)
@@ -565,6 +582,24 @@ void GUIButton::setPressed(bool pressed)
{
ClickTime = porting::getTimeMs();
Pressed = pressed;
+
+ GUISkin* skin = dynamic_cast<GUISkin*>(Environment->getSkin());
+
+ for(IGUIElement *child : getChildren())
+ {
+ core::rect<s32> originalRect = child->getRelativePosition();
+ if (Pressed) {
+ child->setRelativePosition(originalRect +
+ core::dimension2d<s32>(
+ skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
+ skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y)));
+ } else {
+ child->setRelativePosition(originalRect -
+ core::dimension2d<s32>(
+ skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
+ skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y)));
+ }
+ }
}
}
@@ -722,4 +757,42 @@ void GUIButton::setPressedColor(video::SColor color)
PressedColors[i] = base.getInterpolated(color, d);
}
}
+
+//! Set element properties from a StyleSpec
+void GUIButton::setFromStyle(const StyleSpec& style, ISimpleTextureSource *tsrc)
+{
+ if (style.isNotDefault(StyleSpec::BGCOLOR)) {
+ setColor(style.getColor(StyleSpec::BGCOLOR));
+ }
+ if (style.isNotDefault(StyleSpec::BGCOLOR_HOVERED)) {
+ setHoveredColor(style.getColor(StyleSpec::BGCOLOR_HOVERED));
+ }
+ if (style.isNotDefault(StyleSpec::BGCOLOR_PRESSED)) {
+ setPressedColor(style.getColor(StyleSpec::BGCOLOR_PRESSED));
+ }
+
+ if (style.isNotDefault(StyleSpec::TEXTCOLOR)) {
+ setOverrideColor(style.getColor(StyleSpec::TEXTCOLOR));
+ }
+ setNotClipped(style.getBool(StyleSpec::NOCLIP, isNotClipped()));
+ setDrawBorder(style.getBool(StyleSpec::BORDER, DrawBorder));
+ setUseAlphaChannel(style.getBool(StyleSpec::ALPHA, true));
+
+ if (style.isNotDefault(StyleSpec::BGIMG)) {
+ video::ITexture *texture = style.getTexture(StyleSpec::BGIMG, tsrc);
+ video::ITexture *hovered_texture = style.getTexture(StyleSpec::BGIMG_HOVERED, tsrc, texture);
+ video::ITexture *pressed_texture = style.getTexture(StyleSpec::BGIMG_PRESSED, tsrc, texture);
+
+ const core::position2di buttonCenter(AbsoluteRect.getCenter());
+ core::position2d<s32> geom(buttonCenter);
+
+ setImage(guiScalingImageButton(
+ Environment->getVideoDriver(), texture, geom.X, geom.Y));
+ setHoveredImage(guiScalingImageButton(
+ Environment->getVideoDriver(), hovered_texture, geom.X, geom.Y));
+ setPressedImage(guiScalingImageButton(
+ Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
+ setScaleImage(true);
+ }
+}
// END PATCH