UIObject.js 3.5 KB

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