guiButton.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright (C) 2002-2012 Nikolaus Gebhardt
  2. // This file is part of the "Irrlicht Engine".
  3. // For conditions of distribution and use, see copyright notice in irrlicht.h
  4. #pragma once
  5. #include <IGUIStaticText.h>
  6. #include "irrlicht_changes/static_text.h"
  7. #include "IGUIButton.h"
  8. #include "IGUISpriteBank.h"
  9. #include "ITexture.h"
  10. #include "SColor.h"
  11. #include "guiSkin.h"
  12. #include "StyleSpec.h"
  13. using namespace irr;
  14. class ISimpleTextureSource;
  15. class GUIButton : public gui::IGUIButton
  16. {
  17. public:
  18. //! constructor
  19. GUIButton(gui::IGUIEnvironment* environment, gui::IGUIElement* parent,
  20. s32 id, core::rect<s32> rectangle, ISimpleTextureSource *tsrc,
  21. bool noclip=false);
  22. //! destructor
  23. virtual ~GUIButton();
  24. //! called if an event happened.
  25. virtual bool OnEvent(const SEvent& event) override;
  26. //! draws the element and its children
  27. virtual void draw() override;
  28. //! sets another skin independent font. if this is set to zero, the button uses the font of the skin.
  29. virtual void setOverrideFont(gui::IGUIFont* font=0) override;
  30. //! Gets the override font (if any)
  31. virtual gui::IGUIFont* getOverrideFont() const override;
  32. //! Get the font which is used right now for drawing
  33. virtual gui::IGUIFont* getActiveFont() const override;
  34. //! Sets another color for the button text.
  35. virtual void setOverrideColor(video::SColor color) override;
  36. //! Gets the override color
  37. virtual video::SColor getOverrideColor() const override;
  38. //! Gets the currently used text color
  39. virtual video::SColor getActiveColor() const override;
  40. //! Sets if the button text should use the override color or the color in the gui skin.
  41. virtual void enableOverrideColor(bool enable) override;
  42. //! Checks if an override color is enabled
  43. virtual bool isOverrideColorEnabled(void) const override;
  44. // PATCH
  45. //! Sets an image which should be displayed on the button when it is in the given state.
  46. virtual void setImage(gui::EGUI_BUTTON_IMAGE_STATE state,
  47. video::ITexture* image=nullptr,
  48. const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) override;
  49. //! Sets an image which should be displayed on the button when it is in normal state.
  50. virtual void setImage(video::ITexture* image=nullptr) override;
  51. //! Sets an image which should be displayed on the button when it is in normal state.
  52. virtual void setImage(video::ITexture* image, const core::rect<s32>& pos) override;
  53. //! Sets an image which should be displayed on the button when it is in pressed state.
  54. virtual void setPressedImage(video::ITexture* image=nullptr) override;
  55. //! Sets an image which should be displayed on the button when it is in pressed state.
  56. virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& pos) override;
  57. //! Sets the text displayed by the button
  58. virtual void setText(const wchar_t* text) override;
  59. // END PATCH
  60. //! Sets the sprite bank used by the button
  61. virtual void setSpriteBank(gui::IGUISpriteBank* bank=0) override;
  62. //! Sets the animated sprite for a specific button state
  63. /** \param index: Number of the sprite within the sprite bank, use -1 for no sprite
  64. \param state: State of the button to set the sprite for
  65. \param index: The sprite number from the current sprite bank
  66. \param color: The color of the sprite
  67. */
  68. virtual void setSprite(gui::EGUI_BUTTON_STATE state, s32 index,
  69. video::SColor color=video::SColor(255,255,255,255),
  70. bool loop=false, bool scale=false) override;
  71. //! Get the sprite-index for the given state or -1 when no sprite is set
  72. virtual s32 getSpriteIndex(gui::EGUI_BUTTON_STATE state) const override;
  73. //! Get the sprite color for the given state. Color is only used when a sprite is set.
  74. virtual video::SColor getSpriteColor(gui::EGUI_BUTTON_STATE state) const override;
  75. //! Returns if the sprite in the given state does loop
  76. virtual bool getSpriteLoop(gui::EGUI_BUTTON_STATE state) const override;
  77. //! Returns if the sprite in the given state is scaled
  78. virtual bool getSpriteScale(gui::EGUI_BUTTON_STATE state) const override;
  79. //! Sets if the button should behave like a push button. Which means it
  80. //! can be in two states: Normal or Pressed. With a click on the button,
  81. //! the user can change the state of the button.
  82. virtual void setIsPushButton(bool isPushButton=true) override;
  83. //! Checks whether the button is a push button
  84. virtual bool isPushButton() const override;
  85. //! Sets the pressed state of the button if this is a pushbutton
  86. virtual void setPressed(bool pressed=true) override;
  87. //! Returns if the button is currently pressed
  88. virtual bool isPressed() const override;
  89. // PATCH
  90. //! Returns if this element (or one of its direct children) is hovered
  91. bool isHovered() const;
  92. //! Returns if this element (or one of its direct children) is focused
  93. bool isFocused() const;
  94. // END PATCH
  95. //! Sets if the button should use the skin to draw its border
  96. virtual void setDrawBorder(bool border=true) override;
  97. //! Checks if the button face and border are being drawn
  98. virtual bool isDrawingBorder() const override;
  99. //! Sets if the alpha channel should be used for drawing images on the button (default is false)
  100. virtual void setUseAlphaChannel(bool useAlphaChannel=true) override;
  101. //! Checks if the alpha channel should be used for drawing images on the button
  102. virtual bool isAlphaChannelUsed() const override;
  103. //! Sets if the button should scale the button images to fit
  104. virtual void setScaleImage(bool scaleImage=true) override;
  105. //! Checks whether the button scales the used images
  106. virtual bool isScalingImage() const override;
  107. //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event
  108. virtual bool getClickShiftState() const override
  109. {
  110. return ClickShiftState;
  111. }
  112. //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event
  113. virtual bool getClickControlState() const override
  114. {
  115. return ClickControlState;
  116. }
  117. void setColor(video::SColor color);
  118. // PATCH
  119. //! Set element properties from a StyleSpec corresponding to the button state
  120. void setFromState();
  121. //! Set element properties from a StyleSpec
  122. virtual void setFromStyle(const StyleSpec& style);
  123. //! Set the styles used for each state
  124. void setStyles(const std::array<StyleSpec, StyleSpec::NUM_STATES>& styles);
  125. // END PATCH
  126. //! Do not drop returned handle
  127. static GUIButton* addButton(gui::IGUIEnvironment *environment,
  128. const core::rect<s32>& rectangle, ISimpleTextureSource *tsrc,
  129. IGUIElement* parent, s32 id, const wchar_t* text,
  130. const wchar_t *tooltiptext=L"");
  131. protected:
  132. void drawSprite(gui::EGUI_BUTTON_STATE state, u32 startTime, const core::position2di& center);
  133. gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed) const;
  134. ISimpleTextureSource *getTextureSource() { return TSrc; }
  135. struct ButtonImage
  136. {
  137. ButtonImage() = default;
  138. ButtonImage(const ButtonImage& other)
  139. {
  140. *this = other;
  141. }
  142. ~ButtonImage()
  143. {
  144. if ( Texture )
  145. Texture->drop();
  146. }
  147. ButtonImage& operator=(const ButtonImage& other)
  148. {
  149. if ( this == &other )
  150. return *this;
  151. if (other.Texture)
  152. other.Texture->grab();
  153. if ( Texture )
  154. Texture->drop();
  155. Texture = other.Texture;
  156. SourceRect = other.SourceRect;
  157. return *this;
  158. }
  159. bool operator==(const ButtonImage& other) const
  160. {
  161. return Texture == other.Texture && SourceRect == other.SourceRect;
  162. }
  163. video::ITexture* Texture = nullptr;
  164. core::rect<s32> SourceRect = core::rect<s32>(0,0,0,0);
  165. };
  166. gui::EGUI_BUTTON_IMAGE_STATE getImageState(bool pressed, const ButtonImage* images) const;
  167. private:
  168. struct ButtonSprite
  169. {
  170. bool operator==(const ButtonSprite &other) const
  171. {
  172. return Index == other.Index && Color == other.Color && Loop == other.Loop && Scale == other.Scale;
  173. }
  174. s32 Index = -1;
  175. video::SColor Color;
  176. bool Loop = false;
  177. bool Scale = false;
  178. };
  179. ButtonSprite ButtonSprites[gui::EGBS_COUNT];
  180. gui::IGUISpriteBank* SpriteBank = nullptr;
  181. ButtonImage ButtonImages[gui::EGBIS_COUNT];
  182. std::array<StyleSpec, StyleSpec::NUM_STATES> Styles;
  183. gui::IGUIFont* OverrideFont = nullptr;
  184. bool OverrideColorEnabled = false;
  185. video::SColor OverrideColor = video::SColor(101,255,255,255);
  186. u32 ClickTime = 0;
  187. u32 HoverTime = 0;
  188. u32 FocusTime = 0;
  189. bool ClickShiftState = false;
  190. bool ClickControlState = false;
  191. bool IsPushButton = false;
  192. bool Pressed = false;
  193. bool UseAlphaChannel = false;
  194. bool DrawBorder = true;
  195. bool ScaleImage = false;
  196. video::SColor Colors[4];
  197. // PATCH
  198. bool WasHovered = false;
  199. bool WasFocused = false;
  200. ISimpleTextureSource *TSrc;
  201. gui::IGUIStaticText *StaticText;
  202. core::rect<s32> BgMiddle;
  203. core::rect<s32> Padding;
  204. core::vector2d<s32> ContentOffset;
  205. video::SColor BgColor = video::SColor(0xFF,0xFF,0xFF,0xFF);
  206. // END PATCH
  207. };