Created
March 29, 2024 23:12
-
-
Save trentgill/3ec184e4c7aff5b5d1c7e1a42c6f566e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ag = | |
{grid = {} | |
,fns = {}} | |
function ag.init(tab_or_g, fns) | |
if type(tab_or_g) == 'table' then | |
-- TODO for version with switchable layers | |
ag.grid = tab_or_g.grid | |
ag.fns = tab_or_g.fns | |
else -- assume grid obj | |
ag.grid = tab_or_g | |
ag.fns = fns -- save user provided table of actions | |
end | |
-- hook autogrid up to grid interface | |
ag.grid.key = ag.key | |
end | |
local function apply(f, ...) | |
return f(...) | |
end | |
local function maybe_fn_table(obj,...) | |
-- false signals the obj doesn't exist | |
if obj == nil then return false end | |
local typ = type(obj) | |
if typ == 'function' then | |
obj(...) | |
elseif typ == 'table' then | |
apply(unpack(obj),...) | |
end | |
-- true signals this mapping was eval'd | |
return true | |
end | |
function ag.key(x,y,z) | |
if maybe_fn_table(ag.fns[x .. ',' .. y],z) then return -- individual key | |
elseif maybe_fn_table(ag.fns['x' .. x],y,z) then return -- column | |
elseif maybe_fn_table(ag.fns['y' .. y],x,z) then return -- row | |
end | |
end | |
return ag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment