WidgetSlider.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "precompiled.h"
  29. #include "WidgetSlider.h"
  30. #include "Clock.h"
  31. #include "LayoutEngine.h"
  32. #include "../../Include/RmlUi/Core/Element.h"
  33. #include "../../Include/RmlUi/Core/Event.h"
  34. #include "../../Include/RmlUi/Core/Factory.h"
  35. #include "../../Include/RmlUi/Core/Property.h"
  36. namespace Rml {
  37. namespace Core {
  38. static const float DEFAULT_REPEAT_DELAY = 0.5f;
  39. static const float DEFAULT_REPEAT_PERIOD = 0.1f;
  40. WidgetSlider::WidgetSlider(Element* _parent)
  41. {
  42. parent = _parent;
  43. orientation = UNKNOWN;
  44. track = NULL;
  45. bar = NULL;
  46. arrows[0] = NULL;
  47. arrows[1] = NULL;
  48. bar_position = 0;
  49. bar_drag_anchor = 0;
  50. arrow_timers[0] = -1;
  51. arrow_timers[1] = -1;
  52. last_update_time = 0;
  53. }
  54. WidgetSlider::~WidgetSlider()
  55. {
  56. if (bar != NULL)
  57. {
  58. bar->RemoveEventListener(Core::EventId::Drag, this);
  59. bar->RemoveEventListener(Core::EventId::Dragstart, this);
  60. }
  61. if (track != NULL)
  62. track->RemoveEventListener(Core::EventId::Click, this);
  63. for (int i = 0; i < 2; i++)
  64. {
  65. if (arrows[i] != NULL)
  66. {
  67. arrows[i]->RemoveEventListener(Core::EventId::Mousedown, this);
  68. arrows[i]->RemoveEventListener(Core::EventId::Mouseup, this);
  69. arrows[i]->RemoveEventListener(Core::EventId::Mouseout, this);
  70. }
  71. }
  72. }
  73. // Initialises the slider to a given orientation.
  74. bool WidgetSlider::Initialise(Orientation _orientation)
  75. {
  76. // Check that we haven't already been successfully initialised.
  77. if (orientation != UNKNOWN)
  78. {
  79. RMLUI_ERROR;
  80. return false;
  81. }
  82. // Check that a valid orientation has been passed in.
  83. if (_orientation != HORIZONTAL &&
  84. _orientation != VERTICAL)
  85. {
  86. RMLUI_ERROR;
  87. return false;
  88. }
  89. orientation = _orientation;
  90. // Create all of our child elements as standard elements, and abort if we can't create them.
  91. track = Factory::InstanceElement(parent, "*", "slidertrack", XMLAttributes());
  92. bar = Factory::InstanceElement(parent, "*", "sliderbar", XMLAttributes());
  93. bar->SetProperty(Core::PropertyId::Drag, Core::Property(Core::Style::Drag::Drag));
  94. arrows[0] = Factory::InstanceElement(parent, "*", "sliderarrowdec", XMLAttributes());
  95. arrows[1] = Factory::InstanceElement(parent, "*", "sliderarrowinc", XMLAttributes());
  96. if (track == NULL ||
  97. bar == NULL ||
  98. arrows[0] == NULL ||
  99. arrows[1] == NULL)
  100. {
  101. if (track != NULL)
  102. track->RemoveReference();
  103. if (bar != NULL)
  104. bar->RemoveReference();
  105. if (arrows[0] != NULL)
  106. arrows[0]->RemoveReference();
  107. if (arrows[1] != NULL)
  108. arrows[1]->RemoveReference();
  109. return false;
  110. }
  111. // Add them as non-DOM elements.
  112. parent->AppendChild(track, false);
  113. parent->AppendChild(bar, false);
  114. parent->AppendChild(arrows[0], false);
  115. parent->AppendChild(arrows[1], false);
  116. // Remove the initial references on the elements.
  117. track->RemoveReference();
  118. bar->RemoveReference();
  119. arrows[0]->RemoveReference();
  120. arrows[1]->RemoveReference();
  121. // Attach the listeners as appropriate.
  122. bar->AddEventListener(EventId::Drag, this);
  123. bar->AddEventListener(EventId::Dragstart, this);
  124. track->AddEventListener(EventId::Click, this);
  125. for (int i = 0; i < 2; i++)
  126. {
  127. arrows[i]->AddEventListener(EventId::Mousedown, this);
  128. arrows[i]->AddEventListener(EventId::Mouseup, this);
  129. arrows[i]->AddEventListener(EventId::Mouseout, this);
  130. }
  131. return true;
  132. }
  133. // Updates the key repeats for the increment / decrement arrows.
  134. void WidgetSlider::Update()
  135. {
  136. for (int i = 0; i < 2; i++)
  137. {
  138. bool updated_time = false;
  139. float delta_time = 0;
  140. if (arrow_timers[i] > 0)
  141. {
  142. if (!updated_time)
  143. {
  144. double current_time = Clock::GetElapsedTime();
  145. delta_time = float(current_time - last_update_time);
  146. last_update_time = current_time;
  147. }
  148. arrow_timers[i] -= delta_time;
  149. while (arrow_timers[i] <= 0)
  150. {
  151. arrow_timers[i] += DEFAULT_REPEAT_PERIOD;
  152. SetBarPosition(i == 0 ? OnLineDecrement() : OnLineIncrement());
  153. }
  154. }
  155. }
  156. }
  157. // Sets the position of the bar.
  158. void WidgetSlider::SetBarPosition(float _bar_position)
  159. {
  160. bar_position = Math::Clamp(_bar_position, 0.0f, 1.0f);
  161. PositionBar();
  162. // 'parent' is the scrollbar element, its parent again is the actual element we want to scroll
  163. Element* element_scroll = parent->GetParentNode();
  164. if (!element_scroll)
  165. {
  166. RMLUI_ERROR;
  167. return;
  168. }
  169. if (orientation == VERTICAL)
  170. element_scroll->SetScrollTop(bar_position * (element_scroll->GetScrollHeight() - element_scroll->GetClientHeight()));
  171. else if (orientation == HORIZONTAL)
  172. element_scroll->SetScrollLeft(bar_position * (element_scroll->GetScrollWidth() - element_scroll->GetClientWidth()));
  173. }
  174. // Returns the current position of the bar.
  175. float WidgetSlider::GetBarPosition()
  176. {
  177. return bar_position;
  178. }
  179. // Returns the slider's orientation.
  180. WidgetSlider::Orientation WidgetSlider::GetOrientation() const
  181. {
  182. return orientation;
  183. }
  184. // Sets the dimensions to the size of the slider.
  185. void WidgetSlider::GetDimensions(Vector2f& dimensions) const
  186. {
  187. switch (orientation)
  188. {
  189. RMLUI_UNUSED_SWITCH_ENUM(UNKNOWN);
  190. case VERTICAL: dimensions.x = 256; dimensions.y = 16; break;
  191. case HORIZONTAL: dimensions.x = 16; dimensions.y = 256; break;
  192. }
  193. }
  194. // Lays out and resizes the internal elements.
  195. void WidgetSlider::FormatElements(const Vector2f& containing_block, bool resize_element, float slider_length, float bar_length)
  196. {
  197. int length_axis = orientation == VERTICAL ? 1 : 0;
  198. // Build the box for the containing slider element. As the containing block is not guaranteed to have a defined
  199. // height, we must use the width for both axes.
  200. Box parent_box;
  201. LayoutEngine::BuildBox(parent_box, Vector2f(containing_block.x, containing_block.x), parent);
  202. slider_length -= orientation == VERTICAL ? (parent_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + parent_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM)) :
  203. (parent_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + parent_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  204. // Set the length of the slider.
  205. Vector2f content = parent_box.GetSize();
  206. content[length_axis] = slider_length;
  207. parent_box.SetContent(content);
  208. // And set it on the slider element!
  209. if (resize_element)
  210. parent->SetBox(parent_box);
  211. // Generate the initial dimensions for the track. It'll need to be cut down to fit the arrows.
  212. Box track_box;
  213. LayoutEngine::BuildBox(track_box, parent_box.GetSize(), track);
  214. content = track_box.GetSize();
  215. content[length_axis] = slider_length -= orientation == VERTICAL ? (track_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + track_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM)) :
  216. (track_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + track_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  217. // If no height has been explicitly specified for the track, it'll be initialised to -1 as per normal block
  218. // elements. We'll fix that up here.
  219. if (orientation == HORIZONTAL &&
  220. content.y < 0)
  221. content.y = parent_box.GetSize().y;
  222. // Now we size the arrows.
  223. for (int i = 0; i < 2; i++)
  224. {
  225. Box arrow_box;
  226. LayoutEngine::BuildBox(arrow_box, parent_box.GetSize(), arrows[i]);
  227. // Clamp the size to (0, 0).
  228. Vector2f arrow_size = arrow_box.GetSize();
  229. if (arrow_size.x < 0 ||
  230. arrow_size.y < 0)
  231. arrow_box.SetContent(Vector2f(0, 0));
  232. arrows[i]->SetBox(arrow_box);
  233. // Shrink the track length by the arrow size.
  234. content[length_axis] -= arrow_box.GetSize(Box::MARGIN)[length_axis];
  235. }
  236. // Now the track has been sized, we can fix everything into position.
  237. track_box.SetContent(content);
  238. track->SetBox(track_box);
  239. if (orientation == VERTICAL)
  240. {
  241. Vector2f offset(arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::LEFT), arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::TOP));
  242. arrows[0]->SetOffset(offset, parent);
  243. offset.x = track->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  244. offset.y += arrows[0]->GetBox().GetSize(Box::BORDER).y + arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM) + track->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  245. track->SetOffset(offset, parent);
  246. offset.x = arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  247. offset.y += track->GetBox().GetSize(Box::BORDER).y + track->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM) + arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  248. arrows[1]->SetOffset(offset, parent);
  249. }
  250. else
  251. {
  252. Vector2f offset(arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::LEFT), arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::TOP));
  253. arrows[0]->SetOffset(offset, parent);
  254. offset.x += arrows[0]->GetBox().GetSize(Box::BORDER).x + arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::RIGHT) + track->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  255. offset.y = track->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  256. track->SetOffset(offset, parent);
  257. offset.x += track->GetBox().GetSize(Box::BORDER).x + track->GetBox().GetEdge(Box::MARGIN, Box::RIGHT) + arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  258. offset.y = arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  259. arrows[1]->SetOffset(offset, parent);
  260. }
  261. FormatBar(bar_length);
  262. }
  263. // Lays out and positions the bar element.
  264. void WidgetSlider::FormatBar(float bar_length)
  265. {
  266. Box bar_box;
  267. LayoutEngine::BuildBox(bar_box, parent->GetBox().GetSize(), bar);
  268. const auto& computed = bar->GetComputedValues();
  269. const Style::Width width = computed.width;
  270. const Style::Height height = computed.height;
  271. Vector2f bar_box_content = bar_box.GetSize();
  272. if (orientation == HORIZONTAL)
  273. {
  274. if (height.type == height.Auto)
  275. bar_box_content.y = parent->GetBox().GetSize().y;
  276. }
  277. if (bar_length >= 0)
  278. {
  279. Vector2f track_size = track->GetBox().GetSize();
  280. if (orientation == VERTICAL)
  281. {
  282. float track_length = track_size.y - (bar_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + bar_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM));
  283. if (height.type == height.Auto)
  284. {
  285. bar_box_content.y = track_length * bar_length;
  286. // Check for 'min-height' restrictions.
  287. float min_track_length = ResolveValue(computed.min_height, track_length);
  288. bar_box_content.y = Math::Max(min_track_length, bar_box_content.y);
  289. // Check for 'max-height' restrictions.
  290. float max_track_length = ResolveValue(computed.max_height, track_length);
  291. if (max_track_length > 0)
  292. bar_box_content.y = Math::Min(max_track_length, bar_box_content.y);
  293. }
  294. // Make sure we haven't gone further than we're allowed to (min-height may have made us too big).
  295. bar_box_content.y = Math::Min(bar_box_content.y, track_length);
  296. }
  297. else
  298. {
  299. float track_length = track_size.x - (bar_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + bar_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  300. if (width.type == width.Auto)
  301. {
  302. bar_box_content.x = track_length * bar_length;
  303. // Check for 'min-width' restrictions.
  304. float min_track_length = ResolveValue(computed.min_width, track_length);
  305. bar_box_content.x = Math::Max(min_track_length, bar_box_content.x);
  306. // Check for 'max-width' restrictions.
  307. float max_track_length = ResolveValue(computed.max_width, track_length);
  308. if (max_track_length > 0)
  309. bar_box_content.x = Math::Min(max_track_length, bar_box_content.x);
  310. }
  311. // Make sure we haven't gone further than we're allowed to (min-width may have made us too big).
  312. bar_box_content.x = Math::Min(bar_box_content.x, track_length);
  313. }
  314. }
  315. // Set the new dimensions on the bar to re-decorate it.
  316. bar_box.SetContent(bar_box_content);
  317. bar->SetBox(bar_box);
  318. // Now that it's been resized, re-position it.
  319. PositionBar();
  320. }
  321. // Returns the widget's parent element.
  322. Element* WidgetSlider::GetParent() const
  323. {
  324. return parent;
  325. }
  326. // Handles events coming through from the slider's components.
  327. void WidgetSlider::ProcessEvent(Event& event)
  328. {
  329. if (event.GetTargetElement() == bar)
  330. {
  331. if (event == EventId::Drag)
  332. {
  333. if (orientation == HORIZONTAL)
  334. {
  335. float traversable_track_length = track->GetBox().GetSize(Box::CONTENT).x - bar->GetBox().GetSize(Box::CONTENT).x;
  336. if (traversable_track_length > 0)
  337. {
  338. float traversable_track_origin = track->GetAbsoluteOffset().x + bar_drag_anchor;
  339. float new_bar_position = (event.GetParameter< float >("mouse_x", 0) - traversable_track_origin) / traversable_track_length;
  340. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  341. SetBarPosition(OnBarChange(new_bar_position));
  342. }
  343. }
  344. else
  345. {
  346. float traversable_track_length = track->GetBox().GetSize(Box::CONTENT).y - bar->GetBox().GetSize(Box::CONTENT).y;
  347. if (traversable_track_length > 0)
  348. {
  349. float traversable_track_origin = track->GetAbsoluteOffset().y + bar_drag_anchor;
  350. float new_bar_position = (event.GetParameter< float >("mouse_y", 0) - traversable_track_origin) / traversable_track_length;
  351. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  352. SetBarPosition(OnBarChange(new_bar_position));
  353. }
  354. }
  355. }
  356. else if (event == EventId::Dragstart)
  357. {
  358. if (orientation == HORIZONTAL)
  359. bar_drag_anchor = event.GetParameter< int >("mouse_x", 0) - Math::RealToInteger(bar->GetAbsoluteOffset().x);
  360. else
  361. bar_drag_anchor = event.GetParameter< int >("mouse_y", 0) - Math::RealToInteger(bar->GetAbsoluteOffset().y);
  362. }
  363. }
  364. else if (event.GetTargetElement() == track)
  365. {
  366. if (event == EventId::Click)
  367. {
  368. if (orientation == HORIZONTAL)
  369. {
  370. float mouse_position = event.GetParameter< float >("mouse_x", 0);
  371. float click_position = (mouse_position - track->GetAbsoluteOffset().x) / track->GetBox().GetSize().x;
  372. SetBarPosition(click_position <= bar_position ? OnPageDecrement(click_position) : OnPageIncrement(click_position));
  373. }
  374. else
  375. {
  376. float mouse_position = event.GetParameter< float >("mouse_y", 0);
  377. float click_position = (mouse_position - track->GetAbsoluteOffset().y) / track->GetBox().GetSize().y;
  378. SetBarPosition(click_position <= bar_position ? OnPageDecrement(click_position) : OnPageIncrement(click_position));
  379. }
  380. }
  381. }
  382. if (event == EventId::Mousedown)
  383. {
  384. if (event.GetTargetElement() == arrows[0])
  385. {
  386. arrow_timers[0] = DEFAULT_REPEAT_DELAY;
  387. last_update_time = Clock::GetElapsedTime();
  388. SetBarPosition(OnLineDecrement());
  389. }
  390. else if (event.GetTargetElement() == arrows[1])
  391. {
  392. arrow_timers[1] = DEFAULT_REPEAT_DELAY;
  393. last_update_time = Clock::GetElapsedTime();
  394. SetBarPosition(OnLineIncrement());
  395. }
  396. }
  397. else if (event == EventId::Mouseup ||
  398. event == EventId::Mouseout)
  399. {
  400. if (event.GetTargetElement() == arrows[0])
  401. arrow_timers[0] = -1;
  402. else if (event.GetTargetElement() == arrows[1])
  403. arrow_timers[1] = -1;
  404. }
  405. }
  406. void WidgetSlider::PositionBar()
  407. {
  408. const Vector2f& track_dimensions = track->GetBox().GetSize();
  409. const Vector2f& bar_dimensions = bar->GetBox().GetSize(Box::BORDER);
  410. if (orientation == VERTICAL)
  411. {
  412. float traversable_track_length = track_dimensions.y - bar_dimensions.y;
  413. bar->SetOffset(Vector2f(bar->GetBox().GetEdge(Box::MARGIN, Box::LEFT), track->GetRelativeOffset().y + traversable_track_length * bar_position), parent);
  414. }
  415. else
  416. {
  417. float traversable_track_length = track_dimensions.x - bar_dimensions.x;
  418. bar->SetOffset(Vector2f(track->GetRelativeOffset().x + traversable_track_length * bar_position, bar->GetBox().GetEdge(Box::MARGIN, Box::TOP)), parent);
  419. }
  420. }
  421. }
  422. }