UIObject.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. }
  25. // Setters
  26. setPosition(x, y)
  27. {
  28. this.P5Element.position(x, y);
  29. }
  30. setSize(w, h)
  31. {
  32. this.P5Element.size(w, h);
  33. }
  34. setVisibility(vis)
  35. {
  36. if (vis) this.P5Element.show();
  37. else this.P5Element.hide();
  38. this.visible = !this.visible;
  39. }
  40. setValue(val)
  41. {
  42. this.P5Element.value(val)
  43. }
  44. setStyle(style)
  45. {
  46. for (const [key, value] of Object.entries(style))
  47. {
  48. this.P5Element.style(`${key}`, value);
  49. }
  50. }
  51. // Getters
  52. getPosition()
  53. {
  54. return this.P5Element.position();
  55. }
  56. getVisibility()
  57. {
  58. return this.visible;
  59. }
  60. getValue()
  61. {
  62. return this.P5Element.value();
  63. }
  64. // Methods
  65. toggleVisibility()
  66. {
  67. this.setVisibility(!this.visible);
  68. }
  69. addChild(child)
  70. {
  71. child.parent = this;
  72. child.parented = true;
  73. this.children.push(child);
  74. child.P5Element.parent(this.P5Element);
  75. }
  76. // Callbacks
  77. _onMousePressed()
  78. {
  79. }
  80. _onDoubleClicked()
  81. {
  82. }
  83. _onMouseWheel()
  84. {
  85. }
  86. _onMouseReleased()
  87. {
  88. }
  89. _onMouseClicked()
  90. {
  91. }
  92. _onMouseMoved()
  93. {
  94. }
  95. _onMouseOver()
  96. {
  97. }
  98. _onMouseOut()
  99. {
  100. }
  101. _onTouchStarted()
  102. {
  103. }
  104. _onTouchMoved()
  105. {
  106. }
  107. _onTouchEnded()
  108. {
  109. }
  110. _onDragOver()
  111. {
  112. }
  113. _onDragLeave()
  114. {
  115. }
  116. }