123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- class UIObject extends GameObject
- {
- constructor(name)
- {
- super(name);
- this.P5Element = null;
- this.visible = true;
- this.position = new Vector2(0, 0);
- this.size = new Vector2(300, 100);
- this.fontSize = STYLE.DEFAULT_FONT_SIZE;
- }
- connectCallbacks()
- {
- this.P5Element.mousePressed(this.onMousePressed);
- this.P5Element.doubleClicked(this.onDoubleClicked);
- this.P5Element.mouseWheel(this.onMouseWheel);
- this.P5Element.mouseReleased(this.onMouseReleased);
- this.P5Element.mouseClicked(this.onMouseClicked);
- this.P5Element.mouseMoved(this.onMouseMoved);
- this.P5Element.mouseOver(this.onMouseOver);
- this.P5Element.mouseOut(this.onMouseOut);
- this.P5Element.touchStarted(this.onTouchStarted);
- this.P5Element.touchMoved(this.onTouchMoved);
- this.P5Element.touchEnded(this.onTouchEnded);
- this.P5Element.dragOver(this.onDragOver);
- this.P5Element.dragLeave(this.onDragLeave);
- this.P5Element.pandoraObject = this;
- }
-
- setPosition(x, y)
- {
- this.position.x = x;
- this.position.y = y;
- }
- setSize(w, h)
- {
- this.size.x = w;
- this.size.y = h;
- }
- setFontSize(fs)
- {
- this.fontSize = fs;
- }
- setVisibility(vis)
- {
- if (vis) this.P5Element.show();
- else this.P5Element.hide();
- this.visible = !this.visible;
- }
- setValue(val)
- {
- this.P5Element.value(val)
- }
- setStyle(style)
- {
- for (const [key, value] of Object.entries(style))
- {
- this.P5Element.style(`${key}`, value);
- }
- }
-
- getPosition()
- {
- return this.position;
- }
- getSize()
- {
- return this.size;
- }
- getFontSize()
- {
- return this.fontSize;
- }
- getVisibility()
- {
- return this.visible;
- }
- getValue()
- {
- return this.P5Element.value();
- }
-
- toggleVisibility()
- {
- this.setVisibility(!this.visible);
- }
- addChild(child)
- {
- child.parent = this;
- child.parented = true;
- this.children.push(child);
- child.P5Element.parent(this.P5Element);
- }
-
- draw(delta, db)
- {
- let ar = db.screenWidth / db.width;
- let offsetx = (windowWidth - db.screenWidth) / 2;
- let offsety = (windowHeight - db.screenHeight) / 2;
- this.P5Element.position(offsetx + this.position.x * ar, offsety + this.position.y * ar);
- this.P5Element.size(this.size.x * ar, this.size.y * ar);
- this.setStyle(
- {
- "font-size": `${this.fontSize * ar}px`
- });
- this._draw(delta, db);
- for (let i = 0; i < this.children.length; i++)
- this.children[i].draw(delta, db);
- }
- _onMousePressed()
- {
- }
- _onDoubleClicked()
- {
- }
- _onMouseWheel()
- {
- }
- _onMouseReleased()
- {
- }
- _onMouseClicked()
- {
- }
- _onMouseMoved()
- {
- }
- _onMouseOver()
- {
- }
- _onMouseOut()
- {
- }
- _onTouchStarted()
- {
- }
- _onTouchMoved()
- {
- }
- _onTouchEnded()
- {
- }
- _onDragOver()
- {
- }
- _onDragLeave()
- {
- }
-
- onMousePressed()
- {
- this.pandoraObject._onMousePressed();
- }
- onDoubleClicked()
- {
- this.pandoraObject._onDoubleClicked();
- }
- onMouseWheel()
- {
- this.pandoraObject._onMouseWheel();
- }
- onMouseReleased()
- {
- this.pandoraObject._onMouseReleased();
- }
- onMouseClicked()
- {
- this.pandoraObject._onMouseClicked();
- }
- onMouseMoved()
- {
- this.pandoraObject._onMouseMoved();
- }
- onMouseOver()
- {
- this.pandoraObject._onMouseOver();
- }
- onMouseOut()
- {
- this.pandoraObject._onMouseOut();
- }
- onTouchStarted()
- {
- this.pandoraObject._onTouchStarted();
- }
- onTouchMoved()
- {
- this.pandoraObject._onTouchMoved();
- }
- onTouchEnded()
- {
- this.pandoraObject._onTouchEnded();
- }
- onDragOver()
- {
- this.pandoraObject._onDragOver();
- }
- onDragLeave()
- {
- this.pandoraObject._onDragLeave();
- }
- }
|