Button.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #ifndef UI_BUTTON_H
  24. #define UI_BUTTON_H
  25. #include "BorderImage.h"
  26. //! Button UI element states
  27. enum ButtonState
  28. {
  29. BUTTON_INACTIVE = 0,
  30. BUTTON_HOVER,
  31. BUTTON_PRESSED
  32. };
  33. //! An image that reacts to mouse presses
  34. class Button : public BorderImage
  35. {
  36. public:
  37. //! Construct with name
  38. Button(const std::string& name = std::string());
  39. //! Destruct
  40. virtual ~Button();
  41. //! Load parameters from an XML file
  42. virtual XMLElement loadParameters(XMLFile* file, const std::string& elementName, ResourceCache* cache);
  43. //! Perform UI element update
  44. virtual void update(float timeStep);
  45. //! Return UI rendering batches
  46. virtual void getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor);
  47. //! React to mouse hover
  48. virtual void onHover(const IntVector2& position, const IntVector2& screenPosition);
  49. //! React to mouse click
  50. virtual void onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons);
  51. //! Set inactive image rectangle
  52. void setInactiveRect(const IntRect& rect);
  53. //! Set inactive image rectangle
  54. void setInactiveRect(int left, int top, int right, int bottom);
  55. //! Set hovering image rectangle
  56. void setHoverRect(const IntRect& rect);
  57. //! Set hovering image rectangle
  58. void setHoverRect(int left, int top, int right, int bottom);
  59. //! Set pressed image rectangle
  60. void setPressedRect(const IntRect& rect);
  61. //! Set pressed image rectangle
  62. void setPressedRect(int left, int top, int right, int bottom);
  63. //! Set hovering image delay
  64. void setHoverDelay(float delay);
  65. //! Set pressed image delay
  66. void setPressedDelay(float delay);
  67. //! Set optional label UI element
  68. void setLabel(UIElement* label);
  69. //! Return inactive image rectangle
  70. const IntRect& getInactiveRect() const { return mInactiveRect; }
  71. //! Return hovering image rectangle
  72. const IntRect& getHoverRect() const { return mHoverRect; }
  73. //! Return pressed image rectangle
  74. const IntRect& getPressedRect() const { return mPressedRect; }
  75. //! Return hovering image delay
  76. float getHoverDelay() const { return mHoverDelay; }
  77. //! Return pressed image delay
  78. float getPressedDelay() const { return mPressedDelay; }
  79. //! Return label UI element
  80. UIElement* getLabel() const { return mLabel; }
  81. protected:
  82. //! Label UI element
  83. SharedPtr<UIElement> mLabel;
  84. //! Inactive image rectangle
  85. IntRect mInactiveRect;
  86. //! Hovering image rectangle
  87. IntRect mHoverRect;
  88. //! Pressed image rectangle
  89. IntRect mPressedRect;
  90. //! Hovering image delay
  91. float mHoverDelay;
  92. //! Pressed image delay
  93. float mPressedDelay;
  94. //! State timer
  95. float mStateTime;
  96. //! Current state
  97. ButtonState mState;
  98. //! Hovering flag
  99. bool mHoveringThisFrame;
  100. };
  101. #endif // UI_BUTTON_H