forked from IgorTimofeev/GUI
-
Notifications
You must be signed in to change notification settings - Fork 3
/
install_examples.lua
244 lines (208 loc) · 9.96 KB
/
install_examples.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
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
local component = require("component")
local unicode = require("unicode")
local fs = require("filesystem")
local event = require("event")
local gpu = component.gpu
local internet = component.internet
---------------------------------------------------------------------------------------------------------------------------------
-- Specify required files for downloading
local url = "https://raw.githubusercontent.com/kevinkk525/GUI/master/examples/"
local path = "/home/examples/"
local files = {}
local filenames = { 'actionButtons.lua', 'animatedWidget.lua', 'brailleCanvas.lua', 'buttons.lua', 'chart.lua', 'codeView.lua', 'colorSelector.lua', 'combo.lua', 'contextMenu.lua', 'filesystemChooser.lua', 'filesystemTree.lua', 'HDD.pic', 'image.lua', 'input.lua', 'label.lua', 'layout.lua', 'list.lua', 'menu.lua', 'palette.lua', 'panel.lua', 'progressBar.lua', 'progressIndicator.lua', 'resizer.lua', 'scrollBar.lua', 'slider.lua', 'switch.lua', 'text.lua', 'textBox.lua', 'window.lua', 'workspace.lua' }
for i, name in pairs(filenames) do
files[#files + 1] = { ["url"] = url .. name, ["path"] = path .. name }
end
local properties = {
-- Comment any coordinate to calculate it automatically (will centerize window on screen by specified axis)
-- windowX = 2,
-- windowY = 2,
-- Set window width value lower than zero (0.5 for example) to calculate it dependent on screen width
windowWidth = 54,
-- Customize offset by X axis from window corners
GUIElementsOffset = 2,
-- Customize localization as you want to
localization = {
-- Specify title of your installer
title = "GUI framework Examples installer",
-- Use <currentProgress>, <totalProgress> and <currentFile> text insertions to automatically display their values
currentFile = "Downloading \"<currentFile>\"",
totalProgress = "Total progress: <totalProgress>%",
-- Comment this lines to automatically close installer window
finished1 = "GUI framework examples have been successfully downloaded",
finished2 = "Press any key to quit"
},
-- Customize color scheme as you want to
colors = {
window = {
background = 0xEEEEEE,
text = 0x999999,
shadow = 0x3C3C3C
},
title = {
background = 0xCCCCCC,
text = 0x555555,
},
progressBar = {
active = 0x0092FF,
passive = 0xCCCCCC
}
}
}
---------------------------------------------------------------------------------------------------------------------------------
local screenWidth, screenHeight = gpu.getResolution()
properties.windowHeight = 8
if properties.windowWidth < 1 then
properties.windowWidth = math.floor(screenWidth * properties.windowWidth)
end
progressBarWidth = properties.windowWidth - properties.GUIElementsOffset * 2
if not properties.windowX then
properties.windowX = math.floor(screenWidth / 2 - properties.windowWidth / 2)
end
if not properties.windowY then
properties.windowY = math.floor(screenHeight / 2 - properties.windowHeight / 2)
end
local currentBackground, currentForeground
---------------------------------------------------------------------------------------------------------------------------------
local function setBackground(color)
if currentBackground ~= color then
gpu.setBackground(color)
currentBackground = color
end
end
local function setForeground(color)
if currentForeground ~= color then
gpu.setForeground(color)
currentForeground = color
end
end
local function rectangle(x, y, width, height, color)
setBackground(color)
gpu.fill(x, y, width, height, " ")
end
local function centerizedText(y, color, text)
local textLength = unicode.len(text)
if textLength > progressBarWidth then
text = unicode.sub(text, 1, progressBarWidth)
textLength = progressBarWidth
end
setForeground(color)
gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep(" ", progressBarWidth))
gpu.set(math.floor(properties.windowX + properties.GUIElementsOffset + progressBarWidth / 2 - textLength / 2), y, text)
end
local function progressBar(y, percent, text, totalProgress, currentProgress, currentFile)
setForeground(properties.colors.progressBar.passive)
gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", progressBarWidth))
setForeground(properties.colors.progressBar.active)
gpu.set(properties.windowX + properties.GUIElementsOffset, y, string.rep("━", math.ceil(progressBarWidth * percent)))
text = text:gsub("<totalProgress>", totalProgress)
text = text:gsub("<currentProgress>", currentProgress)
text = text:gsub("<currentFile>", currentFile)
centerizedText(y + 1, properties.colors.window.text, text)
end
local function download(url, path, totalProgress)
fs.makeDirectory(fs.path(path))
local file, fileReason = io.open(path, "w")
if file then
local pcallSuccess, requestHandle = pcall(internet.request, url)
if pcallSuccess then
if requestHandle then
-- Drawing progressbar once with zero percentage
local y = properties.windowY + 2
progressBar(y, 0, properties.localization.currentFile, totalProgress, "0", path)
-- Waiting for any response code
local responseCode, responseName, responseData
repeat
responseCode, responseName, responseData = requestHandle:response()
until responseCode
-- Downloading file by chunks
if responseData and responseData["Content-Length"] then
local contentLength = tonumber(responseData["Content-Length"][1])
local currentLength = 0
while true do
local data, reason = requestHandle.read(math.huge)
if data then
currentLength = currentLength + unicode.len(data)
local percent = currentLength / contentLength
progressBar(y, percent, properties.localization.currentFile, totalProgress, tostring(math.ceil(percent)), path)
file:write(data)
else
requestHandle:close()
if reason then
error(reason)
else
file:close()
return
end
end
end
else
error("Response Content-Length header is missing: " .. tostring(responseCode) .. " " .. tostring(responseName))
end
else
error("Invalid URL-address: " .. tostring(url))
end
else
error("Usage: component.internet.request(string url)")
end
file:close()
else
error("Failed to open file for writing: " .. tostring(fileReason))
end
end
---------------------------------------------------------------------------------------------------------------------------------
-- Copying current screen data
local oldPixels = {}
for y = properties.windowY, properties.windowY + properties.windowHeight do
oldPixels[y] = {}
for x = properties.windowX, properties.windowX + properties.windowWidth do
oldPixels[y][x] = { gpu.get(x, y) }
end
end
local function shadowPixel(x, y, symbol)
setBackground(oldPixels[y][x][3])
gpu.set(x, y, symbol)
end
-- Vertical shadow
rectangle(properties.windowX + properties.windowWidth, properties.windowY + 1, 1, properties.windowHeight - 1, properties.colors.window.shadow)
setForeground(properties.colors.window.shadow)
shadowPixel(properties.windowX + properties.windowWidth, properties.windowY, "▄")
-- Horizontal shadow
for i = properties.windowX + 1, properties.windowX + properties.windowWidth do
shadowPixel(i, properties.windowY + properties.windowHeight, "▀")
end
-- Window background
rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
-- Title
rectangle(properties.windowX, properties.windowY, properties.windowWidth, 1, properties.colors.title.background)
centerizedText(properties.windowY, properties.colors.title.text, properties.localization.title)
setBackground(properties.colors.window.background)
-- Downloading
local y = properties.windowY + 5
progressBar(y, 0, properties.localization.totalProgress, "0", "0", files[1].path)
for i = 1, #files do
local percent = i / #files
local totalProgress = tostring(math.ceil(percent * 100))
download(files[i].url, files[i].path, totalProgress)
progressBar(y, percent, properties.localization.totalProgress, totalProgress, "0", files[i].path)
end
-- On exit
if properties.localization.finished1 then
rectangle(properties.windowX, properties.windowY + 1, properties.windowWidth, properties.windowHeight - 1, properties.colors.window.background)
centerizedText(properties.windowY + 3, properties.colors.window.text, properties.localization.finished1)
centerizedText(properties.windowY + 4, properties.colors.window.text, properties.localization.finished2)
while true do
local eventType = event.pull()
if eventType == "key_down" or eventType == "touch" then
break
end
end
end
-- Redraw old pixels
for y = properties.windowY, properties.windowY + properties.windowHeight do
for x = properties.windowX, properties.windowX + properties.windowWidth do
setBackground(oldPixels[y][x][3])
setForeground(oldPixels[y][x][2])
gpu.set(x, y, oldPixels[y][x][1])
end
end