-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileloader.lua
193 lines (152 loc) · 3.9 KB
/
fileloader.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
PCspire.loader = {}
PCspire.loader.dir = "scripts"
function uCol(t)
return t[1], t[2], t[3]
end
function on.create()
local dirList = PCspire.getDirList(PCspire.loader.dir)
local files = {}
local path = PCspire.loader.dir .. "/"
local name
for _, file in ipairs(dirList) do
if PCspire.isFile(path .. file) then
name = file:split("%.")
if name[#name] == "lua" then
table.insert(files, file)
end
end
end
PCspire.loader.list = sList()
PCspire.loader.list.items = files
function PCspire.loader.list:action(n, src)
sList = nil
uCol = nil
scrollBar = nil
on = {}
PCspire.scriptname = "scripts/" .. src
PCspire.init()
end
end
function on.paint(gc)
PCspire.loader.list:paint(gc)
end
function on.arrowKey(a)
PCspire.loader.list:arrowKey(a)
platform.window:invalidate()
end
function on.mouseUp(x, y)
PCspire.loader.list:mouseUp(x, y)
platform.window:invalidate()
end
function on.enterKey()
PCspire.loader.list:enterKey()
platform.window:invalidate()
end
sList = class()
function sList:init()
self.w = platform.window:width()-10
self.h = platform.window:height()-10
self.x = 5
self.y = 5
self.ih = 18
self.top = 0
self.sel = 1
self.font = {"sansserif", "r", 10}
self.colors = {50,150,190}
self.items = {}
self.scrollBar = scrollBar(self.h, self.top, #self.items,#self.items, self.x + self.w - 15, self.y)
end
function sList:paint(gc)
local x = self.x
local y = self.y
local w = self.w
local h = self.h
local ih = self.ih
local top = self.top
local sel = self.sel
local items = self.items
local visible_items = math.floor(h/ih)
gc:setColorRGB(255, 255, 255)
gc:fillRect(x, y, w, h)
gc:setColorRGB(0, 0, 0)
gc:drawRect(x, y, w, h)
gc:clipRect("set", x, y, w, h)
gc:setFont(unpack(self.font))
local label, item
for i=1, math.min(#items-top, visible_items+1) do
item = items[i+top]
label = item
if i+top == sel then
gc:setColorRGB(unpack(self.colors))
gc:fillRect(x+1, y + i*ih-ih + 1, w-(12 + 2 + 2), ih)
gc:setColorRGB(255, 255, 255)
end
gc:drawString(label, x+5, y + i*ih-ih , "top")
gc:setColorRGB(0, 0, 0)
end
gc:clipRect("reset")
self.scrollBar:update(top, visible_items, #items)
self.scrollBar:paint(gc)
end
function sList:arrowKey(arrow)
if arrow=="up" and self.sel>1 then
self.sel = self.sel - 1
if self.top>=self.sel then
self.top = self.top - 1
end
end
if arrow=="down" and self.sel<#self.items then
self.sel = self.sel + 1
if self.sel>(self.h/self.ih)+self.top then
self.top = self.top + 1
end
end
end
function sList:mouseUp(x, y)
if x>=self.x and x<self.x+self.w-16 and y>=self.y and y<self.y+self.h then
local sel = math.floor((y-self.y)/self.ih) + 1 + self.top
if sel==self.sel then
self:enterKey()
return
end
self.sel=sel
if self.sel>(self.h/self.ih)+self.top then
self.top = self.top + 1
end
if self.top>=self.sel then
self.top = self.top - 1
end
end
end
function sList:enterKey()
self:action(self.sel, self.items[self.sel])
end
function sList:action() end
scrollBar = class()
function scrollBar:init(h, top, visible, total, x, y)
self.color1 = {96, 100, 96}
self.color2 = {184, 184, 184}
self.h = h or 100
self.w = 14
self.x = x
self.y = y
self.visible = visible or 10
self.total = total or 15
self.top = top or 4
end
function scrollBar:paint(gc)
gc:setColorRGB(255,255,255)
gc:setColorRGB(uCol(self.color1))
if self.visible<self.total then
local step = (self.h)/self.total
gc:fillRect(self.x + 3, self.y + 1 + step*self.top, 9, step*self.visible)
gc:setColorRGB(uCol(self.color2))
gc:fillRect(self.x + 2 , self.y + 1 + step*self.top, 1, step*self.visible)
gc:fillRect(self.x + 12, self.y + 1 + step*self.top, 1, step*self.visible)
end
end
function scrollBar:update(top, visible, total)
self.top = top or self.top
self.visible = visible or self.visible
self.total = total or self.total
end