-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.ts
360 lines (320 loc) · 7.85 KB
/
gui.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
interface position {
x:number,
y:number,
}
interface size {
width:number,
height:number,
}
interface style {
left?:number,
top?:number,
width?:number,
height?:number,
float?:string, // Replace textHorizontalAlignment
alpha?:number,
color?:string,
fontFamily?:string,
fontSize?:number,
cornerRadius?:number
thickness?:number,
background?:string,
}
// OTHER STYLES NOT USED YET:
// paddingTop
// paddingBottom
// paddingLeft
// paddingRight
// textVerticalAlignment
// lineSpacing
// textWrapping
// resizeToFit
// focusedBackground
// autoStretchWidth
// maxWidth
// margin
class ui {
node:any;
style:any = {};
text:string;
container:any;
texture:any;
size:size;
position:position;
// header (text, control, style) => {
// let header = BABYLON.GUI.Control.AddHeader(button, text, "400px", { isHorizontal: true, controlFirst: true });
// header.text = text;
// control.addControl(header);
// if (style) this.setStyle(header, style);
// return header;
// }
//
// image (url, control, style) => {
// let image = new BABYLON.GUI.Image("", url);
// control.addControl(image);
// if (style) this.setStyle(image, style);
// return image;
// }
setNode (text:string, style:style) {
this.node = new BABYLON.GUI.TextBlock();
this.text = text;
this.node.text = text;
if (style.width != undefined) this.node.width = style.width;
if (style.float == 'right') this.node.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_RIGHT;
else if (style.float == 'left' || style.float == undefined) this.node.textHorizontalAlignment = BABYLON.GUI.Control.HORIZONTAL_ALIGNMENT_LEFT;
}
createContainer (style:any) {
this.container = new BABYLON.GUI.Rectangle("");
this.texture.addControl(this.container);
this.container.thickness = 0;
this.setStyle(style);
}
setText (text:string) {
// if (text == undefined) return console.log(this.icon);
text = text.toString();
this.node.text = text;
this.node.color = this.style.color;
this.text = text;
this.show();
return this;
}
hide () {
this.container.isVisible = false;
return this;
}
show () {
this.container.isVisible = true;
return this;
}
setStyle (style:any, el?:any) {
let rel = (el)? el : this.container;
for (let key in style) {
if (style.hasOwnProperty(key)) {
this.style[key] = style[key];
rel[key] = style[key];
}
}
if (style.color && this.text) this.setText(this.text);
return this;
}
setColor (color:string) {
this.container.color = color;
this.style.color = color;
if (this.text) this.setText(this.text);
return this;
}
setOpacity (op:number) {
this.container.alpha = op;
this.style.alpha = op;
return this;
}
addOpacity (op:number) {
this.container.alpha += op;
this.style.alpha += op;
if (this.container.alpha > 1) this.setOpacity(1);
if (this.container.alpha < 0) this.setOpacity(0);
return this;
}
setSize (size:size) {
let newsize = (size)? size : this.size;
this.size = newsize;
this.container.height = (newsize.height+4)+'px';
}
setPosition (pos?:position) {
let newpos = (pos)? pos : this.position;
this.position = newpos;
this.container.left = pos.x+'px';
this.container.top = pos.y+'px';
return this;
}
renameEvent = {
click:'onPointerUpObservable',
mousedown:'onPointerDownObservable',
mouseenter:'onPointerEnterObservable',
mouseout:'onPointerOutObservable',
}
on (event, funct) {
let nodeevent = this.renameEvent[event];
this.node[nodeevent].add(() => {
funct();
});
}
remove () {
this.texture.removeControl(this.container);
}
}
class ui_text extends ui {
constructor(texture:any, text:string, pos:position, style:style) {
super();
this.texture = texture;
style.fontFamily = 'wznfont';
this.setNode(text, style);
this.createContainer(style);
if (style.width == undefined) this.container.width = '100%';
this.setSize({height:style.fontSize, width:0});
this.setPosition(pos);
this.container.addControl(this.node);
return this;
}
}
class ui_back extends ui {
constructor(texture:any, pos:position, style:style) {
super();
this.texture = texture;
this.createContainer(style);
this.setPosition(pos);
return this;
}
}
class ui_icon extends ui {
icon = true;
constructor(texture:any, icon:string, pos:position, style:style) {
super();
style.fontFamily = 'wznicon';
this.texture = texture;
let text = (icon != '')? this.icontochar[icon] : '';
if (text == undefined) console.log(icon);
this.setNode(text, style);
this.createContainer(style);
if (style.width == undefined) this.container.width = '100%';
this.setSize({height:style.fontSize, width:0});
this.setPosition(pos);
this.container.addControl(this.node);
return this;
}
icontochar = {
wazanaLogo:'a',
damage:'b',
energy:'c',
entity:'d',
matter:'e',
progress:'f',
slice1:'A',
slice2:'B',
slicebis1:'C',
slicebis2:'D',
rate:'g',
scope:'h',
speed:'i',
arrowbottomleft:'j',
arrowbottomright:'k',
arrowtopleft:'l',
arrowtopright:'m',
grade1:'n',
grade2:'o',
grade3:'p',
grade4:'q',
grade5:'r',
grade6:'s',
grade7:'t',
grade8:'u',
grade9:'v',
grade10:'w',
map:'x',
mousecursor:'y',
mousepointer:'z',
mouseselect:'1',
mousetarget:'2',
close:'4',
cure:'5',
health:'5',
fastbuilt:'6',
fastmove:'7',
fasttime:'8',
hole:'9',
invisible:'(',
shield:')',
slowtime:'-',
strong:'_',
teleport:'=',
};
setIcon (icon:string) {
let text = this.icontochar[icon];
if (text == undefined) return console.log(icon);
this.setText(text);
return this;
}
}
class ui_bar extends ui {
bar:any = {};
constructor(texture:any, pos:position, style:style) {
super();
this.texture = texture;
this.createContainer(style);
this.setPosition(pos);
return this;
}
createBar (name:string, style:style) {
let bar = new BABYLON.GUI.Rectangle("");
this.bar[name] = bar;
this.bar[name].thickness = 0;
if (style.float == 'left') this.bar[name].left = '-50%';
else if (style.float == 'right') this.bar[name].left = '50%';
this.container.addControl(bar);
this.setStyle(style, bar);
return this;
}
setSize (size:size) {
this.container.height = size.height+'px';
this.container.width = size.width+'px';
return this;
}
setValue (name:string, value:number) {
this.bar[name].width = (value*2)+'%';
return this;
}
}
class GuiTexture {
texture:any;
nodes:Array<any>
constructor (plane:any, size:size, event:boolean) {
this.texture = BABYLON.GUI.AdvancedDynamicTexture.CreateForMesh(plane, size.width*100, size.height*100, event);
this.texture.renderingGroupId = 1;
this.nodes = [];
return this;
}
addText (text:string, pos:position, style:style) {
let textNode = new ui_text(this.texture, text, pos, style);
this.nodes.push(textNode)
return textNode;
}
addIcon (icon:string, pos:position, style:style) {
let iconNode = new ui_icon(this.texture, icon, pos, style);
this.nodes.push(iconNode)
return iconNode;
}
addBack (pos:position, style:style) {
let back = new ui_back(this.texture, pos, style);
this.nodes.push(back)
return back;
}
addBar (pos:position, style:style) {
let bar = new ui_bar(this.texture, pos, style);
return bar;
}
hideAll () {
for (let i = 0; i < this.nodes.length; i++) {
this.nodes[i].hide();
}
}
showAll () {
for (let i = 0; i < this.nodes.length; i++) {
this.nodes[i].show();
}
}
initAll () {
for (let i = 0; i < this.nodes.length; i++) {
this.nodes[i].setText('').hide();
}
}
removeAll () {
for (let i = 0; i < this.nodes.length; i++) {
this.nodes[i].remove();
}
}
setColorAll (color) {
for (let i = 0; i < this.nodes.length; i++) {
this.nodes[i].setColor(color);
}
}
}