store callbacks in the traditional minetest way, expand tests

This commit is contained in:
wsor4035 2023-12-03 14:51:55 -05:00
parent 6b32442368
commit af05a85d89
2 changed files with 17 additions and 4 deletions

@ -1,19 +1,25 @@
controls = { controls = {
--util values
modpath = minetest.get_modpath("controls"), modpath = minetest.get_modpath("controls"),
testsmode = minetest.settings:get_bool("controls_enable_tests", false) testsmode = minetest.settings:get_bool("controls_enable_tests", false),
--location to store callbacks
registered_on_press = {},
registered_on_hold = {},
registered_on_release = {},
} }
--api functions --api functions
function controls.register_on_press(callback) function controls.register_on_press(callback)
table.insert(controls.registered_on_press, callback)
end end
function controls.register_on_hold(callback) function controls.register_on_hold(callback)
table.insert(controls.registered_on_hold, callback)
end end
function controls.register_on_release(callback) function controls.register_on_release(callback)
table.insert(controls.registered_on_release, callback)
end end
if(controls.testsmode) then if(controls.testsmode) then

@ -9,3 +9,10 @@ end)
controls.register_on_release(function(player, key, length) controls.register_on_release(function(player, key, length)
minetest.chat_send_all(player:get_player_name() .. " released " .. key .. " after " .. length .. " seconds") minetest.chat_send_all(player:get_player_name() .. " released " .. key .. " after " .. length .. " seconds")
end) end)
minetest.register_on_joinplayer(function(player, _)
local pname = player:get_player_name()
minetest.chat_send_player(pname, #controls.registered_on_press .. " registered_on_press callbacks")
minetest.chat_send_player(pname, #controls.registered_on_hold .. " registered_on_hold callbacks")
minetest.chat_send_player(pname, #controls.registered_on_release .. " registered_on_release callbacks")
end)