Input.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /************************************************************************
  2. * Input.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 Input} class represents an UIObject that holds and HTML input box.
  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 Input extends UIObject
  32. {
  33. /**
  34. * Initializes an empty Input with the given parameters.
  35. *
  36. * @param {String} name name for the Input GameObject.
  37. * @param {String} value default message inside the input box.
  38. * @param {String} type type of the input box (e.g: "text", "password").
  39. *
  40. * @constructor
  41. */
  42. constructor(name, value = "", type = "text")
  43. {
  44. super(name);
  45. this.P5Element = createInput(value, type); // This Button's HTML input.
  46. this.setPosition(0, 0); // Set the position of the Input on the secondary buffer.
  47. this.setSize(200, 30); // Set the size of the Input on the secondary buffer.
  48. this.setStyle(STYLE.DEFAULT_STYLE); // Set the default style of the UIObject.
  49. this.connectCallbacks(); // Connect events of the p5.Element.
  50. this.P5Element.input(this.onInput); // Connect the extra event inputs have.
  51. }
  52. /**
  53. * Defines default signals for UIObjects and serves as the caller to this UIObject's
  54. * _initSignals() callbacks. Also adds the extra onChaged signal for CheckBoxes.
  55. *
  56. * @signal mousePressed emited once every time a mouse button is pressed over this
  57. * UIObject.
  58. * @signal doubleClicked emited once every time a mouse button is pressed twice over
  59. * this UIObject.
  60. * @signal mouseWheel emited once everty time a mouse wheel is scrolled over this
  61. * UIObject. Passes one argument {event} that holds the deltaY
  62. * property, that holds a number based on how much was vertically
  63. * scrolled (up is positive) and the deltaX property, that holds a
  64. * number based on how much was horizontaly scrolled (right is positive).
  65. * @signal mouseReleased emited once every time a mouse button is released over this
  66. * UIObject.
  67. * @signal mouseClicked emited once every time a mouse button is pressed and released
  68. * over this UIObject.
  69. * @signal mouseMoved emited once every time a mouse moves over this UIObject.
  70. * @signal mouseOver emited once every time a mouse moves onto this UIObject.
  71. * @signal mouseOut emited once every time a mouse moves out of this UIObject.
  72. * @signal touchStarted emited once every time a touch is regiestered over this UIObject.
  73. * @signal touchMoved emited once every time a touch move is regiestered over this
  74. * UIObject.
  75. * @signal touchEnded emited once every time a touch is regiestered over this UIObject.
  76. * @signal dragOver emited once every time a file is dragged over this UIObject.
  77. * @signal dragLeave emited once every time a dragged file leaves this UIObject's area.
  78. *
  79. * @signal input emited once every time and input is dettected.
  80. *
  81. * @override
  82. */
  83. initSignals()
  84. {
  85. this.addSignal("mousePressed");
  86. this.addSignal("doubleClicked");
  87. this.addSignal("mouseWheel");
  88. this.addSignal("mouseReleased");
  89. this.addSignal("mouseClicked");
  90. this.addSignal("mouseMoved");
  91. this.addSignal("mouseOver");
  92. this.addSignal("mouseOut");
  93. this.addSignal("touchStarted");
  94. this.addSignal("touchMoved");
  95. this.addSignal("touchEnded");
  96. this.addSignal("dragOver");
  97. this.addSignal("dragLeave");
  98. this.addSignal("input");
  99. this._initSignals();
  100. }
  101. /**
  102. * Called once everty this this UIObject's input is triggered i.e typing.
  103. * Connectec to the input event from this UIObject's p5.Element.
  104. * Serves as an emitter to the input signal and calls the _onInput()
  105. * callback.
  106. */
  107. onInput()
  108. {
  109. this.pandoraObject.emitSignal("input");
  110. this.pandoraObject._onInput();
  111. }
  112. /**
  113. * ! This function should be overriden, it provides no default functionality.
  114. * Called once everty this this UIObject's input is triggered i.e typing.
  115. *
  116. * @callback
  117. */
  118. _onInput()
  119. {
  120. }
  121. }