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,
34 ISimpleTextureSource *tsrc, bool noclip)
35 : GUIButton (environment, parent, id, rectangle, tsrc, noclip)
37 m_image = Environment->addImage(
38 core::rect<s32>(0,0,rectangle.getWidth(),rectangle.getHeight()), this);
39 m_image->setScaleImage(isScalingImage());
43 void GUIButtonImage::setForegroundImage(video::ITexture *image)
45 if (image == m_foreground_image)
51 if (m_foreground_image != nullptr)
52 m_foreground_image->drop();
54 m_foreground_image = image;
55 m_image->setImage(image);
58 //! Set element properties from a StyleSpec
59 void GUIButtonImage::setFromStyle(const StyleSpec& style)
61 GUIButton::setFromStyle(style);
63 video::IVideoDriver *driver = Environment->getVideoDriver();
65 const core::position2di buttonCenter(AbsoluteRect.getCenter());
66 core::position2d<s32> geom(buttonCenter);
67 if (style.isNotDefault(StyleSpec::FGIMG)) {
68 video::ITexture *texture = style.getTexture(StyleSpec::FGIMG,
71 setForegroundImage(guiScalingImageButton(driver, texture, geom.X, geom.Y));
74 setForegroundImage(nullptr);
78 void GUIButtonImage::setScaleImage(bool scaleImage)
80 GUIButton::setScaleImage(scaleImage);
81 m_image->setScaleImage(scaleImage);
84 GUIButtonImage *GUIButtonImage::addButton(IGUIEnvironment *environment,
85 const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
86 IGUIElement *parent, s32 id, const wchar_t *text,
87 const wchar_t *tooltiptext)
89 GUIButtonImage *button = new GUIButtonImage(environment,
90 parent ? parent : environment->getRootGUIElement(), id, rectangle, tsrc);
93 button->setText(text);
96 button->setToolTipText(tooltiptext);