Button.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. 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. COPY_BASE_ATTRIBUTES(Button, BorderImage);
  50. REF_ACCESSOR_ATTRIBUTE(Button, VAR_INTVECTOR2, "Pressed Offset", GetPressedOffset, SetPressedOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  51. REF_ACCESSOR_ATTRIBUTE(Button, VAR_INTVECTOR2, "Label Offset", GetLabelOffset, SetLabelOffset, IntVector2, IntVector2::ZERO, AM_FILE);
  52. ACCESSOR_ATTRIBUTE(Button, VAR_FLOAT, "Repeat Delay", GetRepeatDelay, SetRepeatDelay, float, 1.0f, AM_FILE);
  53. ACCESSOR_ATTRIBUTE(Button, VAR_FLOAT, "Repeat Rate", GetRepeatRate, SetRepeatRate, float, 0.0f, AM_FILE);
  54. }
  55. void Button::SetStyle(const XMLElement& element)
  56. {
  57. BorderImage::SetStyle(element);
  58. if (element.HasChild("pressedoffset"))
  59. SetPressedOffset(element.GetChild("pressedoffset").GetIntVector2("value"));
  60. if (element.HasChild("labeloffset"))
  61. SetLabelOffset(element.GetChild("labeloffset").GetIntVector2("value"));
  62. if (element.HasChild("repeat"))
  63. {
  64. XMLElement repeatElem = element.GetChild("repeat");
  65. SetRepeat(repeatElem.GetFloat("delay"), repeatElem.GetFloat("rate"));
  66. }
  67. }
  68. void Button::Update(float timeStep)
  69. {
  70. if (!hovering_ && pressed_ == true)
  71. SetPressed(false);
  72. // Send repeat events if pressed
  73. if (pressed_ && repeatRate_ > 0.0f)
  74. {
  75. repeatTimer_ -= timeStep;
  76. if (repeatTimer_ <= 0.0f)
  77. {
  78. repeatTimer_ += 1.0f / repeatRate_;
  79. using namespace Pressed;
  80. VariantMap eventData;
  81. eventData[P_ELEMENT] = (void*)this;
  82. SendEvent(E_PRESSED, eventData);
  83. }
  84. }
  85. }
  86. void Button::GetBatches(PODVector<UIBatch>& batches, PODVector<UIQuad>& quads, const IntRect& currentScissor)
  87. {
  88. IntVector2 offset(IntVector2::ZERO);
  89. if (hovering_)
  90. offset += hoverOffset_;
  91. if (pressed_ || selected_)
  92. offset += pressedOffset_;
  93. BorderImage::GetBatches(batches, quads, currentScissor, offset);
  94. }
  95. void Button::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  96. {
  97. bool oldPressed = pressed_;
  98. SetPressed((buttons & MOUSEB_LEFT) != 0);
  99. hovering_ = true;
  100. if (oldPressed && !pressed_)
  101. {
  102. using namespace Released;
  103. VariantMap eventData;
  104. eventData[P_ELEMENT] = (void*)this;
  105. SendEvent(E_RELEASED, eventData);
  106. }
  107. }
  108. void Button::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  109. {
  110. if (buttons & MOUSEB_LEFT)
  111. {
  112. SetPressed(true);
  113. repeatTimer_ = repeatDelay_;
  114. hovering_ = true;
  115. using namespace Pressed;
  116. VariantMap eventData;
  117. eventData[P_ELEMENT] = (void*)this;
  118. SendEvent(E_PRESSED, eventData);
  119. }
  120. }
  121. void Button::SetPressedOffset(const IntVector2& offset)
  122. {
  123. pressedOffset_ = offset;
  124. }
  125. void Button::SetPressedOffset(int x, int y)
  126. {
  127. pressedOffset_ = IntVector2(x, y);
  128. }
  129. void Button::SetLabelOffset(const IntVector2& offset)
  130. {
  131. labelOffset_ = offset;
  132. }
  133. void Button::SetLabelOffset(int x, int y)
  134. {
  135. labelOffset_ = IntVector2(x, y);
  136. }
  137. void Button::SetRepeat(float delay, float rate)
  138. {
  139. SetRepeatDelay(delay);
  140. SetRepeatRate(rate);
  141. }
  142. void Button::SetRepeatDelay(float delay)
  143. {
  144. repeatDelay_ = Max(delay, 0.0f);
  145. }
  146. void Button::SetRepeatRate(float rate)
  147. {
  148. repeatRate_ = Max(rate, 0.0f);
  149. }
  150. void Button::SetPressed(bool enable)
  151. {
  152. pressed_ = enable;
  153. SetChildOffset(pressed_ ? labelOffset_ : IntVector2::ZERO);
  154. }
  155. }