Add util.ipairs_rev function

This commit is contained in:
백현웅 2025-01-03 19:28:56 +09:00
parent c055f9d548
commit a340c8f8b0
3 changed files with 23 additions and 3 deletions

View file

@ -41,7 +41,7 @@ inventory.draw = window.draw(function (self)
end) end)
function inventory:pickup(item) function inventory:pickup(item)
self.items[item.name] = item table.insert(self.items, item)
end end
log = { log = {

View file

@ -1,5 +1,17 @@
local m = {} local m = {}
local function rext(a, i)
i = i - 1
local v = a[i]
if v then
return i, v
end
end
function m.ipairs_rev(a)
return rext, a, (#a + 1)
end
function m.between(x, a, b) function m.between(x, a, b)
return a <= x and x <= b return a <= x and x <= b
end end
@ -8,3 +20,5 @@ function m.inside(px, py, x, y, w, h)
return m.between(px, x, x+w) and return m.between(px, x, x+w) and
m.between(py, y, y+h) m.between(py, y, y+h)
end end
return m

View file

@ -1,3 +1,4 @@
local util = require('util')
-- function generator for window functions -- function generator for window functions
local w = {} local w = {}
@ -17,7 +18,10 @@ function w.mousepressed(f)
if 0 <= x and x <= self.width and 0 <= y and y <= self.height then if 0 <= x and x <= self.width and 0 <= y and y <= self.height then
f(self, x, y, button) f(self, x, y, button)
return true
end end
return false
end end
end end
@ -26,8 +30,10 @@ function w.propagate(tnames)
local x, y = x - self.x, y - self.y local x, y = x - self.x, y - self.y
for _, tname in ipairs(tnames) do for _, tname in ipairs(tnames) do
for i, v in pairs(self[tname]) do for i, v in util.ipairs_rev(self[tname]) do
v:mousepressed(x, y, button) if v:mousepressed(x, y, button) then
return
end
end end
end end
end end