代码拉取完成,页面将自动刷新
//@ts-nocheck
/* ********************************************
* Box3EasyGUI.js Version 1.1.0 by 编码喵
* ********************************************/
import "./api"
class Box3EasyGUI {
name: string
entity: GameEntity
document: GUIDocument
rendered: boolean
constructor(name: string, entity: GameEntity) {
this.name = name;
this.entity = entity;
this.document = new GUIDocument(this);
this.rendered = false;
}
render(): void {
this.rendered = true;
gui.init(this.entity, {
[this.name]: {
bindings: this.document.getBindings(),
display: true,
data: this.document.toString()
}
});
}
reload(): void {
if(this.document.childrens.length<1)return;
for (let i of this.document.childrens) gui.remove(this.entity,i.selector);
this.render();
}
}
module.exports = { Box3EasyGUI, GUINode, NodeEventListener, GUIDocument };
function key(node: GUINode){
if(!node.parent)throw new Error("this element have not been append to other elements.So,please append to other elements and then make a key for this elements!");
var length = 16;
var k = "";
var a = [[48,57],[65,90],[97,122]];
for(let i=0;i<length;i++){
let r = a[Math.floor(Math.random()*3)]
k += String.fromCharCode(
Math.floor(Math.random()*(r[1]-r[0]))+r[0]
)
}
if(node.gui&&node.gui.document.getElementByKey(k) != null)
return key(node);
else
return k;
}
class GUINode {
name: string
attributes?: {
[name: string]: string | number
}
childrens: GUINode[]
listeners: NodeEventListener[]
parent: GUINode
selector: string
getSelector: Function
gui: Box3EasyGUI
constructor(name: string, attributes: {
[name: string]: string | number
}) {
this.name = name;
this.attributes = attributes;
this.childrens = [];
this.listeners = [];
this.getSelector = function (): string {
/*let s: string = this.name + (this.attributes.id ? ("#" + this.attributes.id) : "") + (this.attributes.name ? ("." + this.attributes.name) : ""), length: number = 0;
let d: string = "";
for (let i in this.attributes) {
if (!["id","name","x","y","top","left","button","right","height","width","borderWidth","fontSize"].includes(i)) {
length++;
d += "[" + i + "=" + "\"" + this.attributes[i] + "\"]";
}
}
if (length > 0) s += d;
if(this.parent&&this.parent.getSelector)s = this.parent.getSelector() + ">" + s;
return s;*/
return this.name+"[guiKey=\""+this.attributes.guiKey+"\"]";
}
}
toString(): string {
let a: string = "", c: string = "";
for (let i in this.attributes) a += " " + i + "=" + "\"" + this.attributes[i] + "\"";
for (let i of this.childrens) c += i.toString();
return "<" + this.name + a + ">\r\n" + c + "</" + this.name + ">\r\n";
}
getEventListeners(): Array {
let b = this.listeners;
for (let i of this.childrens) b = b.concat(i.getEventListeners());
return b;
}
appendChild(node: GUINode): GUINode {
if (node.selector) throw new Error("this element has been appended to another element.");
this.childrens.push(node);
node.parent = this;
node.gui = this.gui;
function setGUI(node:GUINode,a:Box3EasyGUI){
for(let i of node.childrens)i.gui = a,setGUI(i,a);
}
if(this.gui)setGUI(node,this.gui);
node.attributes.guiKey = key(node);
node.selector = node.getSelector();
return this;
}
remove(): void {
let pos = this.parent.childrens.indexOf(this);
if (pos == -1) throw new Error("can not find this element in " + this.parent.selector + ".Please check have you ever appended this element.");
this.parent.childrens.splice(pos, 1);
if (this.gui&&this.gui.rendered) gui.remove(
this.gui.entity, this.selector
);
delete this;
}
addEventListener(e: string, listener: Function): void {
this.listeners.push(new NodeEventListener(e, listener, this));
gui.onMessage((data) => {
if (data.name == this.getSelector()) listener(data);
})
}
setAttribute(a: string, b: string | number): void {
this.attributes[a] = String(b);
if (this.gui&&this.gui.rendered) gui.setAttribute(this.gui.entity,
this.selector, a, b
);
}
removeAttribute(a: string): void {
this.attributes[a] && function(b){
if(b.gui&&b.gui.rendered)
gui.setAttribute(b.gui.entity,b.selector,a,"");
delete this.attributes[a];
}(this);
}
show(): void{
this.setAttribute("display","block");
this.setAttribute("height",this.attributes.height0);
this.setAttribute("width",this.attributes.width0);
this.setAttribute("height0","");
this.setAttribute("width0","");
}
hide(): void{
this.setAttribute("display","none");
this.setAttribute("height0",this.attributes.height);
this.setAttribute("width0",this.attributes.width);
this.setAttribute("height","0");
this.setAttribute("width","0");
}
}
class NodeEventListener {
constructor(e: string,listener: Function,node: GUINode) {
this.event = e;
this.listener = listener;
this.node = node;
}
}
class GUIDocument extends GUINode {
constructor(gui: Box3EasyGUI) {
super("document", {});
this.selector = "document";
this.gui = gui;
this.getSelector = undefined;
}
toString(): string {
let c: string = "";
for (let i of this.childrens) c += i.toString();
return c;
}
createXml(name: string, attributes: {
[name: string]: string | number
}): GUINode {
return new GUINode(name, attributes);
}
getBindings() {
let e = this.getEventListeners(), b = new Array();
for (let i of e) {
let s = i.node.getSelector();
b.push({ action: "sendMessage", event: i.event, messageName: s, selector: s });
}
return b;
}
remove(): void {
throw new Error("can not remove document!");
}
getElementById(id:string):GUINode{
var e:GUINode = null;
function find(node:GUINode,id:string){
for(let i of node.childrens){
if(i.attributes.id == id)return e = i;
find(i,id);
}
}
find(this,id);
return e;
}
getElementByKey(key:string):GUINode{
var e:GUINode = null;
function find(node:GUINode,key:string){
for(let i of node.childrens){
if(i.attributes.key == key)return e = i;
find(i,key);
}
}
find(this,key);
return e;
}
getElementsByName(name:string):GUINode{
var e:GUINode[] = [];
function find(node:GUINode,name:string){
for(let i of node.childrens){
if(i.attributes.name == name)return e.push(node);
find(i,name);
}
}
find(this,name);
return e;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。