-
Notifications
You must be signed in to change notification settings - Fork 7
/
input-hud.lua
113 lines (96 loc) · 2.84 KB
/
input-hud.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
-- feos, 2019
-- this shows all the inputs current machine has mapped, snes9x style, and also current frame
-------------------------------------------------------------------------------------------
-- Ordered table iterator, allows to iterate on the natural order of the keys of a table
-- http://lua-users.org/wiki/SortedIteration
-------------------------------------------------------------------------------------------
function __genOrderedIndex( t )
local orderedIndex = {}
for key in pairs(t) do
table.insert( orderedIndex, key )
end
table.sort( orderedIndex )
return orderedIndex
end
function orderedNext(t, state)
-- Equivalent of the next function, but returns the keys in the alphabetic
-- order. We use a temporary ordered key table that is stored in the
-- table being iterated.
local key = nil
--print("orderedNext: state = "..tostring(state) )
if state == nil then
-- the first time, generate the index
t.__orderedIndex = __genOrderedIndex( t )
key = t.__orderedIndex[1]
else
-- fetch the next value
for i = 1,table.getn(t.__orderedIndex) do
if t.__orderedIndex[i] == state then
key = t.__orderedIndex[i+1]
end
end
end
if key then
return key, t[key]
end
-- no more value to return, cleanup
t.__orderedIndex = nil
return
end
function orderedPairs(t)
-- Equivalent of the pairs() function on tables. Allows to iterate
-- in order
return orderedNext, t, nil
end
local players = {}
local other = {}
function ParseInput()
local input = joypad.get()
local buttons = string.format("%d\n", movie.framecount())
for k, v in orderedPairs(input) do
if k:find("^P") then
local spacePos = k:find(" ")
local playerNum = tonumber(k:sub(2, spacePos))
local key = k:sub(spacePos + 1)
:gsub("Button ", "")
:gsub("Up", "U")
:gsub("Down", "D")
:gsub("Left", "L")
:gsub("Right", "R")
if players[playerNum] == nil then
players[playerNum] = {}
end
players[playerNum][key] = v
else
local key = k
:gsub("Coin ", "C")
:gsub("Service Mode", "SM")
if k:find("Start") then
local spacePos = k:find(" ")
local playerNum = k:sub(1, spacePos - 1)
key = "S"..playerNum
end
other[key] = v
end
end
for k, v in pairs(players) do
buttons = buttons .. "P" .. k ..": "
for kk, vv in orderedPairs(v) do
local value = " "
if vv then
value = kk
end
buttons = buttons .. value
end
buttons = buttons .. "\n"
end
for k, v in orderedPairs(other) do
local value = " "
if v then
value = k
end
buttons = buttons .. value
end
gui.text(0, 0, buttons)
end
gui.register(ParseInput)