-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow.lua
More file actions
310 lines (278 loc) · 11.9 KB
/
window.lua
File metadata and controls
310 lines (278 loc) · 11.9 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
local expect = require "cc.expect"
return {new = function(parent, wx, wy, w, h, visible)
expect(1, parent, "table")
expect(2, wx, "number")
expect(3, wy, "number")
expect(4, w, "number")
expect(5, h, "number")
expect(6, visible, "boolean", "nil")
if visible == nil then visible = true end
local size = {width = w, height = h}
local buffer = {
cursor = {x = 1, y = 1},
cursorBlink = false,
colors = {fg = '0', bg = 'f'},
palette = {},
dirtyLines = {},
dirtyPalette = {},
}
for y = 1, size.height do
buffer[y] = {(' '):rep(size.width), ('0'):rep(size.width), ('f'):rep(size.width)}
buffer.dirtyLines[y] = true
end
if parent then
for i = 0, 15 do
buffer.palette[i] = {parent.getPaletteColor(i)}
buffer.dirtyPalette[i] = true
end
else
buffer.palette = {
[0] = {0.94117647058824, 0.94117647058824, 0.94117647058824},
{0.94901960784314, 0.69803921568627, 0.2},
{0.89803921568627, 0.49803921568627, 0.84705882352941},
{0.6, 0.69803921568627, 0.94901960784314},
{0.87058823529412, 0.87058823529412, 0.42352941176471},
{0.49803921568627, 0.8, 0.098039215686275},
{0.94901960784314, 0.69803921568627, 0.8},
{0.29803921568627, 0.29803921568627, 0.29803921568627},
{0.6, 0.6, 0.6},
{0.29803921568627, 0.6, 0.69803921568627},
{0.69803921568627, 0.4, 0.89803921568627},
{0.2, 0.4, 0.8},
{0.49803921568627, 0.4, 0.29803921568627},
{0.34117647058824, 0.65098039215686, 0.30588235294118},
{0.8, 0.29803921568627, 0.29803921568627},
{0.066666666666667, 0.066666666666667, 0.066666666666667}
}
for i = 0, 15 do buffer.dirtyPalette[i] = true end
end
local win = setmetatable({}, {__name = "Terminal"})
function win.close() end
function win.write(text)
text = tostring(text)
expect(1, text, "string")
if buffer.cursor.y < 1 or buffer.cursor.y > size.height then return
elseif buffer.cursor.x > size.width or buffer.cursor.x + #text < 1 then
buffer.cursor.x = buffer.cursor.x + #text
return
elseif buffer.cursor.x < 1 then
text = text:sub(-buffer.cursor.x + 2)
buffer.cursor.x = 1
end
local ntext = #text
if buffer.cursor.x + #text > size.width then text = text:sub(1, size.width - buffer.cursor.x + 1) end
buffer[buffer.cursor.y][1] = buffer[buffer.cursor.y][1]:sub(1, buffer.cursor.x - 1) .. text .. buffer[buffer.cursor.y][1]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][2] = buffer[buffer.cursor.y][2]:sub(1, buffer.cursor.x - 1) .. buffer.colors.fg:rep(#text) .. buffer[buffer.cursor.y][2]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][3] = buffer[buffer.cursor.y][3]:sub(1, buffer.cursor.x - 1) .. buffer.colors.bg:rep(#text) .. buffer[buffer.cursor.y][3]:sub(buffer.cursor.x + #text)
buffer.cursor.x = buffer.cursor.x + ntext
buffer.dirtyLines[buffer.cursor.y] = true
win.redraw()
end
function win.blit(text, fg, bg)
text = tostring(text)
expect(1, text, "string")
expect(2, fg, "string")
expect(3, bg, "string")
if #text ~= #fg or #fg ~= #bg then error("Arguments must be the same length", 2) end
if buffer.cursor.y < 1 or buffer.cursor.y > size.height then return
elseif buffer.cursor.x > size.width or buffer.cursor.x < 1 - #text then
buffer.cursor.x = buffer.cursor.x + #text
win.redraw()
return
elseif buffer.cursor.x < 1 then
text, fg, bg = text:sub(-buffer.cursor.x + 2), fg:sub(-buffer.cursor.x + 2), bg:sub(-buffer.cursor.x + 2)
buffer.cursor.x = 1
end
local ntext = #text
if buffer.cursor.x + #text > size.width then text, fg, bg = text:sub(1, size.width - buffer.cursor.x + 1), fg:sub(1, size.width - buffer.cursor.x + 1), bg:sub(1, size.width - buffer.cursor.x + 1) end
buffer[buffer.cursor.y][1] = buffer[buffer.cursor.y][1]:sub(1, buffer.cursor.x - 1) .. text .. buffer[buffer.cursor.y][1]:sub(buffer.cursor.x + #text)
buffer[buffer.cursor.y][2] = buffer[buffer.cursor.y][2]:sub(1, buffer.cursor.x - 1) .. fg .. buffer[buffer.cursor.y][2]:sub(buffer.cursor.x + #fg)
buffer[buffer.cursor.y][3] = buffer[buffer.cursor.y][3]:sub(1, buffer.cursor.x - 1) .. bg .. buffer[buffer.cursor.y][3]:sub(buffer.cursor.x + #bg)
buffer.cursor.x = buffer.cursor.x + ntext
buffer.dirtyLines[buffer.cursor.y] = true
win.redraw()
end
function win.clear()
for y = 1, size.height do
buffer[y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)}
buffer.dirtyLines[y] = true
end
win.redraw()
end
function win.clearLine()
if buffer.cursor.y >= 1 and buffer.cursor.y <= size.height then
buffer[buffer.cursor.y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)}
buffer.dirtyLines[buffer.cursor.y] = true
win.redraw()
end
end
function win.getCursorPos()
return buffer.cursor.x, buffer.cursor.y
end
function win.setCursorPos(cx, cy)
expect(1, cx, "number")
expect(2, cy, "number")
if cx == buffer.cursor.x and cy == buffer.cursor.y then return end
buffer.cursor.x, buffer.cursor.y = math.floor(cx), math.floor(cy)
win.redraw()
end
function win.getCursorBlink()
return buffer.cursorBlink
end
function win.setCursorBlink(b)
expect(1, b, "boolean")
buffer.cursorBlink = b
win.redraw()
end
function win.isColor()
return true
end
function win.getSize()
return size.width, size.height
end
function win.scroll(lines)
expect(1, lines, "number")
if math.abs(lines) >= size.width then
for y = 1, size.height do buffer[y] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)} end
elseif lines > 0 then
for i = lines + 1, size.height do buffer[i - lines] = buffer[i] end
for i = size.height - lines + 1, size.height do buffer[i] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)} end
elseif lines < 0 then
for i = 1, size.height + lines do buffer[i - lines] = buffer[i] end
for i = 1, -lines do buffer[i] = {(' '):rep(size.width), buffer.colors.fg:rep(size.width), buffer.colors.bg:rep(size.width)} end
else return end
for i = 1, size.height do buffer.dirtyLines[i] = true end
win.redraw()
end
function win.getTextColor()
return tonumber(buffer.colors.fg)
end
function win.setTextColor(color)
expect(1, color, "number")
expect.range(color, 0, 15)
buffer.colors.fg = ("%x"):format(color)
end
function win.getBackgroundColor()
return tonumber(buffer.colors.bg)
end
function win.setBackgroundColor(color)
expect(1, color, "number")
expect.range(color, 0, 15)
buffer.colors.bg = ("%x"):format(color)
end
function win.getPaletteColor(color)
expect(1, color, "number")
expect.range(color, 0, 15)
return table.unpack(buffer.palette[math.floor(color)])
end
function win.setPaletteColor(color, r, g, b)
expect(1, color, "number")
expect(2, r, "number")
if g == nil and b == nil then r, g, b = bit32.band(bit32.rshift(r, 16), 0xFF) / 255, bit32.band(bit32.rshift(r, 8), 0xFF) / 255, bit32.band(r, 0xFF) / 255 end
expect(3, g, "number")
expect(4, b, "number")
expect.range(color, 0, 15)
if r < 0 or r > 1 then error("bad argument #2 (value out of range)", 2) end
if g < 0 or g > 1 then error("bad argument #3 (value out of range)", 2) end
if b < 0 or b > 1 then error("bad argument #4 (value out of range)", 2) end
buffer.palette[math.floor(color)] = {r, g, b}
buffer.dirtyPalette[math.floor(color)] = true
win.redraw()
end
function win.getLine(y)
expect(1, y, "number")
local l = buffer[y]
return l and table.unpack(l, 1, 3)
end
function win.getPosition()
return wx, wy
end
function win.reposition(x, y, w, h, p)
expect(1, x, "number", "nil")
expect(2, y, "number", "nil")
wx = x or wx
wy = y or wy
if p then win.reparent(p) end
if x or y then win.redraw(true) end
if w or h then return win.resize(w, h) end
end
function win.resize(width, height)
expect(1, width, "number", "nil")
expect(2, height, "number", "nil")
if width > size.width then
for y = 1, size.height do
buffer[y][1] = buffer[y][1] .. (' '):rep(width - size.width)
buffer[y][2] = buffer[y][2] .. buffer.colors.fg:rep(width - size.width)
buffer[y][3] = buffer[y][3] .. buffer.colors.bg:rep(width - size.width)
buffer.dirtyLines[y] = true
end
elseif width < size.width then
for y = 1, size.height do
buffer[y][1] = buffer[y][1]:sub(1, width)
buffer[y][2] = buffer[y][2]:sub(1, width)
buffer[y][3] = buffer[y][3]:sub(1, width)
end
end
size.width = width
if height > size.height then
for y = size.height + 1, height do
buffer[y] = {(' '):rep(width), buffer.colors.fg:rep(width), buffer.colors.bg:rep(width)}
buffer.dirtyLines[y] = true
end
elseif height < size.height then
for y = height + 1, size.height do
buffer[y] = nil
end
end
size.height = height
end
function win.reparent(p)
expect(1, p, "Terminal", "nil")
parent = p
win.redraw()
end
function win.redraw(full)
if not parent or not visible then return end
parent.setCursorBlink(false)
if full then
parent.clear()
for y = 1, size.height do
parent.setCursorPos(wx, wy+y-1)
parent.blit(buffer[y][1], buffer[y][2], buffer[y][3])
end
for i = 0, 15 do parent.setPaletteColor(i, buffer.palette[i][1], buffer.palette[i][2], buffer.palette[i][3]) end
else
for y in pairs(buffer.dirtyLines) do
parent.setCursorPos(wx, wy+y-1)
if #buffer[y][1] ~= #buffer[y][2] or #buffer[y][2] ~= #buffer[y][3] then error("Internal error: Invalid lengths") end
parent.blit(buffer[y][1], buffer[y][2], buffer[y][3])
end
for i in pairs(buffer.dirtyPalette) do parent.setPaletteColor(i, buffer.palette[i][1], buffer.palette[i][2], buffer.palette[i][3]) end
end
parent.setCursorPos(wx+buffer.cursor.x-1, wy+buffer.cursor.y-1)
parent.setCursorBlink(buffer.cursorBlink)
buffer.dirtyLines, buffer.dirtyPalette = {}, {}
end
function win.restoreCursor()
if not parent or not visible then return end
parent.setCursorPos(wx+buffer.cursor.x-1, wy+buffer.cursor.y-1)
parent.setCursorBlink(buffer.cursorBlink)
end
function win.isVisible()
return visible
end
function win.setVisible(v)
expect(1, v, "boolean")
visible = v
win.redraw()
end
win.isColour = win.isColor
win.getTextColour = win.getTextColor
win.setTextColour = win.setTextColor
win.getBackgroundColour = win.getBackgroundColor
win.setBackgroundColour = win.setBackgroundColor
win.getPaletteColour = win.getPaletteColor
win.setPaletteColour = win.setPaletteColor
win.redraw()
return win
end}