CheckBox.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. * @constructor
  35. * Initializes an empty CheckBox GameObject.
  36. *
  37. * @param {String} name name for the CheckBox GameObject.
  38. * @param {String} label label for the CheckBox GameObject.
  39. * @param {boolean} val initial value for the CheckBox. True
  40. * for checked, false for unchecked.
  41. */
  42. constructor(name, label = "checkbox", val = false)
  43. {
  44. super(name);
  45. this.label = label; // This Button's HTML checkbox
  46. this.P5Element = createCheckbox(label, val); // This Button's label
  47. this.setPosition(0, 0); // Set the position of the Button on the secondary buffer.
  48. this.setStyle(STYLE.DEFAULT_STYLE); // Set the default style of the UIObject.
  49. this.connectCallbacks(); // Connect the events of the p5.Element.
  50. this.P5Element.changed(this.onChanged); // Connect the extra event checkboxes have
  51. }
  52. /**
  53. * Sets the label on this UIObject's checkbox.
  54. *
  55. * @param {String} label new label for the checkbox.
  56. */
  57. setLabel(label)
  58. {
  59. this.label = label;
  60. this.P5Element.html(label);
  61. }
  62. /**
  63. * @override
  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. initSignals()
  93. {
  94. this.addSignal("mousePressed");
  95. this.addSignal("doubleClicked");
  96. this.addSignal("mouseWheel");
  97. this.addSignal("mouseReleased");
  98. this.addSignal("mouseClicked");
  99. this.addSignal("mouseMoved");
  100. this.addSignal("mouseOver");
  101. this.addSignal("mouseOut");
  102. this.addSignal("touchStarted");
  103. this.addSignal("touchMoved");
  104. this.addSignal("touchEnded");
  105. this.addSignal("dragOver");
  106. this.addSignal("dragLeave");
  107. this.addSignal("changed");
  108. this._initSignals();
  109. }
  110. /**
  111. * Called once every time this UIObject's checkbox's value is changed.
  112. * Connected to the changed event from this UIObject's p5.Element.
  113. * Serves as an emiter to the changed signal and calls the _onChanged()
  114. * callback.
  115. */
  116. onChanged()
  117. {
  118. this.pandoraObject.emitSignal("changed");
  119. this.pandoraObject._onChanged();
  120. }
  121. /**
  122. * @callback
  123. * ! This function should be overriden, it provides no default functionality.
  124. * Called once every time this UIObject's checkbox's value is changed.
  125. */
  126. _onChanged()
  127. {
  128. console.log(this.P5Element.checked());
  129. }
  130. }