-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.lua
386 lines (299 loc) · 9.08 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
--
-- EXF: Example Framework
--
-- This should make examples easier and more enjoyable to use.
-- All examples in one application! Yaay!
--
-- Updated by Dresenpai
exf = {}
exf.current = nil
exf.available = {}
function love.load()
exf.list = List:new()
exf.smallfont = love.graphics.newFont(love._vera_ttf,12)
exf.bigfont = love.graphics.newFont(love._vera_ttf, 24)
exf.list.font = exf.smallfont
exf.bigball = love.graphics.newImage("images/love-big-ball.png")
-- Find available demos.
local files = love.filesystem.enumerate("")
for i,v in ipairs(files) do
local unused1, unused2, n = string.find(v, "e(%d%d%d%d)%.lua")
if n then
--table.insert(exf.available, v);
--local file = love.filesystem.newFile(v, love.file_read)
--file:open()
--local contents = file:read(100)
--local s, e, c = string.find(contents, "Example: ([%a%p ]-)[\r\n]")
--file:close(file)
--if not c then c = "Untitled" end
--local title = exf.getn(n) .. " " .. c
--exf.list:add(title)
table.insert(exf.available, v);
local file = love.filesystem.newFile(v, love.file_read)
file:open("r")
local contents = love.filesystem.read(v,100)
local s, e, c = string.find(contents, "Example: ([%a%p ]-)[\r\n]")
file:close(file)
if not c then c = "Untitled" end
local title = exf.getn(n) .. " " .. c
exf.list:add(title)
end
end
exf.list:done()
exf.resume()
end
function love.update(dt) end
function love.draw() end
function love.keypressed(k) end
function love.keyreleased(k) end
function love.mousepressed(x, y, b) end
function love.mousereleased(x, y, b) end
function exf.empty() end
function exf.update(dt)
exf.list:update(dt)
end
function exf.draw()
love.graphics.setBackgroundColor(54, 172, 248)
love.graphics.setColor(255, 255, 255)
love.graphics.setFont(exf.bigfont)
love.graphics.print("Example Browser", 50, 50)
love.graphics.print("Usage", 500, 50)
love.graphics.setFont(exf.smallfont)
love.graphics.print("Browse and click on the example you \nwant to run. To return the the example \nselection screen, press escape.\n\nIf you would like to add an \nexample, please contact us! ^.^)/", 500, 80)
love.graphics.draw(exf.bigball, 800 - 128, 600 - 128)
exf.list:draw()
end
function exf.keypressed(k)
end
function exf.keyreleased(k)
end
function exf.mousepressed(x, y, b)
exf.list:mousepressed(x, y, b)
end
function exf.mousereleased(x, y, b)
exf.list:mousereleased(x, y, b)
end
function exf.getn(n)
local s = ""
n = tonumber(n)
local r = n
if r <= 0 then error("Example IDs must be bigger than 0. (Got: " .. r .. ")") end
if r >= 10000 then error("Example IDs must be less than 10000. (Got: " .. r .. ")") end
while r < 1000 do
s = s .. "0"
r = r * 10
end
s = s .. n
return s
end
function exf.intable(t, e)
for k,v in ipairs(t) do
if v == e then return true end
end
return false
end
function exf.start(item)
local e_id = string.sub(item, 1, 4)
local e_rest = string.sub(item, 5)
local file = "e" .. e_id .. ".lua"
if exf.intable(exf.available, file) then
if not love.filesystem.exists(file) then
print("Could not load game .. " .. file)
else
-- Clear all callbacks.
love.load = exf.empty
love.update = exf.empty
love.draw = exf.empty
love.keypressed = exf.empty
love.keyreleased = exf.empty
love.mousepressed = exf.empty
love.mousereleased = exf.empty
love.filesystem.load(file)()
exf.clear()
love.graphics.setCaption(e_rest)
-- Redirect keypress
local o_keypressed = love.keypressed
love.keypressed =
function(k)
if k == "escape" then
exf.resume()
end
o_keypressed(k)
end
love.load()
end
else
print("Example ".. e_id .. " does not exist.")
end
end
function exf.clear()
love.graphics.setBackgroundColor(0,0,0)
love.graphics.setColor(255, 255, 255)
love.graphics.setLine(1, "smooth")
love.graphics.setColorMode("replace")
love.graphics.setBlendMode("alpha")
love.mouse.setVisible(true)
end
function exf.resume()
load = nil
love.update = exf.update
love.draw = exf.draw
love.keypressed = exf.keypressed
love.keyreleased = exf.keyreleased
love.mousepressed = exf.mousepressed
love.mousereleased = exf.mousereleased
love.mouse.setVisible(true)
love.graphics.setCaption("LOVE Example Browser")
end
function inside(mx, my, x, y, w, h)
return mx >= x and mx <= (x+w) and my >= y and my <= (y+h)
end
----------------------
-- List object
----------------------
List = {}
function List:new()
o = {}
setmetatable(o, self)
self.__index = self
o.items = {}
o.x = 50
o.y = 70
o.width = 400
o.height = 500
o.item_height = 23
o.sum_item_height = 0
o.bar_size = 20
o.bar_pos = 0
o.bar_max_pos = 0
o.bar_width = 15
o.bar_lock = nil
return o
end
function List:add(item)
table.insert(self.items, item)
end
function List:done()
self.items.n = #self.items
-- Recalc bar size.
self.bar_pos = 0
local num_items = (self.height/self.item_height)
local ratio = num_items/self.items.n
self.bar_size = self.height * ratio
self.bar_max_pos = self.height - self.bar_size - 3
-- Calculate height of everything.
self.sum_item_height = (self.item_height+1) * self.items.n + 2
end
function List:hasBar()
return self.sum_item_height > self.height
end
function List:getBarRatio()
return self.bar_pos/self.bar_max_pos
end
function List:getOffset()
local ratio = self.bar_pos/self.bar_max_pos
return math.floor((self.sum_item_height-self.height)*ratio + 0.5)
end
function List:update(dt)
if self.bar_lock then
local dy = math.floor(love.mouse.getY()-self.bar_lock.y+0.5)
self.bar_pos = self.bar_pos + dy
if self.bar_pos < 0 then
self.bar_pos = 0
elseif self.bar_pos > self.bar_max_pos then
self.bar_pos = self.bar_max_pos
end
self.bar_lock.y = love.mouse.getY()
end
end
function List:mousepressed(mx, my, b)
if self:hasBar() then
if b == "l" then
local x, y, w, h = self:getBarRect()
if inside(mx, my, x, y, w, h) then
self.bar_lock = { x = mx, y = my }
end
end
local per_pixel = (self.sum_item_height-self.height)/self.bar_max_pos
local bar_pixel_dt = math.floor(((self.item_height)*3)/per_pixel + 0.5)
if b == "wd" then
self.bar_pos = self.bar_pos + bar_pixel_dt
if self.bar_pos > self.bar_max_pos then self.bar_pos = self.bar_max_pos end
elseif b == "wu" then
self.bar_pos = self.bar_pos - bar_pixel_dt
if self.bar_pos < 0 then self.bar_pos = 0 end
end
end
if b == "l" and inside(mx, my, self.x+2, self.y+1, self.width-3, self.height-3) then
local tx, ty = mx-self.x, my + self:getOffset() - self.y
local index = math.floor((ty/self.sum_item_height)*self.items.n)
local f = self.items[index+1]
if f then
exf.start(f)
end
end
end
function List:mousereleased(x, y, b)
if self:hasBar() then
if b == "l" then
self.bar_lock = nil
end
end
end
function List:getBarRect()
return
self.x+self.width+2, self.y+1+self.bar_pos,
self.bar_width-3, self.bar_size
end
function List:getItemRect(i)
return
self.x+2, self.y+((self.item_height+1)*(i-1)+1)-self:getOffset(),
self.width-3, self.item_height
end
function List:draw()
love.graphics.setLine(1, "rough")
love.graphics.setFont(self.font)
love.graphics.setColor(48, 156, 225)
local mx, my = love.mouse.getPosition()
-- Get interval to display.
local start_i = math.floor( self:getOffset()/(self.item_height+1) ) + 1
local end_i = start_i+math.floor( self.height/(self.item_height+1) ) + 1
if end_i > self.items.n then end_i = self.items.n end
love.graphics.setScissor(self.x, self.y, self.width, self.height)
-- Items.
for i = start_i,end_i do
local x, y, w, h = self:getItemRect(i)
local hover = inside(mx, my, x, y, w, h)
if hover then
love.graphics.setColor(72, 131, 168)
else
love.graphics.setColor(48, 156, 225)
end
love.graphics.rectangle("fill", x, y, w, h)
if hover then
love.graphics.setColor(255, 255, 255)
else
love.graphics.setColor(197, 232, 255)
end
local e_id = string.sub(self.items[i], 1, 5)
local e_rest = string.sub(self.items[i], 5)
love.graphics.print(e_id, x+10, y+2) --Updated y placement -- Used to change position of Example IDs
love.graphics.print(e_rest, x+50, y+2) --Updated y placement -- Used to change position of Example Titles
end
love.graphics.setScissor()
-- Bar.
if self:hasBar() then
local x, y, w, h = self:getBarRect()
local hover = inside(mx, my, x, y, w, h)
if hover or self.bar_lock then
love.graphics.setColor(72, 131, 168)
else
love.graphics.setColor(48, 156, 225)
end
love.graphics.rectangle("fill", x, y, w, h)
end
-- Border.
love.graphics.setColor(72, 131, 168)
love.graphics.rectangle("line", self.x+self.width, self.y, self.bar_width, self.height)
love.graphics.rectangle("line", self.x, self.y, self.width, self.height)
end