SuperTuxKart
icon_button_widget.hpp
1 // SuperTuxKart - a fun racing game with go-kart
2 // Copyright (C) 2009-2015 Marianne Gagnon
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 3
7 // of the License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 
19 
20 #ifndef HEADER_IBTN_HPP
21 #define HEADER_IBTN_HPP
22 
23 #include <irrString.h>
24 namespace irr
25 {
26  namespace gui
27  {
28  class IGUIStaticText;
29  class ScalableFont;
30  }
31  namespace video { class ITexture; }
32 }
33 
34 #include "guiengine/widget.hpp"
35 #include "utils/leak_check.hpp"
36 #include "utils/cpp2011.hpp"
37 
38 namespace GUIEngine
39 {
43  class IconButtonWidget : public Widget
44  {
45  private:
46  irr::core::rect<s32> m_list_header_icon_rect, m_icon_rect, m_label_rect;
47  irr::video::ITexture* m_texture;
48  irr::video::ITexture* m_deactivated_texture;
49  irr::video::ITexture* m_highlight_texture;
50  int m_texture_w, m_texture_h;
51  float m_target_width;
52  float m_target_height;
53 
54  video::ITexture* getDeactivatedTexture(video::ITexture* texture);
55  void setLabelFont();
56  void updateIconRect();
57  void updateLabelRect();
58 
59  public:
60  enum ScaleMode
61  {
62  SCALE_MODE_STRETCH,
63  SCALE_MODE_LIST_WIDGET,
64  SCALE_MODE_KEEP_TEXTURE_ASPECT_RATIO,
65  SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO
66  };
68  {
69  ICON_PATH_TYPE_ABSOLUTE,
75  };
76 
77  protected:
78 
79  IconPathType m_icon_path_type;
80 
81  friend class Skin;
82 
83  irr::gui::IGUIStaticText* m_label;
84  irr::gui::ScalableFont* m_font;
85 
86  ScaleMode m_scale_mode;
87  float m_custom_aspect_ratio;
88 
89  void setTexture(video::ITexture* texture);
90 
91  public:
92 
93  LEAK_CHECK()
94 
95 
96  bool m_tab_stop;
97 
98  IconButtonWidget(ScaleMode scale_mode=SCALE_MODE_KEEP_TEXTURE_ASPECT_RATIO, const bool tab_stop=true,
99  const bool focusable=true, IconPathType pathType=ICON_PATH_TYPE_RELATIVE);
100  virtual ~IconButtonWidget() {};
101 
103  virtual void add() OVERRIDE;
104 
109  void setCustomAspectRatio(float custom_aspect_ratio) { m_custom_aspect_ratio = custom_aspect_ratio; }
110 
119  void setLabel(const irr::core::stringw& new_label);
120 
126  void setLabelFont(irr::gui::ScalableFont* font);
127 
135  void setImage(const char* path_to_texture,
137  // --------------------------------------------------------------------
139  void setImage(const std::string &path_to_texture,
141  {
142  setImage(path_to_texture.c_str(), path_type);
143  }
144 
145  // --------------------------------------------------------------------
153  void setImage(irr::video::ITexture* texture);
154 
155  // --------------------------------------------------------------------
156  void setHighlightedImage(irr::video::ITexture* texture)
157  {
158  m_highlight_texture = texture;
159  }
160 
161  // --------------------------------------------------------------------
162  void setTargetSize(float width, float height)
163  {
164  m_target_width = width;
165  m_target_height = height;
166  }
167 
168  // --------------------------------------------------------------------
170  virtual EventPropagation focused(const int playerID) OVERRIDE;
171 
172  // --------------------------------------------------------------------
174  virtual void unfocused(const int playerID, Widget* new_focus) OVERRIDE;
175  // --------------------------------------------------------------------
177  const video::ITexture* getTexture();
178  // --------------------------------------------------------------------
179  virtual void setVisible(bool visible) OVERRIDE;
180  // --------------------------------------------------------------------
181  virtual void elementRemoved() OVERRIDE
182  {
184  m_label = NULL;
185  }
186  // --------------------------------------------------------------------
187  virtual void resize() OVERRIDE;
188  // --------------------------------------------------------------------
189  const irr::core::rect<s32>& getListHeaderIconRect() const
190  {
191  return m_list_header_icon_rect;
192  }
193  };
194 }
195 
196 #endif
A button widget with an icon and optionnaly a label beneath.
Definition: icon_button_widget.hpp:44
const video::ITexture * getTexture()
Returns the texture of this button.
Definition: icon_button_widget.cpp:251
virtual EventPropagation focused(const int playerID) OVERRIDE
override from base class
Definition: icon_button_widget.cpp:231
virtual void unfocused(const int playerID, Widget *new_focus) OVERRIDE
override from base class
Definition: icon_button_widget.cpp:242
void setLabel(const irr::core::stringw &new_label)
Temporarily change the text label if there is a label (next time this screen is visited,...
Definition: icon_button_widget.cpp:218
void setCustomAspectRatio(float custom_aspect_ratio)
Call this if scale mode is SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO.
Definition: icon_button_widget.hpp:109
virtual void resize() OVERRIDE
Call to resize the widget after its coordinates are updated.
Definition: icon_button_widget.cpp:508
virtual void setVisible(bool visible) OVERRIDE
Sets the widget (and its children, if any) visible or not.
Definition: icon_button_widget.cpp:369
void setImage(irr::video::ITexture *texture)
Change the texture used for this icon.
virtual void elementRemoved() OVERRIDE
Called when irrLicht widgets cleared.
Definition: icon_button_widget.hpp:181
bool m_tab_stop
Whether to make the widget included in keyboard navigation order when adding.
Definition: icon_button_widget.hpp:96
void setImage(const std::string &path_to_texture, IconPathType path_type=ICON_PATH_TYPE_NO_CHANGE)
Convenience function taking std::string.
Definition: icon_button_widget.hpp:139
virtual void add() OVERRIDE
Implement callback from base class Widget.
Definition: icon_button_widget.cpp:69
void setImage(const char *path_to_texture, IconPathType path_type=ICON_PATH_TYPE_NO_CHANGE)
Change the texture used for this icon.
Definition: icon_button_widget.cpp:173
IconPathType
Definition: icon_button_widget.hpp:68
@ ICON_PATH_TYPE_NO_CHANGE
not a valid value per se, but can be passed as argument to leave the path type as it currently is
Definition: icon_button_widget.hpp:74
@ ICON_PATH_TYPE_RELATIVE
relative to the data dir
Definition: icon_button_widget.hpp:71
Object used to render the GUI widgets see Overview of GUI skin for more information about skinning in...
Definition: skin.hpp:280
The nearly-abstract base of all widgets (not fully abstract since a bare Widget can be created for th...
Definition: widget.hpp:147
virtual void elementRemoved()
Called when irrLicht widgets cleared.
Definition: widget.cpp:152
Definition: scalable_font.hpp:35
Contains all GUI engine related classes and functions.
Definition: abstract_state_manager.hpp:33