Slider.cpp 8.2 KB

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