]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiButtonImage.cpp
2f574c1a8085927483534c22e859c071abb821a8
[dragonfireclient.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,
34                 ISimpleTextureSource *tsrc, bool noclip) :
35                 GUIButton(environment, parent, id, rectangle, tsrc, noclip)
36 {
37         m_image = Environment->addImage(core::rect<s32>(0, 0, rectangle.getWidth(),
38                                                         rectangle.getHeight()),
39                         this);
40         m_image->setScaleImage(isScalingImage());
41         sendToBack(m_image);
42 }
43
44 void GUIButtonImage::setForegroundImage(video::ITexture *image)
45 {
46         if (image == m_foreground_image)
47                 return;
48
49         if (image != nullptr)
50                 image->grab();
51
52         if (m_foreground_image != nullptr)
53                 m_foreground_image->drop();
54
55         m_foreground_image = image;
56         m_image->setImage(image);
57 }
58
59 //! Set element properties from a StyleSpec
60 void GUIButtonImage::setFromStyle(const StyleSpec &style)
61 {
62         GUIButton::setFromStyle(style);
63
64         video::IVideoDriver *driver = Environment->getVideoDriver();
65
66         if (style.isNotDefault(StyleSpec::FGIMG)) {
67                 video::ITexture *texture =
68                                 style.getTexture(StyleSpec::FGIMG, getTextureSource());
69
70                 setForegroundImage(guiScalingImageButton(driver, texture,
71                                 AbsoluteRect.getWidth(), AbsoluteRect.getHeight()));
72                 setScaleImage(true);
73         } else {
74                 setForegroundImage(nullptr);
75         }
76 }
77
78 void GUIButtonImage::setScaleImage(bool scaleImage)
79 {
80         GUIButton::setScaleImage(scaleImage);
81         m_image->setScaleImage(scaleImage);
82 }
83
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)
88 {
89         GUIButtonImage *button = new GUIButtonImage(environment,
90                         parent ? parent : environment->getRootGUIElement(), id, rectangle,
91                         tsrc);
92
93         if (text)
94                 button->setText(text);
95
96         if (tooltiptext)
97                 button->setToolTipText(tooltiptext);
98
99         button->drop();
100         return button;
101 }