Slider.cpp 9.4 KB

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