General cleanup and readme improvements (#7)

This commit is contained in:
OgelGames 2023-12-06 23:53:38 +11:00 committed by GitHub
parent 75544e1a6f
commit a15b3628fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 97 additions and 120 deletions

@ -1,25 +1,8 @@
unused_args = false
allow_defined_top = true
exclude_files = {".luacheckrc"}
globals = { globals = {
"minetest", "core",
--mod provided
"controls", "controls",
} }
read_globals = { read_globals = {
string = {fields = {"split"}}, "minetest",
table = {fields = {"copy", "getn"}},
--luac
"math", "table",
-- Builtin
"vector", "ItemStack", "dump", "DIR_DELIM", "VoxelArea", "Settings", "PcgRandom", "VoxelManip", "PseudoRandom",
--mod produced
} }

21
debug.lua Normal file

@ -0,0 +1,21 @@
controls.register_on_press(function(player, key)
local name = player:get_player_name()
minetest.chat_send_player(name, name .. " pressed " .. key)
end)
controls.register_on_hold(function(player, key, length)
local name = player:get_player_name()
minetest.chat_send_player(name, name .. " held " .. key .. " for " .. length .. " seconds")
end)
controls.register_on_release(function(player, key, length)
local name = player:get_player_name()
minetest.chat_send_player(name, name .. " released " .. key .. " after " .. length .. " seconds")
end)
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
minetest.chat_send_player(name, #controls.registered_on_press .. " registered_on_press callbacks")
minetest.chat_send_player(name, #controls.registered_on_hold .. " registered_on_hold callbacks")
minetest.chat_send_player(name, #controls.registered_on_release .. " registered_on_release callbacks")
end)

@ -1,18 +1,10 @@
controls = { controls = {
--util values
modpath = minetest.get_modpath("controls"),
testsmode = minetest.settings:get_bool("controls_enable_tests", false),
--location to store callbacks
registered_on_press = {}, registered_on_press = {},
registered_on_hold = {}, registered_on_hold = {},
registered_on_release = {}, registered_on_release = {},
--store player control data
players = {}, players = {},
} }
--api functions
function controls.register_on_press(callback) function controls.register_on_press(callback)
table.insert(controls.registered_on_press, callback) table.insert(controls.registered_on_press, callback)
end end
@ -25,58 +17,49 @@ function controls.register_on_release(callback)
table.insert(controls.registered_on_release, callback) table.insert(controls.registered_on_release, callback)
end end
--set up key store on join minetest.register_on_joinplayer(function(player)
minetest.register_on_joinplayer(function(player, _) local name = player:get_player_name()
local pname = player:get_player_name() controls.players[name] = {}
local controls_names = player:get_player_control() for key in pairs(player:get_player_control()) do
controls.players[name][key] = {false}
--note: could hardcode this, but this is more future proof in case minetest adds more controls
controls.players[pname] = {}
for key, _ in pairs(controls_names) do
--[[
in theory the control value is false when they join, but hard coding just in case
consider changing this to named key table instead of numeric for better readability???
]]
controls.players[pname][key] = {false}
end end
end) end)
--discard when leaving minetest.register_on_leaveplayer(function(player)
minetest.register_on_leaveplayer(function(player, _) local name = player:get_player_name()
local pname = player:get_player_name() controls.players[name] = nil
controls.players[pname] = nil
end) end)
--event loop local function update_player_controls(player, player_controls)
minetest.register_globalstep(function(dtime) local time_now = minetest.get_us_time()
for _, player in pairs(minetest.get_connected_players()) do for key, pressed in pairs(player:get_player_control()) do
local pname = player:get_player_name() if pressed and not player_controls[key][1] then
local pcontrols = player:get_player_control()
if not controls.players[pname] then break end --safety check
--consider using minetest.get_us_time() instead of os.clock()? would need to convert to seconds however
for key, key_status in pairs(pcontrols) do
if key_status and not controls.players[pname][key][1] then
for _, callback in pairs(controls.registered_on_press) do for _, callback in pairs(controls.registered_on_press) do
callback(player, key) callback(player, key)
end end
controls.players[pname][key] = {true, minetest.get_us_time()} player_controls[key] = {true, time_now}
elseif key_status and controls.players[pname][key][1] then elseif pressed and player_controls[key][1] then
for _, callback in pairs(controls.registered_on_hold) do for _, callback in pairs(controls.registered_on_hold) do
callback(player, key, (minetest.get_us_time() - controls.players[pname][key][2]) / 1e6) callback(player, key, (time_now - player_controls[key][2]) / 1e6)
end end
elseif not key_status and controls.players[pname][key][1] then elseif not pressed and player_controls[key][1] then
for _, callback in pairs(controls.registered_on_release) do for _, callback in pairs(controls.registered_on_release) do
callback(player, key, (minetest.get_us_time() - controls.players[pname][key][2]) / 1e6) callback(player, key, (time_now - player_controls[key][2]) / 1e6)
end end
controls.players[pname][key] = {false} player_controls[key] = {false}
end end
end end
end end
minetest.register_globalstep(function()
for _, player in pairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if controls.players[name] then
update_player_controls(player, controls.players[name])
end
end
end) end)
--tests if minetest.settings:get_bool("controls_enable_debug", false) then
if(controls.testsmode) then dofile(minetest.get_modpath("controls") .. "/debug.lua")
dofile(controls.modpath .. "/test.lua")
end end

@ -1,2 +1,2 @@
name = controls name = controls
description=util library for subscribing to events description = Utility library for control press/hold/release events

@ -1,25 +1,35 @@
# api # Controls [controls]
note: supports all keys minetest does [![luacheck](https://github.com/mt-mods/controls/workflows/luacheck/badge.svg)](https://github.com/mt-mods/controls/actions)
[![ContentDB](https://content.minetest.net/packages/mt-mods/controls/shields/downloads/)](https://content.minetest.net/packages/mt-mods/controls/)
Utility library for control press/hold/release events.
Rewritten and maintained version of [Arcelmi/minetest-controls](https://github.com/Arcelmi/minetest-controls).
## API
Callbacks are supported for all keys in `player:get_player_control()`.
```lua ```lua
controls.register_on_press(function(player, key) controls.register_on_press(function(player, key)
-- called on key down -- Called when a key is pressed
-- @player: player object -- player: player object
-- @key: key pressed -- key: key pressed
end) end)
controls.register_on_hold(function(player, key, length) controls.register_on_hold(function(player, key, length)
-- called while key is held -- Called every globalstep while a key is held
-- @player: player object -- player: player object
-- @key: key pressed -- key: key pressed
-- @length: length of time key was held in seconds -- length: length of time key has been held in seconds
end) end)
controls.register_on_release(function(player, key, length) controls.register_on_release(function(player, key, length)
-- called on key up -- Called when a key is released
-- @player: player object -- player: player object
-- @key: key pressed -- key: key pressed
-- @length: length of time key was held in seconds -- length: length of time key was held in seconds
end) end)
``` ```

@ -1 +1,2 @@
controls_enable_tests (enable testing mode for player controls) bool false # Enable debug mod for player controls. Sends chat messages to the player when callbacks are called.
controls_enable_debug (Enable debug mode) bool false

@ -1,21 +0,0 @@
controls.register_on_press(function(player, key)
local pname = player:get_player_name()
minetest.chat_send_player(pname, pname .. " pressed " .. key)
end)
controls.register_on_hold(function(player, key, length)
local pname = player:get_player_name()
minetest.chat_send_player(pname, pname .. " held " .. key .. " for " .. length .. " seconds")
end)
controls.register_on_release(function(player, key, length)
local pname = player:get_player_name()
minetest.chat_send_player(pname, pname .. " released " .. key .. " after " .. length .. " seconds")
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)