Button.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "../Precompiled.h"
  23. #include "../Core/Context.h"
  24. #include "../Input/InputEvents.h"
  25. #include "../UI/Button.h"
  26. #include "../UI/UI.h"
  27. #include "../UI/UIEvents.h"
  28. #include "../DebugNew.h"
  29. namespace Urho3D
  30. {
  31. extern const char* UI_CATEGORY;
  32. Button::Button(Context* context) :
  33. BorderImage(context),
  34. pressedOffset_(IntVector2::ZERO),
  35. disabledOffset_(IntVector2::ZERO),
  36. pressedChildOffset_(IntVector2::ZERO),
  37. repeatDelay_(1.0f),
  38. repeatRate_(0.0f),
  39. repeatTimer_(0.0f),
  40. pressed_(false)
  41. {
  42. SetEnabled(true);
  43. focusMode_ = FM_FOCUSABLE;
  44. }
  45. Button::~Button()
  46. {
  47. }
  48. void Button::RegisterObject(Context* context)
  49. {
  50. context->RegisterFactory<Button>(UI_CATEGORY);
  51. URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
  52. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  53. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Focus Mode", FM_FOCUSABLE);
  54. URHO3D_ACCESSOR_ATTRIBUTE("Pressed Image Offset", GetPressedOffset, SetPressedOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  55. URHO3D_ACCESSOR_ATTRIBUTE("Disabled Image Offset", GetDisabledOffset, SetDisabledOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  56. URHO3D_ACCESSOR_ATTRIBUTE("Pressed Child Offset", GetPressedChildOffset, SetPressedChildOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  57. URHO3D_ACCESSOR_ATTRIBUTE("Repeat Delay", GetRepeatDelay, SetRepeatDelay, float, 1.0f, AM_FILE);
  58. URHO3D_ACCESSOR_ATTRIBUTE("Repeat Rate", GetRepeatRate, SetRepeatRate, float, 0.0f, AM_FILE);
  59. }
  60. void Button::Update(float timeStep)
  61. {
  62. if (!hovering_ && pressed_)
  63. SetPressed(false);
  64. // Send repeat events if pressed
  65. if (pressed_ && repeatRate_ > 0.0f)
  66. {
  67. repeatTimer_ -= timeStep;
  68. if (repeatTimer_ <= 0.0f)
  69. {
  70. repeatTimer_ += 1.0f / repeatRate_;
  71. using namespace Pressed;
  72. VariantMap& eventData = GetEventDataMap();
  73. eventData[P_ELEMENT] = this;
  74. SendEvent(E_PRESSED, eventData);
  75. }
  76. }
  77. }
  78. void Button::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  79. {
  80. IntVector2 offset(IntVector2::ZERO);
  81. if (enabled_)
  82. {
  83. if (hovering_ || HasFocus())
  84. offset += hoverOffset_;
  85. if (pressed_ || selected_)
  86. offset += pressedOffset_;
  87. }
  88. else
  89. {
  90. offset += disabledOffset_;
  91. }
  92. BorderImage::GetBatches(batches, vertexData, currentScissor, offset);
  93. }
  94. void Button::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers,
  95. Cursor* cursor)
  96. {
  97. if (button == MOUSEB_LEFT)
  98. {
  99. SetPressed(true);
  100. repeatTimer_ = repeatDelay_;
  101. hovering_ = true;
  102. using namespace Pressed;
  103. VariantMap& eventData = GetEventDataMap();
  104. eventData[P_ELEMENT] = this;
  105. SendEvent(E_PRESSED, eventData);
  106. }
  107. }
  108. void Button::OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers,
  109. Cursor* cursor, UIElement* beginElement)
  110. {
  111. if (pressed_ && button == MOUSEB_LEFT)
  112. {
  113. SetPressed(false);
  114. // If mouse was released on top of the element, consider it hovering on this frame yet (see issue #1453)
  115. if (IsInside(screenPosition, true))
  116. hovering_ = true;
  117. using namespace Released;
  118. VariantMap& eventData = GetEventDataMap();
  119. eventData[P_ELEMENT] = this;
  120. SendEvent(E_RELEASED, eventData);
  121. }
  122. }
  123. void Button::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, int buttons,
  124. int qualifiers, Cursor* cursor)
  125. {
  126. SetPressed(true);
  127. }
  128. void Button::OnKey(int key, int buttons, int qualifiers)
  129. {
  130. if (HasFocus() && (key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER || key == KEY_SPACE))
  131. {
  132. // Simulate LMB click
  133. OnClickBegin(IntVector2(), IntVector2(), MOUSEB_LEFT, 0, 0, nullptr);
  134. OnClickEnd(IntVector2(), IntVector2(), MOUSEB_LEFT, 0, 0, nullptr, nullptr);
  135. }
  136. }
  137. void Button::SetPressedOffset(const IntVector2& offset)
  138. {
  139. pressedOffset_ = offset;
  140. }
  141. void Button::SetPressedOffset(int x, int y)
  142. {
  143. pressedOffset_ = IntVector2(x, y);
  144. }
  145. void Button::SetDisabledOffset(const IntVector2& offset)
  146. {
  147. disabledOffset_ = offset;
  148. }
  149. void Button::SetDisabledOffset(int x, int y)
  150. {
  151. disabledOffset_ = IntVector2(x, y);
  152. }
  153. void Button::SetPressedChildOffset(const IntVector2& offset)
  154. {
  155. pressedChildOffset_ = offset;
  156. }
  157. void Button::SetPressedChildOffset(int x, int y)
  158. {
  159. pressedChildOffset_ = IntVector2(x, y);
  160. }
  161. void Button::SetRepeat(float delay, float rate)
  162. {
  163. SetRepeatDelay(delay);
  164. SetRepeatRate(rate);
  165. }
  166. void Button::SetRepeatDelay(float delay)
  167. {
  168. repeatDelay_ = Max(delay, 0.0f);
  169. }
  170. void Button::SetRepeatRate(float rate)
  171. {
  172. repeatRate_ = Max(rate, 0.0f);
  173. }
  174. void Button::SetPressed(bool enable)
  175. {
  176. pressed_ = enable;
  177. SetChildOffset(pressed_ ? pressedChildOffset_ : IntVector2::ZERO);
  178. }
  179. }