-
Notifications
You must be signed in to change notification settings - Fork 9
/
input.lua
175 lines (162 loc) · 3.84 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
local isDown = love.keyboard.isDown
local G = love.graphics
Input = Object:New {
list = {},
}
function Input:init(joy)
table.insert(self.list, self)
self.state = {}
self.prev_state = {}
if joy then
self.joy = joy
else
self.keyboard_state = {}
end
end
function Input:_isDown(key)
return self.keyboard_state[key] or isDown(key)
end
function Input:keypressed(key)
self.keyboard_state[key] = true
end
function Input:update()
local s
if self.joy then -- joystick
local x, y = self.joy:getAxes()
x = x or 0
y = y or 0
s = {
left = self.joy:isGamepadDown("dpleft") or x < -0.1,
right = self.joy:isGamepadDown("dpright") or x > 0.1,
up = self.joy:isGamepadDown("dpup") or y < -0.1,
down = self.joy:isGamepadDown("dpdown") or y > 0.1,
a = self.joy:isGamepadDown("a") or self.joy:isDown(1),
b = self.joy:isGamepadDown("b") or self.joy:isDown(2),
start = self.joy:isGamepadDown("start"),
enter = self.joy:isGamepadDown("start"),
back = self.joy:isGamepadDown("back"),
}
else -- keyboard
s = {
left = self:_isDown("left"),
right = self:_isDown("right"),
up = self:_isDown("up"),
down = self:_isDown("down"),
a = self:_isDown("x"),
b = self:_isDown("y") or self:_isDown("z"),
start = self:_isDown("space") or self:_isDown("return"),
enter = self:_isDown("return"),
back = self:_isDown("escape"),
}
self.keyboard_state = {}
end
s.dx = bool[s.right] - bool[s.left]
s.dy = bool[s.down] - bool[s.up]
self.prev_state = self.state
self.state = s
end
function Input:gotPressed(key)
return self.state[key] and not self.prev_state[key]
end
function Input:gotAnyPressed(key)
for _, input in ipairs(self.list) do
if input:gotPressed(key) then
return input
end
end
return false
end
if not MOBILE then
function love.joystickadded(joy)
Input(joy)
end
function love.joystickremoved(joy)
local i = 1
while self.list[i] do
if self.list[i].joy == joy then
table.remove(joy, i)
return
else
i = i + 1
end
end
end
else
-- touch
TouchInput = Input:New {
state = {},
prev_state = {},
touches = {},
touch_a = { active = false },
touch_b = { active = false },
touch_dpad = { active = false },
}
table.insert(Input.list, TouchInput)
function TouchInput:update()
local dist = G.getHeight() / 20
local s = {
a = self.touch_a.active,
b = self.touch_b.active,
start = false,
enter = self.touch_a.active,
back = false,
dx = 0,
dy = 0,
}
if self.touch_dpad.active then
local dx = self.touch_dpad.x - self.touch_dpad.ox
local dy = self.touch_dpad.y - self.touch_dpad.oy
s.dx = math.max(-1, math.min(1, dx / dist))
s.dy = math.max(-1, math.min(1, dy / dist))
s.dx = math.floor(s.dx * 10 + 0.5) / 10
s.dy = math.floor(s.dy * 10 + 0.5) / 10
end
s.left = s.dx < -0.9
s.right = s.dx > 0.9
s.up = s.dy < -0.9
s.down = s.dy > 0.9
self.prev_state = self.state
self.state = s
end
function love.touchpressed(id, x, y, dx, dy, pressure)
local self = TouchInput
local w, h = G.getDimensions()
if x / w > 0.5 then
local touch
if y / h < 0.3 then
touch = self.touch_b
else
touch = self.touch_a
end
self.touches[id] = touch
touch.active = true
touch.x = x
touch.y = y
else
self.touches[id] = self.touch_dpad
self.touch_dpad.active = true
self.touch_dpad.x = x
self.touch_dpad.y = y
self.touch_dpad.ox = x
self.touch_dpad.oy = y
end
end
function love.touchreleased(id, x, y, dx, dy, pressure)
local self = TouchInput
local touch = self.touches[id]
if touch then
touch.active = false
end
end
function love.touchmoved(id, x, y, dx, dy, pressure)
local dist = G.getHeight() / 20
local self = TouchInput
local touch = self.touches[id]
if touch == self.touch_dpad then
touch.x = x
touch.y = y
touch.ox = math.max(x - dist * 1.5, math.min(x + dist * 1.5, touch.ox))
touch.oy = math.max(y - dist * 1.5, math.min(y + dist * 1.5, touch.oy))
end
end
end