WidgetSlider.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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(DRAG, this);
  59. bar->RemoveEventListener(DRAGSTART, this);
  60. }
  61. if (track != NULL)
  62. track->RemoveEventListener(CLICK, this);
  63. for (int i = 0; i < 2; i++)
  64. {
  65. if (arrows[i] != NULL)
  66. {
  67. arrows[i]->RemoveEventListener(MOUSEDOWN, this);
  68. arrows[i]->RemoveEventListener(MOUSEUP, this);
  69. arrows[i]->RemoveEventListener(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(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(DRAG, this);
  123. bar->AddEventListener(DRAGSTART, this);
  124. track->AddEventListener(CLICK, this);
  125. for (int i = 0; i < 2; i++)
  126. {
  127. arrows[i]->AddEventListener(MOUSEDOWN, this);
  128. arrows[i]->AddEventListener(MOUSEUP, this);
  129. arrows[i]->AddEventListener(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. Dictionary parameters;
  163. parameters.Set("value", bar_position);
  164. parent->DispatchEvent("scrollchange", parameters);
  165. }
  166. // Returns the current position of the bar.
  167. float WidgetSlider::GetBarPosition()
  168. {
  169. return bar_position;
  170. }
  171. // Returns the slider's orientation.
  172. WidgetSlider::Orientation WidgetSlider::GetOrientation() const
  173. {
  174. return orientation;
  175. }
  176. // Sets the dimensions to the size of the slider.
  177. void WidgetSlider::GetDimensions(Vector2f& dimensions) const
  178. {
  179. switch (orientation)
  180. {
  181. RMLUI_UNUSED_SWITCH_ENUM(UNKNOWN);
  182. case VERTICAL: dimensions.x = 256; dimensions.y = 16; break;
  183. case HORIZONTAL: dimensions.x = 16; dimensions.y = 256; break;
  184. }
  185. }
  186. // Lays out and resizes the internal elements.
  187. void WidgetSlider::FormatElements(const Vector2f& containing_block, bool resize_element, float slider_length, float bar_length)
  188. {
  189. int length_axis = orientation == VERTICAL ? 1 : 0;
  190. // Build the box for the containing slider element. As the containing block is not guaranteed to have a defined
  191. // height, we must use the width for both axes.
  192. Box parent_box;
  193. LayoutEngine::BuildBox(parent_box, Vector2f(containing_block.x, containing_block.x), parent);
  194. slider_length -= orientation == VERTICAL ? (parent_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + parent_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM)) :
  195. (parent_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + parent_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  196. // Set the length of the slider.
  197. Vector2f content = parent_box.GetSize();
  198. content[length_axis] = slider_length;
  199. parent_box.SetContent(content);
  200. // And set it on the slider element!
  201. if (resize_element)
  202. parent->SetBox(parent_box);
  203. // Generate the initial dimensions for the track. It'll need to be cut down to fit the arrows.
  204. Box track_box;
  205. LayoutEngine::BuildBox(track_box, parent_box.GetSize(), track);
  206. content = track_box.GetSize();
  207. content[length_axis] = slider_length -= orientation == VERTICAL ? (track_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + track_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM)) :
  208. (track_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + track_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  209. // If no height has been explicitly specified for the track, it'll be initialised to -1 as per normal block
  210. // elements. We'll fix that up here.
  211. if (orientation == HORIZONTAL &&
  212. content.y < 0)
  213. content.y = parent_box.GetSize().y;
  214. // Now we size the arrows.
  215. for (int i = 0; i < 2; i++)
  216. {
  217. Box arrow_box;
  218. LayoutEngine::BuildBox(arrow_box, parent_box.GetSize(), arrows[i]);
  219. // Clamp the size to (0, 0).
  220. Vector2f arrow_size = arrow_box.GetSize();
  221. if (arrow_size.x < 0 ||
  222. arrow_size.y < 0)
  223. arrow_box.SetContent(Vector2f(0, 0));
  224. arrows[i]->SetBox(arrow_box);
  225. // Shrink the track length by the arrow size.
  226. content[length_axis] -= arrow_box.GetSize(Box::MARGIN)[length_axis];
  227. }
  228. // Now the track has been sized, we can fix everything into position.
  229. track_box.SetContent(content);
  230. track->SetBox(track_box);
  231. if (orientation == VERTICAL)
  232. {
  233. Vector2f offset(arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::LEFT), arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::TOP));
  234. arrows[0]->SetOffset(offset, parent);
  235. offset.x = track->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  236. offset.y += arrows[0]->GetBox().GetSize(Box::BORDER).y + arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM) + track->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  237. track->SetOffset(offset, parent);
  238. offset.x = arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  239. offset.y += track->GetBox().GetSize(Box::BORDER).y + track->GetBox().GetEdge(Box::MARGIN, Box::BOTTOM) + arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  240. arrows[1]->SetOffset(offset, parent);
  241. }
  242. else
  243. {
  244. Vector2f offset(arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::LEFT), arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::TOP));
  245. arrows[0]->SetOffset(offset, parent);
  246. offset.x += arrows[0]->GetBox().GetSize(Box::BORDER).x + arrows[0]->GetBox().GetEdge(Box::MARGIN, Box::RIGHT) + track->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  247. offset.y = track->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  248. track->SetOffset(offset, parent);
  249. offset.x += track->GetBox().GetSize(Box::BORDER).x + track->GetBox().GetEdge(Box::MARGIN, Box::RIGHT) + arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::LEFT);
  250. offset.y = arrows[1]->GetBox().GetEdge(Box::MARGIN, Box::TOP);
  251. arrows[1]->SetOffset(offset, parent);
  252. }
  253. FormatBar(bar_length);
  254. }
  255. // Lays out and positions the bar element.
  256. void WidgetSlider::FormatBar(float bar_length)
  257. {
  258. Box bar_box;
  259. LayoutEngine::BuildBox(bar_box, parent->GetBox().GetSize(), bar);
  260. const Property *local_width, *local_height;
  261. bar->GetLocalDimensionProperties(&local_width, &local_height);
  262. Vector2f bar_box_content = bar_box.GetSize();
  263. if (orientation == HORIZONTAL)
  264. {
  265. if (local_height == NULL)
  266. bar_box_content.y = parent->GetBox().GetSize().y;
  267. }
  268. if (bar_length >= 0)
  269. {
  270. Vector2f track_size = track->GetBox().GetSize();
  271. if (orientation == VERTICAL)
  272. {
  273. float track_length = track_size.y - (bar_box.GetCumulativeEdge(Box::CONTENT, Box::TOP) + bar_box.GetCumulativeEdge(Box::CONTENT, Box::BOTTOM));
  274. if (local_height == NULL)
  275. {
  276. bar_box_content.y = track_length * bar_length;
  277. // Check for 'min-height' restrictions.
  278. float min_track_length = bar->ResolveProperty(MIN_HEIGHT, track_length);
  279. bar_box_content.y = Math::Max(min_track_length, bar_box_content.y);
  280. // Check for 'max-height' restrictions.
  281. float max_track_length = bar->ResolveProperty(MAX_HEIGHT, track_length);
  282. if (max_track_length > 0)
  283. bar_box_content.y = Math::Min(max_track_length, bar_box_content.y);
  284. }
  285. // Make sure we haven't gone further than we're allowed to (min-height may have made us too big).
  286. bar_box_content.y = Math::Min(bar_box_content.y, track_length);
  287. }
  288. else
  289. {
  290. float track_length = track_size.x - (bar_box.GetCumulativeEdge(Box::CONTENT, Box::LEFT) + bar_box.GetCumulativeEdge(Box::CONTENT, Box::RIGHT));
  291. if (local_width == NULL)
  292. {
  293. bar_box_content.x = track_length * bar_length;
  294. // Check for 'min-width' restrictions.
  295. float min_track_length = bar->ResolveProperty(MIN_WIDTH, track_length);
  296. bar_box_content.x = Math::Max(min_track_length, bar_box_content.x);
  297. // Check for 'max-width' restrictions.
  298. float max_track_length = bar->ResolveProperty(MAX_WIDTH, track_length);
  299. if (max_track_length > 0)
  300. bar_box_content.x = Math::Min(max_track_length, bar_box_content.x);
  301. }
  302. // Make sure we haven't gone further than we're allowed to (min-width may have made us too big).
  303. bar_box_content.x = Math::Min(bar_box_content.x, track_length);
  304. }
  305. }
  306. // Set the new dimensions on the bar to re-decorate it.
  307. bar_box.SetContent(bar_box_content);
  308. bar->SetBox(bar_box);
  309. // Now that it's been resized, re-position it.
  310. PositionBar();
  311. }
  312. // Returns the widget's parent element.
  313. Element* WidgetSlider::GetParent() const
  314. {
  315. return parent;
  316. }
  317. // Handles events coming through from the slider's components.
  318. void WidgetSlider::ProcessEvent(Event& event)
  319. {
  320. if (event.GetTargetElement() == bar)
  321. {
  322. if (event == DRAG)
  323. {
  324. if (orientation == HORIZONTAL)
  325. {
  326. float traversable_track_length = track->GetBox().GetSize(Box::CONTENT).x - bar->GetBox().GetSize(Box::CONTENT).x;
  327. if (traversable_track_length > 0)
  328. {
  329. float traversable_track_origin = track->GetAbsoluteOffset().x + bar_drag_anchor;
  330. float new_bar_position = (event.GetParameter< float >("mouse_x", 0) - traversable_track_origin) / traversable_track_length;
  331. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  332. SetBarPosition(OnBarChange(new_bar_position));
  333. }
  334. }
  335. else
  336. {
  337. float traversable_track_length = track->GetBox().GetSize(Box::CONTENT).y - bar->GetBox().GetSize(Box::CONTENT).y;
  338. if (traversable_track_length > 0)
  339. {
  340. float traversable_track_origin = track->GetAbsoluteOffset().y + bar_drag_anchor;
  341. float new_bar_position = (event.GetParameter< float >("mouse_y", 0) - traversable_track_origin) / traversable_track_length;
  342. new_bar_position = Math::Clamp(new_bar_position, 0.0f, 1.0f);
  343. SetBarPosition(OnBarChange(new_bar_position));
  344. }
  345. }
  346. }
  347. else if (event == DRAGSTART)
  348. {
  349. if (orientation == HORIZONTAL)
  350. bar_drag_anchor = event.GetParameter< int >("mouse_x", 0) - Math::RealToInteger(bar->GetAbsoluteOffset().x);
  351. else
  352. bar_drag_anchor = event.GetParameter< int >("mouse_y", 0) - Math::RealToInteger(bar->GetAbsoluteOffset().y);
  353. }
  354. }
  355. else if (event.GetTargetElement() == track)
  356. {
  357. if (event == CLICK)
  358. {
  359. if (orientation == HORIZONTAL)
  360. {
  361. float mouse_position = event.GetParameter< float >("mouse_x", 0);
  362. float click_position = (mouse_position - track->GetAbsoluteOffset().x) / track->GetBox().GetSize().x;
  363. SetBarPosition(click_position <= bar_position ? OnPageDecrement(click_position) : OnPageIncrement(click_position));
  364. }
  365. else
  366. {
  367. float mouse_position = event.GetParameter< float >("mouse_y", 0);
  368. float click_position = (mouse_position - track->GetAbsoluteOffset().y) / track->GetBox().GetSize().y;
  369. SetBarPosition(click_position <= bar_position ? OnPageDecrement(click_position) : OnPageIncrement(click_position));
  370. }
  371. }
  372. }
  373. if (event == MOUSEDOWN)
  374. {
  375. if (event.GetTargetElement() == arrows[0])
  376. {
  377. arrow_timers[0] = DEFAULT_REPEAT_DELAY;
  378. last_update_time = Clock::GetElapsedTime();
  379. SetBarPosition(OnLineDecrement());
  380. }
  381. else if (event.GetTargetElement() == arrows[1])
  382. {
  383. arrow_timers[1] = DEFAULT_REPEAT_DELAY;
  384. last_update_time = Clock::GetElapsedTime();
  385. SetBarPosition(OnLineIncrement());
  386. }
  387. }
  388. else if (event == MOUSEUP ||
  389. event == MOUSEOUT)
  390. {
  391. if (event.GetTargetElement() == arrows[0])
  392. arrow_timers[0] = -1;
  393. else if (event.GetTargetElement() == arrows[1])
  394. arrow_timers[1] = -1;
  395. }
  396. }
  397. void WidgetSlider::PositionBar()
  398. {
  399. const Vector2f& track_dimensions = track->GetBox().GetSize();
  400. const Vector2f& bar_dimensions = bar->GetBox().GetSize(Box::BORDER);
  401. if (orientation == VERTICAL)
  402. {
  403. float traversable_track_length = track_dimensions.y - bar_dimensions.y;
  404. bar->SetOffset(Vector2f(bar->GetBox().GetEdge(Box::MARGIN, Box::LEFT), track->GetRelativeOffset().y + traversable_track_length * bar_position), parent);
  405. }
  406. else
  407. {
  408. float traversable_track_length = track_dimensions.x - bar_dimensions.x;
  409. bar->SetOffset(Vector2f(track->GetRelativeOffset().x + traversable_track_length * bar_position, bar->GetBox().GetEdge(Box::MARGIN, Box::TOP)), parent);
  410. }
  411. }
  412. }
  413. }