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 { 40 /** \brief A button widget with an icon and optionnaly a label beneath 41 * \ingroup widgetsgroup 42 */ 43 class IconButtonWidget : public Widget 44 { 45 private: 46 irr::core::rect<s32> m_list_header_icon_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 52 video::ITexture* getDeactivatedTexture(video::ITexture* texture); 53 void setLabelFont(); 54 55 public: 56 enum ScaleMode 57 { 58 SCALE_MODE_STRETCH, 59 SCALE_MODE_LIST_WIDGET, 60 SCALE_MODE_KEEP_TEXTURE_ASPECT_RATIO, 61 SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO 62 }; 63 enum IconPathType 64 { 65 ICON_PATH_TYPE_ABSOLUTE, 66 /** relative to the data dir */ 67 ICON_PATH_TYPE_RELATIVE, 68 /** not a valid value per se, but can be passed as argument to leave 69 * the path type as it currently is */ 70 ICON_PATH_TYPE_NO_CHANGE 71 }; 72 73 protected: 74 75 IconPathType m_icon_path_type; 76 77 friend class Skin; 78 79 irr::gui::IGUIStaticText* m_label; 80 irr::gui::ScalableFont* m_font; 81 82 ScaleMode m_scale_mode; 83 float m_custom_aspect_ratio; 84 85 void setTexture(video::ITexture* texture); 86 87 public: 88 89 LEAK_CHECK() 90 91 /** Whether to make the widget included in keyboard navigation order when adding */ 92 bool m_tab_stop; 93 94 IconButtonWidget(ScaleMode scale_mode=SCALE_MODE_KEEP_TEXTURE_ASPECT_RATIO, const bool tab_stop=true, 95 const bool focusable=true, IconPathType pathType=ICON_PATH_TYPE_RELATIVE); ~IconButtonWidget()96 virtual ~IconButtonWidget() {}; 97 98 /** \brief Implement callback from base class Widget */ 99 virtual void add() OVERRIDE; 100 101 /** 102 * \brief Call this if scale mode is SCALE_MODE_KEEP_CUSTOM_ASPECT_RATIO. 103 * \param custom_aspect_ratio The width/height aspect ratio 104 */ setCustomAspectRatio(float custom_aspect_ratio)105 void setCustomAspectRatio(float custom_aspect_ratio) { m_custom_aspect_ratio = custom_aspect_ratio; } 106 107 /** 108 * \brief Temporarily change the text label if there is a label (next time this screen is 109 * visited, the previous label will be back. For a permanent change, edit the 'text' 110 * property in the base Widget class). 111 * 112 * \pre Must be called after this widget is add()ed to have any effect 113 * \note Calling this method on a button without label will have no effect 114 */ 115 void setLabel(const irr::core::stringw& new_label); 116 117 /** 118 * \brief Sets the font used to draw the label. 119 * 120 * \note Calling this method on a button without label will have no effect 121 */ 122 void setLabelFont(irr::gui::ScalableFont* font); 123 124 /** 125 * Change the texture used for this icon. 126 * \pre At the moment, the new texture must have the same aspct ratio 127 * as the previous one since the object will not 128 * be resized to fit a different aspect ratio. 129 * \note May safely be called no matter if the widget is add()ed or not 130 */ 131 void setImage(const char* path_to_texture, 132 IconPathType path_type=ICON_PATH_TYPE_NO_CHANGE); 133 // -------------------------------------------------------------------- 134 /** Convenience function taking std::string. */ setImage(const std::string & path_to_texture,IconPathType path_type=ICON_PATH_TYPE_NO_CHANGE)135 void setImage(const std::string &path_to_texture, 136 IconPathType path_type=ICON_PATH_TYPE_NO_CHANGE) 137 { 138 setImage(path_to_texture.c_str(), path_type); 139 } 140 141 // -------------------------------------------------------------------- 142 /** 143 * Change the texture used for this icon. 144 * \pre At the moment, the new texture must have the same aspct ratio 145 * as the previous one since the object will not 146 * be resized to fit a different aspect ratio. 147 * \note May safely be called no matter if the widget is add()ed or not 148 */ 149 void setImage(irr::video::ITexture* texture); 150 151 // -------------------------------------------------------------------- setHighlightedImage(irr::video::ITexture * texture)152 void setHighlightedImage(irr::video::ITexture* texture) 153 { 154 m_highlight_texture = texture; 155 } 156 157 // -------------------------------------------------------------------- 158 /** \brief override from base class */ 159 virtual EventPropagation focused(const int playerID) OVERRIDE; 160 161 // -------------------------------------------------------------------- 162 /** \brief override from base class */ 163 virtual void unfocused(const int playerID, Widget* new_focus) OVERRIDE; 164 // -------------------------------------------------------------------- 165 /** Returns the texture of this button. */ 166 const video::ITexture* getTexture(); 167 // -------------------------------------------------------------------- 168 virtual void setVisible(bool visible) OVERRIDE; 169 // -------------------------------------------------------------------- elementRemoved()170 virtual void elementRemoved() OVERRIDE 171 { 172 Widget::elementRemoved(); 173 m_label = NULL; 174 } 175 // -------------------------------------------------------------------- getListHeaderIconRect() const176 const irr::core::rect<s32>& getListHeaderIconRect() const 177 { 178 return m_list_header_icon_rect; 179 } 180 }; 181 } 182 183 #endif 184