Slider.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 "Context.h"
  24. #include "InputEvents.h"
  25. #include "Log.h"
  26. #include "Slider.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. const char* orientations[] =
  32. {
  33. "Horizontal",
  34. "Vertical",
  35. 0
  36. };
  37. template<> Orientation Variant::Get<Orientation>() const
  38. {
  39. return (Orientation)GetInt();
  40. }
  41. OBJECTTYPESTATIC(Slider);
  42. Slider::Slider(Context* context) :
  43. BorderImage(context),
  44. orientation_(O_HORIZONTAL),
  45. range_(1.0f),
  46. value_(0.0f),
  47. dragSlider_(false),
  48. repeatRate_(0.0f)
  49. {
  50. active_ = true;
  51. knob_ = CreateChild<BorderImage>();
  52. knob_->SetInternal(true);
  53. UpdateSlider();
  54. }
  55. Slider::~Slider()
  56. {
  57. }
  58. void Slider::RegisterObject(Context* context)
  59. {
  60. context->RegisterFactory<Slider>();
  61. ENUM_ACCESSOR_ATTRIBUTE(Slider, "Orientation", GetOrientation, SetOrientation, Orientation, orientations, O_HORIZONTAL, AM_FILE);
  62. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Range", GetRange, SetRange, float, 1.0f, AM_FILE);
  63. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Value", GetValue, SetValue, float, 0.0f, AM_FILE);
  64. ACCESSOR_ATTRIBUTE(Slider, VAR_FLOAT, "Repeat Rate", GetRepeatRate, SetRepeatRate, float, 0.0f, AM_FILE);
  65. COPY_BASE_ATTRIBUTES(Slider, BorderImage);
  66. }
  67. void Slider::Update(float timeStep)
  68. {
  69. if (dragSlider_)
  70. hovering_ = true;
  71. // Propagate hover effect to the slider knob
  72. knob_->SetHovering(hovering_);
  73. knob_->SetSelected(hovering_);
  74. }
  75. void Slider::OnHover(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  76. {
  77. BorderImage::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  78. // Show hover effect if inside the slider knob
  79. hovering_ = knob_->IsInside(screenPosition, true);
  80. // If not hovering on the knob, send it as page event
  81. if (!hovering_)
  82. Page(position, buttons, qualifiers);
  83. }
  84. void Slider::OnClick(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  85. {
  86. BorderImage::OnClick(position, screenPosition, buttons, qualifiers, cursor);
  87. selected_ = true;
  88. hovering_ = knob_->IsInside(screenPosition, true);
  89. if (!hovering_)
  90. Page(position, buttons, qualifiers);
  91. }
  92. void Slider::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  93. {
  94. dragBeginCursor_ = position;
  95. dragBeginPosition_ = knob_->GetPosition();
  96. dragSlider_ = knob_->IsInside(screenPosition, true);
  97. }
  98. void Slider::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, int buttons, int qualifiers, Cursor* cursor)
  99. {
  100. if (!dragSlider_ || GetSize() == knob_->GetSize())
  101. return;
  102. float newValue = value_;
  103. IntVector2 delta = position - dragBeginCursor_;
  104. if (orientation_ == O_HORIZONTAL)
  105. {
  106. int newX = Clamp(dragBeginPosition_.x_ + delta.x_, 0, GetWidth() - knob_->GetWidth());
  107. knob_->SetPosition(newX, 0);
  108. newValue = (float)newX * range_ / (float)(GetWidth() - knob_->GetWidth());
  109. }
  110. else
  111. {
  112. int newY = Clamp(dragBeginPosition_.y_ + delta.y_, 0, GetHeight() - knob_->GetHeight());
  113. knob_->SetPosition(0, newY);
  114. newValue = (float)newY * range_ / (float)(GetHeight() - knob_->GetHeight());
  115. }
  116. SetValue(newValue);
  117. }
  118. void Slider::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, Cursor* cursor)
  119. {
  120. dragSlider_ = false;
  121. selected_ = false;
  122. }
  123. void Slider::OnResize()
  124. {
  125. UpdateSlider();
  126. }
  127. void Slider::SetOrientation(Orientation type)
  128. {
  129. orientation_ = type;
  130. UpdateSlider();
  131. }
  132. void Slider::SetRange(float range)
  133. {
  134. range = Max(range, 0.0f);
  135. if (range != range_)
  136. {
  137. range_ = range;
  138. UpdateSlider();
  139. }
  140. }
  141. void Slider::SetValue(float value)
  142. {
  143. value = Clamp(value, 0.0f, range_);
  144. if (value != value_)
  145. {
  146. value_ = value;
  147. UpdateSlider();
  148. using namespace SliderChanged;
  149. VariantMap eventData;
  150. eventData[P_ELEMENT] = (void*)this;
  151. eventData[P_VALUE] = value_;
  152. SendEvent(E_SLIDERCHANGED, eventData);
  153. }
  154. }
  155. void Slider::ChangeValue(float delta)
  156. {
  157. SetValue(value_ + delta);
  158. }
  159. void Slider::SetRepeatRate(float rate)
  160. {
  161. repeatRate_ = Max(rate, 0.0f);
  162. }
  163. void Slider::UpdateSlider()
  164. {
  165. const IntRect& border = knob_->GetBorder();
  166. if (range_ > 0.0f)
  167. {
  168. if (orientation_ == O_HORIZONTAL)
  169. {
  170. int sliderLength = (int)Max((float)GetWidth() / (range_ + 1.0f), (float)(border.left_ + border.right_));
  171. float sliderPos = (float)(GetWidth() - sliderLength) * value_ / range_;
  172. knob_->SetSize(sliderLength, GetHeight());
  173. knob_->SetPosition(Clamp((int)(sliderPos + 0.5f), 0, GetWidth() - knob_->GetWidth()), 0);
  174. }
  175. else
  176. {
  177. int sliderLength = (int)Max((float)GetHeight() / (range_ + 1.0f), (float)(border.top_ + border.bottom_));
  178. float sliderPos = (float)(GetHeight() - sliderLength) * value_ / range_;
  179. knob_->SetSize(GetWidth(), sliderLength);
  180. knob_->SetPosition(0, Clamp((int)(sliderPos + 0.5f), 0, GetHeight() - knob_->GetHeight()));
  181. }
  182. }
  183. else
  184. {
  185. knob_->SetSize(GetSize());
  186. knob_->SetPosition(0, 0);
  187. }
  188. }
  189. void Slider::Page(const IntVector2& position, int buttons, int qualifiers)
  190. {
  191. IntVector2 offsetXY = position - knob_->GetPosition() - knob_->GetSize() / 2;
  192. int offset = orientation_ == O_HORIZONTAL ? offsetXY.x_ : offsetXY.y_;
  193. float length = (float)(orientation_ == O_HORIZONTAL ? GetWidth() : GetHeight());
  194. using namespace SliderPaged;
  195. VariantMap eventData;
  196. eventData[P_ELEMENT] = (void*)this;
  197. eventData[P_OFFSET] = offset;
  198. // Only generate the 'click' variant of the event when the slider is selected
  199. // i.e. when it has received the first initial click.
  200. if (selected_ && repeatRate_ > 0.0f && repeatTimer_.GetMSec(false) >= Lerp(1000.0f / repeatRate_, 0, Abs(offset) / length))
  201. {
  202. repeatTimer_.Reset();
  203. eventData[P_BUTTONS] = buttons;
  204. eventData[P_QUALIFIERS] = qualifiers;
  205. }
  206. // When without buttons & qualifiers parameters, the receiver should interpret
  207. // this event as just mouse hovering on slider's 'paging' area instead
  208. SendEvent(E_SLIDERPAGED, eventData);
  209. }
  210. }