From 69a2099c04527404f2d0942f2088b3d22dd75b5a Mon Sep 17 00:00:00 2001 From: Hugues Ross Date: Sat, 12 Oct 2019 12:44:23 -0400 Subject: Add more visual feedback for button states (#8916) - Add style properties for overriding the the hovered/pressed state - By default, hovered buttons are a lighter version of the base color - By default, pressed buttons are a darker version of the base color - Add hovered bg image support for image buttons (style property) --- src/gui/guiButton.cpp | 84 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 80 insertions(+), 4 deletions(-) (limited to 'src/gui/guiButton.cpp') diff --git a/src/gui/guiButton.cpp b/src/gui/guiButton.cpp index 60d330f4a..6ed5d3772 100644 --- a/src/gui/guiButton.cpp +++ b/src/gui/guiButton.cpp @@ -14,6 +14,12 @@ using namespace irr; using namespace gui; +// Multiply with a color to get the default corresponding hovered color +#define COLOR_HOVERED_MOD 1.25f + +// Multiply with a color to get the default corresponding pressed color +#define COLOR_PRESSED_MOD 0.85f + //! constructor GUIButton::GUIButton(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect rectangle, bool noclip) @@ -34,6 +40,14 @@ GUIButton::GUIButton(IGUIEnvironment* environment, IGUIElement* parent, // PATCH for (size_t i = 0; i < 4; i++) { Colors[i] = Environment->getSkin()->getColor((EGUI_DEFAULT_COLOR)i); + HoveredColors[i] = irr::video::SColor(Colors[i].getAlpha(), + core::clamp(Colors[i].getRed() * COLOR_HOVERED_MOD, 0, 255), + core::clamp(Colors[i].getGreen() * COLOR_HOVERED_MOD, 0, 255), + core::clamp(Colors[i].getBlue() * COLOR_HOVERED_MOD, 0, 255)); + PressedColors[i] = irr::video::SColor(Colors[i].getAlpha(), + core::clamp(Colors[i].getRed() * COLOR_PRESSED_MOD, 0, 255), + core::clamp(Colors[i].getGreen() * COLOR_PRESSED_MOD, 0, 255), + core::clamp(Colors[i].getBlue() * COLOR_PRESSED_MOD, 0, 255)); } // END PATCH } @@ -247,13 +261,15 @@ void GUIButton::draw() if (!Pressed) { // PATCH - skin->drawColored3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect, Colors); + skin->drawColored3DButtonPaneStandard(this, AbsoluteRect, &AbsoluteClippingRect, + Environment->getHovered() == this ? HoveredColors : Colors); // END PATCH } else { // PATCH - skin->drawColored3DButtonPanePressed(this, AbsoluteRect, &AbsoluteClippingRect, Colors); + skin->drawColored3DButtonPanePressed(this, + AbsoluteRect, &AbsoluteClippingRect, PressedColors); // END PATCH } } @@ -389,10 +405,11 @@ 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 ) { + // PATCH switch ( state ) { case EGBIS_IMAGE_UP_FOCUSED: - state = EGBIS_IMAGE_UP_MOUSEOVER; + state = EGBIS_IMAGE_UP; break; case EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER: state = EGBIS_IMAGE_UP_FOCUSED; @@ -401,7 +418,7 @@ EGUI_BUTTON_IMAGE_STATE GUIButton::getImageState(bool pressed) const state = EGBIS_IMAGE_DOWN; break; case EGBIS_IMAGE_DOWN_FOCUSED: - state = EGBIS_IMAGE_DOWN_MOUSEOVER; + state = EGBIS_IMAGE_DOWN; break; case EGBIS_IMAGE_DOWN_FOCUSED_MOUSEOVER: state = EGBIS_IMAGE_DOWN_FOCUSED; @@ -415,6 +432,7 @@ EGUI_BUTTON_IMAGE_STATE GUIButton::getImageState(bool pressed) const default: state = EGBIS_IMAGE_UP; } + // END PATCH } return state; @@ -490,6 +508,40 @@ void GUIButton::setImage(EGUI_BUTTON_IMAGE_STATE state, video::ITexture* image, ButtonImages[stateIdx].SourceRect = sourceRect; } +// PATCH +void GUIButton::setImage(video::ITexture* image) +{ + setImage(gui::EGBIS_IMAGE_UP, image); +} + +void GUIButton::setImage(video::ITexture* image, const core::rect& pos) +{ + setImage(gui::EGBIS_IMAGE_UP, image, pos); +} + +void GUIButton::setPressedImage(video::ITexture* image) +{ + setImage(gui::EGBIS_IMAGE_DOWN, image); +} + +void GUIButton::setPressedImage(video::ITexture* image, const core::rect& pos) +{ + setImage(gui::EGBIS_IMAGE_DOWN, image, pos); +} + +void GUIButton::setHoveredImage(video::ITexture* image) +{ + setImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image); + setImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image); +} + +void GUIButton::setHoveredImage(video::ITexture* image, const core::rect& pos) +{ + setImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image, pos); + setImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image, pos); +} +// END PATCH + //! 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. @@ -644,6 +696,30 @@ void GUIButton::setColor(video::SColor color) for (size_t i = 0; i < 4; i++) { video::SColor base = Environment->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i); Colors[i] = base.getInterpolated(color, d); + HoveredColors[i] = irr::video::SColor(Colors[i].getAlpha(), + core::clamp(Colors[i].getRed() * COLOR_HOVERED_MOD, 0, 255), + core::clamp(Colors[i].getGreen() * COLOR_HOVERED_MOD, 0, 255), + core::clamp(Colors[i].getBlue() * COLOR_HOVERED_MOD, 0, 255)); + PressedColors[i] = irr::video::SColor(Colors[i].getAlpha(), + core::clamp(Colors[i].getRed() * COLOR_PRESSED_MOD, 0, 255), + core::clamp(Colors[i].getGreen() * COLOR_PRESSED_MOD, 0, 255), + core::clamp(Colors[i].getBlue() * COLOR_PRESSED_MOD, 0, 255)); + } +} +void GUIButton::setHoveredColor(video::SColor color) +{ + float d = 0.65f; + for (size_t i = 0; i < 4; i++) { + video::SColor base = Environment->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i); + HoveredColors[i] = base.getInterpolated(color, d); + } +} +void GUIButton::setPressedColor(video::SColor color) +{ + float d = 0.65f; + for (size_t i = 0; i < 4; i++) { + video::SColor base = Environment->getSkin()->getColor((gui::EGUI_DEFAULT_COLOR)i); + PressedColors[i] = base.getInterpolated(color, d); } } // END PATCH -- cgit v1.2.3