Select.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /************************************************************************
  2. * Select.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 Select} class represents an UIObject that holds and HTML select.
  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 Select extends UIObject
  32. {
  33. /**
  34. * Intializes an empty Select.
  35. *
  36. * @param {String} name name for the Select GameObject.
  37. *
  38. * @constructor
  39. */
  40. constructor(name)
  41. {
  42. super(name);
  43. this.P5Element = createSelect(); // This Select's HTML select.
  44. this.setPosition(0, 0); // Set this Select's position on the secondary buffer.
  45. this.setSize(100, 20); // Set this Select's size on the secondary buffer.
  46. this.setStyle(STYLE.DEFAULT_STYLE); // Set this UIObject's default style.
  47. this.connectCallbacks(); // Connect the events of the p5.Element.
  48. this.P5Element.changed(this.onChanged); // Connect the extra event selects have.
  49. }
  50. /**
  51. * Set this Select's selected option based on the option's value.
  52. *
  53. * @param {String} value value of the option to be selected.
  54. */
  55. setSelected(value)
  56. {
  57. this.P5Element.selected(value);
  58. }
  59. /**
  60. * Returns the selecte option's value.
  61. *
  62. * @returns {String}
  63. */
  64. getSelected()
  65. {
  66. return this.P5Element.selected();
  67. }
  68. /**
  69. * Add a new option to this Select's HTML select with the given value.
  70. *
  71. * @param {String} value value for the new option.
  72. */
  73. addOption(value)
  74. {
  75. this.P5Element.option(value);
  76. }
  77. // TODO confirm if disable methods really dont exist or if something is broken.
  78. // disableAll()
  79. // {
  80. // this.P5Element.disable();
  81. // }
  82. // disableOption(value)
  83. // {
  84. // this.P5Element.disable(value);
  85. // }
  86. /**
  87. * Defines default signals for UIObjects and serves as the caller to this UIObject's
  88. * _initSignals() callbacks. Also adds the extra onChaged signal for CheckBoxes.
  89. *
  90. * @signal mousePressed emited once every time a mouse button is pressed over this
  91. * UIObject.
  92. * @signal doubleClicked emited once every time a mouse button is pressed twice over
  93. * this UIObject.
  94. * @signal mouseWheel emited once everty time a mouse wheel is scrolled over this
  95. * UIObject. Passes one argument {event} that holds the deltaY
  96. * property, that holds a number based on how much was vertically
  97. * scrolled (up is positive) and the deltaX property, that holds a
  98. * number based on how much was horizontaly scrolled (right is positive).
  99. * @signal mouseReleased emited once every time a mouse button is released over this
  100. * UIObject.
  101. * @signal mouseClicked emited once every time a mouse button is pressed and released
  102. * over this UIObject.
  103. * @signal mouseMoved emited once every time a mouse moves over this UIObject.
  104. * @signal mouseOver emited once every time a mouse moves onto this UIObject.
  105. * @signal mouseOut emited once every time a mouse moves out of this UIObject.
  106. * @signal touchStarted emited once every time a touch is regiestered over this UIObject.
  107. * @signal touchMoved emited once every time a touch move is regiestered over this
  108. * UIObject.
  109. * @signal touchEnded emited once every time a touch is regiestered over this UIObject.
  110. * @signal dragOver emited once every time a file is dragged over this UIObject.
  111. * @signal dragLeave emited once every time a dragged file leaves this UIObject's area.
  112. *
  113. * @signal changed emited once every time this UIObject's select's value is changed.
  114. *
  115. * @override
  116. */
  117. initSignals()
  118. {
  119. this.addSignal("mousePressed");
  120. this.addSignal("doubleClicked");
  121. this.addSignal("mouseWheel");
  122. this.addSignal("mouseReleased");
  123. this.addSignal("mouseClicked");
  124. this.addSignal("mouseMoved");
  125. this.addSignal("mouseOver");
  126. this.addSignal("mouseOut");
  127. this.addSignal("touchStarted");
  128. this.addSignal("touchMoved");
  129. this.addSignal("touchEnded");
  130. this.addSignal("dragOver");
  131. this.addSignal("dragLeave");
  132. this.addSignal("changed");
  133. this._initSignals();
  134. }
  135. /**
  136. * Called once every time this UIObject's select's value is changed.
  137. * Connected to the changed event from this UIObject's p5.Element.
  138. * Serves as an emiter to the changed signal and calls the _onChanged()
  139. * callback.
  140. */
  141. onChanged()
  142. {
  143. this.pandoraObject.emitSignal("changed");
  144. this.pandoraObject._onChanged();
  145. }
  146. /**
  147. * ! This function should be overriden, it provides no default functionality.
  148. * Called once every time this UIObject's select's value is changed.
  149. *
  150. * @callback
  151. */
  152. _onChanged()
  153. {
  154. }
  155. }