UIObject.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. destroy()
  131. {
  132. for (let i = 0; i < this.children.length; i++)
  133. this.children[i].destroy();
  134. if (this.P5Element)
  135. this.P5Element.remove();
  136. for (var prop in this)
  137. this[prop] = null;
  138. }
  139. // Callbacks
  140. _onMousePressed()
  141. {
  142. }
  143. _onDoubleClicked()
  144. {
  145. }
  146. _onMouseWheel()
  147. {
  148. }
  149. _onMouseReleased()
  150. {
  151. }
  152. _onMouseClicked()
  153. {
  154. }
  155. _onMouseMoved()
  156. {
  157. }
  158. _onMouseOver()
  159. {
  160. }
  161. _onMouseOut()
  162. {
  163. }
  164. _onTouchStarted()
  165. {
  166. }
  167. _onTouchMoved()
  168. {
  169. }
  170. _onTouchEnded()
  171. {
  172. }
  173. _onDragOver()
  174. {
  175. }
  176. _onDragLeave()
  177. {
  178. }
  179. // -----------------------------------------------
  180. initSignals()
  181. {
  182. this.addSignal("mousePressed");
  183. this.addSignal("doubleClicked");
  184. this.addSignal("mouseWheel");
  185. this.addSignal("mouseReleased");
  186. this.addSignal("mouseClicked");
  187. this.addSignal("mouseMoved");
  188. this.addSignal("mouseOver");
  189. this.addSignal("mouseOut");
  190. this.addSignal("touchStarted");
  191. this.addSignal("touchMoved");
  192. this.addSignal("touchEnded");
  193. this.addSignal("dragOver");
  194. this.addSignal("dragLeave");
  195. this._initSignals();
  196. }
  197. draw(delta, db)
  198. {
  199. let ar = db.screenWidth / db.width;
  200. let offsetx = (windowWidth - db.screenWidth) / 2;
  201. let offsety = (windowHeight - db.screenHeight) / 2;
  202. this.P5Element.position(offsetx + this.position.x * ar, offsety + this.position.y * ar);
  203. this.P5Element.size(this.size.x * ar, this.size.y * ar);
  204. this.setStyle(
  205. {
  206. "font-size": `${this.fontSize * ar}px`
  207. });
  208. this._draw(delta, db);
  209. for (let i = 0; i < this.children.length; i++)
  210. this.children[i].draw(delta, db);
  211. }
  212. onMousePressed()
  213. {
  214. this.pandoraObject.emitSignal("mousePressed");
  215. this.pandoraObject._onMousePressed();
  216. }
  217. onDoubleClicked()
  218. {
  219. this.pandoraObject.emitSignal("doubleClicked");
  220. this.pandoraObject._onDoubleClicked();
  221. }
  222. onMouseWheel()
  223. {
  224. this.pandoraObject.emitSignal("mouseWheel");
  225. this.pandoraObject._onMouseWheel();
  226. }
  227. onMouseReleased()
  228. {
  229. this.pandoraObject.emitSignal("mouseReleased");
  230. this.pandoraObject._onMouseReleased();
  231. }
  232. onMouseClicked()
  233. {
  234. this.pandoraObject.emitSignal("mouseClicked");
  235. this.pandoraObject._onMouseClicked();
  236. }
  237. onMouseMoved()
  238. {
  239. this.pandoraObject.emitSignal("mouseMoved");
  240. this.pandoraObject._onMouseMoved();
  241. }
  242. onMouseOver()
  243. {
  244. this.pandoraObject.emitSignal("mouseOver");
  245. this.pandoraObject._onMouseOver();
  246. }
  247. onMouseOut()
  248. {
  249. this.pandoraObject.emitSignal("mouseOut");
  250. this.pandoraObject._onMouseOut();
  251. }
  252. onTouchStarted()
  253. {
  254. this.pandoraObject.emitSignal("touchStarted");
  255. this.pandoraObject._onTouchStarted();
  256. }
  257. onTouchMoved()
  258. {
  259. this.pandoraObject.emitSignal("touchMoved");
  260. this.pandoraObject._onTouchMoved();
  261. }
  262. onTouchEnded()
  263. {
  264. this.pandoraObject.emitSignal("touchEnded");
  265. this.pandoraObject._onTouchEnded();
  266. }
  267. onDragOver()
  268. {
  269. this.pandoraObject.emitSignal("dragOver");
  270. this.pandoraObject._onDragOver();
  271. }
  272. onDragLeave()
  273. {
  274. this.pandoraObject.emitSignal("dragLeave");
  275. this.pandoraObject._onDragLeave();
  276. }
  277. }