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 = {
"minetest", "core",
--mod provided
"controls",
}
read_globals = {
string = {fields = {"split"}},
table = {fields = {"copy", "getn"}},
--luac
"math", "table",
-- Builtin
"vector", "ItemStack", "dump", "DIR_DELIM", "VoxelArea", "Settings", "PcgRandom", "VoxelManip", "PseudoRandom",
--mod produced
"minetest",
}

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 = {
--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_hold = {},
registered_on_release = {},
--store player control data
players = {},
}
--api functions
function controls.register_on_press(callback)
table.insert(controls.registered_on_press, callback)
end
@ -25,58 +17,49 @@ function controls.register_on_release(callback)
table.insert(controls.registered_on_release, callback)
end
--set up key store on join
minetest.register_on_joinplayer(function(player, _)
local pname = player:get_player_name()
local controls_names = player:get_player_control()
--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}
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name()
controls.players[name] = {}
for key in pairs(player:get_player_control()) do
controls.players[name][key] = {false}
end
end)
--discard when leaving
minetest.register_on_leaveplayer(function(player, _)
local pname = player:get_player_name()
controls.players[pname] = nil
minetest.register_on_leaveplayer(function(player)
local name = player:get_player_name()
controls.players[name] = nil
end)
--event loop
minetest.register_globalstep(function(dtime)
for _, player in pairs(minetest.get_connected_players()) do
local pname = player:get_player_name()
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
local function update_player_controls(player, player_controls)
local time_now = minetest.get_us_time()
for key, pressed in pairs(player:get_player_control()) do
if pressed and not player_controls[key][1] then
for _, callback in pairs(controls.registered_on_press) do
callback(player, key)
end
controls.players[pname][key] = {true, minetest.get_us_time()}
elseif key_status and controls.players[pname][key][1] then
player_controls[key] = {true, time_now}
elseif pressed and player_controls[key][1] then
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
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
callback(player, key, (minetest.get_us_time() - controls.players[pname][key][2]) / 1e6)
callback(player, key, (time_now - player_controls[key][2]) / 1e6)
end
controls.players[pname][key] = {false}
player_controls[key] = {false}
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)
--tests
if(controls.testsmode) then
dofile(controls.modpath .. "/test.lua")
if minetest.settings:get_bool("controls_enable_debug", false) then
dofile(minetest.get_modpath("controls") .. "/debug.lua")
end

@ -1,2 +1,2 @@
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
controls.register_on_press(function(player, key)
-- called on key down
-- @player: player object
-- @key: key pressed
-- Called when a key is pressed
-- player: player object
-- key: key pressed
end)
controls.register_on_hold(function(player, key, length)
-- called while key is held
-- @player: player object
-- @key: key pressed
-- @length: length of time key was held in seconds
-- Called every globalstep while a key is held
-- player: player object
-- key: key pressed
-- length: length of time key has been held in seconds
end)
controls.register_on_release(function(player, key, length)
-- called on key up
-- @player: player object
-- @key: key pressed
-- @length: length of time key was held in seconds
-- Called when a key is released
-- player: player object
-- key: key pressed
-- length: length of time key was held in seconds
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)