]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiButtonImage.cpp
GUIHyperText: Fix bug with UTF8 chars in action name + simplify UTF8 stringw conversi...
[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         {
50                 m_image->setImage(texture);
51         }
52
53         m_image->setVisible(texture != nullptr);
54
55         return result;
56 }
57
58 void GUIButtonImage::setForegroundImage(EGUI_BUTTON_IMAGE_STATE state,
59                 video::ITexture *image, const core::rect<s32> &sourceRect)
60 {
61         if (state >= EGBIS_COUNT)
62                 return;
63
64         if (image)
65                 image->grab();
66
67         u32 stateIdx = (u32)state;
68         if (m_foreground_images[stateIdx].Texture)
69                 m_foreground_images[stateIdx].Texture->drop();
70
71         m_foreground_images[stateIdx].Texture = image;
72         m_foreground_images[stateIdx].SourceRect = sourceRect;
73
74         EGUI_BUTTON_IMAGE_STATE imageState = getImageState(isPressed(), m_foreground_images);
75         if (imageState == stateIdx)
76                 m_image->setImage(image);
77 }
78
79 void GUIButtonImage::setForegroundImage(video::ITexture *image)
80 {
81         setForegroundImage(gui::EGBIS_IMAGE_UP, image);
82 }
83
84 void GUIButtonImage::setForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
85 {
86         setForegroundImage(gui::EGBIS_IMAGE_UP, image, pos);
87 }
88
89 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image)
90 {
91         setForegroundImage(gui::EGBIS_IMAGE_DOWN, image);
92 }
93
94 void GUIButtonImage::setPressedForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
95 {
96         setForegroundImage(gui::EGBIS_IMAGE_DOWN, image, pos);
97 }
98
99 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image)
100 {
101         setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image);
102         setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image);
103 }
104
105 void GUIButtonImage::setHoveredForegroundImage(video::ITexture *image, const core::rect<s32> &pos)
106 {
107         setForegroundImage(gui::EGBIS_IMAGE_UP_MOUSEOVER, image, pos);
108         setForegroundImage(gui::EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER, image, pos);
109 }
110
111 void GUIButtonImage::setFromStyle(const StyleSpec &style, ISimpleTextureSource *tsrc)
112 {
113         GUIButton::setFromStyle(style, tsrc);
114
115         video::IVideoDriver *driver = Environment->getVideoDriver();
116
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);
121
122                 setForegroundImage(guiScalingImageButton(driver, texture, geom.X, geom.Y));
123                 setScaleImage(true);
124         }
125         if (style.isNotDefault(StyleSpec::FGIMG_HOVERED)) {
126                 video::ITexture *hovered_texture = style.getTexture(StyleSpec::FGIMG_HOVERED, tsrc);
127
128                 setHoveredForegroundImage(guiScalingImageButton(driver, hovered_texture, geom.X, geom.Y));
129                 setScaleImage(true);
130         }
131         if (style.isNotDefault(StyleSpec::FGIMG_PRESSED)) {
132                 video::ITexture *pressed_texture = style.getTexture(StyleSpec::FGIMG_PRESSED, tsrc);
133
134                 setPressedForegroundImage(guiScalingImageButton(driver, pressed_texture, geom.X, geom.Y));
135                 setScaleImage(true);
136         }
137 }
138
139 void GUIButtonImage::setScaleImage(bool scaleImage)
140 {
141         GUIButton::setScaleImage(scaleImage);
142         m_image->setScaleImage(scaleImage);
143 }
144
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)
148 {
149         GUIButtonImage *button = new GUIButtonImage(environment,
150                         parent ? parent : environment->getRootGUIElement(), id, rectangle);
151
152         if (text)
153                 button->setText(text);
154
155         if (tooltiptext)
156                 button->setToolTipText(tooltiptext);
157
158         button->drop();
159         return button;
160 }