Skip to content

Commit 8a80f63

Browse files
authored
Merge pull request #1 from AtomicGmod/develop
Add bind library
2 parents bd4cf63 + 14a34f8 commit 8a80f63

4 files changed

Lines changed: 68 additions & 5 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@
1111
- [ ] ``atomic.web``
1212
- [ ] ``atomic.database``
1313
- [ ] ``atomic.net`` (autumnnet as base)
14-
- [ ] ``atomic.command``
15-
- [ ] Command parser
14+
- [x] ``atomic.command``
15+
- [ ] Command parser
16+
- [x] ``atomic.bind``
17+
18+
### Misc
19+
- [ ] Normal documentation everywhere

lua/atomic/libraries/bind.lua

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
atomic.bind = atomic.bind or {
2+
---@type table<number, table<number, fun(player: Player)>>
3+
_storage = {},
4+
---@type table<number, number> -- [registrationId] = key
5+
_map = {},
6+
_lastBindId = 0
7+
}
8+
9+
---@param key number Index of the key
10+
---@param callback fun(player: Player)
11+
---@return number registerationId
12+
function atomic.bind.bind(key, callback)
13+
if (type(atomic.bind._storage[key]) ~= "table") then
14+
atomic.bind._storage[key] = {}
15+
end
16+
17+
atomic.bind._lastBindId = atomic.bind._lastBindId + 1
18+
local id = atomic.bind._lastBindId
19+
20+
atomic.bind._storage[key][id] = callback
21+
atomic.bind._map[id] = key
22+
23+
return id
24+
end
25+
26+
---@param registerationId number
27+
function atomic.bind.unbind(registerationId)
28+
local key = atomic.bind._map[registerationId]
29+
if (type(key) ~= "number") then
30+
return
31+
end
32+
33+
local tab = atomic.bind._storage[key]
34+
35+
if (type(tab) ~= "table") then
36+
return
37+
end
38+
39+
tab[registerationId] = nil
40+
atomic.bind._map[registerationId] = nil
41+
end
42+
43+
---@param player Player
44+
---@param key number
45+
local function keybind_handler(player, key)
46+
if (not IsFirstTimePredicted()) then
47+
return
48+
end
49+
50+
local tab = atomic.bind._storage[key]
51+
if (type(tab) ~= "table") then return end
52+
53+
for _, callback in pairs(tab) do
54+
callback(player)
55+
end
56+
end
57+
58+
hook.Add("PlayerButtonDown", "atomic.bind", keybind_handler)

lua/atomic/libraries/package.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ atomic.package = {
2626
---@field private _isLoaded boolean? Internal
2727
---@field private _isEnabled boolean? Internal
2828
---@field private logger Atomic.STD.Logger? Internal
29-
---@field private _events table<string, function> Internal
30-
---@field private _commands table<string, Atomic.STD.Command> Internal
29+
---@field private _events table<string, function>? Internal
30+
---@field private _commands table<string, Atomic.STD.Command>? Internal
3131
local package = {}
3232
package.__index = package
3333

lua/autorun/atomic_autorun.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
atomic = {
22
meta = {
33
author = "smokingplaya",
4-
version = "0.1.4"
4+
version = "0.1.5"
55
}
66
}
77

@@ -21,6 +21,7 @@ atomic.loader.shared("atomic/utils/table.lua")
2121
atomic.loader.shared("atomic/utils/coroutine.lua")
2222
atomic.loader.shared("atomic/utils/semver.lua")
2323
-- libraries
24+
atomic.loader.shared("atomic/libraries/bind.lua")
2425
atomic.loader.client("atomic/libraries/web.lua")
2526
atomic.loader.server("atomic/libraries/command.lua")
2627
atomic.loader.server("atomic/libraries/git.lua")

0 commit comments

Comments
 (0)