WidgetSlider.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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 "WidgetSlider.h"
  29. #include "../../Include/RmlUi/Core.h"
  30. #include "../../Include/RmlUi/Controls/ElementFormControl.h"
  31. #include "../Core/Clock.h"
  32. namespace Rml {
  33. namespace Controls {
  34. static const float DEFAULT_REPEAT_DELAY = 0.5f;
  35. static const float DEFAULT_REPEAT_PERIOD = 0.1f;
  36. WidgetSlider::WidgetSlider(ElementFormControl* _parent)
  37. {
  38. parent = _parent;
  39. orientation = HORIZONTAL;
  40. track = NULL;
  41. bar = NULL;
  42. arrows[0] = NULL;
  43. arrows[1] = NULL;
  44. bar_position = 0;
  45. bar_drag_anchor = 0;
  46. arrow_timers[0] = -1;
  47. arrow_timers[1] = -1;
  48. last_update_time = 0;
  49. }
  50. WidgetSlider::~WidgetSlider()
  51. {
  52. if (bar != NULL)
  53. {
  54. parent->RemoveChild(bar);
  55. }
  56. if (track != NULL)
  57. {
  58. parent->RemoveChild(track);
  59. }
  60. using Core::EventId;
  61. parent->RemoveEventListener(EventId::Blur, this);
  62. parent->RemoveEventListener(EventId::Focus, this);
  63. parent->RemoveEventListener(EventId::Keydown, this, true);
  64. parent->RemoveEventListener(EventId::Mousedown, this);
  65. parent->RemoveEventListener(EventId::Mouseup, this);
  66. parent->RemoveEventListener(EventId::Mouseout, this);
  67. parent->RemoveEventListener(EventId::Drag, this);
  68. parent->RemoveEventListener(EventId::Dragstart, this);
  69. parent->RemoveEventListener(EventId::Dragend, this);
  70. for (int i = 0; i < 2; i++)
  71. {
  72. if (arrows[i] != NULL)
  73. {
  74. parent->RemoveChild(arrows[i]);
  75. }
  76. }
  77. }
  78. // Initialises the slider to a given orientation.
  79. bool WidgetSlider::Initialise()
  80. {
  81. Core::Property drag_property = Core::Property(Core::Style::Drag::Drag);
  82. parent->SetProperty(Core::PropertyId::Drag, drag_property);
  83. // Create all of our child elements as standard elements, and abort if we can't create them.
  84. Core::ElementPtr track_element = Core::Factory::InstanceElement(parent, "*", "slidertrack", Core::XMLAttributes());
  85. Core::ElementPtr bar_element = Core::Factory::InstanceElement(parent, "*", "sliderbar", Core::XMLAttributes());
  86. Core::ElementPtr arrow0_element = Core::Factory::InstanceElement(parent, "*", "sliderarrowdec", Core::XMLAttributes());
  87. Core::ElementPtr arrow1_element = Core::Factory::InstanceElement(parent, "*", "sliderarrowinc", Core::XMLAttributes());
  88. if (!track_element || !bar_element || !arrow0_element || !arrow1_element)
  89. {
  90. return false;
  91. }
  92. // Add them as non-DOM elements.
  93. track = parent->AppendChild(std::move(track_element), false);
  94. bar = parent->AppendChild(std::move(bar_element), false);
  95. arrows[0] = parent->AppendChild(std::move(arrow0_element), false);
  96. arrows[1] = parent->AppendChild(std::move(arrow1_element), false);
  97. arrows[0]->SetProperty(Core::PropertyId::Drag, drag_property);
  98. arrows[1]->SetProperty(Core::PropertyId::Drag, drag_property);
  99. // Attach the listeners
  100. // All listeners are attached to parent, ensuring that we don't get duplicate events when it bubbles from child to parent
  101. using Core::EventId;
  102. parent->AddEventListener(EventId::Blur, this);
  103. parent->AddEventListener(EventId::Focus, this);
  104. parent->AddEventListener(EventId::Keydown, this, true);
  105. parent->AddEventListener(EventId::Mousedown, this);
  106. parent->AddEventListener(EventId::Mouseup, this);
  107. parent->AddEventListener(EventId::Mouseout, this);
  108. parent->AddEventListener(EventId::Drag, this);
  109. parent->AddEventListener(EventId::Dragstart, this);
  110. parent->AddEventListener(EventId::Dragend, this);
  111. return true;
  112. }
  113. // Updates the key repeats for the increment / decrement arrows.
  114. void WidgetSlider::Update()
  115. {
  116. for (int i = 0; i < 2; i++)
  117. {
  118. bool updated_time = false;
  119. float delta_time = 0;
  120. if (arrow_timers[i] > 0)
  121. {
  122. if (!updated_time)
  123. {
  124. double current_time = Core::Clock::GetElapsedTime();
  125. delta_time = float(current_time - last_update_time);
  126. last_update_time = current_time;
  127. }
  128. arrow_timers[i] -= delta_time;
  129. while (arrow_timers[i] <= 0)
  130. {
  131. arrow_timers[i] += DEFAULT_REPEAT_PERIOD;
  132. SetBarPosition(i == 0 ? OnLineDecrement() : OnLineIncrement());
  133. }
  134. }
  135. }
  136. }
  137. // Sets the position of the bar.
  138. void WidgetSlider::SetBarPosition(float _bar_position)
  139. {
  140. bar_position = Rml::Core::Math::Clamp(_bar_position, 0.0f, 1.0f);
  141. PositionBar();
  142. Rml::Core::Dictionary parameters;
  143. parameters["value"] = bar_position;
  144. parent->DispatchEvent(Core::EventId::Change, parameters);
  145. }
  146. // Returns the current position of the bar.
  147. float WidgetSlider::GetBarPosition()
  148. {
  149. return bar_position;
  150. }
  151. // Sets the orientation of the slider.
  152. void WidgetSlider::SetOrientation(Orientation _orientation)
  153. {
  154. orientation = _orientation;
  155. }
  156. // Returns the slider's orientation.
  157. WidgetSlider::Orientation WidgetSlider::GetOrientation() const
  158. {
  159. return orientation;
  160. }
  161. // Sets the dimensions to the size of the slider.
  162. void WidgetSlider::GetDimensions(Rml::Core::Vector2f& dimensions) const
  163. {
  164. switch (orientation)
  165. {
  166. case VERTICAL: dimensions.x = 16; dimensions.y = 256; break;
  167. case HORIZONTAL: dimensions.x = 256; dimensions.y = 16; break;
  168. }
  169. }
  170. // Lays out and resizes the internal elements.
  171. void WidgetSlider::FormatElements(const Rml::Core::Vector2f& containing_block, float slider_length, float bar_length)
  172. {
  173. int length_axis = orientation == VERTICAL ? 1 : 0;
  174. // Build the box for the containing slider element. As the containing block is not guaranteed to have a defined
  175. // height, we must use the width for both axes.
  176. Core::Box parent_box;
  177. Core::ElementUtilities::BuildBox(parent_box, Rml::Core::Vector2f(containing_block.x, containing_block.x), parent);
  178. // Set the length of the slider.
  179. Rml::Core::Vector2f content = parent_box.GetSize();
  180. content[length_axis] = slider_length;
  181. parent_box.SetContent(content);
  182. // Generate the initial dimensions for the track. It'll need to be cut down to fit the arrows.
  183. Core::Box track_box;
  184. Core::ElementUtilities::BuildBox(track_box, parent_box.GetSize(), track);
  185. content = track_box.GetSize();
  186. content[length_axis] = slider_length -= orientation == VERTICAL ? (track_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::TOP) + track_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::BOTTOM)) :
  187. (track_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::LEFT) + track_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::RIGHT));
  188. // If no height has been explicitly specified for the track, it'll be initialised to -1 as per normal block
  189. // elements. We'll fix that up here.
  190. if (orientation == HORIZONTAL &&
  191. content.y < 0)
  192. content.y = parent_box.GetSize().y;
  193. // Now we size the arrows.
  194. for (int i = 0; i < 2; i++)
  195. {
  196. Core::Box arrow_box;
  197. Core::ElementUtilities::BuildBox(arrow_box, parent_box.GetSize(), arrows[i]);
  198. // Clamp the size to (0, 0).
  199. Rml::Core::Vector2f arrow_size = arrow_box.GetSize();
  200. if (arrow_size.x < 0 ||
  201. arrow_size.y < 0)
  202. arrow_box.SetContent(Rml::Core::Vector2f(0, 0));
  203. arrows[i]->SetBox(arrow_box);
  204. // Shrink the track length by the arrow size.
  205. content[length_axis] -= arrow_box.GetSize(Core::Box::MARGIN)[length_axis];
  206. }
  207. // Now the track has been sized, we can fix everything into position.
  208. track_box.SetContent(content);
  209. track->SetBox(track_box);
  210. if (orientation == VERTICAL)
  211. {
  212. Rml::Core::Vector2f offset(arrows[0]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::LEFT), arrows[0]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::TOP));
  213. arrows[0]->SetOffset(offset, parent);
  214. offset.x = track->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::LEFT);
  215. offset.y += arrows[0]->GetBox().GetSize(Core::Box::BORDER).y + arrows[0]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::BOTTOM) + track->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::TOP);
  216. track->SetOffset(offset, parent);
  217. offset.x = arrows[1]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::LEFT);
  218. offset.y += track->GetBox().GetSize(Core::Box::BORDER).y + track->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::BOTTOM) + arrows[1]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::TOP);
  219. arrows[1]->SetOffset(offset, parent);
  220. }
  221. else
  222. {
  223. Rml::Core::Vector2f offset(arrows[0]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::LEFT), arrows[0]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::TOP));
  224. arrows[0]->SetOffset(offset, parent);
  225. offset.x += arrows[0]->GetBox().GetSize(Core::Box::BORDER).x + arrows[0]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::RIGHT) + track->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::LEFT);
  226. offset.y = track->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::TOP);
  227. track->SetOffset(offset, parent);
  228. offset.x += track->GetBox().GetSize(Core::Box::BORDER).x + track->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::RIGHT) + arrows[1]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::LEFT);
  229. offset.y = arrows[1]->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::TOP);
  230. arrows[1]->SetOffset(offset, parent);
  231. }
  232. FormatBar(bar_length);
  233. if (parent->IsDisabled())
  234. {
  235. // Propagate disabled state to child elements
  236. bar->SetPseudoClass("disabled", true);
  237. track->SetPseudoClass("disabled", true);
  238. arrows[0]->SetPseudoClass("disabled", true);
  239. arrows[1]->SetPseudoClass("disabled", true);
  240. }
  241. }
  242. // Lays out and positions the bar element.
  243. void WidgetSlider::FormatBar(float bar_length)
  244. {
  245. Core::Box bar_box;
  246. Core::ElementUtilities::BuildBox(bar_box, parent->GetBox().GetSize(), bar);
  247. auto& computed = bar->GetComputedValues();
  248. Rml::Core::Vector2f bar_box_content = bar_box.GetSize();
  249. if (orientation == HORIZONTAL)
  250. {
  251. if (computed.height.value == Core::Style::Height::Auto)
  252. bar_box_content.y = parent->GetBox().GetSize().y;
  253. }
  254. if (bar_length >= 0)
  255. {
  256. Rml::Core::Vector2f track_size = track->GetBox().GetSize();
  257. if (orientation == VERTICAL)
  258. {
  259. float track_length = track_size.y - (bar_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::TOP) + bar_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::BOTTOM));
  260. if (computed.height.value == Core::Style::Height::Auto)
  261. {
  262. bar_box_content.y = track_length * bar_length;
  263. // Check for 'min-height' restrictions.
  264. float min_track_length = Core::ResolveValue(computed.min_height, track_length);
  265. bar_box_content.y = Rml::Core::Math::Max(min_track_length, bar_box_content.y);
  266. // Check for 'max-height' restrictions.
  267. float max_track_length = Core::ResolveValue(computed.max_height, track_length);
  268. if (max_track_length > 0)
  269. bar_box_content.y = Rml::Core::Math::Min(max_track_length, bar_box_content.y);
  270. }
  271. // Make sure we haven't gone further than we're allowed to (min-height may have made us too big).
  272. bar_box_content.y = Rml::Core::Math::Min(bar_box_content.y, track_length);
  273. }
  274. else
  275. {
  276. float track_length = track_size.x - (bar_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::LEFT) + bar_box.GetCumulativeEdge(Core::Box::CONTENT, Core::Box::RIGHT));
  277. if (computed.width.value == Core::Style::Height::Auto)
  278. {
  279. bar_box_content.x = track_length * bar_length;
  280. // Check for 'min-width' restrictions.
  281. float min_track_length = Core::ResolveValue(computed.min_width, track_length);
  282. bar_box_content.x = Rml::Core::Math::Max(min_track_length, bar_box_content.x);
  283. // Check for 'max-width' restrictions.
  284. float max_track_length = Core::ResolveValue(computed.max_width, track_length);
  285. if (max_track_length > 0)
  286. bar_box_content.x = Rml::Core::Math::Min(max_track_length, bar_box_content.x);
  287. }
  288. // Make sure we haven't gone further than we're allowed to (min-width may have made us too big).
  289. bar_box_content.x = Rml::Core::Math::Min(bar_box_content.x, track_length);
  290. }
  291. }
  292. // Set the new dimensions on the bar to re-decorate it.
  293. bar_box.SetContent(bar_box_content);
  294. bar->SetBox(bar_box);
  295. // Now that it's been resized, re-position it.
  296. PositionBar();
  297. }
  298. // Returns the widget's parent element.
  299. Core::Element* WidgetSlider::GetParent() const
  300. {
  301. return parent;
  302. }
  303. // Handles events coming through from the slider's components.
  304. void WidgetSlider::ProcessEvent(Core::Event& event)
  305. {
  306. if (parent->IsDisabled())
  307. return;
  308. using Rml::Core::EventId;
  309. switch (event.GetId())
  310. {
  311. case EventId::Mousedown:
  312. {
  313. if (event.GetTargetElement() == parent || event.GetTargetElement() == track)
  314. {
  315. if (orientation == HORIZONTAL)
  316. {
  317. float mouse_position = event.GetParameter< float >("mouse_x", 0);
  318. float click_position = (mouse_position - track->GetAbsoluteOffset().x) / track->GetBox().GetSize().x;
  319. SetBarPosition(click_position <= bar_position ? OnPageDecrement(click_position) : OnPageIncrement(click_position));
  320. }
  321. else
  322. {
  323. float mouse_position = event.GetParameter< float >("mouse_y", 0);
  324. float click_position = (mouse_position - track->GetAbsoluteOffset().y) / track->GetBox().GetSize().y;
  325. SetBarPosition(click_position <= bar_position ? OnPageDecrement(click_position) : OnPageIncrement(click_position));
  326. }
  327. }
  328. else if (event.GetTargetElement() == arrows[0])
  329. {
  330. arrow_timers[0] = DEFAULT_REPEAT_DELAY;
  331. last_update_time = Core::Clock::GetElapsedTime();
  332. SetBarPosition(OnLineDecrement());
  333. }
  334. else if (event.GetTargetElement() == arrows[1])
  335. {
  336. arrow_timers[1] = DEFAULT_REPEAT_DELAY;
  337. last_update_time = Core::Clock::GetElapsedTime();
  338. SetBarPosition(OnLineIncrement());
  339. }
  340. }
  341. break;
  342. case EventId::Mouseup:
  343. case EventId::Mouseout:
  344. {
  345. if (event.GetTargetElement() == arrows[0])
  346. arrow_timers[0] = -1;
  347. else if (event.GetTargetElement() == arrows[1])
  348. arrow_timers[1] = -1;
  349. }
  350. break;
  351. case EventId::Dragstart:
  352. {
  353. if (event.GetTargetElement() == parent)
  354. {
  355. bar->SetPseudoClass("active", true);
  356. if (orientation == HORIZONTAL)
  357. bar_drag_anchor = event.GetParameter< int >("mouse_x", 0) - Rml::Core::Math::RealToInteger(bar->GetAbsoluteOffset().x);
  358. else
  359. bar_drag_anchor = event.GetParameter< int >("mouse_y", 0) - Rml::Core::Math::RealToInteger(bar->GetAbsoluteOffset().y);
  360. }
  361. }
  362. break;
  363. case EventId::Drag:
  364. {
  365. if (event.GetTargetElement() == parent)
  366. {
  367. if (orientation == HORIZONTAL)
  368. {
  369. float traversable_track_length = track->GetBox().GetSize(Core::Box::CONTENT).x - bar->GetBox().GetSize(Core::Box::CONTENT).x;
  370. if (traversable_track_length > 0)
  371. {
  372. float traversable_track_origin = track->GetAbsoluteOffset().x + bar_drag_anchor;
  373. float new_bar_position = (event.GetParameter< float >("mouse_x", 0) - traversable_track_origin) / traversable_track_length;
  374. new_bar_position = Rml::Core::Math::Clamp(new_bar_position, 0.0f, 1.0f);
  375. SetBarPosition(OnBarChange(new_bar_position));
  376. }
  377. }
  378. else
  379. {
  380. float traversable_track_length = track->GetBox().GetSize(Core::Box::CONTENT).y - bar->GetBox().GetSize(Core::Box::CONTENT).y;
  381. if (traversable_track_length > 0)
  382. {
  383. float traversable_track_origin = track->GetAbsoluteOffset().y + bar_drag_anchor;
  384. float new_bar_position = (event.GetParameter< float >("mouse_y", 0) - traversable_track_origin) / traversable_track_length;
  385. new_bar_position = Rml::Core::Math::Clamp(new_bar_position, 0.0f, 1.0f);
  386. SetBarPosition(OnBarChange(new_bar_position));
  387. }
  388. }
  389. }
  390. }
  391. break;
  392. case EventId::Dragend:
  393. {
  394. if (event.GetTargetElement() == parent)
  395. {
  396. bar->SetPseudoClass("active", false);
  397. }
  398. }
  399. break;
  400. case EventId::Keydown:
  401. {
  402. Core::Input::KeyIdentifier key_identifier = (Core::Input::KeyIdentifier) event.GetParameter< int >("key_identifier", 0);
  403. switch (key_identifier)
  404. {
  405. case Core::Input::KI_LEFT:
  406. if (orientation == HORIZONTAL) SetBarPosition(OnLineDecrement());
  407. break;
  408. case Core::Input::KI_UP:
  409. if (orientation == VERTICAL) SetBarPosition(OnLineDecrement());
  410. break;
  411. case Core::Input::KI_RIGHT:
  412. if (orientation == HORIZONTAL) SetBarPosition(OnLineIncrement());
  413. break;
  414. case Core::Input::KI_DOWN:
  415. if (orientation == VERTICAL) SetBarPosition(OnLineIncrement());
  416. break;
  417. default:
  418. break;
  419. }
  420. }
  421. break;
  422. case EventId::Focus:
  423. {
  424. if (event.GetTargetElement() == parent)
  425. bar->SetPseudoClass("focus", true);
  426. }
  427. break;
  428. case EventId::Blur:
  429. {
  430. if (event.GetTargetElement() == parent)
  431. bar->SetPseudoClass("focus", false);
  432. }
  433. break;
  434. default:
  435. break;
  436. }
  437. }
  438. void WidgetSlider::PositionBar()
  439. {
  440. const Rml::Core::Vector2f& track_dimensions = track->GetBox().GetSize();
  441. const Rml::Core::Vector2f& bar_dimensions = bar->GetBox().GetSize(Core::Box::BORDER);
  442. if (orientation == VERTICAL)
  443. {
  444. float traversable_track_length = track_dimensions.y - bar_dimensions.y;
  445. bar->SetOffset(Rml::Core::Vector2f(bar->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::LEFT), track->GetRelativeOffset().y + traversable_track_length * bar_position), parent);
  446. }
  447. else
  448. {
  449. float traversable_track_length = track_dimensions.x - bar_dimensions.x;
  450. bar->SetOffset(Rml::Core::Vector2f(track->GetRelativeOffset().x + traversable_track_length * bar_position, bar->GetBox().GetEdge(Core::Box::MARGIN, Core::Box::TOP)), parent);
  451. }
  452. }
  453. }
  454. }