UIObject.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. this.position = new Vector2(0, 0);
  29. this.size = new Vector2(300, 100);
  30. this.fontSize = STYLE.DEFAULT_FONT_SIZE;
  31. }
  32. connectCallbacks()
  33. {
  34. this.P5Element.mousePressed(this.onMousePressed);
  35. this.P5Element.doubleClicked(this.onDoubleClicked);
  36. this.P5Element.mouseWheel(this.onMouseWheel);
  37. this.P5Element.mouseReleased(this.onMouseReleased);
  38. this.P5Element.mouseClicked(this.onMouseClicked);
  39. this.P5Element.mouseMoved(this.onMouseMoved);
  40. this.P5Element.mouseOver(this.onMouseOver);
  41. this.P5Element.mouseOut(this.onMouseOut);
  42. this.P5Element.touchStarted(this.onTouchStarted);
  43. this.P5Element.touchMoved(this.onTouchMoved);
  44. this.P5Element.touchEnded(this.onTouchEnded);
  45. this.P5Element.dragOver(this.onDragOver);
  46. this.P5Element.dragLeave(this.onDragLeave);
  47. this.P5Element.pandoraObject = this;
  48. }
  49. // Setters
  50. setPosition(x, y)
  51. {
  52. this.position.x = x;
  53. this.position.y = y;
  54. }
  55. setSize(w, h)
  56. {
  57. this.size.x = w;
  58. this.size.y = h;
  59. }
  60. setFontSize(fs)
  61. {
  62. this.fontSize = fs;
  63. }
  64. setVisibility(vis)
  65. {
  66. if (vis) this.P5Element.show();
  67. else this.P5Element.hide();
  68. this.visible = !this.visible;
  69. }
  70. setValue(val)
  71. {
  72. this.P5Element.value(val)
  73. }
  74. setStyle(style)
  75. {
  76. for (const [key, value] of Object.entries(style))
  77. {
  78. this.P5Element.style(`${key}`, value);
  79. }
  80. }
  81. // Getters
  82. getPosition()
  83. {
  84. return this.position;
  85. }
  86. getSize()
  87. {
  88. return this.size;
  89. }
  90. getFontSize()
  91. {
  92. return this.fontSize;
  93. }
  94. getVisibility()
  95. {
  96. return this.visible;
  97. }
  98. getValue()
  99. {
  100. return this.P5Element.value();
  101. }
  102. // Methods
  103. show()
  104. {
  105. this.visible = true;
  106. this.P5Element.show();
  107. for (let i = 0; i < this.children.length; i++)
  108. {
  109. if (!this.children[i].show) continue;
  110. this.children[i].show();
  111. }
  112. }
  113. hide()
  114. {
  115. this.visible = false;
  116. this.P5Element.hide();
  117. for (let i = 0; i < this.children.length; i++)
  118. {
  119. if (!this.children[i].hide) continue;
  120. this.children[i].hide();
  121. }
  122. }
  123. addChild(child)
  124. {
  125. child.parent = this;
  126. child.parented = true;
  127. this.children.push(child);
  128. child.P5Element.parent(this.P5Element);
  129. }
  130. // Callbacks
  131. _onMousePressed()
  132. {
  133. }
  134. _onDoubleClicked()
  135. {
  136. }
  137. _onMouseWheel()
  138. {
  139. }
  140. _onMouseReleased()
  141. {
  142. }
  143. _onMouseClicked()
  144. {
  145. }
  146. _onMouseMoved()
  147. {
  148. }
  149. _onMouseOver()
  150. {
  151. }
  152. _onMouseOut()
  153. {
  154. }
  155. _onTouchStarted()
  156. {
  157. }
  158. _onTouchMoved()
  159. {
  160. }
  161. _onTouchEnded()
  162. {
  163. }
  164. _onDragOver()
  165. {
  166. }
  167. _onDragLeave()
  168. {
  169. }
  170. // -----------------------------------------------
  171. initSignals()
  172. {
  173. this.addSignal("mousePressed");
  174. this.addSignal("doubleClicked");
  175. this.addSignal("mouseWheel");
  176. this.addSignal("mouseReleased");
  177. this.addSignal("mouseClicked");
  178. this.addSignal("mouseMoved");
  179. this.addSignal("mouseOver");
  180. this.addSignal("mouseOut");
  181. this.addSignal("touchStarted");
  182. this.addSignal("touchMoved");
  183. this.addSignal("touchEnded");
  184. this.addSignal("dragOver");
  185. this.addSignal("dragLeave");
  186. this._initSignals();
  187. }
  188. draw(delta, db)
  189. {
  190. let ar = db.screenWidth / db.width;
  191. let offsetx = (windowWidth - db.screenWidth) / 2;
  192. let offsety = (windowHeight - db.screenHeight) / 2;
  193. this.P5Element.position(offsetx + this.position.x * ar, offsety + this.position.y * ar);
  194. this.P5Element.size(this.size.x * ar, this.size.y * ar);
  195. this.setStyle(
  196. {
  197. "font-size": `${this.fontSize * ar}px`
  198. });
  199. this._draw(delta, db);
  200. for (let i = 0; i < this.children.length; i++)
  201. this.children[i].draw(delta, db);
  202. }
  203. onMousePressed()
  204. {
  205. this.pandoraObject.emitSignal("mousePressed");
  206. this.pandoraObject._onMousePressed();
  207. }
  208. onDoubleClicked()
  209. {
  210. this.pandoraObject.emitSignal("doubleClicked");
  211. this.pandoraObject._onDoubleClicked();
  212. }
  213. onMouseWheel()
  214. {
  215. this.pandoraObject.emitSignal("mouseWheel");
  216. this.pandoraObject._onMouseWheel();
  217. }
  218. onMouseReleased()
  219. {
  220. this.pandoraObject.emitSignal("mouseReleased");
  221. this.pandoraObject._onMouseReleased();
  222. }
  223. onMouseClicked()
  224. {
  225. this.pandoraObject.emitSignal("mouseClicked");
  226. this.pandoraObject._onMouseClicked();
  227. }
  228. onMouseMoved()
  229. {
  230. this.pandoraObject.emitSignal("mouseMoved");
  231. this.pandoraObject._onMouseMoved();
  232. }
  233. onMouseOver()
  234. {
  235. this.pandoraObject.emitSignal("mouseOver");
  236. this.pandoraObject._onMouseOver();
  237. }
  238. onMouseOut()
  239. {
  240. this.pandoraObject.emitSignal("mouseOut");
  241. this.pandoraObject._onMouseOut();
  242. }
  243. onTouchStarted()
  244. {
  245. this.pandoraObject.emitSignal("touchStarted");
  246. this.pandoraObject._onTouchStarted();
  247. }
  248. onTouchMoved()
  249. {
  250. this.pandoraObject.emitSignal("touchMoved");
  251. this.pandoraObject._onTouchMoved();
  252. }
  253. onTouchEnded()
  254. {
  255. this.pandoraObject.emitSignal("touchEnded");
  256. this.pandoraObject._onTouchEnded();
  257. }
  258. onDragOver()
  259. {
  260. this.pandoraObject.emitSignal("dragOver");
  261. this.pandoraObject._onDragOver();
  262. }
  263. onDragLeave()
  264. {
  265. this.pandoraObject.emitSignal("dragLeave");
  266. this.pandoraObject._onDragLeave();
  267. }
  268. }