Button.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include "Precompiled.h"
  24. #include "Button.h"
  25. #include "InputEvents.h"
  26. #include "UIEvents.h"
  27. #include "DebugNew.h"
  28. Button::Button(const std::string& name) :
  29. BorderImage(name),
  30. mInactiveRect(IntRect::sZero),
  31. mPressedRect(IntRect::sZero),
  32. mLabelOffset(IntVector2::sZero),
  33. mPressed(false)
  34. {
  35. mClipChildren = true;
  36. mEnabled = true;
  37. }
  38. Button::~Button()
  39. {
  40. }
  41. void Button::setStyle(const XMLElement& element, ResourceCache* cache)
  42. {
  43. BorderImage::setStyle(element, cache);
  44. if (element.hasChildElement("inactiverect"))
  45. setInactiveRect(element.getChildElement("inactiverect").getIntRect("value"));
  46. if (element.hasChildElement("pressedrect"))
  47. setPressedRect(element.getChildElement("pressedrect").getIntRect("value"));
  48. if (element.hasChildElement("labeloffset"))
  49. setLabelOffset(element.getChildElement("labeloffset").getIntVector2("value"));
  50. }
  51. void Button::update(float timeStep)
  52. {
  53. if (!mHovering)
  54. setPressed(false);
  55. }
  56. void Button::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  57. {
  58. if (mPressed)
  59. mImageRect = mPressedRect;
  60. else
  61. mImageRect = mInactiveRect;
  62. BorderImage::getBatches(batches, quads, currentScissor);
  63. }
  64. void Button::onHover(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  65. {
  66. setPressed((buttons & MOUSEB_LEFT) != 0);
  67. mHovering = true;
  68. }
  69. void Button::onClick(const IntVector2& position, const IntVector2& screenPosition, unsigned buttons)
  70. {
  71. if (buttons & MOUSEB_LEFT)
  72. {
  73. setPressed(true);
  74. mHovering = true;
  75. using namespace Pressed;
  76. VariantMap eventData;
  77. eventData[P_ELEMENT] = (void*)this;
  78. sendEvent(EVENT_PRESSED, eventData);
  79. }
  80. }
  81. void Button::setInactiveRect(const IntRect& rect)
  82. {
  83. mInactiveRect = rect;
  84. }
  85. void Button::setInactiveRect(int left, int top, int right, int bottom)
  86. {
  87. mInactiveRect = IntRect(left, top, right, bottom);
  88. }
  89. void Button::setPressedRect(const IntRect& rect)
  90. {
  91. mPressedRect = rect;
  92. }
  93. void Button::setPressedRect(int left, int top, int right, int bottom)
  94. {
  95. mPressedRect = IntRect(left, top, right, bottom);
  96. }
  97. void Button::setLabelOffset(const IntVector2& offset)
  98. {
  99. mLabelOffset = offset;
  100. }
  101. void Button::setLabelOffset(int x, int y)
  102. {
  103. mLabelOffset = IntVector2(x, y);
  104. }
  105. void Button::setPressed(bool enable)
  106. {
  107. mPressed = enable;
  108. setChildOffset(mPressed ? mLabelOffset : IntVector2::sZero);
  109. }