Button.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. //
  2. // Copyright (c) 2008-2013 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 "Button.h"
  24. #include "Context.h"
  25. #include "InputEvents.h"
  26. #include "UI.h"
  27. #include "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. pressedChildOffset_(IntVector2::ZERO),
  36. repeatDelay_(1.0f),
  37. repeatRate_(0.0f),
  38. repeatTimer_(0.0f),
  39. pressed_(false)
  40. {
  41. enabled_ = true;
  42. focusMode_ = FM_FOCUSABLE;
  43. }
  44. Button::~Button()
  45. {
  46. }
  47. void Button::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<Button>(UI_CATEGORY);
  50. COPY_BASE_ATTRIBUTES(Button, BorderImage);
  51. UPDATE_ATTRIBUTE_DEFAULT_VALUE(Button, "Is Enabled", true);
  52. REF_ACCESSOR_ATTRIBUTE(Button, VAR_INTVECTOR2, "Pressed Image Offset", GetPressedOffset, SetPressedOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  53. REF_ACCESSOR_ATTRIBUTE(Button, VAR_INTVECTOR2, "Pressed Child Offset", GetPressedChildOffset, SetPressedChildOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  54. ACCESSOR_ATTRIBUTE(Button, VAR_FLOAT, "Repeat Delay", GetRepeatDelay, SetRepeatDelay, float, 1.0f, AM_FILE);
  55. ACCESSOR_ATTRIBUTE(Button, VAR_FLOAT, "Repeat Rate", GetRepeatRate, SetRepeatRate, float, 0.0f, AM_FILE);
  56. }
  57. void Button::Update(float timeStep)
  58. {
  59. if (!hovering_ && pressed_)
  60. SetPressed(false);
  61. // Send repeat events if pressed
  62. if (pressed_ && repeatRate_ > 0.0f)
  63. {
  64. repeatTimer_ -= timeStep;
  65. if (repeatTimer_ <= 0.0f)
  66. {
  67. repeatTimer_ += 1.0f / repeatRate_;
  68. using namespace Pressed;
  69. VariantMap eventData;
  70. eventData[P_ELEMENT] = (void*)this;
  71. SendEvent(E_PRESSED, eventData);
  72. }
  73. }
  74. }
  75. void Button::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  76. {
  77. IntVector2 offset(IntVector2::ZERO);
  78. if (hovering_)
  79. offset += hoverOffset_;
  80. if (pressed_ || selected_)
  81. offset += pressedOffset_;
  82. BorderImage::GetBatches(batches, vertexData, currentScissor, offset);
  83. }
  84. void Button::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor)
  85. {
  86. if (button == MOUSEB_LEFT)
  87. {
  88. SetPressed(true);
  89. repeatTimer_ = repeatDelay_;
  90. hovering_ = true;
  91. using namespace Pressed;
  92. VariantMap eventData;
  93. eventData[P_ELEMENT] = (void*)this;
  94. SendEvent(E_PRESSED, eventData);
  95. }
  96. }
  97. void Button::OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, int button, int buttons, int qualifiers, Cursor* cursor, UIElement* beginElement)
  98. {
  99. if (pressed_ && button == MOUSEB_LEFT)
  100. {
  101. SetPressed(false);
  102. using namespace Released;
  103. VariantMap eventData;
  104. eventData[P_ELEMENT] = (void*)this;
  105. SendEvent(E_RELEASED, eventData);
  106. }
  107. }
  108. void Button::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  109. {
  110. SetPressed(true);
  111. }
  112. void Button::OnKey(int key, int buttons, int qualifiers)
  113. {
  114. UI* ui = GetSubsystem<UI>();
  115. if (ui->GetFocusElement() == this && (key == KEY_RETURN || key == KEY_RETURN2 || key == KEY_KP_ENTER || key == KEY_SPACE))
  116. {
  117. // Simulate LMB
  118. OnClickBegin(IntVector2(), IntVector2(), MOUSEB_LEFT, 0, 0, 0);
  119. OnClickEnd(IntVector2(), IntVector2(), MOUSEB_LEFT, 0, 0, 0, 0);
  120. }
  121. }
  122. void Button::SetPressedOffset(const IntVector2& offset)
  123. {
  124. pressedOffset_ = offset;
  125. }
  126. void Button::SetPressedOffset(int x, int y)
  127. {
  128. pressedOffset_ = IntVector2(x, y);
  129. }
  130. void Button::SetPressedChildOffset(const IntVector2& offset)
  131. {
  132. pressedChildOffset_ = offset;
  133. }
  134. void Button::SetPressedChildOffset(int x, int y)
  135. {
  136. pressedChildOffset_ = IntVector2(x, y);
  137. }
  138. void Button::SetRepeat(float delay, float rate)
  139. {
  140. SetRepeatDelay(delay);
  141. SetRepeatRate(rate);
  142. }
  143. void Button::SetRepeatDelay(float delay)
  144. {
  145. repeatDelay_ = Max(delay, 0.0f);
  146. }
  147. void Button::SetRepeatRate(float rate)
  148. {
  149. repeatRate_ = Max(rate, 0.0f);
  150. }
  151. void Button::SetPressed(bool enable)
  152. {
  153. pressed_ = enable;
  154. SetChildOffset(pressed_ ? pressedChildOffset_ : IntVector2::ZERO);
  155. }
  156. }