-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphics.lua
More file actions
169 lines (142 loc) · 4.45 KB
/
Graphics.lua
File metadata and controls
169 lines (142 loc) · 4.45 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
Graphics = {
terminal = nil,
width = 0,
height = 0
}
function Graphics:new(term)
local self = {
terminal = term
}
setmetatable(self, {
__index = Graphics
})
self:updateSize()
return self
end
function Graphics:updateSize()
local w, h = self.terminal.getSize()
self.width = w
self.height = h
end
function Graphics:getWidth()
return self.width
end
function Graphics:getHeight()
return self.height
end
function Graphics:clearLine(index, color)
self:fillBox(1, index, self:getWidth(), 1, color or "f")
end
function Graphics:write(x, y, str, color, background)
color = color or "0"
if str:len() == 0 then
return
end
background = background or "f"
if str:len() > 1 then
if color:len() == 1 then
color = str.rep(color, str:len())
end
if background:len() == 1 then
background = str.rep(background, str:len())
end
end
local term = self.terminal;
local ox, oy = term.getCursorPos()
term.setCursorPos(x, y)
term.blit(str, color, background);
term.setCursorPos(ox, oy)
end
function Graphics:drawBox(x, y, width, height, color, background)
if width > self:getWidth() - (x - 1) or height > self:getHeight() - (y - 1) then
-- return false
print(string.format("warning box to long: x: %d, y: %d, w: %d, h: %d, sw: %d, sh: %d", x, y, width, height,
self:getWidth(), self:getHeight()));
end
if color == nil then
color = "0"
end
if background == nil then
background = "f"
end
local boxStr = string.rep(" ", width);
local colorStr = string.rep(color, width);
local innerStr = colorStr;
if width >= 3 then
innerStr = string.rep(background, width - 2);
innerStr = color .. innerStr .. color
end
self:write(x, y, boxStr, colorStr, colorStr);
for i = y + 1, height + y - 1, 1 do
self:write(x, i, boxStr, innerStr, innerStr)
end
if height > 1 then
self:write(x, y + height - 1, boxStr, colorStr, colorStr)
end
return true
end
function Graphics:fillBox(x, y, width, height, color)
return self:drawBox(x, y, width, height, color, color)
end
function Graphics:round(x)
return x >= 0 and math.floor(x + 0.5) or math.ceil(x - 0.5)
end
function Graphics:drawBarHorizontal(x, y, width, height, val, max, color, background, border, borderColor, flipped,
percentageColor)
if border == nil or height < 3 or width < 3 then
border = false
end
if (border) then
self:fillBox(x, y, width, height, borderColor)
x = x + 1
y = y + 1
width = width - 2
height = height - 2
end
local widthAdjusted = Graphics:round((val / max) * width)
self:fillBox(x, y, width, height, background);
if flipped then
local offset = width - widthAdjusted;
self:fillBox(x + offset, y, widthAdjusted, height, color)
else
self:fillBox(x, y, widthAdjusted, height, color)
end
if percentageColor ~= nil then
local percentage = Graphics:round((val / max) * 100) .. "%"
if percentage:len() > width then
return
end
local yOffset = Graphics:round(height / 2) - 1 + y
local offset = math.min(widthAdjusted, width - percentage:len())
local bg = background
local overLap = widthAdjusted - offset
if overLap > 0 then
bg = string.rep(color, overLap) .. string.rep(background, percentage:len() - overLap)
end
if flipped then
self:write(x + width - offset - percentage:len(), yOffset, percentage, percentageColor, string.reverse(bg))
else
self:write(x + offset, yOffset, percentage, percentageColor, bg)
end
end
end
function Graphics:drawBarVertical(x, y, width, height, val, max, color, background, border, borderColor, flipped)
if border == nil or height < 3 or width < 3 then
border = false
end
if (border) then
self:fillBox(x, y, width, height, borderColor)
x = x + 1
y = y + 1
width = width - 2
height = height - 2
end
local heightAdjusted = Graphics:round((val / max) * height);
self:fillBox(x, y, width, height, background);
if flipped then
local offset = height - heightAdjusted
self:fillBox(x, y + offset, width, heightAdjusted, color)
else
self:fillBox(x, y, width, heightAdjusted, color)
end
end