Fixed hotkey up/down handling

This commit is contained in:
2026-04-13 16:08:23 -04:00
parent 85a6fad139
commit f3d9a903d2
4 changed files with 58 additions and 96 deletions

View File

@@ -4,77 +4,67 @@
-- be adding a function 'lookhotkeys' to the appropriate class,
-- like this:
--
-- function brickoven.lookhotkeys(keys)
-- keys:add("X", "Light Oven", function () ... end)
-- keys:add("A", "Add Fuel", function () ... end)
-- 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.
-- an 'invoke' to find the right closure. The 'add' function
-- which is passed in can therefore be one of two functions.
--
-- When the probe happens, we construct a "hotkeylist" to hold
-- the complete list of hotkeys. We populate this hotkeylist as
-- follows:
-- In probe mode, the goal is to build up an array that looks
-- like this:
--
-- local keys = hotkeylist.create()
-- brickoven.lookhotkeys(keys)
--
-- At this point, if you were to pprint(keys), the output would
-- look like this:
-- {"X", "Light Oven", "A", "Add Fuel"}
--
-- { "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.
--
-- Notice that a hotkeylist doesn't store the closures, they are
-- silently ignored. The resulting array is in a format that can
-- be returned directly to Unreal.
--
-- Later, when the user presses a key, such as the "Light Oven" key,
-- we will construct a "hotkeypress" object whose job is to record
-- the name of the hotkey that was pressed, and the closure that
-- goes with it. We populate this object as follows:
--
-- local keys = hotkeypress.create("Light Oven")
-- brickoven.lookhotkeys(keys)
--
-- So as you can see, we're using the same 'brickoven.lookhotkeys'
-- function, but this time, we've passed in a parameter of class
-- hotkeypress instead of a parameter of class hotkeylist. This
-- changes the behavior of the 'add' method: the add function of
-- a hotkeylist stores all the hotkeys into the list. But the 'add'
-- function of a hotkeypress only stores the one closure that
-- we are interested in. If you were to pprint(keys), the result
-- would look like this:
--
-- { pressed="Light Oven", closure=<function> }
--
-- So the only data stored by a 'hotkeypress' object is the name
-- of the pressed hotkey, and the closure that goes with it.
-- 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("hotkeylist")
makeclass("hotkeypress")
makeclass('engio')
function hotkeylist.create()
local result = {}
setmetatable(result, hotkeylist)
return result
function engio.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 hotkeylist.add(self, key, action, closure)
table.insert(self, key)
table.insert(self, action)
end
function engio.presshotkey(pressed)
local class = tangible.getclass(place)
function hotkeypress.create(key)
local result = { pressed=key }
setmetatable(result, hotkeypress)
return result
end
-- if the tangible doesn't have a 'lookhotkeys' function, do nothing
if class == nil or class.lookhotkeys == nil then
return
end
function hotkeypress.add(self, key, action, closure)
if action == self.pressed then
self.closure = closure
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

View File

@@ -33,16 +33,16 @@ function moveto(x, y)
tangible.animate{tan=actor, anim={action="moveto", xyz={x, y, z}, facing=math.auto}}
end
function cube.lookhotkeys(keys)
keys:add("Z", "Cube Hi", function () dprint("Doing Cube Hi") end)
keys:add("X", "Cube Bye", function () dprint("Doing Cube Bye") end)
keys:add("C", "Cube Yo", function () dprint("Doing Cube Yo") end)
function cube.lookhotkeys(add)
add("Z", "Cube Hi", function () dprint("Doing Cube Hi") end)
add("X", "Cube Bye", function () dprint("Doing Cube Bye") end)
add("C", "Cube Yo", function () dprint("Doing Cube Yo") end)
end
function sphere.lookhotkeys(keys)
keys:add("Z", "Sphere Hi", function () dprint("Doing Sphere Hi") end)
keys:add("X", "Sphere Bye", function () dprint("Doing Sphere Bye") end)
keys:add("C", "Sphere Yo", function () dprint("Doing Sphere Yo") end)
function sphere.lookhotkeys(add)
add("Z", "Sphere Hi", function () dprint("Doing Sphere Hi") end)
add("X", "Sphere Bye", function () dprint("Doing Sphere Bye") end)
add("C", "Sphere Yo", function () dprint("Doing Sphere Yo") end)
end
@@ -69,35 +69,7 @@ function engio.getlookat()
return ""
end
function engio.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 = hotkeylist.create()
class.lookhotkeys(keys)
setmetatable(keys, nil)
return keys
end
function engio.presshotkey(action)
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 press = hotkeypress.create(action)
class.lookhotkeys(press)
local closure = press.closure
if closure ~= nil then
closure()
end
end
function jp3()
tangible.animate{tan=actor, anim={action="play", seq="jump"}}