-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaintutils.lua
More file actions
169 lines (159 loc) · 4.87 KB
/
paintutils.lua
File metadata and controls
169 lines (159 loc) · 4.87 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
local expect = require "cc.expect"
local syscall = require "_syscall"
local term = require "term"
local paintutils = {}
function paintutils.drawPixel(x, y, color)
expect(1, x, "number")
expect(2, y, "number")
local ox, oy = term.getCursorPos()
local c = term.getBackgroundColor()
term.setCursorPos(x, y)
if color then term.setBackgroundColor(color) end
term.write(" ")
term.setCursorPos(ox, oy)
term.setBackgroundColor(c)
end
local function drawLineInternal(sp, x1, y1, x2, y2)
if math.abs(y2 - y1) < math.abs(x2 - x1) then
if x1 > x2 then x1, y1, x2, y2 = x2, y2, x1, y1 end
local dx, dy, yi = x2 - x1, y2 - y1, 1
if dy < 0 then yi, dy = -1, -dy end
local D, y = 2*dy - dx, y1
if yi < 0 then
y = y2
for x = x2, x1, -1 do
sp(x, y)
if D > 0 then
y = y + 1
D = D + 2*(dy - dx)
else
D = D + 2*dy
end
end
else
for x = x1, x2 do
sp(x, y)
if D > 0 then
y = y + 1
D = D + 2*(dy - dx)
else
D = D + 2*dy
end
end
end
else
if y1 > y2 then x1, y1, x2, y2 = x2, y2, x1, y1 end
local dx, dy, xi = x2 - x1, y2 - y1, 1
if dx < 0 then xi, dx = -1, -dx end
local D, x = 2*dx - dy, x1
for y = y1, y2 do
sp(x, y)
if D > 0 then
x = x + xi
D = D + 2*(dx - dy)
else
D = D + 2*dx
end
end
end
end
function paintutils.drawLine(x1, y1, x2, y2, color)
expect(1, x1, "number")
expect(2, y1, "number")
expect(3, x2, "number")
expect(4, y2, "number")
expect(5, color, "number")
if y1 == y2 then
local width = math.abs(x2 - x1) + 1
local b, f = term.getBackgroundColor(), term.getTextColor()
local ox, oy = term.getCursorPos()
local fg, bg = ("%x"):format(f), ("%x"):format(color)
term.setCursorPos(x1, y1)
term.blit((" "):rep(width), fg:rep(width), bg:rep(width))
term.setCursorPos(ox, oy)
term.setBackgroundColor(b)
return
end
local sp, rst
local c = term.getBackgroundColor()
local ox, oy = term.getCursorPos()
term.setBackgroundColor(color)
sp, rst = function(x, y)
term.setCursorPos(x, y)
term.write(" ")
end, function()
term.setBackgroundColor(c)
term.setCursorPos(ox, oy)
end
drawLineInternal(sp, x1, y1, x2, y2)
rst()
end
function paintutils.drawBox(x, y, width, height, color)
expect(1, x, "number")
expect(2, y, "number")
expect(3, width, "number")
expect(4, height, "number")
expect(5, color, "number")
local b, f = term.getBackgroundColor(), term.getTextColor()
local ox, oy = term.getCursorPos()
local fg, bg = ("%x"):format(f), ("%x"):format(color)
term.setCursorPos(x, y)
term.blit((" "):rep(width), fg:rep(width), bg:rep(width))
term.setCursorPos(x, y+height-1)
term.blit((" "):rep(width), fg:rep(width), bg:rep(width))
for py = y + 1, y + height - 2 do
term.setCursorPos(x, py)
term.blit(" ", fg, bg)
term.setCursorPos(x+width-1, py)
term.blit(" ", fg, bg)
end
term.setBackgroundColor(b)
term.setCursorPos(ox, oy)
end
function paintutils.drawFilledBox(x, y, width, height, color)
expect(1, x, "number")
expect(2, y, "number")
expect(3, width, "number")
expect(4, height, "number")
expect(5, color, "number")
local b, f = term.getBackgroundColor(), term.getTextColor()
local ox, oy = term.getCursorPos()
local text, fg, bg = (" "):rep(width), ("%x"):format(f):rep(width), ("%x"):format(color):rep(width)
for py = y, y + height - 1 do
term.setCursorPos(x, py)
term.blit(text, fg, bg)
end
term.setBackgroundColor(b)
term.setCursorPos(ox, oy)
end
function paintutils.drawImage(x, y, image)
expect(1, x, "number")
expect(2, y, "number")
expect(3, image, "table")
for i, line in ipairs(image) do
term.setCursorPos(x, y + i - 1)
for _, c in ipairs(line) do
term.setBackgroundColor(c)
term.write(" ")
end
end
end
function paintutils.parseImage(image)
expect(1, image, "string")
local retval = {}
for line in image:gmatch "[^\n]+" do
local l = {}
for c in line:gmatch "." do l[#l+1] = tonumber(c, 16) end
retval[#retval+1] = l
end
return retval
end
function paintutils.loadImage(path)
expect(1, path, "string")
local file = syscall.open(path, "r")
if not file then return nil end
local data = file.readAll()
file.close()
return paintutils.parseImage(data)
end
return paintutils