Button.cpp 4.8 KB

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