Skip to content

Commit

Permalink
Refactoring: use base Event in controls mod. Relates to lord-serv…
Browse files Browse the repository at this point in the history
  • Loading branch information
alek13 committed Jul 21, 2024
1 parent 39bac19 commit 30d5bbc
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 46 deletions.
43 changes: 3 additions & 40 deletions mods/lord/Core/controls/src/controls/Event.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
local pairs
= pairs

--- @static
--- @class controls.Event
local Event = {}
--- @class controls.Event: base_classes.Event
local Event = base_classes.Event:extended()

--- @class controls.Event.Type
Event.Type = {
Expand All @@ -17,7 +14,7 @@ Event.Type = {
--- @alias controls.callbacks.OnRelease fun(player:Player, key:string, hold_time:number)
--- @alias controls.callback controls.callbacks.OnPress|controls.callbacks.OnHold|controls.callbacks.OnRelease

local subscribers = {
Event.subscribers = {
---@type controls.callbacks.OnPress[]
on_press = {},
---@type controls.callbacks.OnHold[]
Expand All @@ -26,39 +23,5 @@ local subscribers = {
on_release = {},
}

--- @param event string name of event (One of `Event.Type::<const>`)
---
--- @return fun(callback:controls.callback)
function Event.on(event)
return function(callback)
Event.subscribe(event, callback)
end
end

--- @param event string name of event (One of `Event.Type::<const>`)
--- @param callback controls.callback
function Event.subscribe(event, callback)
assert(Event.Type[event], "Unknown controls.Event.Type: " .. event)
assert(type(callback) == "function")

table.insert(subscribers[event], callback)
end

--- @private
--- @param event string name of event (One of `Event.Type::<const>`)
--- @vararg any pass args that will be passed to subscribers callbacks. See `controls.callbacks.<func-types>`
function Event.notify(event, ...)
assert(Event.Type[event], "Unknown controls.Event.Type: " .. event)

for _, func in pairs(subscribers[event]) do
func(...)
end
end

--- @param event string name of event (One of `Event.Type::<const>`)
--- @vararg any pass args that will be passed to subscribers callbacks. See `controls.callbacks.<func-types>`
function Event.trigger(event, ...)
Event.notify(event, ...)
end

return Event
12 changes: 6 additions & 6 deletions mods/lord/Core/controls/src/controls/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ minetest.foreach_player_every(0, function(player, _)
-- Нажатие
if is_pressed and not last_controls[key][was_pressed] then

Event.trigger(Event.Type.on_press, player, key)
Event:trigger(Event.Type.on_press, player, key)
last_controls[key] = {
[was_pressed] = true,
[pressed_at] = now,
Expand All @@ -54,13 +54,13 @@ minetest.foreach_player_every(0, function(player, _)
elseif is_pressed and last_controls[key][was_pressed] then

local hold_time = (now - last_controls[key][pressed_at]) / MICROSECONDS
Event.trigger(Event.Type.on_hold, player, key, hold_time)
Event:trigger(Event.Type.on_hold, player, key, hold_time)

-- Отпуск
elseif not is_pressed and last_controls[key][was_pressed] then

local hold_time = (now - last_controls[key][pressed_at]) / MICROSECONDS
Event.trigger(Event.Type.on_release, player, key, hold_time)
Event:trigger(Event.Type.on_release, player, key, hold_time)
last_controls[key] = {
[was_pressed] = false,
}
Expand All @@ -73,9 +73,9 @@ end)

return {
--- @type fun(callback:controls.callbacks.OnPress)
on_press = Event.on(Event.Type.on_press),
on_press = Event:on(Event.Type.on_press),
--- @type fun(callback:controls.callbacks.OnHold)
on_hold = Event.on(Event.Type.on_hold),
on_hold = Event:on(Event.Type.on_hold),
--- @type fun(callback:controls.callbacks.OnRelease)
on_release = Event.on(Event.Type.on_release),
on_release = Event:on(Event.Type.on_release),
}

0 comments on commit 30d5bbc

Please sign in to comment.