UIObject.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /************************************************************************
  2. * UIObject.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. class UIObject extends GameObject
  22. {
  23. constructor(name)
  24. {
  25. super(name);
  26. this.P5Element = null;
  27. this.visible = true;
  28. }
  29. connectCallbacks()
  30. {
  31. this.P5Element.mousePressed(this.onMousePressed);
  32. this.P5Element.doubleClicked(this.onDoubleClicked);
  33. this.P5Element.mouseWheel(this.onMouseWheel);
  34. this.P5Element.mouseReleased(this.onMouseReleased);
  35. this.P5Element.mouseClicked(this.onMouseClicked);
  36. this.P5Element.mouseMoved(this.onMouseMoved);
  37. this.P5Element.mouseOver(this.onMouseOver);
  38. this.P5Element.mouseOut(this.onMouseOut);
  39. this.P5Element.touchStarted(this.onTouchStarted);
  40. this.P5Element.touchMoved(this.onTouchMoved);
  41. this.P5Element.touchEnded(this.onTouchEnded);
  42. this.P5Element.dragOver(this.onDragOver);
  43. this.P5Element.dragLeave(this.onDragLeave);
  44. this.P5Element.pandoraObject = this;
  45. }
  46. // Setters
  47. setPosition(x, y)
  48. {
  49. this.P5Element.position(x, y);
  50. }
  51. setSize(w, h)
  52. {
  53. this.P5Element.size(w, h);
  54. }
  55. setVisibility(vis)
  56. {
  57. if (vis) this.P5Element.show();
  58. else this.P5Element.hide();
  59. this.visible = !this.visible;
  60. }
  61. setValue(val)
  62. {
  63. this.P5Element.value(val)
  64. }
  65. setStyle(style)
  66. {
  67. for (const [key, value] of Object.entries(style))
  68. {
  69. this.P5Element.style(`${key}`, value);
  70. }
  71. }
  72. // Getters
  73. getPosition()
  74. {
  75. return this.P5Element.position();
  76. }
  77. getVisibility()
  78. {
  79. return this.visible;
  80. }
  81. getValue()
  82. {
  83. return this.P5Element.value();
  84. }
  85. // Methods
  86. toggleVisibility()
  87. {
  88. this.setVisibility(!this.visible);
  89. }
  90. addChild(child)
  91. {
  92. child.parent = this;
  93. child.parented = true;
  94. this.children.push(child);
  95. child.P5Element.parent(this.P5Element);
  96. }
  97. // Callbacks
  98. _onMousePressed()
  99. {
  100. }
  101. _onDoubleClicked()
  102. {
  103. }
  104. _onMouseWheel()
  105. {
  106. }
  107. _onMouseReleased()
  108. {
  109. }
  110. _onMouseClicked()
  111. {
  112. }
  113. _onMouseMoved()
  114. {
  115. }
  116. _onMouseOver()
  117. {
  118. }
  119. _onMouseOut()
  120. {
  121. }
  122. _onTouchStarted()
  123. {
  124. }
  125. _onTouchMoved()
  126. {
  127. }
  128. _onTouchEnded()
  129. {
  130. }
  131. _onDragOver()
  132. {
  133. }
  134. _onDragLeave()
  135. {
  136. }
  137. // -----------------------------------------------
  138. onMousePressed()
  139. {
  140. this.pandoraObject._onMousePressed();
  141. }
  142. onDoubleClicked()
  143. {
  144. this.pandoraObject._onDoubleClicked();
  145. }
  146. onMouseWheel()
  147. {
  148. this.pandoraObject._onMouseWheel();
  149. }
  150. onMouseReleased()
  151. {
  152. this.pandoraObject._onMouseReleased();
  153. }
  154. onMouseClicked()
  155. {
  156. this.pandoraObject._onMouseClicked();
  157. }
  158. onMouseMoved()
  159. {
  160. this.pandoraObject._onMouseMoved();
  161. }
  162. onMouseOver()
  163. {
  164. this.pandoraObject._onMouseOver();
  165. }
  166. onMouseOut()
  167. {
  168. this.pandoraObject._onMouseOut();
  169. }
  170. onTouchStarted()
  171. {
  172. this.pandoraObject._onTouchStarted();
  173. }
  174. onTouchMoved()
  175. {
  176. this.pandoraObject._onTouchMoved();
  177. }
  178. onTouchEnded()
  179. {
  180. this.pandoraObject._onTouchEnded();
  181. }
  182. onDragOver()
  183. {
  184. this.pandoraObject._onDragOver();
  185. }
  186. onDragLeave()
  187. {
  188. this.pandoraObject._onDragLeave();
  189. }
  190. }