Radio.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /************************************************************************
  2. * Radio.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 Radio} class represents an UIObject that holds and HTML radio button.
  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 Radio extends UIObject
  32. {
  33. /**
  34. * Initializes an empty Radio.
  35. *
  36. * @param {String} name name for the Radio GameObject.
  37. *
  38. * @constructor
  39. */
  40. constructor(name)
  41. {
  42. super(name);
  43. this.P5Element = createRadio(); // This Radio's HTML radio.
  44. this.setPosition(10, 10); // Set this position of the Radio on the secondary buffer.
  45. this.multiLine = false; // Should the different radio buttons be shown on separate lines?
  46. this.setStyle(STYLE.DEFAULT_STYLE); // Set the default style of the UIObject.
  47. this.connectCallbacks(); // Connect events of the p5.Element.
  48. this.P5Element.changed(this.onChanged); // Connect the extra event radios have.
  49. }
  50. /**
  51. * Sets the selected option on this Radio's HTML radio button based on its value.
  52. *
  53. * @param {String} value value of the radio button to be selected.
  54. */
  55. setSelected(value)
  56. {
  57. this.P5Element.selected(value);
  58. }
  59. /**
  60. * Returns the value of the selected option from this Radio's HTML radio button.
  61. *
  62. * @returns {String} the value of the selected option.
  63. */
  64. getSelected()
  65. {
  66. return this.P5Element.selected();
  67. }
  68. /**
  69. * Inserts a new option on this Radio's HTML radio button 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. if (this.multiLine) this.makeMultiline();
  77. }
  78. // TODO make this work question mark?
  79. // removeOption(value)
  80. // {
  81. // this.P5Element.remove(value);
  82. // }
  83. /**
  84. * This fucntion creates separete HTML divs for each option on this Radio's HTML radio button ]
  85. * in order to make them appear on different lines.
  86. */
  87. makeMultiline()
  88. {
  89. this.multiLine = true;
  90. const inputs = selectAll('input', this.P5Element),
  91. labels = selectAll('label', this.P5Element),
  92. len = inputs.length;
  93. for (let i = 0; i < len; ++i)
  94. createDiv().parent(this.P5Element).child(inputs[i]).child(labels[i]);
  95. this.fixRadioDivElement();
  96. }
  97. /**
  98. * Fixes de div elements after the call to makeMultiLine().
  99. */
  100. fixRadioDivElement()
  101. {
  102. this.P5Element._getInputChildrenArray = function()
  103. {
  104. return this.elt.getElementsByTagName('input');
  105. }
  106. }
  107. /**
  108. * Defines default signals for UIObjects and serves as the caller to this UIObject's
  109. * _initSignals() callbacks. Also adds the extra onChaged signal for CheckBoxes.
  110. *
  111. * @signal mousePressed emited once every time a mouse button is pressed over this
  112. * UIObject.
  113. * @signal doubleClicked emited once every time a mouse button is pressed twice over
  114. * this UIObject.
  115. * @signal mouseWheel emited once everty time a mouse wheel is scrolled over this
  116. * UIObject. Passes one argument {event} that holds the deltaY
  117. * property, that holds a number based on how much was vertically
  118. * scrolled (up is positive) and the deltaX property, that holds a
  119. * number based on how much was horizontaly scrolled (right is positive).
  120. * @signal mouseReleased emited once every time a mouse button is released over this
  121. * UIObject.
  122. * @signal mouseClicked emited once every time a mouse button is pressed and released
  123. * over this UIObject.
  124. * @signal mouseMoved emited once every time a mouse moves over this UIObject.
  125. * @signal mouseOver emited once every time a mouse moves onto this UIObject.
  126. * @signal mouseOut emited once every time a mouse moves out of this UIObject.
  127. * @signal touchStarted emited once every time a touch is regiestered over this UIObject.
  128. * @signal touchMoved emited once every time a touch move is regiestered over this
  129. * UIObject.
  130. * @signal touchEnded emited once every time a touch is regiestered over this UIObject.
  131. * @signal dragOver emited once every time a file is dragged over this UIObject.
  132. * @signal dragLeave emited once every time a dragged file leaves this UIObject's area.
  133. *
  134. * @signal changed emited once every time this UIObject's radio button's value is changed.
  135. *
  136. * @override
  137. */
  138. initSignals()
  139. {
  140. this.addSignal("mousePressed");
  141. this.addSignal("doubleClicked");
  142. this.addSignal("mouseWheel");
  143. this.addSignal("mouseReleased");
  144. this.addSignal("mouseClicked");
  145. this.addSignal("mouseMoved");
  146. this.addSignal("mouseOver");
  147. this.addSignal("mouseOut");
  148. this.addSignal("touchStarted");
  149. this.addSignal("touchMoved");
  150. this.addSignal("touchEnded");
  151. this.addSignal("dragOver");
  152. this.addSignal("dragLeave");
  153. this.emitSignal("changed");
  154. this._initSignals();
  155. }
  156. /**
  157. * Called once every time this UIObject's radio buttons's value is changed.
  158. * Connected to the changed event from this UIObject's p5.Element.
  159. * Serves as an emiter to the changed signal and calls the _onChanged()
  160. * callback.
  161. */
  162. onChanged()
  163. {
  164. this.pandoraObject.emitSignal("changed");
  165. this.pandoraObject._onChanged();
  166. }
  167. /**
  168. * ! This function should be overriden, it provides no default functionality.
  169. * Called once every time this UIObject's radio button's value is changed.
  170. *
  171. * @callback
  172. */
  173. _onChanged()
  174. {
  175. console.log(this.getSelected());
  176. }
  177. }