Button.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. mPressedOffset(IntVector2::sZero),
  31. mLabelOffset(IntVector2::sZero),
  32. mRepeatDelay(1.0f),
  33. mRepeatRate(0.0f),
  34. mRepeatTimer(0.0f),
  35. mPressed(false)
  36. {
  37. mEnabled = true;
  38. }
  39. Button::~Button()
  40. {
  41. }
  42. void Button::setStyle(const XMLElement& element, ResourceCache* cache)
  43. {
  44. BorderImage::setStyle(element, cache);
  45. if (element.hasChildElement("pressedoffset"))
  46. setPressedOffset(element.getChildElement("pressedoffset").getIntVector2("value"));
  47. if (element.hasChildElement("labeloffset"))
  48. setLabelOffset(element.getChildElement("labeloffset").getIntVector2("value"));
  49. if (element.hasChildElement("repeat"))
  50. {
  51. XMLElement repeatElem = element.getChildElement("repeat");
  52. setRepeat(repeatElem.getFloat("delay"), repeatElem.getFloat("rate"));
  53. }
  54. }
  55. void Button::update(float timeStep)
  56. {
  57. if (!mHovering)
  58. setPressed(false);
  59. // Send repeat events if pressed
  60. if ((mPressed) && (mRepeatRate > 0.0f))
  61. {
  62. mRepeatTimer -= timeStep;
  63. if (mRepeatTimer <= 0.0f)
  64. {
  65. mRepeatTimer += 1.0f / mRepeatRate;
  66. using namespace Pressed;
  67. VariantMap eventData;
  68. eventData[P_ELEMENT] = (void*)this;
  69. sendEvent(EVENT_PRESSED, eventData);
  70. }
  71. }
  72. }
  73. void Button::getBatches(std::vector<UIBatch>& batches, std::vector<UIQuad>& quads, const IntRect& currentScissor)
  74. {
  75. IntVector2 offset(IntVector2::sZero);
  76. if ((mHovering) || (mSelected))
  77. offset += mHoverOffset;
  78. if (mPressed)
  79. offset += mPressedOffset;
  80. BorderImage::getBatches(batches, quads, currentScissor, offset);
  81. }
  82. void Button::onHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  83. {
  84. setPressed((buttons & MOUSEB_LEFT) != 0);
  85. mHovering = true;
  86. }
  87. void Button::onClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers)
  88. {
  89. if (buttons & MOUSEB_LEFT)
  90. {
  91. setPressed(true);
  92. mRepeatTimer = mRepeatDelay;
  93. mHovering = true;
  94. using namespace Pressed;
  95. VariantMap eventData;
  96. eventData[P_ELEMENT] = (void*)this;
  97. sendEvent(EVENT_PRESSED, eventData);
  98. }
  99. }
  100. void Button::setPressedOffset(const IntVector2& offset)
  101. {
  102. mPressedOffset = offset;
  103. }
  104. void Button::setPressedOffset(int x, int y)
  105. {
  106. mPressedOffset = IntVector2(x, y);
  107. }
  108. void Button::setLabelOffset(const IntVector2& offset)
  109. {
  110. mLabelOffset = offset;
  111. }
  112. void Button::setLabelOffset(int x, int y)
  113. {
  114. mLabelOffset = IntVector2(x, y);
  115. }
  116. void Button::setRepeat(float delay, float rate)
  117. {
  118. mRepeatDelay = max(delay, 0.0f);
  119. mRepeatRate = max(rate, 0.0f);
  120. }
  121. void Button::setPressed(bool enable)
  122. {
  123. mPressed = enable;
  124. setChildOffset(mPressed ? mLabelOffset : IntVector2::sZero);
  125. }