forked from eanders-ms/microcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.ts
More file actions
333 lines (310 loc) · 9.32 KB
/
Copy pathscreen.ts
File metadata and controls
333 lines (310 loc) · 9.32 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
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
namespace microcode {
export class Screen {
private static image_: ImageG
public static WIDTH = screen.width
public static HEIGHT = screen.height
public static HALF_WIDTH = screen.width >> 1
public static HALF_HEIGHT = screen.height >> 1
public static LEFT_EDGE = -Screen.HALF_WIDTH
public static RIGHT_EDGE = Screen.HALF_WIDTH
public static TOP_EDGE = -Screen.HALF_HEIGHT
public static BOTTOM_EDGE = Screen.HALF_HEIGHT
public static BOUNDS = new Bounds({
left: Screen.LEFT_EDGE,
top: Screen.TOP_EDGE,
width: Screen.WIDTH,
height: Screen.HEIGHT,
})
public static x(v: number) {
return v + Screen.HALF_WIDTH
}
public static y(v: number) {
return v + Screen.HALF_HEIGHT
}
public static pos(v: Vec2) {
return new Vec2(Screen.x(v.x), Screen.y(v.y))
}
public static get image(): ImageG {
if (!Screen.image_) {
Screen.image_ = image.create(screen.width, screen.height)
}
return Screen.image_
}
public static drawTransparentImage(from: ImageG, x: number, y: number) {
Screen.image.drawTransparentImage(from, Screen.x(x), Screen.y(y))
}
public static drawTransparentImageXfrm(
xfrm: Affine,
from: ImageG,
x: number,
y: number
) {
Screen.image.drawTransparentImage(
from,
Screen.x(x + xfrm.worldPos.x),
Screen.y(y + xfrm.worldPos.y)
)
}
public static drawLine(
x0: number,
y0: number,
x1: number,
y1: number,
c: number
) {
if (c) {
Screen.image.drawLine(
Screen.x(x0),
Screen.y(y0),
Screen.x(x1),
Screen.y(y1),
c
)
}
}
public static drawLineXfrm(
xfrm: Affine,
x0: number,
y0: number,
x1: number,
y1: number,
c: number
) {
Screen.drawLine(
x0 + xfrm.worldPos.x,
y0 + xfrm.worldPos.y,
x1 + xfrm.worldPos.x,
y1 + xfrm.worldPos.y,
c
)
}
public static drawLineShaded(
x0: number,
y0: number,
x1: number,
y1: number,
shader: (x: number, y: number) => number
) {
let sx0 = Screen.x(x0)
let sy0 = Screen.y(y0)
let sx1 = Screen.x(x1)
let sy1 = Screen.y(y1)
for (let x = sx0, tx = x0; x <= sx1; x++, tx++) {
for (let y = sy0, ty = y0; y <= sy1; y++, ty++) {
const c = shader(tx, ty)
if (c) {
Screen.image.setPixel(x, y, c)
}
}
}
}
public static drawRect(
x: number,
y: number,
width: number,
height: number,
c: number
) {
if (c) {
Screen.image.drawRect(
Screen.x(x),
Screen.y(y),
width,
height,
c
)
}
}
public static drawRectXfrm(
xfrm: Affine,
x: number,
y: number,
width: number,
height: number,
c: number
) {
Screen.drawRect(
x + xfrm.worldPos.x,
y + xfrm.worldPos.y,
width,
height,
c
)
}
public static fillRect(
x: number,
y: number,
width: number,
height: number,
c: number
) {
if (c) {
Screen.image.fillRect(
Screen.x(x),
Screen.y(y),
width,
height,
c
)
}
}
public static fillRectXfrm(
xfrm: Affine,
x: number,
y: number,
width: number,
height: number,
c: number
) {
Screen.fillRect(
x + xfrm.worldPos.x,
y + xfrm.worldPos.y,
width,
height,
c
)
}
public static fillBoundsXfrm(xfrm: Affine, bounds: Bounds, c: number) {
Screen.fillRectXfrm(
xfrm,
bounds.left,
bounds.top,
bounds.width,
bounds.height,
c
)
}
public static drawBoundsXfrm(xfrm: Affine, bounds: Bounds, c: number) {
Screen.drawRectXfrm(
xfrm,
bounds.left,
bounds.top,
bounds.width,
bounds.height,
c
)
}
// Draws a rounded outline rectangle of the bounds.
public static outlineBoundsXfrm(
xfrm: Affine,
bounds: Bounds,
dist: number,
c: number
) {
const left = bounds.left + xfrm.worldPos.x
const top = bounds.top + xfrm.worldPos.y
const right = bounds.right + xfrm.worldPos.x
const bottom = bounds.bottom + xfrm.worldPos.y
// Left
Screen.drawLine(left - dist, top, left - dist, bottom, c)
// Right
Screen.drawLine(right + dist, top, right + dist, bottom, c)
// Top
Screen.drawLine(left, top - dist, right, top - dist, c)
// Bottom
Screen.drawLine(left, bottom + dist, right, bottom + dist, c)
// Connect corners
if (dist > 1) {
// Left-Top
Screen.drawLine(left - dist, top, left, top - dist, c)
// Right-Top
Screen.drawLine(right + dist, top, right, top - dist, c)
// Left-Bottom
Screen.drawLine(left - dist, bottom, left, bottom + dist, c)
// Right-Bottom
Screen.drawLine(right + dist, bottom, right, bottom + dist, c)
}
}
// Draws a rounded outline rectangle of the bounds.
public static outlineBoundsXfrm4(
xfrm: Affine,
bounds: Bounds,
dist: number,
colors: { top: number; left: number; right: number; bottom: number }
) {
const left = bounds.left + xfrm.worldPos.x
const top = bounds.top + xfrm.worldPos.y
const right = bounds.right + xfrm.worldPos.x
const bottom = bounds.bottom + xfrm.worldPos.y
// Left
Screen.drawLine(left - dist, top, left - dist, bottom, colors.left)
// Right
Screen.drawLine(
right + dist,
top,
right + dist,
bottom,
colors.right
)
// Top
Screen.drawLine(left, top - dist, right, top - dist, colors.top)
// Bottom
Screen.drawLine(
left,
bottom + dist,
right,
bottom + dist,
colors.bottom
)
// Connect corners
if (dist > 1) {
// Left-Top
Screen.drawLine(left - dist, top, left, top - dist, colors.left)
// Right-Top
Screen.drawLine(
right + dist,
top,
right,
top - dist,
colors.right
)
// Left-Bottom
Screen.drawLine(
left - dist,
bottom,
left,
bottom + dist,
colors.left
)
// Right-Bottom
Screen.drawLine(
right + dist,
bottom,
right,
bottom + dist,
colors.right
)
}
}
public static setPixel(x: number, y: number, c: number) {
if (c) {
Screen.image.setPixel(Screen.x(x), Screen.y(y), c)
}
}
public static setPixelXfrm(
xfrm: Affine,
x: number,
y: number,
c: number
) {
Screen.setPixel(x + xfrm.worldPos.x, y + xfrm.worldPos.y, c)
}
public static print(
text: string,
x: number,
y: number,
color?: number,
font?: image.Font,
offsets?: texteffects.TextEffectState[]
) {
Screen.image.print(
text,
Screen.x(x),
Screen.y(y),
color,
font,
offsets
)
}
}
}