Files
integration/luprex/lua/hotkeys.lua

71 lines
2.0 KiB
Lua
Raw Normal View History

----------------------------------------------------------------
--
-- To add hotkeys to an object (such as a Brick Oven), you will
-- be adding a function 'lookhotkeys' to the appropriate class,
-- like this:
--
2026-04-13 16:08:23 -04:00
-- 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
2026-04-13 16:08:23 -04:00
-- an 'invoke' to find the right closure. The 'add' function
-- which is passed in can therefore be one of two functions.
--
2026-04-13 16:08:23 -04:00
-- In probe mode, the goal is to build up an array that looks
-- like this:
--
2026-04-13 16:08:23 -04:00
-- {"X", "Light Oven", "A", "Add Fuel"}
--
2026-04-13 16:08:23 -04:00
-- So therefore, in probe mode, the 'add' function is a closure
-- that adds the hotkey and the action to the array.
--
2026-04-13 16:08:23 -04:00
-- 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.
--
----------------------------------------------------------------
2026-04-13 16:08:23 -04:00
makeclass('engio')
2026-04-13 16:08:23 -04:00
function engio.gethotkeys()
local class = tangible.getclass(place)
2026-04-13 16:08:23 -04:00
-- if the tangible doesn't have a 'lookhotkeys' function, do nothing
if class == nil or class.lookhotkeys == nil then
return {}
end
2026-04-13 16:08:23 -04:00
local keys = {}
local add = function(key, action, closure)
table.insert(keys, key)
table.insert(keys, action)
end
class.lookhotkeys(add)
return keys
end
2026-04-13 16:08:23 -04:00
function engio.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
2026-04-13 16:08:23 -04:00