Button.cpp 5.0 KB

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