-
Notifications
You must be signed in to change notification settings - Fork 2
/
drawing.lib.lua
207 lines (171 loc) · 4.96 KB
/
drawing.lib.lua
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
-----------------------------------------------------------------------------
-- Midcraft Commander
-- Author: MewK
--
-- This program is released under the MIT License (MIT).
-----------------------------------------------------------------------------
local w, h = term.getSize()
local hw = math.floor(w / 2)
local hh = math.floor(h / 2)
function trimText(text, width)
if width > 6 and # text > 0 and # text > width then
text = string.sub(text, 0, width - 3)..'...'
end
return text
end
function padText(text, width, char)
if # text <= width then
return text..string.rep(char, width - # text)
end
return text
end
function drawLineH(x, y, length, drawCorner)
term.setCursorPos(x, y)
for i = 1, length do
if drawCorner and (i == 1 or i == length) then
term.write('+')
else
term.write('-')
end
end
end
function drawLabeledLineH(x, y, length, text, drawCorner)
text = trimText(text, length - 4)
drawLineH(x, y, length, drawCorner)
term.setCursorPos(x + 1, y)
term.write(' '..text..' ')
end
function drawLineV(x, y, length, drawCorner)
for i = 1, length do
term.setCursorPos(x, y + i - 1)
if drawCorner and (i == 1 or i == length) then
term.write('+')
else
term.write('|')
end
end
end
function clearBox(_x, _y, _w, _h)
for __y = _y, _y + _h - 1 do
for __x = _x, _x + _w - 1 do
term.setCursorPos(__x, __y)
term.write(' ')
end
end
end
function drawBox(_x, _y, _w, _h, title, text)
clearBox(_x, _y, _w, _h)
drawLineV(_x, _y, _h, false)
drawLineV(_x + _w - 1, _y, _h, false)
drawLineH(_x, _y, _w, true)
drawLineH(_x, _y + 2, _w, true)
drawLineH(_x, _y + _h - 1, _w, true)
term.setCursorPos(_x + 2, _y + 1)
term.write(trimText(title, _w - 4))
for index, line in ipairs(text) do
term.setCursorPos(_x + 2, _y + 2 + index)
term.write(trimText(line, _w - 4))
end
end
function drawBoxCentered(_w, _h, title, text)
local _x = hw - math.floor(_w / 2)
local _y = hh - math.floor(_h / 2)
drawBox(_x, _y, _w, _h, title, text)
end
function drawEntries(view, clipboard, clipboardAction, virtualEntries, buildPath)
local y = 0
local _start = view.scrollOffset + 1
local _end = view.scrollOffset + view.height
if _end > # view.entryList then
_end = # view.entryList
end
for index = _start, _end do
local entry = view.entryList[index]
local prefix = ''
local suffix = ''
-- Folder prefix
if entry.type == 0 then
prefix = '/'..prefix
end
-- Position indicator
if view.active and view.selectionIndex == index then
prefix = '» '..prefix
else
prefix = ' '..prefix
end
-- Play indicator
for index, _entry in ipairs(virtualEntries) do
if _entry.name == 'music' then
if _entry.meta and _entry.meta.playing and entry.meta and _entry.meta.playing == entry.meta.side then
suffix = suffix..' >'
end
break
end
end
-- Selection indicator
local selected = false
for index, _entry in ipairs(clipboard) do
if _entry.fullpath == entry.fullpath then
-- Default indicator
if clipboardAction == 0 then
suffix = suffix..' ?'
-- Copy indicator
elseif clipboardAction == 1 then
suffix = suffix..' +'
-- Cut indicator
elseif clipboardAction == 2 then
suffix = suffix..' -'
end
break
end
end
term.setCursorPos(view.x, view.y + y)
term.write(prefix..trimText(entry.name, view.width - (2 + # suffix + 1))..suffix)
y = y + 1
end
end
function drawMenu(x, y, menuFunctions, menuItemSelection, subMenuItemSelection)
local currentX = x
-- Clear menu area
term.setCursorPos(x, y)
term.clearLine()
-- Draw menu entry
for index, menuFunction in ipairs(menuFunctions) do
term.setCursorPos(currentX, y)
if menuItemSelection and index == menuItemSelection then
term.write('['..menuFunction.name..']')
-- Draw sub-menu
if menuFunction.children then
-- Calculate sub-menu width
local maxSubItemWidth = 0
for subIndex, subMenuFunction in ipairs(menuFunction.children) do
local currentLen = # subMenuFunction.name
if currentLen > maxSubItemWidth then
maxSubItemWidth = currentLen
end
end
local menuWidth = maxSubItemWidth + 6
-- Calculate sub-menu height
local menuHeight = # menuFunction.children + 2
-- Draw menu border
local currentY = y - menuHeight
drawLineH(currentX, currentY, menuWidth, true)
drawLineV(currentX, currentY, menuHeight, true)
drawLineV(currentX + menuWidth - 1, currentY, menuHeight, true)
-- Draw sub-menu items
for subIndex, subMenuFunction in ipairs(menuFunction.children) do
currentY = currentY + 1
term.setCursorPos(currentX + 1, currentY)
if subIndex == subMenuItemSelection then
term.write(' ['..padText(subMenuFunction.name, maxSubItemWidth, ' ')..'] ')
else
term.write(' '..padText(subMenuFunction.name, maxSubItemWidth, ' ')..' ')
end
end
end
else
term.write(' '..menuFunction.name..' ')
end
currentX = currentX + # menuFunction.name + 2
end
end