3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "guiButtonImage.h"
22 #include "client/guiscalingfilter.h"
24 #include "IGUIEnvironment.h"
25 #include "IGUIImage.h"
26 #include "IVideoDriver.h"
27 #include "StyleSpec.h"
32 GUIButtonImage::GUIButtonImage(gui::IGUIEnvironment *environment,
33 gui::IGUIElement *parent, s32 id, core::rect<s32> rectangle, bool noclip)
34 : GUIButton (environment, parent, id, rectangle, noclip)
36 m_image = Environment->addImage(
37 core::rect<s32>(0,0,rectangle.getWidth(),rectangle.getHeight()), this);
38 m_image->setScaleImage(isScalingImage());
42 bool GUIButtonImage::OnEvent(const SEvent& event)
44 bool result = GUIButton::OnEvent(event);
46 EGUI_BUTTON_IMAGE_STATE imageState = getImageState(isPressed(), m_foreground_images);
47 video::ITexture *texture = m_foreground_images[(u32)imageState].Texture;
48 if (texture != nullptr)
50 m_image->setImage(texture);
53 m_image->setVisible(texture != nullptr);
58 void GUIButtonImage::setForegroundImage(EGUI_BUTTON_IMAGE_STATE state,
59 video::ITexture *image, const core::rect<s32> &sourceRect)
61 if (state >= EGBIS_COUNT)
67 u32 stateIdx = (u32)state;
68 if (m_foreground_images[stateIdx].Texture)
69 m_foreground_images[stateIdx].Texture->drop();
71 m_foreground_images[stateIdx].Texture = image;
72 m_foreground_images[stateIdx].SourceRect = sourceRect;
74 EGUI_BUTTON_IMAGE_STATE imageState = getImageState(isPressed(), m_foreground_images);
75 if (imageState == stateIdx)
76 m_image->setImage(image);
79 void GUIButtonImage::setForegroundImage(video::ITexture *image)
81 setForegroundImage(gui::EGBIS_IMAGE_UP, image);
84 void GUIButtonImage::setForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
86 setForegroundImage(gui::EGBIS_IMAGE_UP, image, pos);
89 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image)
91 setForegroundImage(gui::EGBIS_IMAGE_DOWN, image);
94 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
96 setForegroundImage(gui::EGBIS_IMAGE_DOWN, image, pos);
99 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image)
101 setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image);
102 setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image);
105 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
107 setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image, pos);
108 setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image, pos);
111 void GUIButtonImage::setFromStyle(const StyleSpec &style, ISimpleTextureSource *tsrc)
113 GUIButton::setFromStyle(style, tsrc);
115 video::IVideoDriver *driver = Environment->getVideoDriver();
117 const core::position2di buttonCenter(AbsoluteRect.getCenter());
118 core::position2d<s32> geom(buttonCenter);
119 if (style.isNotDefault(StyleSpec::FGIMG)) {
120 video::ITexture *texture = style.getTexture(StyleSpec::FGIMG, tsrc);
122 setForegroundImage(guiScalingImageButton(driver, texture, geom.X, geom.Y));
125 if (style.isNotDefault(StyleSpec::FGIMG_HOVERED)) {
126 video::ITexture *hovered_texture = style.getTexture(StyleSpec::FGIMG_HOVERED, tsrc);
128 setHoveredForegroundImage(guiScalingImageButton(driver, hovered_texture, geom.X, geom.Y));
131 if (style.isNotDefault(StyleSpec::FGIMG_PRESSED)) {
132 video::ITexture *pressed_texture = style.getTexture(StyleSpec::FGIMG_PRESSED, tsrc);
134 setPressedForegroundImage(guiScalingImageButton(driver, pressed_texture, geom.X, geom.Y));
139 void GUIButtonImage::setScaleImage(bool scaleImage)
141 GUIButton::setScaleImage(scaleImage);
142 m_image->setScaleImage(scaleImage);
145 GUIButtonImage *GUIButtonImage::addButton(IGUIEnvironment *environment,
146 const core::rect<s32> &rectangle, IGUIElement *parent, s32 id,
147 const wchar_t *text, const wchar_t *tooltiptext)
149 GUIButtonImage *button = new GUIButtonImage(environment,
150 parent ? parent : environment->getRootGUIElement(), id, rectangle);
153 button->setText(text);
156 button->setToolTipText(tooltiptext);