-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathinput.go
223 lines (200 loc) · 5.84 KB
/
input.go
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package menu
import (
"github.com/libretro/ludo/audio"
"github.com/libretro/ludo/input"
"github.com/libretro/ludo/libretro"
ntf "github.com/libretro/ludo/notifications"
"github.com/libretro/ludo/settings"
"github.com/libretro/ludo/state"
)
var (
repeatRight = withRepeat()
repeatLeft = withRepeat()
repeatUp = withRepeat()
repeatDown = withRepeat()
repeatY = withRepeat()
)
// Update takes care of calling the update method of the current scene.
// Each scene has it's own input logic to allow a variety of navigation systems.
func (m *Menu) Update(dt float32) {
currentScene := m.stack[len(m.stack)-1]
currentScene.update(dt)
}
// Used to increase scroll speed during long presses
func scrollSpeed(length float32) float32 {
if length > 4 {
return 0.005
} else if length > 3 {
return 0.01
} else if length > 2 {
return 0.02
} else if length > 1 {
return 0.04
} else if length > 0.1 {
return 0.08
}
return 0.15
}
// withRepeat wraps the logic that to allows firing repeated events when a key
// or button is hold. It is used mainly for scrolling, where the scroll speed
// increases with time. It's better to use 1 withRepeat per key, to achieve
// isolation.
func withRepeat() func(dt float32, pressed bool, f func()) {
// A closure to store the values of these 3 vars across repeated calls
var cooldown, length, delay float32
return func(dt float32, pressed bool, f func()) {
cooldown -= dt
if pressed {
if cooldown <= 0 {
f()
cooldown = delay
}
length += dt
} else {
length = 0
}
delay = scrollSpeed(length)
}
}
// This is the generic menu input handler. It encapsulate the logic to scroll
// vertically in entry lists, and also respond to presses on OK and Cancel.
func genericInput(list *entry, dt float32) {
// Down
repeatDown(dt, input.NewState[0][libretro.DeviceIDJoypadDown] == 1, func() {
list.ptr++
if list.ptr >= len(list.children) {
list.ptr = 0
}
audio.PlayEffect(audio.Effects["down"])
genericAnimate(list)
})
// Up
repeatUp(dt, input.NewState[0][libretro.DeviceIDJoypadUp] == 1, func() {
list.ptr--
if list.ptr < 0 {
list.ptr = len(list.children) - 1
}
audio.PlayEffect(audio.Effects["up"])
genericAnimate(list)
})
// OK
if input.Released[0][libretro.DeviceIDJoypadA] == 1 {
if list.children[list.ptr].callbackOK != nil {
audio.PlayEffect(audio.Effects["ok"])
list.children[list.ptr].callbackOK()
}
}
// X
if input.Released[0][libretro.DeviceIDJoypadX] == 1 {
if list.children[list.ptr].callbackX != nil {
audio.PlayEffect(audio.Effects["ok"])
list.children[list.ptr].callbackX()
}
}
// Right
if input.Released[0][libretro.DeviceIDJoypadRight] == 1 {
if list.children[list.ptr].incr != nil {
audio.PlayEffect(audio.Effects["up"])
list.children[list.ptr].incr(1)
}
}
// Left
if input.Released[0][libretro.DeviceIDJoypadLeft] == 1 {
if list.children[list.ptr].incr != nil {
audio.PlayEffect(audio.Effects["down"])
list.children[list.ptr].incr(-1)
}
}
// Cancel
if input.Released[0][libretro.DeviceIDJoypadB] == 1 {
if len(menu.stack) > 1 {
audio.PlayEffect(audio.Effects["cancel"])
menu.stack[len(menu.stack)-2].segueBack()
menu.stack = menu.stack[:len(menu.stack)-1]
}
}
// Jump to next letter
if input.Released[0][libretro.DeviceIDJoypadR] == 1 && len(list.indexes) > 0 {
list.ptr = indexed(list, +1)
audio.PlayEffect(audio.Effects["down"])
genericAnimate(list)
}
// Jump to previous letter
if input.Released[0][libretro.DeviceIDJoypadL] == 1 && len(list.indexes) > 0 {
list.ptr = indexed(list, -1)
audio.PlayEffect(audio.Effects["up"])
genericAnimate(list)
}
}
// indexed allows jumping directly to the next letter in playlists
func indexed(list *entry, offset int) int {
curr := list.children[list.ptr].label[0]
for i, t := range list.indexes {
if curr == t.Char {
if i+offset < 0 {
return len(list.children) - 1
}
if i+offset > len(list.indexes)-1 {
return 0
}
return list.indexes[i+offset].Index
}
}
return 0
}
var combo1, combo2 int
// ProcessHotkeys checks if certain keys are pressed and perform corresponding actions
func (m *Menu) ProcessHotkeys() {
// Disable all hot keys on the exit dialog
currentScene := m.stack[len(m.stack)-1]
if currentScene.Entry().label == "Confirm Dialog" {
return
}
// First menu combo
if input.NewState[0][libretro.DeviceIDJoypadL3] == 1 && input.NewState[0][libretro.DeviceIDJoypadR3] == 1 {
combo1++
} else {
combo1 = 0
}
// Second menu combo
if input.NewState[0][libretro.DeviceIDJoypadStart] == 1 && input.NewState[0][libretro.DeviceIDJoypadSelect] == 1 {
combo2++
} else {
combo2 = 0
}
// Toggle the menu if ActionMenuToggle or the combo L3+R3 is pressed
if (input.Pressed[0][input.ActionMenuToggle] == 1 || combo1 == 1 || combo2 == 1) && state.CoreRunning {
state.MenuActive = !state.MenuActive
state.FastForward = false
if state.MenuActive {
audio.PlayEffect(audio.Effects["notice"])
} else {
audio.PlayEffect(audio.Effects["notice_back"])
}
}
// Toggle fullscreen if ActionFullscreenToggle is released
if input.Released[0][input.ActionFullscreenToggle] == 1 {
settings.Current.VideoFullscreen = !settings.Current.VideoFullscreen
m.Reconfigure(settings.Current.VideoFullscreen)
m.ContextReset()
err := settings.Save()
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Menu", "Error saving settings: %s", err)
}
}
if input.Pressed[0][input.ActionFastForwardToggle] == 1 && !state.MenuActive {
state.FastForward = !state.FastForward
if state.FastForward {
ntf.DisplayAndLog(ntf.Info, "Menu", "Fast forward ON")
} else {
ntf.DisplayAndLog(ntf.Info, "Menu", "Fast forward OFF")
}
}
// Close if ActionShouldClose is pressed, but display a confirmation dialog
// in case a game is running
if input.Pressed[0][input.ActionShouldClose] == 1 {
askQuitConfirmation(func() {
m.SetShouldClose(true)
})
}
}