Refactor to centralize GUIButton styling/rendering code (#9090)
[oweals/minetest.git] / src / gui / guiButtonImage.cpp
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
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)
35 {
36         m_image = Environment->addImage(
37                         core::rect<s32>(0,0,rectangle.getWidth(),rectangle.getHeight()), this);
38         m_image->setScaleImage(isScalingImage());
39         sendToBack(m_image);
40 }
41
42 bool GUIButtonImage::OnEvent(const SEvent& event)
43 {
44         bool result = GUIButton::OnEvent(event);
45
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);
50
51         return result;
52 }
53
54 void GUIButtonImage::setForegroundImage(EGUI_BUTTON_IMAGE_STATE state,
55                 video::ITexture *image, const core::rect<s32> &sourceRect)
56 {
57         if (state >= EGBIS_COUNT)
58                 return;
59
60         if (image)
61                 image->grab();
62
63         u32 stateIdx = (u32)state;
64         if (m_foreground_images[stateIdx].Texture)
65                 m_foreground_images[stateIdx].Texture->drop();
66
67         m_foreground_images[stateIdx].Texture = image;
68         m_foreground_images[stateIdx].SourceRect = sourceRect;
69
70         EGUI_BUTTON_IMAGE_STATE imageState = getImageState(isPressed(), m_foreground_images);
71         if (imageState == stateIdx)
72                 m_image->setImage(image);
73 }
74
75 void GUIButtonImage::setForegroundImage(video::ITexture *image)
76 {
77         setForegroundImage(gui::EGBIS_IMAGE_UP, image);
78 }
79
80 void GUIButtonImage::setForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
81 {
82         setForegroundImage(gui::EGBIS_IMAGE_UP, image, pos);
83 }
84
85 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image)
86 {
87         setForegroundImage(gui::EGBIS_IMAGE_DOWN, image);
88 }
89
90 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
91 {
92         setForegroundImage(gui::EGBIS_IMAGE_DOWN, image, pos);
93 }
94
95 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image)
96 {
97         setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image);
98         setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image);
99 }
100
101 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
102 {
103         setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image, pos);
104         setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image, pos);
105 }
106
107 void GUIButtonImage::setFromStyle(const StyleSpec &style, ISimpleTextureSource *tsrc)
108 {
109         GUIButton::setFromStyle(style, tsrc);
110
111         video::IVideoDriver *driver = Environment->getVideoDriver();
112
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);
117
118                 const core::position2di buttonCenter(AbsoluteRect.getCenter());
119                 core::position2d<s32> geom(buttonCenter);
120
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));
124                 setScaleImage(true);
125         }
126 }
127
128 void GUIButtonImage::setScaleImage(bool scaleImage)
129 {
130         GUIButton::setScaleImage(scaleImage);
131         m_image->setScaleImage(scaleImage);
132 }
133
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)
137 {
138         GUIButtonImage *button = new GUIButtonImage(environment,
139                         parent ? parent : environment->getRootGUIElement(), id, rectangle);
140
141         if (text)
142                 button->setText(text);
143
144         if (tooltiptext)
145                 button->setToolTipText(tooltiptext);
146
147         button->drop();
148         return button;
149 }