Select.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. * @constructor
  35. * Intializes an empty Select.
  36. *
  37. * @param {String} name name for the Select GameObject.
  38. */
  39. constructor(name)
  40. {
  41. super(name);
  42. this.P5Element = createSelect(); // This Select's HTML select.
  43. this.setPosition(0, 0); // Set this Select's position on the secondary buffer.
  44. this.setSize(100, 20); // Set this Select's size on the secondary buffer.
  45. this.setStyle(STYLE.DEFAULT_STYLE); // Set this UIObject's default style.
  46. this.connectCallbacks(); // Connect the events of the p5.Element.
  47. this.P5Element.changed(this.onChanged); // Connect the extra event selects have.
  48. }
  49. /**
  50. * Set this Select's selected option based on the option's value.
  51. *
  52. * @param {String} value value of the option to be selected.
  53. */
  54. setSelected(value)
  55. {
  56. this.P5Element.selected(value);
  57. }
  58. /**
  59. * Returns the selecte option's value.
  60. *
  61. * @returns {String}
  62. */
  63. getSelected()
  64. {
  65. return this.P5Element.selected();
  66. }
  67. /**
  68. * Add a new option to this Select's HTML select with the given value.
  69. *
  70. * @param {String} value value for the new option.
  71. */
  72. addOption(value)
  73. {
  74. this.P5Element.option(value);
  75. }
  76. // TODO confirm if disable methods really dont exist or if something is broken.
  77. // disableAll()
  78. // {
  79. // this.P5Element.disable();
  80. // }
  81. // disableOption(value)
  82. // {
  83. // this.P5Element.disable(value);
  84. // }
  85. /**
  86. * @override
  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. initSignals()
  116. {
  117. this.addSignal("mousePressed");
  118. this.addSignal("doubleClicked");
  119. this.addSignal("mouseWheel");
  120. this.addSignal("mouseReleased");
  121. this.addSignal("mouseClicked");
  122. this.addSignal("mouseMoved");
  123. this.addSignal("mouseOver");
  124. this.addSignal("mouseOut");
  125. this.addSignal("touchStarted");
  126. this.addSignal("touchMoved");
  127. this.addSignal("touchEnded");
  128. this.addSignal("dragOver");
  129. this.addSignal("dragLeave");
  130. this.addSignal("changed");
  131. this._initSignals();
  132. }
  133. /**
  134. * Called once every time this UIObject's select's value is changed.
  135. * Connected to the changed event from this UIObject's p5.Element.
  136. * Serves as an emiter to the changed signal and calls the _onChanged()
  137. * callback.
  138. */
  139. onChanged()
  140. {
  141. this.pandoraObject.emitSignal("changed");
  142. this.pandoraObject._onChanged();
  143. }
  144. /**
  145. * @callback
  146. * ! This function should be overriden, it provides no default functionality.
  147. * Called once every time this UIObject's select's value is changed.
  148. */
  149. _onChanged()
  150. {
  151. }
  152. }