Slider.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. // Copyright (c) 2008-2022 the Urho3D project
  2. // License: MIT
  3. #include "../Precompiled.h"
  4. #include "../Core/Context.h"
  5. #include "../Input/InputEvents.h"
  6. #include "../IO/Log.h"
  7. #include "../UI/Slider.h"
  8. #include "../UI/UIEvents.h"
  9. #include "../DebugNew.h"
  10. namespace Urho3D
  11. {
  12. const char* orientations[] =
  13. {
  14. "Horizontal",
  15. "Vertical",
  16. nullptr
  17. };
  18. extern const char* UI_CATEGORY;
  19. Slider::Slider(Context* context) :
  20. BorderImage(context),
  21. orientation_(O_HORIZONTAL),
  22. range_(1.0f),
  23. value_(0.0f),
  24. dragSlider_(false),
  25. repeatRate_(0.0f)
  26. {
  27. SetEnabled(true);
  28. knob_ = CreateChild<BorderImage>("S_Knob");
  29. knob_->SetInternal(true);
  30. UpdateSlider();
  31. }
  32. Slider::~Slider() = default;
  33. void Slider::RegisterObject(Context* context)
  34. {
  35. context->RegisterFactory<Slider>(UI_CATEGORY);
  36. URHO3D_COPY_BASE_ATTRIBUTES(BorderImage);
  37. URHO3D_UPDATE_ATTRIBUTE_DEFAULT_VALUE("Is Enabled", true);
  38. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Orientation", GetOrientation, SetOrientation, orientations, O_HORIZONTAL, AM_FILE);
  39. URHO3D_ACCESSOR_ATTRIBUTE("Range", GetRange, SetRange, 1.0f, AM_FILE);
  40. URHO3D_ACCESSOR_ATTRIBUTE("Value", GetValue, SetValue, 0.0f, AM_FILE);
  41. URHO3D_ACCESSOR_ATTRIBUTE("Repeat Rate", GetRepeatRate, SetRepeatRate, 0.0f, AM_FILE);
  42. }
  43. void Slider::Update(float timeStep)
  44. {
  45. if (dragSlider_)
  46. hovering_ = true;
  47. // Propagate hover effect to the slider knob
  48. knob_->SetHovering(hovering_);
  49. knob_->SetSelected(hovering_);
  50. }
  51. void Slider::OnHover(const IntVector2& position, const IntVector2& screenPosition, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor)
  52. {
  53. BorderImage::OnHover(position, screenPosition, buttons, qualifiers, cursor);
  54. // Show hover effect if inside the slider knob
  55. hovering_ = knob_->IsInside(screenPosition, true);
  56. // If not hovering on the knob, send it as page event
  57. if (!hovering_)
  58. Page(position, (bool)(buttons & MOUSEB_LEFT));
  59. }
  60. void Slider::OnClickBegin(const IntVector2& position, const IntVector2& screenPosition, MouseButton button, MouseButtonFlags buttons, QualifierFlags qualifiers,
  61. Cursor* cursor)
  62. {
  63. selected_ = true;
  64. hovering_ = knob_->IsInside(screenPosition, true);
  65. if (!hovering_ && button == MOUSEB_LEFT)
  66. Page(position, true);
  67. }
  68. void Slider::OnClickEnd(const IntVector2& position, const IntVector2& screenPosition, MouseButton button, MouseButtonFlags buttons, QualifierFlags qualifiers,
  69. Cursor* cursor, UIElement* beginElement)
  70. {
  71. hovering_ = knob_->IsInside(screenPosition, true);
  72. if (!hovering_ && button == MOUSEB_LEFT)
  73. Page(position, false);
  74. }
  75. void Slider::OnDragBegin(const IntVector2& position, const IntVector2& screenPosition, MouseButtonFlags buttons, QualifierFlags qualifiers, Cursor* cursor)
  76. {
  77. UIElement::OnDragBegin(position, screenPosition, buttons, qualifiers, cursor);
  78. if (buttons == MOUSEB_LEFT)
  79. {
  80. dragBeginCursor_ = position;
  81. dragBeginPosition_ = knob_->GetPosition();
  82. dragSlider_ = knob_->IsInside(screenPosition, true);
  83. }
  84. }
  85. void Slider::OnDragMove(const IntVector2& position, const IntVector2& screenPosition, const IntVector2& deltaPos, MouseButtonFlags buttons,
  86. QualifierFlags qualifiers, Cursor* cursor)
  87. {
  88. if (!editable_ || !dragSlider_ || GetSize() == knob_->GetSize())
  89. return;
  90. float newValue;
  91. IntVector2 delta = position - dragBeginCursor_;
  92. if (orientation_ == O_HORIZONTAL)
  93. {
  94. int newX = Clamp(dragBeginPosition_.x_ + delta.x_, 0, GetWidth() - knob_->GetWidth());
  95. knob_->SetPosition(newX, 0);
  96. newValue = (float)newX * range_ / (float)(GetWidth() - knob_->GetWidth());
  97. }
  98. else
  99. {
  100. int newY = Clamp(dragBeginPosition_.y_ + delta.y_, 0, GetHeight() - knob_->GetHeight());
  101. knob_->SetPosition(0, newY);
  102. newValue = (float)newY * range_ / (float)(GetHeight() - knob_->GetHeight());
  103. }
  104. SetValue(newValue);
  105. }
  106. void Slider::OnDragEnd(const IntVector2& position, const IntVector2& screenPosition, MouseButtonFlags dragButtons, MouseButtonFlags releaseButtons, Cursor* cursor)
  107. {
  108. UIElement::OnDragEnd(position, screenPosition, dragButtons, releaseButtons, cursor);
  109. if (dragButtons == MOUSEB_LEFT)
  110. {
  111. dragSlider_ = false;
  112. selected_ = false;
  113. }
  114. }
  115. void Slider::OnResize(const IntVector2& newSize, const IntVector2& delta)
  116. {
  117. UpdateSlider();
  118. }
  119. void Slider::SetOrientation(Orientation orientation)
  120. {
  121. orientation_ = orientation;
  122. UpdateSlider();
  123. }
  124. void Slider::SetRange(float range)
  125. {
  126. range = Max(range, 0.0f);
  127. if (range != range_)
  128. {
  129. range_ = range;
  130. UpdateSlider();
  131. }
  132. }
  133. void Slider::SetValue(float value)
  134. {
  135. value = Clamp(value, 0.0f, range_);
  136. if (value != value_)
  137. {
  138. value_ = value;
  139. UpdateSlider();
  140. using namespace SliderChanged;
  141. VariantMap& eventData = GetEventDataMap();
  142. eventData[P_ELEMENT] = this;
  143. eventData[P_VALUE] = value_;
  144. SendEvent(E_SLIDERCHANGED, eventData);
  145. }
  146. }
  147. void Slider::ChangeValue(float delta)
  148. {
  149. SetValue(value_ + delta);
  150. }
  151. void Slider::SetRepeatRate(float rate)
  152. {
  153. repeatRate_ = Max(rate, 0.0f);
  154. }
  155. bool Slider::FilterImplicitAttributes(XMLElement& dest) const
  156. {
  157. if (!BorderImage::FilterImplicitAttributes(dest))
  158. return false;
  159. XMLElement childElem = dest.GetChild("element");
  160. if (!childElem)
  161. return false;
  162. if (!RemoveChildXML(childElem, "Name", "S_Knob"))
  163. return false;
  164. if (!RemoveChildXML(childElem, "Position"))
  165. return false;
  166. if (!RemoveChildXML(childElem, "Size"))
  167. return false;
  168. return true;
  169. }
  170. void Slider::UpdateSlider()
  171. {
  172. const IntRect& border = knob_->GetBorder();
  173. if (range_ > 0.0f)
  174. {
  175. if (orientation_ == O_HORIZONTAL)
  176. {
  177. auto sliderLength = (int)Max((float)GetWidth() / (range_ + 1.0f), (float)(border.left_ + border.right_));
  178. if (knob_->IsFixedWidth())
  179. sliderLength = knob_->GetWidth();
  180. float sliderPos = (float)(GetWidth() - sliderLength) * value_ / range_;
  181. if (!knob_->IsFixedSize())
  182. {
  183. knob_->SetSize(sliderLength, GetHeight());
  184. knob_->SetPosition(Clamp(RoundToInt(sliderPos), 0, GetWidth() - knob_->GetWidth()), 0);
  185. }
  186. else
  187. knob_->SetPosition(Clamp((int)(sliderPos), 0, GetWidth() - knob_->GetWidth()), 0);
  188. }
  189. else
  190. {
  191. auto sliderLength = (int)Max((float)GetHeight() / (range_ + 1.0f), (float)(border.top_ + border.bottom_));
  192. if (knob_->IsFixedHeight())
  193. sliderLength = knob_->GetHeight();
  194. float sliderPos = (float)(GetHeight() - sliderLength) * value_ / range_;
  195. if (!knob_->IsFixedSize())
  196. {
  197. knob_->SetSize(GetWidth(), sliderLength);
  198. knob_->SetPosition(0, Clamp(RoundToInt(sliderPos), 0, GetHeight() - knob_->GetHeight()));
  199. }
  200. else
  201. knob_->SetPosition(0, Clamp(RoundToInt(sliderPos), 0, GetHeight() - knob_->GetHeight()));
  202. }
  203. }
  204. else
  205. {
  206. if (!knob_->IsFixedSize())
  207. knob_->SetSize(GetSize());
  208. knob_->SetPosition(0, 0);
  209. }
  210. }
  211. void Slider::Page(const IntVector2& position, bool pressed)
  212. {
  213. if (!editable_)
  214. return;
  215. IntVector2 offsetXY = position - knob_->GetPosition() - knob_->GetSize() / 2;
  216. int offset = orientation_ == O_HORIZONTAL ? offsetXY.x_ : offsetXY.y_;
  217. auto length = (float)(orientation_ == O_HORIZONTAL ? GetWidth() : GetHeight());
  218. using namespace SliderPaged;
  219. VariantMap& eventData = GetEventDataMap();
  220. eventData[P_ELEMENT] = this;
  221. eventData[P_OFFSET] = offset;
  222. // Start transmitting repeated pages after the initial press
  223. if (selected_ && pressed && repeatRate_ > 0.0f &&
  224. repeatTimer_.GetMSec(false) >= Lerp(1000.0f / repeatRate_, 0.0f, Abs(offset) / length))
  225. repeatTimer_.Reset();
  226. else
  227. pressed = false;
  228. eventData[P_PRESSED] = pressed;
  229. SendEvent(E_SLIDERPAGED, eventData);
  230. }
  231. }