|
class Button { |
|
constructor(addToClient, { id, label, shown }) { |
|
this.id = id; |
|
this.label = label; |
|
this._element = document.createElement("button"); |
|
this._element.style = `position:absolute;left:0%;top:0%` |
|
if (shown === false) { |
|
this._element.style.display = "none"; |
|
} |
|
if (label) { |
|
this._element.innerText = label; |
|
} else { |
|
this._element.innerText = "Button"; |
|
} |
|
|
|
addToClient.AddToCanvas(this._element); |
|
if (addToClient.buttons[id]) { |
|
addToClient.buttons[id].dispose(); |
|
delete addToClient.buttons[id]; |
|
} |
|
addToClient.buttons[id] = this; |
|
} |
|
show() { |
|
this._element.style.display = ""; |
|
} |
|
hide() { |
|
this._element.style.display = "none"; |
|
} |
|
|
|
dispose() { |
|
this._element.remove(); |
|
} |
|
} |
|
|
|
class UI { |
|
constructor(runtime) { |
|
this.runtime = runtime; |
|
this._div = document.createElement("div"); |
|
this.Realign(); |
|
|
|
this.buttons = {}; |
|
} |
|
|
|
static Button = Button; |
|
|
|
|
|
|
|
|
|
|
|
Realign() { |
|
this._div.style = `position: absolute;left: 0px;width: 100%;height: 100%;top: 0px;z-index: 1000;` |
|
if (!this.runtime.renderer) return; |
|
this.runtime.renderer.canvas.parentElement.prepend(this._div); |
|
} |
|
|
|
|
|
|
|
|
|
AddToCanvas(element) { |
|
this._div.append(element); |
|
this.Realign(); |
|
} |
|
|
|
|
|
|
|
|
|
DisposeAll() { |
|
const buttons = Object.values(this.buttons); |
|
const elements = [].concat(buttons); |
|
|
|
elements.forEach(element => { |
|
element.dispose(); |
|
}) |
|
|
|
this.Realign(); |
|
} |
|
} |
|
|
|
module.exports = UI; |