----------------------------------------------------------------
--
-- To add hotkeys to an object (such as a Brick Oven), you will
-- be adding a function 'lookhotkeys' to the appropriate class,
-- like this:
--
-- function brickoven.lookhotkeys(add)
--    add("X", "Light Oven", function () ... end)
--    add("A", "Add Fuel", function () ... end)
-- end
--
-- This function will get called twice: once in a 'probe' when
-- Unreal wants to know what hotkeys exist, and a second time in
-- an 'invoke' to find the right closure.  The 'add' function
-- which is passed in can therefore be one of two functions.
--
-- In probe mode, the goal is to build up an array that looks
-- like this:
--
--   {"X", "Light Oven", "A", "Add Fuel"}
--
-- So therefore, in probe mode, the 'add' function is a closure
-- that adds the hotkey and the action to the array.
--
-- In invoke mode, the goal is to find the right closure.  So
-- therefore, in invoke mode, the 'add' function is a closure
-- that compares the action to the button which was already
-- pressed.  If there's a match, it records the closure.
--
----------------------------------------------------------------

makeclass('probe')
makeclass('invoke')

function probe.gethotkeys()
    local class = tangible.getclass(place)

    -- if the tangible doesn't have a 'lookhotkeys' function, do nothing
    if class == nil or class.lookhotkeys == nil then
        return {}
    end

    local keys = {}
    local add = function(key, action, closure)
        table.insert(keys, key)
        table.insert(keys, action)
    end
    class.lookhotkeys(add)
    return keys
end

function invoke.presshotkey(pressed)
    local class = tangible.getclass(place)

    -- if the tangible doesn't have a 'lookhotkeys' function, do nothing
    if class == nil or class.lookhotkeys == nil then
        return
    end

    local result = nil
    local add = function(key, action, closure)
        if (action == pressed) then
            result = closure
        end
    end
    class.lookhotkeys(add)
    if result ~= nil then
        result()
    end
end

