-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUIManager.js
More file actions
201 lines (188 loc) · 4.41 KB
/
GUIManager.js
File metadata and controls
201 lines (188 loc) · 4.41 KB
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
/**
* Manages the UI state, input, rendering
*/
class GUIManager
{
/**
* Contains UI elements that are on the screen
*/
components = [];
/**
* Layer new components will be added to.
*/
activeLayer = "system";
screenW=1;
screenH=1;
currentMessage="";
messageTimer = 0;
messageColour="#000000";
messageBlink=0;
messageBlinkTimer=0;
messageOn=false;
contextParams = [];
w=1;
h=1;
hitbox = new Rectangle(0,0,1,1);
constructor(w,h)
{
this.w=w;
this.h=h;
this.hitbox=new Rectangle(0,0,w,h);
console.log(w,h,this.w,this.h);
window.$id=(id)=>{
console.error(id);
return this.find(id);
};
window.$destroy=(id)=>this.remove(id);
window.$param=(param)=>{
return this.contextParams[param];
};
}
/**
* Adds an UI Element to the screen
* @param {UIElement} component
* @param {string} layer
*/
add(component, layer="")
{
if(layer=="")
layer = this.activeLayer;
component.layer=layer;
component.parent = this;
this.components.push(component);
}
remove(component)
{
this.components=this.components.filter((c)=>c!=component);
}
find(id)
{
let result = null;
this.components.forEach((c)=>{
console.log("checking "+id);
console.log(c);
let result2 = c.find(id);
if(result2)
result = result2;
});
return result;
}
message(text, colour, blink = 0, time = 0)
{
this.currentMessage=text;
this.messageTimer=time;
this.messageBlink=blink;
this.messageBlinkTimer = blink;
this.messageColour=colour;
this.messageOn=true;
}
/**
* Updates the state
* @param {number} dT
*/
update(dT)
{
this.messageTimer-=dT;
if(this.messageTimer<=0)
{
this.currentMessage="";
}
if(this.messageBlink!=0)
{
this.messageBlinkTimer-=dT;
if(this.messageBlinkTimer<=0)
{
this.messageOn=!this.messageOn;
this.messageBlinkTimer+=this.messageBlink;
if(this.messageBlinkTimer<0)
{
this.messageBlinkTimer=this.messageBlink;
}
}
}
}
/**
* Draws UI elements on the screen
* @param {CanvasRenderingContext2D} ctx
*/
draw(ctx, layer="")
{
// just draw every component
if(layer=="")
{
this.components.forEach((c)=>{
c.draw(ctx);
});
}
else
{
let selection = this.components.filter((e)=>e.layer==layer);
selection.forEach((c)=>{
c.draw(ctx);
});
}
if(this.currentMessage!="")
{
if(this.messageOn)
{
ctx.font = "small-caps 32px sixtyfour";
ctx.textAlign="center";
ctx.textBaseline="middle";
ctx.fillStyle=this.messageColour;
ctx.fillText(this.currentMessage,this.w/2,this.h/3);
}
}
}
handlePrimaryPointerMove(e)
{
}
handleSecondaryPointerMove(e)
{
}
handlePrimaryPointerUp(e)
{
}
handleSecondaryPointerUp(e)
{
}
handlePrimaryPointerDown(e)
{
}
handleSecondaryPointerDown(e)
{
}
handlePrimaryPointerClick(e)
{
}
/**
* Handles a "click" and returns a value indicating whether any UI element received and handled the click
* @param {PointerEvent} e
* @returns true if a UI element received the click, false otherwise
*/
handleClick(e)
{
let handled = false;
// check all UI elements
for(let i = this.components.length-1;i>=0;i--)
{
let c = this.components[i];
const target = c.checkhit(e.offsetX,e.offsetY);
// if an element is found, click it and set the flag
if(target)
{
target.click(e);
return true;
}
}
return handled;
}
handleKeyDown(e)
{
}
handleKeyUp(e)
{
}
handleKeyPress(e)
{
}
}