mirror of
https://github.com/mt-mods/controls.git
synced 2024-11-08 16:13:46 +01:00
General cleanup and readme improvements (#7)
This commit is contained in:
parent
75544e1a6f
commit
a15b3628fe
19
.luacheckrc
19
.luacheckrc
@ -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
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)
|
113
init.lua
113
init.lua
@ -1,82 +1,65 @@
|
|||||||
controls = {
|
controls = {
|
||||||
--util values
|
registered_on_press = {},
|
||||||
modpath = minetest.get_modpath("controls"),
|
registered_on_hold = {},
|
||||||
testsmode = minetest.settings:get_bool("controls_enable_tests", false),
|
registered_on_release = {},
|
||||||
|
players = {},
|
||||||
--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)
|
function controls.register_on_press(callback)
|
||||||
table.insert(controls.registered_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)
|
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)
|
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
|
end
|
||||||
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)
|
||||||
|
|
||||||
--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()
|
for _, callback in pairs(controls.registered_on_press) do
|
||||||
|
callback(player, key)
|
||||||
if not controls.players[pname] then break end --safety check
|
end
|
||||||
|
player_controls[key] = {true, time_now}
|
||||||
--consider using minetest.get_us_time() instead of os.clock()? would need to convert to seconds however
|
elseif pressed and player_controls[key][1] then
|
||||||
for key, key_status in pairs(pcontrols) do
|
for _, callback in pairs(controls.registered_on_hold) do
|
||||||
if key_status and not controls.players[pname][key][1] then
|
callback(player, key, (time_now - player_controls[key][2]) / 1e6)
|
||||||
for _, callback in pairs(controls.registered_on_press) do
|
end
|
||||||
callback(player, key)
|
elseif not pressed and player_controls[key][1] then
|
||||||
end
|
for _, callback in pairs(controls.registered_on_release) do
|
||||||
controls.players[pname][key] = {true, minetest.get_us_time()}
|
callback(player, key, (time_now - player_controls[key][2]) / 1e6)
|
||||||
elseif key_status and controls.players[pname][key][1] then
|
end
|
||||||
for _, callback in pairs(controls.registered_on_hold) do
|
player_controls[key] = {false}
|
||||||
callback(player, key, (minetest.get_us_time() - controls.players[pname][key][2]) / 1e6)
|
end
|
||||||
end
|
end
|
||||||
elseif not key_status and controls.players[pname][key][1] then
|
end
|
||||||
for _, callback in pairs(controls.registered_on_release) do
|
|
||||||
callback(player, key, (minetest.get_us_time() - controls.players[pname][key][2]) / 1e6)
|
minetest.register_globalstep(function()
|
||||||
end
|
for _, player in pairs(minetest.get_connected_players()) do
|
||||||
controls.players[pname][key] = {false}
|
local name = player:get_player_name()
|
||||||
end
|
if controls.players[name] then
|
||||||
end
|
update_player_controls(player, controls.players[name])
|
||||||
end
|
end
|
||||||
end)
|
end
|
||||||
|
end)
|
||||||
--tests
|
|
||||||
if(controls.testsmode) then
|
if minetest.settings:get_bool("controls_enable_debug", false) then
|
||||||
dofile(controls.modpath .. "/test.lua")
|
dofile(minetest.get_modpath("controls") .. "/debug.lua")
|
||||||
end
|
end
|
4
mod.conf
4
mod.conf
@ -1,2 +1,2 @@
|
|||||||
name=controls
|
name = controls
|
||||||
description=util library for subscribing to events
|
description = Utility library for control press/hold/release events
|
||||||
|
36
readme.md
36
readme.md
@ -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
|
||||||
|
21
test.lua
21
test.lua
@ -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)
|
|
Loading…
Reference in New Issue
Block a user