123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648 |
- /************************************************************************
- * UIObject.js
- ************************************************************************
- * Copyright (c) 2021 Pedro Tonini Rosenberg Schneider.
- *
- * This file is part of Pandora.
- *
- * Pandora is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * Pandora is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Pandora. If not, see <https://www.gnu.org/licenses/>.
- *************************************************************************/
- /**
- * The {@code UIObject} class represents a minimal structure for any GameObject that
- * hold an HTML interface element.
- *
- * ! All GameObjects need to be inside the tree to do anything (can be added as a child
- * ! of another GameObject on the tree or as a root).
- *
- * @author Pedro Schneider
- *
- * @class
- */
- class UIObject extends GameObject
- {
- /**
- * Initializes an empty UIObject.
- *
- * @param {String} name name for this UIObject.
- *
- * @constructor
- */
- constructor(name)
- {
- super(name);
- this.P5Element = null; // This UIOBject's p5.Element (that holds an HTML element).
- this.visible = true; // Is this UIObject visible at the moment?
- this.position = new Vector2(0, 0); // This UIObject's position on the secondary buffer.
- this.size = new Vector2(300, 100); // The UIObject's size on the secondary buffer.
- this.fontSize = STYLE.DEFAULT_FONT_SIZE; // This UIObject's font style.
- }
- /**
- * Connects this UIObject's p5.Element's events to this GameObject's appropriate
- * methods.
- */
- 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;
- }
- /**
- * Sets the position of this UIObject on the secondary buffer.
- *
- * @param {number} x pixel position on the X axis.
- * @param {number} y pixel position on the Y axis.
- */
- setPosition(x, y)
- {
- this.position.x = x;
- this.position.y = y;
- }
- /**
- * Sets the size of this UIObject on the secondary buffer.
- *
- * @param {number} w pixel width of this UIObject.
- * @param {number} h pixel height of this UIObject.
- */
- setSize(w, h)
- {
- this.size.x = w;
- this.size.y = h;
- }
- /**
- * Sets this UIObject's font size.
- *
- * @param {number} fs new font size.
- */
- setFontSize(fs)
- {
- this.fontSize = fs;
- }
- /**
- * Sets the visibility of this UIObject's p5.Element's visibility.
- *
- * @param {boolean} vis new value for the visibility of the UIObject.
- */
- setVisibility(vis)
- {
- if (vis) this.P5Element.show();
- else this.P5Element.hide();
- this.visible = !this.visible;
- }
- /**
- * Sets the inner html value of this UIObject's p5.Element.
- *
- * @param {*} val new inner HTML value for this UIObject.
- */
- setValue(val)
- {
- this.P5Element.value(val)
- }
- /**
- * Sets the CSS style of this UIObject's p5.Element.
- *
- * @param {Object} style object containing the CSS style for this
- * UIObject.
- */
- setStyle(style)
- {
- for (const [key, value] of Object.entries(style))
- {
- this.P5Element.style(`${key}`, value);
- }
- }
- /**
- * Returns this UIOBject's position on the secondary buffer.
- *
- * @returns {Vector2} this UIObject's position on the secondary buffer.
- */
- getPosition()
- {
- return this.position;
- }
- /**
- * Returns this Object's position on the secondary buffer.
- *
- * @returns {Vector2} this UIObject's size on the secondary buffer.
- */
- getSize()
- {
- return this.size;
- }
- /**
- * Returns this UIObject's font size.
- *
- * @returns {number} this UIObject's font size.
- */
- getFontSize()
- {
- return this.fontSize;
- }
- /**
- * Returns this UIObject's visibility flag.
- *
- * @returns {boolean} true if this UIObject is visible, false if not.
- */
- getVisibility()
- {
- return this.visible;
- }
- /**
- * Return the inner HTML value of this UIObject's p5.Element.
- *
- * @returns {String} inner HTML value of this UIObject's p5.Element.
- */
- getValue()
- {
- return this.P5Element.value();
- }
- /**
- * Sets this UIObject's visibility flag to true, shows the p5.Element, and
- * recursively calls the show() method on all of this GameObject's children
- * if they have it.
- */
- show()
- {
- this.visible = true;
- this.P5Element.show();
- for (let i = 0; i < this.children.length; i++)
- {
- if (!this.children[i].show) continue;
- this.children[i].show();
- }
- }
- /**
- * Sets this UIObject's visibility flag to false, hides the p5.Element, and
- * recursively calls the hide() method on all of this GameObject's children
- * if they have it.
- */
- hide()
- {
- this.visible = false;
- this.P5Element.hide();
- for (let i = 0; i < this.children.length; i++)
- {
- if (!this.children[i].hide) continue;
- this.children[i].hide();
- }
- }
- /**
- * Adds a GameObject as a child of this UIObject, and parents the child's p5.Element
- * if they are a UIObject.
- *
- * @param {GameObject} child
- *
- * @override
- */
- addChild(child)
- {
- child.parent = this;
- child.parented = true;
- this.children.push(child);
- if (child.P5Element)
- child.P5Element.parent(this.P5Element);
- }
- /**
- * Recursively marks this GameObject's and all of its children's
- * memory for garbage collection. Also removes this UIObject's p5.Element.
- *
- * @override
- */
- destroy()
- {
- for (let i = 0; i < this.children.length; i++)
- this.children[i].destroy();
- if (this.P5Element)
- this.P5Element.remove();
- for (var prop in this)
- this[prop] = null;
- }
- /**
- * Defines default signals for UIObjects and serves as the caller to this UIObject's
- * _initSignals() callbacks.
- *
- * @signal mousePressed emited once every time a mouse button is pressed over this
- * UIObject.
- * @signal doubleClicked emited once every time a mouse button is pressed twice over
- * this UIObject.
- * @signal mouseWheel emited once everty time a mouse wheel is scrolled over this
- * UIObject. Passes one argument {event} that holds the deltaY
- * property, that holds a number based on how much was vertically
- * scrolled (up is positive) and the deltaX property, that holds a
- * number based on how much was horizontaly scrolled (right is positive).
- * @signal mouseReleased emited once every time a mouse button is released over this
- * UIObject.
- * @signal mouseClicked emited once every time a mouse button is pressed and released
- * over this UIObject.
- * @signal mouseMoved emited once every time a mouse moves over this UIObject.
- * @signal mouseOver emited once every time a mouse moves onto this UIObject.
- * @signal mouseOut emited once every time a mouse moves out of this UIObject.
- * @signal touchStarted emited once every time a touch is regiestered over this UIObject.
- * @signal touchMoved emited once every time a touch move is regiestered over this
- * UIObject.
- * @signal touchEnded emited once every time a touch is regiestered over this UIObject.
- * @signal dragOver emited once every time a file is dragged over this UIObject.
- * @signal dragLeave emited once every time a dragged file leaves this UIObject's area.
- *
- * @override
- */
- initSignals()
- {
- this.addSignal("mousePressed");
- this.addSignal("doubleClicked");
- this.addSignal("mouseWheel");
- this.addSignal("mouseReleased");
- this.addSignal("mouseClicked");
- this.addSignal("mouseMoved");
- this.addSignal("mouseOver");
- this.addSignal("mouseOut");
- this.addSignal("touchStarted");
- this.addSignal("touchMoved");
- this.addSignal("touchEnded");
- this.addSignal("dragOver");
- this.addSignal("dragLeave");
- this._initSignals();
- }
- /**
- * Updates this UIObject's p5.Element size, position and font size based
- * on the secondary buffer's size on the actual window. This gives the
- * impression that the HTML element is actually beeing drawn to the secondary
- * buffer instead of the main one.
- * ? is it possible to draw them directly to the secondary buffer?
- *
- * @param {number} delta number of ellapsed seconds since the last frame.
- * @param {p5.Graphics} db secondary buffer to draw to.
- *
- * @override
- */
- 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.drawChildren(delta, db);
- }
- /**
- * Called once every time a mouse button is pressed over this UIObject.
- * Connected to the mousePressed event from this UIObject's p5.Element.
- * Serves as an emiter to the mousePressed signal and calls the _onMousePressed()
- * callback.
- */
- onMousePressed()
- {
- this.pandoraObject.emitSignal("mousePressed");
- this.pandoraObject._onMousePressed();
- }
- /**
- * Called once every time a mouse button in pressed twice over this UIObject.
- * Connected to the doubleClicked event from this UIObject's p5.Element.
- * Serves as an emiter to the doubleClicked signal and calls the _onDoubleClicked()
- * callback.
- */
- onDoubleClicked()
- {
- this.pandoraObject.emitSignal("doubleClicked");
- this.pandoraObject._onDoubleClicked();
- }
- /**
- * Called once every time a mouse wheel is scrolled over this UIObject.
- * Connected to the mouseWheel event from this UIObject's p5.Element.
- * Serves as an emiter to the mouseWheel signal and calls the _onMouseWheel()
- * callback.
- *
- * @param {Object} event contains data about the wheen scroll, with the deltaY
- * and deltaX property, containing data about the vertical
- * and horizontal scrolling, repsctively.
- */
- onMouseWheel(event)
- {
- this.pandoraObject.emitSignal("mouseWheel", event);
- this.pandoraObject._onMouseWheel(event);
- }
- /**
- * Called once every time a mouse button is released over this UIObject.
- * Connected to the mouseReleased event from this UIObject's p5.Element.
- * Serves as an emiter to the mouseReleased signal and calls the _onMouseReleased()
- * callback.
- */
- onMouseReleased()
- {
- this.pandoraObject.emitSignal("mouseReleased");
- this.pandoraObject._onMouseReleased();
- }
- /**
- * Called once every time a mouse button is pressed and released over this UIObject.
- * Connected to the mouseClicked event from this UIObject's p5.Element.
- * Serves as an emiter to the mouseClicked signal and calls the _onMouseClicked()
- * callback.
- */
- onMouseClicked()
- {
- this.pandoraObject.emitSignal("mouseClicked");
- this.pandoraObject._onMouseClicked();
- }
- /**
- * Called once everty time a mouse moves over this UIObject.
- * Connected to the mouseMoved event from this UIObject's p5.Element.
- * Serves as an emiter to the mouseMoved signal and calls the _onMouseMoved()
- * callback.
- */
- onMouseMoved()
- {
- this.pandoraObject.emitSignal("mouseMoved");
- this.pandoraObject._onMouseMoved();
- }
- /**
- * Called once every time a mouse moves onto this UIObject.
- * Connected to the mouseOver event from this UIObject's p5.Element.
- * Serves as an emiter to the mouseOver signal and calls the _onMouseOver()
- * callback.
- */
- onMouseOver()
- {
- this.pandoraObject.emitSignal("mouseOver");
- this.pandoraObject._onMouseOver();
- }
- /**
- * Called once every time a mouse moves off the UIObject.
- * Connected to the mouseOut event from this UIObject's p5.Element.
- * Serves as an emiter to the mouseOut signal and calls the _onMouseOut()
- * callback.
- */
- onMouseOut()
- {
- this.pandoraObject.emitSignal("mouseOut");
- this.pandoraObject._onMouseOut();
- }
- /**
- * Called once every time a touch is registered over this UIObject.
- * Connected to the touchStarted event from this UIObject's p5.Element.
- * Serves as an emiter to the touchStarted signal and calls the _onTouchStarted()
- * callback.
- */
- onTouchStarted()
- {
- this.pandoraObject.emitSignal("touchStarted");
- this.pandoraObject._onTouchStarted();
- }
- /**
- * Called once every time a touch move is registered over this UIObject.
- * Connected to the touchMoved event from this UIObject's p5.Element.
- * Serves as an emiter to the touchMoved signal and calls the _onTouchMoved()
- * callback.
- */
- onTouchMoved()
- {
- this.pandoraObject.emitSignal("touchMoved");
- this.pandoraObject._onTouchMoved();
- }
- /**
- * Called once every time a touch is registered over this UIObject.
- * Connected to the touchEnded event from this UIObject's p5.Element.
- * Serves as an emiter to the touchEnded signal and calls the _onTouchEnded()
- * callback.
- */
- onTouchEnded()
- {
- this.pandoraObject.emitSignal("touchEnded");
- this.pandoraObject._onTouchEnded();
- }
- /**
- * Called once every time a file is dragged over this UIObject's area.
- * Connected to the dragOver event from this UIObject's p5.Element.
- * Serves as an emiter to the dragOver signal and calls the _onDragOver()
- * callback.
- */
- onDragOver()
- {
- this.pandoraObject.emitSignal("dragOver");
- this.pandoraObject._onDragOver();
- }
- /**
- * Called once every time a dragged file leaves tis UIObject's area.
- * Connected to the dragLeave event from this UIObject's p5.Element.
- * Serves as an emiter to the dragLeave signal and calls the _onDragLeave()
- * callback.
- */
- onDragLeave()
- {
- this.pandoraObject.emitSignal("dragLeave");
- this.pandoraObject._onDragLeave();
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a mouse button is pressed over this UIObject.
- *
- * @callback
- */
- _onMousePressed()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a mouse button in pressed twice over this UIObject.
- *
- * @callback
- */
- _onDoubleClicked()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a mouse wheel is scrolled over this UIObject.
- *
- * @callback
- *
- * @param {Object} event contains data about the wheen scroll, with the deltaY
- * and deltaX property, containing data about the vertical
- * and horizontal scrolling, repsctively.
- */
- _onMouseWheel(event)
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a mouse button is released over this UIObject.
- *
- * @callback
- */
- _onMouseReleased()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a mouse button is pressed and released over this UIObject.
- *
- * @callback
- */
- _onMouseClicked()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once everty time a mouse moves over this UIObject.
- *
- * @callback
- */
- _onMouseMoved()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a mouse moves onto this UIObject.
- *
- * @callback
- */
- _onMouseOver()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a mouse moves off the UIObject.
- *
- * @callback
- */
- _onMouseOut()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a touch is registered over this UIObject.
- *
- * @callback
- */
- _onTouchStarted()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a touch move is registered over this UIObject.
- *
- * @callback
- */
- _onTouchMoved()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a touch is registered over this UIObject.
- *
- * @callback
- */
- _onTouchEnded()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a file is dragged over this UIObject's area.
- *
- * @callback
- */
- _onDragOver()
- {
- }
- /**
- * ! This function should be overriden, it provides no default functionality.
- * Called once every time a dragged file leaves tis UIObject's area.
- *
- * @callback
- */
- _onDragLeave()
- {
- }
- }
|