From 9284313d17ba906de449c6d39ef3ddacb6c73791 Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Mon, 9 Dec 2019 15:06:51 -0500 Subject: Refactor to centralize GUIButton styling/rendering code (#9090) --- src/gui/guiButton.cpp | 115 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 94 insertions(+), 21 deletions(-) (limited to 'src/gui/guiButton.cpp') 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(Colors[i].getGreen() * COLOR_PRESSED_MOD, 0, 255), core::clamp(Colors[i].getBlue() * COLOR_PRESSED_MOD, 0, 255)); } + + StaticText = gui::StaticText::add(Environment, Text.c_str(), core::rect(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 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(); } @@ -371,11 +361,18 @@ 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(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& 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(Environment->getSkin()); + + for(IGUIElement *child : getChildren()) + { + core::rect originalRect = child->getRelativePosition(); + if (Pressed) { + child->setRelativePosition(originalRect + + core::dimension2d( + 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( + 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 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 -- cgit v1.2.3