garden/window.lua
2025-01-14 19:32:59 +09:00

59 lines
1.3 KiB
Lua

local util = require('util')
-- function generator for window functions
local w = {}
function w.draw(f)
return function (self)
love.graphics.push('all')
love.graphics.translate(self.x, self.y)
f(self)
love.graphics.pop()
end
end
function w.mousepressed(f)
return function (self, x, y, button)
local x, y = x - self.x, y - self.y
if 0 <= x and x <= self.width and 0 <= y and y <= self.height then
f(self, x, y, button)
return true
end
return false
end
end
function w.iterate(t, x, y, button)
for i, v in util.ipairs_rev(t) do
if v:mousepressed(x, y, button) then
return true
end
end
end
function w.propagate(tnames)
return function (self, x, y, button)
local x, y = x - self.x, y - self.y
for _, tname in ipairs(tnames) do
w.iterate(self[tname], x, y, button)
end
end
end
function w.portal(room)
return w.mousepressed(function (self, x, y, button)
set_room_raw(self[room])
end)
end
function w.draw_border(width, height)
love.graphics.push('all')
love.graphics.setLineWidth(5)
love.graphics.rectangle('line', 0, 0, width, height, 5)
love.graphics.pop()
end
return w