Slider.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /************************************************************************
  2. * Slider.js
  3. ************************************************************************
  4. * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
  5. *
  6. * This file is part of Pandora.
  7. *
  8. * Pandora is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * Pandora is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
  20. *************************************************************************/
  21. /**
  22. * The {@code Slider} class represents an UIObject that holds and HTML slider.
  23. *
  24. * ! All GameObjects need to be inside the tree to do anything (can be added as a child
  25. * ! of another GameObject on the tree or as a root).
  26. *
  27. * @author Pedro Schneider
  28. *
  29. * @class
  30. */
  31. class Slider extends UIObject
  32. {
  33. /**
  34. * Initializes a Slider with the specified parameters.
  35. *
  36. * @param {String} name name for the Slider GameObject.
  37. * @param {number} min minimum value on the slider.
  38. * @param {number} max maximum value on the slider.
  39. * @param {number} value initial value for the slider.
  40. * @param {number} step step value for the slider.
  41. *
  42. * @constructor
  43. */
  44. constructor(name, min = 0, max = 100, value = 0, step = 0)
  45. {
  46. super(name);
  47. this.P5Element = createSlider(min, max, value, step); // This Slider's HTML slider.
  48. this.setPosition(0, 0); // Set this Slider's position on the secondary buffer.
  49. this.setSize(200, 25); // Set this Slider's size on the secondary buffer.
  50. this.setStyle(STYLE.DEFAULT_STYLE); // Set this UIObject's default style.
  51. this.connectCallbacks(); // Connect the events of the p5.Element.
  52. this.P5Element.changed(this.onChanged); // Connect the extra event sliders have.
  53. }
  54. /**
  55. * Defines default signals for UIObjects and serves as the caller to this UIObject's
  56. * _initSignals() callbacks. Also adds the extra onChaged signal for CheckBoxes.
  57. *
  58. * @signal mousePressed emited once every time a mouse button is pressed over this
  59. * UIObject.
  60. * @signal doubleClicked emited once every time a mouse button is pressed twice over
  61. * this UIObject.
  62. * @signal mouseWheel emited once everty time a mouse wheel is scrolled over this
  63. * UIObject. Passes one argument {event} that holds the deltaY
  64. * property, that holds a number based on how much was vertically
  65. * scrolled (up is positive) and the deltaX property, that holds a
  66. * number based on how much was horizontaly scrolled (right is positive).
  67. * @signal mouseReleased emited once every time a mouse button is released over this
  68. * UIObject.
  69. * @signal mouseClicked emited once every time a mouse button is pressed and released
  70. * over this UIObject.
  71. * @signal mouseMoved emited once every time a mouse moves over this UIObject.
  72. * @signal mouseOver emited once every time a mouse moves onto this UIObject.
  73. * @signal mouseOut emited once every time a mouse moves out of this UIObject.
  74. * @signal touchStarted emited once every time a touch is regiestered over this UIObject.
  75. * @signal touchMoved emited once every time a touch move is regiestered over this
  76. * UIObject.
  77. * @signal touchEnded emited once every time a touch is regiestered over this UIObject.
  78. * @signal dragOver emited once every time a file is dragged over this UIObject.
  79. * @signal dragLeave emited once every time a dragged file leaves this UIObject's area.
  80. *
  81. * @signal changed emited once every time this UIObject's slider's value is changed.
  82. *
  83. * @override
  84. */
  85. initSignals()
  86. {
  87. this.addSignal("mousePressed");
  88. this.addSignal("doubleClicked");
  89. this.addSignal("mouseWheel");
  90. this.addSignal("mouseReleased");
  91. this.addSignal("mouseClicked");
  92. this.addSignal("mouseMoved");
  93. this.addSignal("mouseOver");
  94. this.addSignal("mouseOut");
  95. this.addSignal("touchStarted");
  96. this.addSignal("touchMoved");
  97. this.addSignal("touchEnded");
  98. this.addSignal("dragOver");
  99. this.addSignal("dragLeave");
  100. this.addSignal("changed");
  101. this._initSignals();
  102. }
  103. /**
  104. * Called once every time this UIObject's slider's value is changed.
  105. * Connected to the changed event from this UIObject's p5.Element.
  106. * Serves as an emiter to the changed signal and calls the _onChanged()
  107. * callback.
  108. */
  109. onChanged()
  110. {
  111. this.pandoraObject.emitSignal("changed");
  112. this.pandoraObject._onChanged();
  113. }
  114. /**
  115. * ! This function should be overriden, it provides no default functionality.
  116. * Called once every time this UIObject's slider's value is changed.
  117. *
  118. * @callback
  119. */
  120. _onChanged()
  121. {
  122. }
  123. }