Button.cpp 3.7 KB

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