Slider.cpp 8.5 KB

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