1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "guiButtonImage.h"
21
22 #include "client/guiscalingfilter.h"
23 #include "debug.h"
24 #include "IGUIEnvironment.h"
25 #include "IGUIImage.h"
26 #include "IVideoDriver.h"
27 #include "StyleSpec.h"
28
29 using namespace irr;
30 using namespace gui;
31
GUIButtonImage(gui::IGUIEnvironment * environment,gui::IGUIElement * parent,s32 id,core::rect<s32> rectangle,ISimpleTextureSource * tsrc,bool noclip)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)
36 {
37 m_image = Environment->addImage(
38 core::rect<s32>(0,0,rectangle.getWidth(),rectangle.getHeight()), this);
39 m_image->setScaleImage(isScalingImage());
40 sendToBack(m_image);
41 }
42
setForegroundImage(video::ITexture * image)43 void GUIButtonImage::setForegroundImage(video::ITexture *image)
44 {
45 if (image == m_foreground_image)
46 return;
47
48 if (image != nullptr)
49 image->grab();
50
51 if (m_foreground_image != nullptr)
52 m_foreground_image->drop();
53
54 m_foreground_image = image;
55 m_image->setImage(image);
56 }
57
58 //! Set element properties from a StyleSpec
setFromStyle(const StyleSpec & style)59 void GUIButtonImage::setFromStyle(const StyleSpec& style)
60 {
61 GUIButton::setFromStyle(style);
62
63 video::IVideoDriver *driver = Environment->getVideoDriver();
64
65 if (style.isNotDefault(StyleSpec::FGIMG)) {
66 video::ITexture *texture = style.getTexture(StyleSpec::FGIMG,
67 getTextureSource());
68
69 setForegroundImage(guiScalingImageButton(driver, texture,
70 AbsoluteRect.getWidth(), AbsoluteRect.getHeight()));
71 setScaleImage(true);
72 } else {
73 setForegroundImage(nullptr);
74 }
75 }
76
setScaleImage(bool scaleImage)77 void GUIButtonImage::setScaleImage(bool scaleImage)
78 {
79 GUIButton::setScaleImage(scaleImage);
80 m_image->setScaleImage(scaleImage);
81 }
82
addButton(IGUIEnvironment * environment,const core::rect<s32> & rectangle,ISimpleTextureSource * tsrc,IGUIElement * parent,s32 id,const wchar_t * text,const wchar_t * tooltiptext)83 GUIButtonImage *GUIButtonImage::addButton(IGUIEnvironment *environment,
84 const core::rect<s32> &rectangle, ISimpleTextureSource *tsrc,
85 IGUIElement *parent, s32 id, const wchar_t *text,
86 const wchar_t *tooltiptext)
87 {
88 GUIButtonImage *button = new GUIButtonImage(environment,
89 parent ? parent : environment->getRootGUIElement(), id, rectangle, tsrc);
90
91 if (text)
92 button->setText(text);
93
94 if (tooltiptext)
95 button->setToolTipText(tooltiptext);
96
97 button->drop();
98 return button;
99 }
100