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)
49 m_image->setImage(texture);
54 void GUIButtonImage::setForegroundImage(EGUI_BUTTON_IMAGE_STATE state,
55 video::ITexture *image, const core::rect<s32> &sourceRect)
57 if (state >= EGBIS_COUNT)
63 u32 stateIdx = (u32)state;
64 if (m_foreground_images[stateIdx].Texture)
65 m_foreground_images[stateIdx].Texture->drop();
67 m_foreground_images[stateIdx].Texture = image;
68 m_foreground_images[stateIdx].SourceRect = sourceRect;
70 EGUI_BUTTON_IMAGE_STATE imageState = getImageState(isPressed(), m_foreground_images);
71 if (imageState == stateIdx)
72 m_image->setImage(image);
75 void GUIButtonImage::setForegroundImage(video::ITexture *image)
77 setForegroundImage(gui::EGBIS_IMAGE_UP, image);
80 void GUIButtonImage::setForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
82 setForegroundImage(gui::EGBIS_IMAGE_UP, image, pos);
85 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image)
87 setForegroundImage(gui::EGBIS_IMAGE_DOWN, image);
90 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
92 setForegroundImage(gui::EGBIS_IMAGE_DOWN, image, pos);
95 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image)
97 setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image);
98 setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image);
101 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
103 setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image, pos);
104 setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image, pos);
107 void GUIButtonImage::setFromStyle(const StyleSpec &style, ISimpleTextureSource *tsrc)
109 GUIButton::setFromStyle(style, tsrc);
111 video::IVideoDriver *driver = Environment->getVideoDriver();
113 if (style.isNotDefault(StyleSpec::FGIMG)) {
114 video::ITexture *texture = style.getTexture(StyleSpec::FGIMG, tsrc);
115 video::ITexture *hovered_texture = style.getTexture(StyleSpec::FGIMG_HOVERED, tsrc, texture);
116 video::ITexture *pressed_texture = style.getTexture(StyleSpec::FGIMG_PRESSED, tsrc, texture);
118 const core::position2di buttonCenter(AbsoluteRect.getCenter());
119 core::position2d<s32> geom(buttonCenter);
121 setForegroundImage(guiScalingImageButton(driver, texture, geom.X, geom.Y));
122 setHoveredForegroundImage(guiScalingImageButton(driver, hovered_texture, geom.X, geom.Y));
123 setPressedForegroundImage(guiScalingImageButton(driver, pressed_texture, geom.X, geom.Y));
128 void GUIButtonImage::setScaleImage(bool scaleImage)
130 GUIButton::setScaleImage(scaleImage);
131 m_image->setScaleImage(scaleImage);
134 GUIButtonImage *GUIButtonImage::addButton(IGUIEnvironment *environment,
135 const core::rect<s32> &rectangle, IGUIElement *parent, s32 id,
136 const wchar_t *text, const wchar_t *tooltiptext)
138 GUIButtonImage *button = new GUIButtonImage(environment,
139 parent ? parent : environment->getRootGUIElement(), id, rectangle);
142 button->setText(text);
145 button->setToolTipText(tooltiptext);