WidgetSlider.cpp 17 KB

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