WidgetSlider.cpp 15 KB

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