forked from tanema/light_world.lua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexf.lua
206 lines (170 loc) · 5.49 KB
/
exf.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
local ProFi = require 'examples.vendor.ProFi'
local List = require 'list'
local mountme = require 'mountme'
local exf = {
current = nil,
available = {},
exdir = 'examples',
}
local function 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
local function intable(t, e)
for k, v in ipairs(t) do
if v == e then return true end
end
return false
end
local function empty() end
function exf:loadExamples(dir)
dir = dir or 'examples'
self.exdir = dir
local files = love.filesystem.getDirectoryItems(dir)
local n = 0
for i, v in ipairs(files) do
local is_file = love.filesystem.isFile(dir.."/".. v )
local is_folder = love.filesystem.isDirectory(dir.."/".. v )
if is_file then
n = n + 1
table.insert(exf.available, v);
local file = love.filesystem.newFile(v, love.file_read)
file:open("r")
local contents = love.filesystem.read(dir .. "/" .. 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 = getn(n) .. " " .. c .. " (" .. v .. ")"
self.list:add(title, v)
elseif is_folder then
if love.filesystem.isFile(dir.."/"..v.."/main.lua") then
local tv = v .. "/main.lua"
n = n + 1
table.insert(self.available, v);
local file = love.filesystem.newFile(dir .. "/" .. tv, love.file_read)
file:open("r")
local contents = love.filesystem.read(dir .. "/" .. tv, 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 = getn(n) .. " " .. c .. " (" .. v .. ")"
self.list:add(title, v)
end
end
end
end
function exf:load()
self.list = List:new()
self.list.start_callback = function(i, f) self:start(i, f) end
self.smallfont = love.graphics.newFont(love._vera_ttf,12)
self.bigfont = love.graphics.newFont(love._vera_ttf, 24)
self.list.font = self.smallfont
self.bigball = love.graphics.newImage("examples/gfx/love-big-ball.png")
self:loadExamples('examples')
self.list:done()
self:resume()
end
function exf:update(dt)
local d = self.list
d:update(dt)
end
function exf:draw()
love.graphics.setBackgroundColor(0, 0, 0)
love.graphics.setColor(48, 156, 225)
love.graphics.rectangle("fill", 0, 0, love.window.getWidth(), love.window.getHeight())
love.graphics.setColor(255, 255, 255, 191)
love.graphics.setFont(self.bigfont)
love.graphics.print("Examples:", 50, 50)
love.graphics.setFont(self.smallfont)
love.graphics.print("Browse and click on the example you \nwant to run. To return the the example \nselection screen, press escape.", 500, 80)
self.list:draw()
love.graphics.setColor(255, 255, 255)
love.graphics.draw(self.bigball, 800 - 128, 600 - 128, love.timer.getTime(), 1, 1, self.bigball:getWidth() * 0.5, self.bigball:getHeight() * 0.5)
end
function exf:keypressed(k)
end
function exf.keyreleased(k)
end
function exf:mousepressed(x, y, b)
self.list:mousepressed(x, y, b)
end
function exf:mousereleased(x, y, b)
self.list:mousereleased(x, y, b)
end
function exf:start(item, file)
local e_id = string.sub(item, 1, 4)
local e_rest = string.sub(item, 5)
local unused1, unused2, n = string.find(item, "(%s)%.lua")
if intable(self.available, file) then
if not love.filesystem.exists(self.exdir .. "/" .. file) then
print("Could not load game .. " .. file)
else
local is_file = love.filesystem.isFile(self.exdir .. "/" .. file)
local is_folder = love.filesystem.isDirectory(self.exdir .. "/" .. file)
-- Clear all callbacks.
love.load = empty
love.update = empty
love.draw = empty
love.keypressed = empty
love.keyreleased = empty
love.mousepressed = empty
love.mousereleased = empty
if is_file then
love.filesystem.load(self.exdir .. "/" .. file)()
elseif is_folder then
mountme:setBase(self.exdir .. "/" .. file)
mountme:wrap()
self.current = self.exdir .. "/" .. file
love.filesystem.load(self.exdir .. "/" .. file .. "/main.lua")()
end
self:clear()
--love.window.setTitle(e_rest)
-- Redirect keypress
local o_keypressed = love.keypressed
love.keypressed = function(k)
if k == "escape" then
self: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.setLineWidth(1)
love.graphics.setLineStyle("smooth")
love.graphics.setBlendMode("alpha")
love.mouse.setVisible(true)
end
function exf:resume()
ProFi:stop()
ProFi:writeReport( 'light_world_profiling_report.txt' )
love.audio.stop()
if self.current ~= nil then
mountme:unwrap()
end
load = nil
love.update = function(dt) self:update(dt) end
love.draw = function() self:draw() end
love.keypressed = function(k) self:keypressed(k) end
love.keyreleased = function(k) self:keyreleased(k) end
love.mousepressed = function(x,y,b) self:mousepressed(x,y,b) end
love.mousereleased = function(x,y,b) self:mousereleased(x,y,b) end
love.mouse.setVisible(true)
love.window.setTitle("LOVE Example Browser")
end
return exf