Slider.cpp 8.4 KB

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