Compare commits

..

2 Commits

Author SHA1 Message Date
7a48a54ae5 Overhaul code to press hotkeys 2026-04-13 16:35:53 -04:00
f3d9a903d2 Fixed hotkey up/down handling 2026-04-13 16:08:23 -04:00
5 changed files with 60 additions and 98 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@@ -4,77 +4,67 @@
-- be adding a function 'lookhotkeys' to the appropriate class, -- be adding a function 'lookhotkeys' to the appropriate class,
-- like this: -- like this:
-- --
-- function brickoven.lookhotkeys(keys) -- function brickoven.lookhotkeys(add)
-- keys:add("X", "Light Oven", function () ... end) -- add("X", "Light Oven", function () ... end)
-- keys:add("A", "Add Fuel", function () ... end) -- add("A", "Add Fuel", function () ... end)
-- end -- end
-- --
-- This function will get called twice: once in a 'probe' when -- This function will get called twice: once in a 'probe' when
-- Unreal wants to know what hotkeys exist, and a second time in -- 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 -- In probe mode, the goal is to build up an array that looks
-- the complete list of hotkeys. We populate this hotkeylist as -- like this:
-- follows:
-- --
-- local keys = hotkeylist.create() -- {"X", "Light Oven", "A", "Add Fuel"}
-- brickoven.lookhotkeys(keys)
--
-- At this point, if you were to pprint(keys), the output would
-- look 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.
-- --
-- Notice that a hotkeylist doesn't store the closures, they are -- In invoke mode, the goal is to find the right closure. So
-- silently ignored. The resulting array is in a format that can -- therefore, in invoke mode, the 'add' function is a closure
-- be returned directly to Unreal. -- that compares the action to the button which was already
-- -- pressed. If there's a match, it records the closure.
-- 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.
-- --
---------------------------------------------------------------- ----------------------------------------------------------------
makeclass("hotkeylist") makeclass('engio')
makeclass("hotkeypress")
function hotkeylist.create() function engio.gethotkeys()
local result = {} local class = tangible.getclass(place)
setmetatable(result, hotkeylist)
return result -- 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 end
function hotkeylist.add(self, key, action, closure) function engio.presshotkey(pressed)
table.insert(self, key) local class = tangible.getclass(place)
table.insert(self, action)
end
function hotkeypress.create(key) -- if the tangible doesn't have a 'lookhotkeys' function, do nothing
local result = { pressed=key } if class == nil or class.lookhotkeys == nil then
setmetatable(result, hotkeypress) return
return result end
end
function hotkeypress.add(self, key, action, closure) local result = nil
if action == self.pressed then local add = function(key, action, closure)
self.closure = closure if (action == pressed) then
result = closure
end
end
class.lookhotkeys(add)
if result ~= nil then
result()
end end
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}} tangible.animate{tan=actor, anim={action="moveto", xyz={x, y, z}, facing=math.auto}}
end end
function cube.lookhotkeys(keys) function cube.lookhotkeys(add)
keys:add("Z", "Cube Hi", function () dprint("Doing Cube Hi") end) add("Z", "Cube Hi", function () dprint("Doing Cube Hi") end)
keys:add("X", "Cube Bye", function () dprint("Doing Cube Bye") end) add("X", "Cube Bye", function () dprint("Doing Cube Bye") end)
keys:add("C", "Cube Yo", function () dprint("Doing Cube Yo") end) add("C", "Cube Yo", function () dprint("Doing Cube Yo") end)
end end
function sphere.lookhotkeys(keys) function sphere.lookhotkeys(add)
keys:add("Z", "Sphere Hi", function () dprint("Doing Sphere Hi") end) add("Z", "Sphere Hi", function () dprint("Doing Sphere Hi") end)
keys:add("X", "Sphere Bye", function () dprint("Doing Sphere Bye") end) add("X", "Sphere Bye", function () dprint("Doing Sphere Bye") end)
keys:add("C", "Sphere Yo", function () dprint("Doing Sphere Yo") end) add("C", "Sphere Yo", function () dprint("Doing Sphere Yo") end)
end end
@@ -69,35 +69,7 @@ function engio.getlookat()
return "" return ""
end 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() function jp3()
tangible.animate{tan=actor, anim={action="play", seq="jump"}} tangible.animate{tan=actor, anim={action="play", seq="jump"}}