mirror of
https://github.com/tanema/light_world.lua.git
synced 2024-12-11 01:14:26 +00:00
just some reformatting
This commit is contained in:
parent
1c5eb2c804
commit
c370e9ecc2
2
conf.lua
2
conf.lua
@ -32,4 +32,4 @@ function love.conf(t)
|
|||||||
t.modules.system = true -- Enable the system module (boolean)
|
t.modules.system = true -- Enable the system module (boolean)
|
||||||
t.modules.timer = true -- Enable the timer module (boolean)
|
t.modules.timer = true -- Enable the timer module (boolean)
|
||||||
t.modules.window = true -- Enable the window module (boolean)
|
t.modules.window = true -- Enable the window module (boolean)
|
||||||
end
|
end
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
extern Image glowImage;
|
extern Image glowImage;
|
||||||
|
|
||||||
extern float glowTime;
|
extern float glowTime;
|
||||||
|
|
||||||
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
|
vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
|
||||||
@ -17,4 +16,4 @@ vec4 effect(vec4 color, Image texture, vec2 texture_coords, vec2 pixel_coords) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return vec4(Texel(texture, texture_coords).rgb * glowInfo.r, 1.0);
|
return vec4(Texel(texture, texture_coords).rgb * glowInfo.r, 1.0);
|
||||||
}
|
}
|
||||||
|
346
main.lua
346
main.lua
@ -15,32 +15,32 @@ exf.current = nil
|
|||||||
exf.available = {}
|
exf.available = {}
|
||||||
|
|
||||||
function love.load()
|
function love.load()
|
||||||
exf.list = List:new()
|
exf.list = List:new()
|
||||||
exf.smallfont = love.graphics.newFont(love._vera_ttf,12)
|
exf.smallfont = love.graphics.newFont(love._vera_ttf,12)
|
||||||
exf.bigfont = love.graphics.newFont(love._vera_ttf, 24)
|
exf.bigfont = love.graphics.newFont(love._vera_ttf, 24)
|
||||||
exf.list.font = exf.smallfont
|
exf.list.font = exf.smallfont
|
||||||
|
|
||||||
exf.bigball = love.graphics.newImage("gfx/love-big-ball.png")
|
exf.bigball = love.graphics.newImage("gfx/love-big-ball.png")
|
||||||
|
|
||||||
-- Find available demos.
|
-- Find available demos.
|
||||||
local files = love.filesystem.getDirectoryItems("examples")
|
local files = love.filesystem.getDirectoryItems("examples")
|
||||||
local n = 0
|
local n = 0
|
||||||
|
|
||||||
for i, v in ipairs(files) do
|
for i, v in ipairs(files) do
|
||||||
n = n + 1
|
n = n + 1
|
||||||
table.insert(exf.available, v);
|
table.insert(exf.available, v);
|
||||||
local file = love.filesystem.newFile(v, love.file_read)
|
local file = love.filesystem.newFile(v, love.file_read)
|
||||||
file:open("r")
|
file:open("r")
|
||||||
local contents = love.filesystem.read("examples/" .. v, 100)
|
local contents = love.filesystem.read("examples/" .. v, 100)
|
||||||
local s, e, c = string.find(contents, "Example: ([%a%p ]-)[\r\n]")
|
local s, e, c = string.find(contents, "Example: ([%a%p ]-)[\r\n]")
|
||||||
file:close(file)
|
file:close(file)
|
||||||
if not c then c = "Untitled" end
|
if not c then c = "Untitled" end
|
||||||
local title = exf.getn(n) .. " " .. c .. " (" .. v .. ")"
|
local title = exf.getn(n) .. " " .. c .. " (" .. v .. ")"
|
||||||
exf.list:add(title, v)
|
exf.list:add(title, v)
|
||||||
end
|
end
|
||||||
|
|
||||||
exf.list:done()
|
exf.list:done()
|
||||||
exf.resume()
|
exf.resume()
|
||||||
end
|
end
|
||||||
|
|
||||||
function love.update(dt) end
|
function love.update(dt) end
|
||||||
@ -49,7 +49,6 @@ function love.keypressed(k) end
|
|||||||
function love.keyreleased(k) end
|
function love.keyreleased(k) end
|
||||||
function love.mousepressed(x, y, b) end
|
function love.mousepressed(x, y, b) end
|
||||||
function love.mousereleased(x, y, b) end
|
function love.mousereleased(x, y, b) end
|
||||||
|
|
||||||
function exf.empty() end
|
function exf.empty() end
|
||||||
|
|
||||||
function exf.update(dt)
|
function exf.update(dt)
|
||||||
@ -89,32 +88,32 @@ function exf.keyreleased(k)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function exf.mousepressed(x, y, b)
|
function exf.mousepressed(x, y, b)
|
||||||
exf.list:mousepressed(x, y, b)
|
exf.list:mousepressed(x, y, b)
|
||||||
end
|
end
|
||||||
|
|
||||||
function exf.mousereleased(x, y, b)
|
function exf.mousereleased(x, y, b)
|
||||||
exf.list:mousereleased(x, y, b)
|
exf.list:mousereleased(x, y, b)
|
||||||
end
|
end
|
||||||
|
|
||||||
function exf.getn(n)
|
function exf.getn(n)
|
||||||
local s = ""
|
local s = ""
|
||||||
n = tonumber(n)
|
n = tonumber(n)
|
||||||
local r = n
|
local r = n
|
||||||
if r <= 0 then error("Example IDs must be bigger than 0. (Got: " .. r .. ")") end
|
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
|
if r >= 10000 then error("Example IDs must be less than 10000. (Got: " .. r .. ")") end
|
||||||
while r < 1000 do
|
while r < 1000 do
|
||||||
s = s .. "0"
|
s = s .. "0"
|
||||||
r = r * 10
|
r = r * 10
|
||||||
end
|
end
|
||||||
s = s .. n
|
s = s .. n
|
||||||
return s
|
return s
|
||||||
end
|
end
|
||||||
|
|
||||||
function exf.intable(t, e)
|
function exf.intable(t, e)
|
||||||
for k, v in ipairs(t) do
|
for k, v in ipairs(t) do
|
||||||
if v == e then return true end
|
if v == e then return true end
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
function exf.start(item, file)
|
function exf.start(item, file)
|
||||||
@ -159,27 +158,25 @@ function exf.start(item, file)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function exf.clear()
|
function exf.clear()
|
||||||
love.graphics.setBackgroundColor(0,0,0)
|
love.graphics.setBackgroundColor(0,0,0)
|
||||||
love.graphics.setColor(255, 255, 255)
|
love.graphics.setColor(255, 255, 255)
|
||||||
love.graphics.setLineWidth(1)
|
love.graphics.setLineWidth(1)
|
||||||
love.graphics.setLineStyle("smooth")
|
love.graphics.setLineStyle("smooth")
|
||||||
--love.graphics.setLine(1, "smooth")
|
love.graphics.setBlendMode("alpha")
|
||||||
--love.graphics.setColorMode("replace")
|
love.mouse.setVisible(true)
|
||||||
love.graphics.setBlendMode("alpha")
|
|
||||||
love.mouse.setVisible(true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function exf.resume()
|
function exf.resume()
|
||||||
load = nil
|
load = nil
|
||||||
love.update = exf.update
|
love.update = exf.update
|
||||||
love.draw = exf.draw
|
love.draw = exf.draw
|
||||||
love.keypressed = exf.keypressed
|
love.keypressed = exf.keypressed
|
||||||
love.keyreleased = exf.keyreleased
|
love.keyreleased = exf.keyreleased
|
||||||
love.mousepressed = exf.mousepressed
|
love.mousepressed = exf.mousepressed
|
||||||
love.mousereleased = exf.mousereleased
|
love.mousereleased = exf.mousereleased
|
||||||
|
|
||||||
love.mouse.setVisible(true)
|
love.mouse.setVisible(true)
|
||||||
love.window.setTitle("LOVE Example Browser")
|
love.window.setTitle("LOVE Example Browser")
|
||||||
|
|
||||||
-- create light world
|
-- create light world
|
||||||
lightWorld = LightWorld({
|
lightWorld = LightWorld({
|
||||||
@ -197,7 +194,7 @@ function exf.resume()
|
|||||||
end
|
end
|
||||||
|
|
||||||
function inside(mx, my, x, y, w, h)
|
function inside(mx, my, x, y, w, h)
|
||||||
return mx >= x and mx <= (x+w) and my >= y and my <= (y+h)
|
return mx >= x and mx <= (x+w) and my >= y and my <= (y+h)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -208,29 +205,29 @@ end
|
|||||||
List = {}
|
List = {}
|
||||||
|
|
||||||
function List:new()
|
function List:new()
|
||||||
o = {}
|
o = {}
|
||||||
setmetatable(o, self)
|
setmetatable(o, self)
|
||||||
self.__index = self
|
self.__index = self
|
||||||
|
|
||||||
o.items = {}
|
o.items = {}
|
||||||
o.files = {}
|
o.files = {}
|
||||||
|
|
||||||
o.x = 50
|
o.x = 50
|
||||||
o.y = 70
|
o.y = 70
|
||||||
|
|
||||||
o.width = 400
|
o.width = 400
|
||||||
o.height = 500
|
o.height = 500
|
||||||
|
|
||||||
o.item_height = 23
|
o.item_height = 23
|
||||||
o.sum_item_height = 0
|
o.sum_item_height = 0
|
||||||
|
|
||||||
o.bar_size = 20
|
o.bar_size = 20
|
||||||
o.bar_pos = 0
|
o.bar_pos = 0
|
||||||
o.bar_max_pos = 0
|
o.bar_max_pos = 0
|
||||||
o.bar_width = 15
|
o.bar_width = 15
|
||||||
o.bar_lock = nil
|
o.bar_lock = nil
|
||||||
|
|
||||||
return o
|
return o
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:add(item, file)
|
function List:add(item, file)
|
||||||
@ -239,161 +236,164 @@ function List:add(item, file)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function List:done()
|
function List:done()
|
||||||
self.items.n = #self.items
|
self.items.n = #self.items
|
||||||
|
|
||||||
-- Recalc bar size.
|
-- Recalc bar size.
|
||||||
self.bar_pos = 0
|
self.bar_pos = 0
|
||||||
|
|
||||||
local num_items = (self.height/self.item_height)
|
local num_items = (self.height/self.item_height)
|
||||||
local ratio = num_items/self.items.n
|
local ratio = num_items/self.items.n
|
||||||
self.bar_size = self.height * ratio
|
self.bar_size = self.height * ratio
|
||||||
self.bar_max_pos = self.height - self.bar_size - 3
|
self.bar_max_pos = self.height - self.bar_size - 3
|
||||||
|
|
||||||
-- Calculate height of everything.
|
-- Calculate height of everything.
|
||||||
self.sum_item_height = (self.item_height+1) * self.items.n + 2
|
self.sum_item_height = (self.item_height+1) * self.items.n + 2
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:hasBar()
|
function List:hasBar()
|
||||||
return self.sum_item_height > self.height
|
return self.sum_item_height > self.height
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:getBarRatio()
|
function List:getBarRatio()
|
||||||
return self.bar_pos/self.bar_max_pos
|
return self.bar_pos/self.bar_max_pos
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:getOffset()
|
function List:getOffset()
|
||||||
local ratio = self.bar_pos/self.bar_max_pos
|
local ratio = self.bar_pos/self.bar_max_pos
|
||||||
return math.floor((self.sum_item_height-self.height)*ratio + 0.5)
|
return math.floor((self.sum_item_height-self.height)*ratio + 0.5)
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:update(dt)
|
function List:update(dt)
|
||||||
if self.bar_lock then
|
if self.bar_lock then
|
||||||
local dy = math.floor(love.mouse.getY()-self.bar_lock.y+0.5)
|
local dy = math.floor(love.mouse.getY()-self.bar_lock.y+0.5)
|
||||||
self.bar_pos = self.bar_pos + dy
|
self.bar_pos = self.bar_pos + dy
|
||||||
|
|
||||||
if self.bar_pos < 0 then
|
if self.bar_pos < 0 then
|
||||||
self.bar_pos = 0
|
self.bar_pos = 0
|
||||||
elseif self.bar_pos > self.bar_max_pos then
|
elseif self.bar_pos > self.bar_max_pos then
|
||||||
self.bar_pos = self.bar_max_pos
|
self.bar_pos = self.bar_max_pos
|
||||||
end
|
end
|
||||||
|
|
||||||
self.bar_lock.y = love.mouse.getY()
|
self.bar_lock.y = love.mouse.getY()
|
||||||
|
end
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:mousepressed(mx, my, b)
|
function List:mousepressed(mx, my, b)
|
||||||
if self:hasBar() then
|
if self:hasBar() then
|
||||||
if b == "l" then
|
if b == "l" then
|
||||||
local x, y, w, h = self:getBarRect()
|
local x, y, w, h = self:getBarRect()
|
||||||
if inside(mx, my, x, y, w, h) then
|
if inside(mx, my, x, y, w, h) then
|
||||||
self.bar_lock = { x = mx, y = my }
|
self.bar_lock = { x = mx, y = my }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local per_pixel = (self.sum_item_height-self.height)/self.bar_max_pos
|
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)
|
local bar_pixel_dt = math.floor(((self.item_height)*3)/per_pixel + 0.5)
|
||||||
|
|
||||||
if b == "wd" then
|
if b == "wd" then
|
||||||
self.bar_pos = self.bar_pos + bar_pixel_dt
|
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
|
if self.bar_pos > self.bar_max_pos then
|
||||||
elseif b == "wu" then
|
self.bar_pos = self.bar_max_pos
|
||||||
|
end
|
||||||
|
elseif b == "wu" then
|
||||||
self.bar_pos = self.bar_pos - bar_pixel_dt
|
self.bar_pos = self.bar_pos - bar_pixel_dt
|
||||||
if self.bar_pos < 0 then self.bar_pos = 0 end
|
if self.bar_pos < 0 then
|
||||||
end
|
self.bar_pos = 0
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if b == "l" and inside(mx, my, self.x+2, self.y+1, self.width-3, self.height-3) then
|
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 tx, ty = mx-self.x, my + self:getOffset() - self.y
|
||||||
local index = math.floor((ty/self.sum_item_height)*self.items.n)
|
local index = math.floor((ty/self.sum_item_height)*self.items.n)
|
||||||
local i = self.items[index+1]
|
local i = self.items[index+1]
|
||||||
local f = self.files[index+1]
|
local f = self.files[index+1]
|
||||||
if f then
|
if f then
|
||||||
exf.start(i, f)
|
exf.start(i, f)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:mousereleased(x, y, b)
|
function List:mousereleased(x, y, b)
|
||||||
if self:hasBar() then
|
if self:hasBar() then
|
||||||
if b == "l" then
|
if b == "l" then
|
||||||
self.bar_lock = nil
|
self.bar_lock = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:getBarRect()
|
function List:getBarRect()
|
||||||
return
|
return self.x+self.width+2, self.y+1+self.bar_pos,
|
||||||
self.x+self.width+2, self.y+1+self.bar_pos,
|
self.bar_width-3, self.bar_size
|
||||||
self.bar_width-3, self.bar_size
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:getItemRect(i)
|
function List:getItemRect(i)
|
||||||
return
|
return self.x+2, self.y+((self.item_height+1)*(i-1)+1)-self:getOffset(),
|
||||||
self.x+2, self.y+((self.item_height+1)*(i-1)+1)-self:getOffset(),
|
self.width-3, self.item_height
|
||||||
self.width-3, self.item_height
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function List:draw()
|
function List:draw()
|
||||||
love.graphics.setLineWidth(2)
|
love.graphics.setLineWidth(2)
|
||||||
love.graphics.setLineStyle("rough")
|
love.graphics.setLineStyle("rough")
|
||||||
love.graphics.setFont(self.font)
|
love.graphics.setFont(self.font)
|
||||||
|
|
||||||
love.graphics.setColor(48, 156, 225)
|
love.graphics.setColor(48, 156, 225)
|
||||||
|
|
||||||
local mx, my = love.mouse.getPosition()
|
local mx, my = love.mouse.getPosition()
|
||||||
|
|
||||||
-- Get interval to display.
|
-- Get interval to display.
|
||||||
local start_i = math.floor( self:getOffset()/(self.item_height+1) ) + 1
|
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
|
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
|
|
||||||
|
if end_i > self.items.n then
|
||||||
|
end_i = self.items.n
|
||||||
|
end
|
||||||
|
|
||||||
|
love.graphics.setScissor(self.x, self.y, self.width, self.height)
|
||||||
|
|
||||||
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)
|
||||||
|
|
||||||
-- Items.
|
if hover then
|
||||||
for i = start_i,end_i do
|
love.graphics.setColor(0, 0, 0, 127)
|
||||||
local x, y, w, h = self:getItemRect(i)
|
else
|
||||||
local hover = inside(mx, my, x, y, w, h)
|
love.graphics.setColor(0, 0, 0, 63)
|
||||||
|
|
||||||
if hover then
|
|
||||||
love.graphics.setColor(0, 0, 0, 127)
|
|
||||||
else
|
|
||||||
love.graphics.setColor(0, 0, 0, 63)
|
|
||||||
end
|
|
||||||
|
|
||||||
love.graphics.rectangle("fill", x+1, y+i+1, w-3, h)
|
|
||||||
|
|
||||||
if hover then
|
|
||||||
love.graphics.setColor(255, 255, 255)
|
|
||||||
else
|
|
||||||
love.graphics.setColor(255, 255, 255, 127)
|
|
||||||
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+i+6) --Updated y placement -- Used to change position of Example IDs
|
|
||||||
love.graphics.print(e_rest, x+50, y+i+6) --Updated y placement -- Used to change position of Example Titles
|
|
||||||
end
|
end
|
||||||
|
|
||||||
love.graphics.setScissor()
|
love.graphics.rectangle("fill", x+1, y+i+1, w-3, h)
|
||||||
|
|
||||||
-- Bar.
|
if hover then
|
||||||
if self:hasBar() then
|
love.graphics.setColor(255, 255, 255)
|
||||||
local x, y, w, h = self:getBarRect()
|
else
|
||||||
local hover = inside(mx, my, x, y, w, h)
|
love.graphics.setColor(255, 255, 255, 127)
|
||||||
|
|
||||||
if hover or self.bar_lock then
|
|
||||||
love.graphics.setColor(0, 0, 0, 127)
|
|
||||||
else
|
|
||||||
love.graphics.setColor(0, 0, 0, 63)
|
|
||||||
end
|
|
||||||
love.graphics.rectangle("fill", x, y, w, h)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Border.
|
local e_id = string.sub(self.items[i], 1, 5)
|
||||||
love.graphics.setColor(0, 0, 0, 63)
|
local e_rest = string.sub(self.items[i], 5)
|
||||||
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)
|
love.graphics.print(e_id, x+10, y+i+6) --Updated y placement -- Used to change position of Example IDs
|
||||||
|
love.graphics.print(e_rest, x+50, y+i+6) --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(0, 0, 0, 127)
|
||||||
|
else
|
||||||
|
love.graphics.setColor(0, 0, 0, 63)
|
||||||
|
end
|
||||||
|
love.graphics.rectangle("fill", x, y, w, h)
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Border.
|
||||||
|
love.graphics.setColor(0, 0, 0, 63)
|
||||||
|
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user