-
Notifications
You must be signed in to change notification settings - Fork 18
/
screen.ts
367 lines (340 loc) · 11 KB
/
screen.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
361
362
363
364
365
366
367
namespace microcode {
export class Screen {
private static image_: Image
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,
})
private static updateBounds() {
Screen.WIDTH = Screen.image_.width
Screen.HEIGHT = Screen.image_.height
Screen.HALF_WIDTH = Screen.WIDTH >> 1
Screen.HALF_HEIGHT = Screen.HEIGHT >> 1
Screen.LEFT_EDGE = -Screen.HALF_WIDTH
Screen.RIGHT_EDGE = Screen.HALF_WIDTH
Screen.TOP_EDGE = -Screen.HALF_HEIGHT
Screen.BOTTOM_EDGE = Screen.HALF_HEIGHT
Screen.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(): Image {
if (!Screen.image_) {
Screen.image_ = screen
Screen.updateBounds()
}
return Screen.image_
}
public static resetScreenImage() {
Screen.image_ = screen
Screen.updateBounds()
}
public static setImageSize(width: number, height: number) {
Screen.image_ = image.create(width, height)
Screen.updateBounds()
}
public static drawTransparentImage(from: Image, x: number, y: number) {
Screen.image.drawTransparentImage(from, Screen.x(x), Screen.y(y))
}
public static drawTransparentImageXfrm(
xfrm: Affine,
from: Image,
x: number,
y: number
) {
const w = xfrm.worldPos
Screen.image.drawTransparentImage(
from,
Screen.x(x + w.x),
Screen.y(y + w.y)
)
}
public static drawLine(
x0: number,
y0: number,
x1: number,
y1: number,
c: number
) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
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
) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
const w = xfrm.worldPos
Screen.drawLine(x0 + w.x, y0 + w.y, x1 + w.x, y1 + w.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
) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
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
) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
const w = xfrm.worldPos
Screen.drawRect(x + w.x, y + w.y, width, height, c)
}
public static fillRect(
x: number,
y: number,
width: number,
height: number,
c: number
) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
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
) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
const w = xfrm.worldPos
Screen.fillRect(x + w.x, y + w.y, width, height, c)
}
public static fillBoundsXfrm(xfrm: Affine, bounds: Bounds, c: number) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
Screen.fillRectXfrm(
xfrm,
bounds.left,
bounds.top,
bounds.width,
bounds.height,
c
)
}
public static drawBoundsXfrm(xfrm: Affine, bounds: Bounds, c: number) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
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
) {
if (!c) return
const w = xfrm.worldPos
const left = bounds.left + w.x
const top = bounds.top + w.y
const right = bounds.right + w.x
const bottom = bounds.bottom + w.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 }
) {
// no borders!
if (!colors.top && !colors.left && !colors.right && !colors.bottom)
return
const w = xfrm.worldPos
const left = bounds.left + w.x
const top = bounds.top + w.y
const right = bounds.right + w.x
const bottom = bounds.bottom + w.y
// Left
if (colors.left)
Screen.drawLine(
left - dist,
top,
left - dist,
bottom,
colors.left
)
// Right
if (colors.right)
Screen.drawLine(
right + dist,
top,
right + dist,
bottom,
colors.right
)
// Top
if (colors.top)
Screen.drawLine(left, top - dist, right, top - dist, colors.top)
// Bottom
if (colors.bottom)
Screen.drawLine(
left,
bottom + dist,
right,
bottom + dist,
colors.bottom
)
// Connect corners
if (dist > 1) {
// Left-Top
if (colors.left)
Screen.drawLine(
left - dist,
top,
left,
top - dist,
colors.left
)
// Right-Top
if (colors.right)
Screen.drawLine(
right + dist,
top,
right,
top - dist,
colors.right
)
// Left-Bottom
if (colors.left)
Screen.drawLine(
left - dist,
bottom,
left,
bottom + dist,
colors.left
)
// Right-Bottom
if (colors.right)
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
) {
control.assert(c !== 0, ERROR_NOT_INTEGER)
const w = xfrm.worldPos
Screen.setPixel(x + w.x, y + w.y, c)
}
public static print(
text: string,
x: number,
y: number,
color?: number,
font?: image.Font,
offsets?: texteffects.TextEffectState[]
) {
control.assert(color !== 0, ERROR_NOT_INTEGER)
Screen.image.print(
text,
Screen.x(x),
Screen.y(y),
color,
font,
offsets
)
}
}
}