Add util.ipairs_rev function
This commit is contained in:
parent
c055f9d548
commit
a340c8f8b0
3 changed files with 23 additions and 3 deletions
2
main.lua
2
main.lua
|
@ -41,7 +41,7 @@ inventory.draw = window.draw(function (self)
|
|||
end)
|
||||
|
||||
function inventory:pickup(item)
|
||||
self.items[item.name] = item
|
||||
table.insert(self.items, item)
|
||||
end
|
||||
|
||||
log = {
|
||||
|
|
14
util.lua
14
util.lua
|
@ -1,5 +1,17 @@
|
|||
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)
|
||||
return a <= x and x <= b
|
||||
end
|
||||
|
@ -8,3 +20,5 @@ function m.inside(px, py, x, y, w, h)
|
|||
return m.between(px, x, x+w) and
|
||||
m.between(py, y, y+h)
|
||||
end
|
||||
|
||||
return m
|
||||
|
|
10
window.lua
10
window.lua
|
@ -1,3 +1,4 @@
|
|||
local util = require('util')
|
||||
-- function generator for window functions
|
||||
|
||||
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
|
||||
f(self, x, y, button)
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -26,8 +30,10 @@ function w.propagate(tnames)
|
|||
local x, y = x - self.x, y - self.y
|
||||
|
||||
for _, tname in ipairs(tnames) do
|
||||
for i, v in pairs(self[tname]) do
|
||||
v:mousepressed(x, y, button)
|
||||
for i, v in util.ipairs_rev(self[tname]) do
|
||||
if v:mousepressed(x, y, button) then
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Reference in a new issue