Button.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2012 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 "Context.h"
  26. #include "InputEvents.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. OBJECTTYPESTATIC(Button);
  30. Button::Button(Context* context) :
  31. BorderImage(context),
  32. pressedOffset_(IntVector2::ZERO),
  33. labelOffset_(IntVector2::ZERO),
  34. repeatDelay_(1.0f),
  35. repeatRate_(0.0f),
  36. repeatTimer_(0.0f),
  37. pressed_(false)
  38. {
  39. active_ = true;
  40. }
  41. Button::~Button()
  42. {
  43. }
  44. void Button::RegisterObject(Context* context)
  45. {
  46. context->RegisterFactory<Button>();
  47. }
  48. void Button::SetStyle(const XMLElement& element)
  49. {
  50. BorderImage::SetStyle(element);
  51. if (element.HasChild("pressedoffset"))
  52. SetPressedOffset(element.GetChild("pressedoffset").GetIntVector2("value"));
  53. if (element.HasChild("labeloffset"))
  54. SetLabelOffset(element.GetChild("labeloffset").GetIntVector2("value"));
  55. if (element.HasChild("repeat"))
  56. {
  57. XMLElement repeatElem = element.GetChild("repeat");
  58. SetRepeat(repeatElem.GetFloat("delay"), repeatElem.GetFloat("rate"));
  59. }
  60. }
  61. void Button::Update(float timeStep)
  62. {
  63. if (!hovering_ && pressed_ == true)
  64. SetPressed(false);
  65. // Send repeat events if pressed
  66. if (pressed_ && repeatRate_ > 0.0f)
  67. {
  68. repeatTimer_ -= timeStep;
  69. if (repeatTimer_ <= 0.0f)
  70. {
  71. repeatTimer_ += 1.0f / repeatRate_;
  72. using namespace Pressed;
  73. VariantMap eventData;
  74. eventData[P_ELEMENT] = (void*)this;
  75. SendEvent(E_PRESSED, eventData);
  76. }
  77. }
  78. }
  79. void Button::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  80. {
  81. IntVector2 offset(IntVector2::ZERO);
  82. if (hovering_)
  83. offset += hoverOffset_;
  84. if (pressed_ || selected_)
  85. offset += pressedOffset_;
  86. BorderImage::GetBatches(batches, quads, currentScissor, offset);
  87. }
  88. void Button::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  89. {
  90. bool oldPressed = pressed_;
  91. SetPressed((buttons & MOUSEB_LEFT) != 0);
  92. hovering_ = true;
  93. if (oldPressed && !pressed_)
  94. {
  95. using namespace Released;
  96. VariantMap eventData;
  97. eventData[P_ELEMENT] = (void*)this;
  98. SendEvent(E_RELEASED, eventData);
  99. }
  100. }
  101. void Button::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  102. {
  103. if (buttons & MOUSEB_LEFT)
  104. {
  105. SetPressed(true);
  106. repeatTimer_ = repeatDelay_;
  107. hovering_ = true;
  108. using namespace Pressed;
  109. VariantMap eventData;
  110. eventData[P_ELEMENT] = (void*)this;
  111. SendEvent(E_PRESSED, eventData);
  112. }
  113. }
  114. void Button::SetPressedOffset(const IntVector2& offset)
  115. {
  116. pressedOffset_ = offset;
  117. }
  118. void Button::SetPressedOffset(int x, int y)
  119. {
  120. pressedOffset_ = IntVector2(x, y);
  121. }
  122. void Button::SetLabelOffset(const IntVector2& offset)
  123. {
  124. labelOffset_ = offset;
  125. }
  126. void Button::SetLabelOffset(int x, int y)
  127. {
  128. labelOffset_ = IntVector2(x, y);
  129. }
  130. void Button::SetRepeat(float delay, float rate)
  131. {
  132. SetRepeatDelay(delay);
  133. SetRepeatRate(rate);
  134. }
  135. void Button::SetRepeatDelay(float delay)
  136. {
  137. repeatDelay_ = Max(delay, 0.0f);
  138. }
  139. void Button::SetRepeatRate(float rate)
  140. {
  141. repeatRate_ = Max(rate, 0.0f);
  142. }
  143. void Button::SetPressed(bool enable)
  144. {
  145. pressed_ = enable;
  146. SetChildOffset(pressed_ ? labelOffset_ : IntVector2::ZERO);
  147. }