-
Notifications
You must be signed in to change notification settings - Fork 10
/
input.lua
140 lines (127 loc) · 2.92 KB
/
input.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
local jpexists, jpname, jrname
for k,v in pairs(love.handlers) do
if k=="jp" then
jpexists = true
end
end
if jpexists then
jpname = "jp"
jrname = "jr"
else
jpname = "joystickpressed"
jrname = "joystickreleased"
end
local __old_jp_handler = love.handlers[jpname]
local __old_jr_handler = love.handlers[jrname]
love.handlers[jpname] = function(a, b)
__old_jp_handler(a,b)
love.keypressed("j"..a:getID()..b)
end
love.handlers[jrname] = function(a,b)
__old_jr_handler(a,b)
love.keyreleased("j"..a:getID()..b)
end
local prev_ax = {}
local axis_to_button = function(idx, value)
local prev = prev_ax[idx] or 0
if value > .5 then
if prev < .5 then
love.keypressed("ja"..idx.."+")
end
elseif value < -.5 then
if prev > -.5 then
love.keypressed("ja"..idx.."-")
end
else
if prev > .5 then
love.keyreleased("ja"..idx.."+")
elseif prev < -.5 then
love.keyreleased("ja"..idx.."-")
end
end
prev_ax[idx] = value
end
local prev_hat = {{},{}}
local hat_to_button = function(idx, value)
if string.len(value) == 1 then
if value == "l" or value == "r" then
value = value .. "c"
else
value = "c" .. value
end
end
value = procat(value)
for i=1,2 do
local prev = prev_hat[i][idx] or "c"
if value[i] ~= prev and value[i] ~= "c" then
love.keypressed("jh"..idx..value[i])
end
if prev ~= value[i] and prev ~= "c" then
love.keyreleased("jh"..idx..prev)
end
prev_hat[i][idx] = value[i]
end
end
function love.joystick.getHats(joystick)
local n = joystick:getHatCount()
local ret = {}
for i=1,n do
ret[i] = joystick:getHat(i)
end
return unpack(ret)
end
function joystick_ax()
local joysticks = love.joystick.getJoysticks()
for k,v in ipairs(joysticks) do
local axes = {v:getAxes()}
for idx,value in ipairs(axes) do
axis_to_button(k..idx, value)
end
local hats = {love.joystick.getHats(v)}
for idx,value in ipairs(hats) do
hat_to_button(k..idx, value)
end
end
end
function love.keypressed(key, rep)
if not rep then
keys[key] = 0
end
this_frame_keys[key] = true
end
function love.textinput(text)
this_frame_unicodes[#this_frame_unicodes+1] = text
end
function love.keyreleased(key, unicode)
keys[key] = nil
end
function key_counts()
for key,value in pairs(keys) do
keys[key] = value + 1
end
end
function Stack.controls(self)
local new_dir = nil
local sdata = self.input_state
local raise, swap, up, down, left, right = unpack(base64decode[sdata])
self.raise = raise
self.swap_1 = swap
self.swap_2 = swap
if up then
new_dir = "up"
elseif down then
new_dir = "down"
elseif left then
new_dir = "left"
elseif right then
new_dir = "right"
end
if new_dir == self.cur_dir then
if self.cur_timer ~= self.cur_wait_time then
self.cur_timer = self.cur_timer + 1
end
else
self.cur_dir = new_dir
self.cur_timer = 0
end
end