IGUIButton.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. #ifndef IRR_I_GUI_BUTTON_H_INCLUDED
  5. #define IRR_I_GUI_BUTTON_H_INCLUDED
  6. #include "IGUIElement.h"
  7. namespace irr
  8. {
  9. namespace video
  10. {
  11. class ITexture;
  12. } // end namespace video
  13. namespace gui
  14. {
  15. class IGUIFont;
  16. class IGUISpriteBank;
  17. //! Current state of buttons used for drawing sprites.
  18. //! Note that up to 3 states can be active at the same time:
  19. //! EGBS_BUTTON_UP or EGBS_BUTTON_DOWN
  20. //! EGBS_BUTTON_MOUSE_OVER or EGBS_BUTTON_MOUSE_OFF
  21. //! EGBS_BUTTON_FOCUSED or EGBS_BUTTON_NOT_FOCUSED
  22. enum EGUI_BUTTON_STATE
  23. {
  24. //! The button is not pressed.
  25. EGBS_BUTTON_UP=0,
  26. //! The button is currently pressed down.
  27. EGBS_BUTTON_DOWN,
  28. //! The mouse cursor is over the button
  29. EGBS_BUTTON_MOUSE_OVER,
  30. //! The mouse cursor is not over the button
  31. EGBS_BUTTON_MOUSE_OFF,
  32. //! The button has the focus
  33. EGBS_BUTTON_FOCUSED,
  34. //! The button doesn't have the focus
  35. EGBS_BUTTON_NOT_FOCUSED,
  36. //! The button is disabled All other states are ignored in that case.
  37. EGBS_BUTTON_DISABLED,
  38. //! not used, counts the number of enumerated items
  39. EGBS_COUNT
  40. };
  41. //! Names for gui button state icons
  42. const c8* const GUIButtonStateNames[EGBS_COUNT+1] =
  43. {
  44. "buttonUp",
  45. "buttonDown",
  46. "buttonMouseOver",
  47. "buttonMouseOff",
  48. "buttonFocused",
  49. "buttonNotFocused",
  50. "buttonDisabled",
  51. 0 // count
  52. };
  53. //! State of buttons used for drawing texture images.
  54. //! Note that only a single state is active at a time
  55. //! Also when no image is defined for a state it will use images from another state
  56. //! and if that state is not set from the replacement for that,etc.
  57. //! So in many cases setting EGBIS_IMAGE_UP and EGBIS_IMAGE_DOWN is sufficient.
  58. enum EGUI_BUTTON_IMAGE_STATE
  59. {
  60. //! When no other states have images they will all use this one.
  61. EGBIS_IMAGE_UP,
  62. //! When not set EGBIS_IMAGE_UP is used.
  63. EGBIS_IMAGE_UP_MOUSEOVER,
  64. //! When not set EGBIS_IMAGE_UP_MOUSEOVER is used.
  65. EGBIS_IMAGE_UP_FOCUSED,
  66. //! When not set EGBIS_IMAGE_UP_FOCUSED is used.
  67. EGBIS_IMAGE_UP_FOCUSED_MOUSEOVER,
  68. //! When not set EGBIS_IMAGE_UP is used.
  69. EGBIS_IMAGE_DOWN,
  70. //! When not set EGBIS_IMAGE_DOWN is used.
  71. EGBIS_IMAGE_DOWN_MOUSEOVER,
  72. //! When not set EGBIS_IMAGE_DOWN_MOUSEOVER is used.
  73. EGBIS_IMAGE_DOWN_FOCUSED,
  74. //! When not set EGBIS_IMAGE_DOWN_FOCUSED is used.
  75. EGBIS_IMAGE_DOWN_FOCUSED_MOUSEOVER,
  76. //! When not set EGBIS_IMAGE_UP or EGBIS_IMAGE_DOWN are used (depending on button state).
  77. EGBIS_IMAGE_DISABLED,
  78. //! not used, counts the number of enumerated items
  79. EGBIS_COUNT
  80. };
  81. //! Names for gui button image states
  82. const c8* const GUIButtonImageStateNames[EGBIS_COUNT+1] =
  83. {
  84. "Image", // not "ImageUp" as it otherwise breaks serialization of old files
  85. "ImageUpOver",
  86. "ImageUpFocused",
  87. "ImageUpFocusedOver",
  88. "PressedImage", // not "ImageDown" as it otherwise breaks serialization of old files
  89. "ImageDownOver",
  90. "ImageDownFocused",
  91. "ImageDownFocusedOver",
  92. "ImageDisabled",
  93. 0 // count
  94. };
  95. //! GUI Button interface.
  96. /** \par This element can create the following events of type EGUI_EVENT_TYPE:
  97. \li EGET_BUTTON_CLICKED
  98. */
  99. class IGUIButton : public IGUIElement
  100. {
  101. public:
  102. //! constructor
  103. IGUIButton(IGUIEnvironment* environment, IGUIElement* parent, s32 id, core::rect<s32> rectangle)
  104. : IGUIElement(EGUIET_BUTTON, environment, parent, id, rectangle) {}
  105. //! Sets another skin independent font.
  106. /** If this is set to zero, the button uses the font of the skin.
  107. \param font: New font to set. */
  108. virtual void setOverrideFont(IGUIFont* font=0) = 0;
  109. //! Gets the override font (if any)
  110. /** \return The override font (may be 0) */
  111. virtual IGUIFont* getOverrideFont(void) const = 0;
  112. //! Get the font which is used right now for drawing
  113. /** Currently this is the override font when one is set and the
  114. font of the active skin otherwise */
  115. virtual IGUIFont* getActiveFont() const = 0;
  116. //! Sets another color for the button text.
  117. /** When set, this color is used instead of EGDC_BUTTON_TEXT/EGDC_GRAY_TEXT.
  118. You don't need to call enableOverrideColor(true), that's done by this function.
  119. If you want the the color of the skin back, call enableOverrideColor(false);
  120. \param color: New color of the text. */
  121. virtual void setOverrideColor(video::SColor color) = 0;
  122. //! Gets the override color
  123. /** \return: The override color */
  124. virtual video::SColor getOverrideColor(void) const = 0;
  125. //! Gets the currently used text color
  126. /** Either a skin-color for the current state or the override color */
  127. virtual video::SColor getActiveColor() const = 0;
  128. //! Sets if the button text should use the override color or the color in the gui skin.
  129. /** \param enable: If set to true, the override color, which can be set
  130. with IGUIStaticText::setOverrideColor is used, otherwise the
  131. EGDC_BUTTON_TEXT or EGDC_GRAY_TEXT color of the skin. */
  132. virtual void enableOverrideColor(bool enable) = 0;
  133. //! Checks if an override color is enabled
  134. /** \return true if the override color is enabled, false otherwise */
  135. virtual bool isOverrideColorEnabled(void) const = 0;
  136. //! Sets an image which should be displayed on the button when it is in the given state.
  137. /** Only one image-state can be active at a time. Images are drawn below sprites.
  138. If a state is without image it will try to use images from other states as described
  139. in ::EGUI_BUTTON_IMAGE_STATE.
  140. Images are a little less flexible than sprites, but easier to use.
  141. \param state: One of ::EGUI_BUTTON_IMAGE_STATE
  142. \param image: Image to be displayed or NULL to remove the image
  143. \param sourceRect: Source rectangle on the image texture. When width or height are 0 then the full texture-size is used (default). */
  144. virtual void setImage(EGUI_BUTTON_IMAGE_STATE state, video::ITexture* image=0, const core::rect<s32>& sourceRect=core::rect<s32>(0,0,0,0)) = 0;
  145. //! Sets an image which should be displayed on the button when it is in normal state.
  146. /** This is identical to calling setImage(EGBIS_IMAGE_UP, image); and might be deprecated in future revisions.
  147. \param image: Image to be displayed */
  148. virtual void setImage(video::ITexture* image=0) = 0;
  149. //! Sets a background image for the button when it is in normal state.
  150. /** This is identical to calling setImage(EGBIS_IMAGE_UP, image, sourceRect); and might be deprecated in future revisions.
  151. \param image: Texture containing the image to be displayed
  152. \param sourceRect: Position in the texture, where the image is located.
  153. When width or height are 0 then the full texture-size is used */
  154. virtual void setImage(video::ITexture* image, const core::rect<s32>& sourceRect) = 0;
  155. //! Sets a background image for the button when it is in pressed state.
  156. /** This is identical to calling setImage(EGBIS_IMAGE_DOWN, image); and might be deprecated in future revisions.
  157. If no images is specified for the pressed state via
  158. setPressedImage(), this image is also drawn in pressed state.
  159. \param image: Image to be displayed */
  160. virtual void setPressedImage(video::ITexture* image=0) = 0;
  161. //! Sets an image which should be displayed on the button when it is in pressed state.
  162. /** This is identical to calling setImage(EGBIS_IMAGE_DOWN, image, sourceRect); and might be deprecated in future revisions.
  163. \param image: Texture containing the image to be displayed
  164. \param sourceRect: Position in the texture, where the image is located */
  165. virtual void setPressedImage(video::ITexture* image, const core::rect<s32>& sourceRect) = 0;
  166. //! Sets the sprite bank used by the button
  167. /** NOTE: The spritebank itself is _not_ serialized so far. The sprites are serialized.
  168. Which means after loading the gui you still have to set the spritebank manually. */
  169. virtual void setSpriteBank(IGUISpriteBank* bank=0) = 0;
  170. //! Sets the animated sprite for a specific button state
  171. /** Several sprites can be drawn at the same time.
  172. Sprites can be animated.
  173. Sprites are drawn above the images.
  174. \param index: Number of the sprite within the sprite bank, use -1 for no sprite
  175. \param state: State of the button to set the sprite for
  176. \param index: The sprite number from the current sprite bank
  177. \param color: The color of the sprite
  178. \param loop: True if the animation should loop, false if not
  179. \param scale: True if the sprite should scale to button size, false if not */
  180. virtual void setSprite(EGUI_BUTTON_STATE state, s32 index,
  181. video::SColor color=video::SColor(255,255,255,255), bool loop=false, bool scale=false) = 0;
  182. //! Get the sprite-index for the given state or -1 when no sprite is set
  183. virtual s32 getSpriteIndex(EGUI_BUTTON_STATE state) const = 0;
  184. //! Get the sprite color for the given state. Color is only used when a sprite is set.
  185. virtual video::SColor getSpriteColor(EGUI_BUTTON_STATE state) const = 0;
  186. //! Returns if the sprite in the given state does loop
  187. virtual bool getSpriteLoop(EGUI_BUTTON_STATE state) const = 0;
  188. //! Returns if the sprite in the given state is scaled
  189. virtual bool getSpriteScale(EGUI_BUTTON_STATE state) const = 0;
  190. //! Sets if the button should behave like a push button.
  191. /** Which means it can be in two states: Normal or Pressed. With a click on the button,
  192. the user can change the state of the button. */
  193. virtual void setIsPushButton(bool isPushButton=true) = 0;
  194. //! Sets the pressed state of the button if this is a pushbutton
  195. virtual void setPressed(bool pressed=true) = 0;
  196. //! Returns if the button is currently pressed
  197. virtual bool isPressed() const = 0;
  198. //! Sets if the alpha channel should be used for drawing background images on the button (default is false)
  199. virtual void setUseAlphaChannel(bool useAlphaChannel=true) = 0;
  200. //! Returns if the alpha channel should be used for drawing background images on the button
  201. virtual bool isAlphaChannelUsed() const = 0;
  202. //! Returns whether the button is a push button
  203. virtual bool isPushButton() const = 0;
  204. //! Sets if the button should use the skin to draw its border and button face (default is true)
  205. virtual void setDrawBorder(bool border=true) = 0;
  206. //! Returns if the border and button face are being drawn using the skin
  207. virtual bool isDrawingBorder() const = 0;
  208. //! Sets if the button should scale the button images to fit
  209. virtual void setScaleImage(bool scaleImage=true) = 0;
  210. //! Checks whether the button scales the used images
  211. virtual bool isScalingImage() const = 0;
  212. //! Get if the shift key was pressed in last EGET_BUTTON_CLICKED event
  213. /** Generated together with event, so info is available in the event-receiver. */
  214. virtual bool getClickShiftState() const = 0;
  215. //! Get if the control key was pressed in last EGET_BUTTON_CLICKED event
  216. /** Generated together with event, so info is available in the event-receiver. */
  217. virtual bool getClickControlState() const = 0;
  218. };
  219. } // end namespace gui
  220. } // end namespace irr
  221. #endif