CheckBox.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /************************************************************************
  2. * CheckBox.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 CheckBox} class represents an UIObject that holds and HTML checkbox.
  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 CheckBox extends UIObject
  32. {
  33. /**
  34. * Initializes an empty CheckBox GameObject.
  35. *
  36. * @param {String} name name for the CheckBox GameObject.
  37. * @param {String} label label for the CheckBox GameObject.
  38. * @param {boolean} val initial value for the CheckBox. True
  39. * for checked, false for unchecked.
  40. *
  41. * @constructor
  42. */
  43. constructor(name, label = "checkbox", val = false)
  44. {
  45. super(name);
  46. this.label = label; // This Button's HTML checkbox
  47. this.P5Element = createCheckbox(label, val); // This Button's label
  48. this.setPosition(0, 0); // Set the position of the Button on the secondary buffer.
  49. this.setStyle(STYLE.DEFAULT_STYLE); // Set the default style of the UIObject.
  50. this.connectCallbacks(); // Connect the events of the p5.Element.
  51. this.P5Element.changed(this.onChanged); // Connect the extra event checkboxes have
  52. }
  53. /**
  54. * Sets the label on this UIObject's checkbox.
  55. *
  56. * @param {String} label new label for the checkbox.
  57. */
  58. setLabel(label)
  59. {
  60. this.label = label;
  61. this.P5Element.html(label);
  62. }
  63. /**
  64. * Defines default signals for UIObjects and serves as the caller to this UIObject's
  65. * _initSignals() callbacks. Also adds the extra onChaged signal for CheckBoxes.
  66. *
  67. * @signal mousePressed emited once every time a mouse button is pressed over this
  68. * UIObject.
  69. * @signal doubleClicked emited once every time a mouse button is pressed twice over
  70. * this UIObject.
  71. * @signal mouseWheel emited once everty time a mouse wheel is scrolled over this
  72. * UIObject. Passes one argument {event} that holds the deltaY
  73. * property, that holds a number based on how much was vertically
  74. * scrolled (up is positive) and the deltaX property, that holds a
  75. * number based on how much was horizontaly scrolled (right is positive).
  76. * @signal mouseReleased emited once every time a mouse button is released over this
  77. * UIObject.
  78. * @signal mouseClicked emited once every time a mouse button is pressed and released
  79. * over this UIObject.
  80. * @signal mouseMoved emited once every time a mouse moves over this UIObject.
  81. * @signal mouseOver emited once every time a mouse moves onto this UIObject.
  82. * @signal mouseOut emited once every time a mouse moves out of this UIObject.
  83. * @signal touchStarted emited once every time a touch is regiestered over this UIObject.
  84. * @signal touchMoved emited once every time a touch move is regiestered over this
  85. * UIObject.
  86. * @signal touchEnded emited once every time a touch is regiestered over this UIObject.
  87. * @signal dragOver emited once every time a file is dragged over this UIObject.
  88. * @signal dragLeave emited once every time a dragged file leaves this UIObject's area.
  89. *
  90. * @signal changed emited once every time this UIObject's checkbox's value is changed.
  91. *
  92. * @override
  93. */
  94. initSignals()
  95. {
  96. this.addSignal("mousePressed");
  97. this.addSignal("doubleClicked");
  98. this.addSignal("mouseWheel");
  99. this.addSignal("mouseReleased");
  100. this.addSignal("mouseClicked");
  101. this.addSignal("mouseMoved");
  102. this.addSignal("mouseOver");
  103. this.addSignal("mouseOut");
  104. this.addSignal("touchStarted");
  105. this.addSignal("touchMoved");
  106. this.addSignal("touchEnded");
  107. this.addSignal("dragOver");
  108. this.addSignal("dragLeave");
  109. this.addSignal("changed");
  110. this._initSignals();
  111. }
  112. /**
  113. * Called once every time this UIObject's checkbox's value is changed.
  114. * Connected to the changed event from this UIObject's p5.Element.
  115. * Serves as an emiter to the changed signal and calls the _onChanged()
  116. * callback.
  117. */
  118. onChanged()
  119. {
  120. this.pandoraObject.emitSignal("changed");
  121. this.pandoraObject._onChanged();
  122. }
  123. /**
  124. * ! This function should be overriden, it provides no default functionality.
  125. * Called once every time this UIObject's checkbox's value is changed.
  126. *
  127. * @callback
  128. */
  129. _onChanged()
  130. {
  131. console.log(this.P5Element.checked());
  132. }
  133. }