-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathInputSchemeManager.ts
More file actions
223 lines (190 loc) · 8.86 KB
/
Copy pathInputSchemeManager.ts
File metadata and controls
223 lines (190 loc) · 8.86 KB
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
import EventSystem from "@/systems/EventSystem.ts"
import type { DriveType } from "@/systems/simulation/behavior/Behavior.ts"
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain.ts"
import { random } from "@/util/Random"
import PreferencesSystem from "../preferences/PreferencesSystem"
import DefaultInputs from "./DefaultInputs"
import InputSystem from "./InputSystem"
import { type InputScheme, type InputSchemeAvailability, InputSchemeUseType, type KeyDescriptor } from "./InputTypes"
import AxisInput from "./inputs/AxisInput"
import ButtonInput from "./inputs/ButtonInput"
import type Input from "./inputs/Input"
class InputSchemeManager {
// References to the current custom schemes to avoid parsing every time they are requested
private static _customSchemes: InputScheme[] | undefined
/** Fetches custom input schemes from preferences manager */
public static get customInputSchemes(): InputScheme[] {
if (this._customSchemes) return this._customSchemes
// Load schemes from preferences and parse into objects
this._customSchemes = PreferencesSystem.getUserPreference("InputSchemes")
this._customSchemes.forEach(scheme => this.parseScheme(scheme))
return this._customSchemes
}
/** Registers a new custom scheme */
public static addCustomScheme(scheme: InputScheme, panelId?: string) {
this.customInputSchemes.push(scheme)
EventSystem.dispatch("InputSchemeChanged", { panelId })
}
/** Parses a schemes inputs into working Input instances */
private static parseScheme(rawInputs: InputScheme) {
for (let i = 0; i < rawInputs.inputs.length; i++) {
const rawInput = rawInputs.inputs[i]
let parsedInput: Input
if ((rawInput as ButtonInput).keyCode != undefined) {
const rawButton = rawInput as ButtonInput
parsedInput = new ButtonInput(
rawButton.inputName,
rawButton.keyCode,
rawButton.gamepadButton,
rawButton.keyModifiers
)
} else {
const rawAxis = rawInput as AxisInput
parsedInput = new AxisInput(
rawAxis.inputName,
rawAxis.posKeyCode,
rawAxis.negKeyCode,
rawAxis.gamepadAxisNumber,
rawAxis.joystickInverted,
rawAxis.useGamepadButtons,
rawAxis.posGamepadButton,
rawAxis.negGamepadButton,
rawAxis.touchControlAxis,
rawAxis.posKeyModifiers,
rawAxis.negKeyModifiers
)
}
rawInputs.inputs[i] = parsedInput
}
}
private static _defaultInputSchemes: InputScheme[] | undefined
public static get defaultInputSchemes(): InputScheme[] {
if (!this._defaultInputSchemes) {
this._defaultInputSchemes = DefaultInputs.defaultInputCopies
}
return this._defaultInputSchemes
}
public static resetDefaultSchemes(panelId?: string) {
this._defaultInputSchemes = DefaultInputs.defaultInputCopies
this._customSchemes = undefined
EventSystem.dispatch("InputSchemeChanged", { panelId })
}
public static rebindOldBrainSchemes() {
const schemesByName = new Map(this.allInputSchemes.map(s => [s.schemeName, s] as const))
for (const [brainIndex, scheme] of InputSystem.brainIndexSchemeMap) {
const reverted = schemesByName.get(scheme.schemeName)
if (reverted && scheme.customized) {
InputSystem.brainIndexSchemeMap.set(brainIndex, reverted)
}
}
}
/** Creates an array of every input scheme that is either a default or customized by the user. Custom themes will appear on top. */
public static get allInputSchemes(): InputScheme[] {
// Start with custom input schemes
const allSchemes: InputScheme[] = []
this.customInputSchemes.forEach(s => allSchemes.push(s))
// Add default schemes if they have not been customized
this.defaultInputSchemes.forEach(defaultScheme => {
if (allSchemes.some(s => s.schemeName === defaultScheme.schemeName)) return
allSchemes.push(defaultScheme)
})
return allSchemes
}
/** Creates an array of every input scheme that is not currently in use by a robot */
private static get _availableInputSchemes(): InputSchemeAvailability[] {
const allSchemes = this.allInputSchemes
// Remove schemes that have conflicts
const usedKeyMap = new Map<KeyDescriptor, string[]>()
const result: Record<string, InputSchemeAvailability> = {}
for (const scheme of InputSystem.brainIndexSchemeMap.values()) {
result[scheme.schemeName] = {
scheme,
status: InputSchemeUseType.IN_USE,
}
scheme?.inputs?.forEach(input => {
input
.keysUsed(scheme.playerSlot ?? 0)
.filter(key => key != null)
.forEach(key => {
const entry = usedKeyMap.get(key)
if (entry != null) {
entry.push(scheme.schemeName)
} else {
usedKeyMap.set(key, [scheme.schemeName])
}
})
})
}
allSchemes.forEach(scheme => {
const conflictingSchemes = scheme.inputs.flatMap(input =>
input.keysUsed(scheme.playerSlot ?? 0).flatMap(key => usedKeyMap.get(key) ?? [])
)
if (conflictingSchemes.length > 0) {
result[scheme.schemeName] ??= {
scheme,
status: InputSchemeUseType.CONFLICT,
conflictingSchemeNames: [...new Set(conflictingSchemes)].join(", "),
}
} else {
result[scheme.schemeName] ??= {
scheme,
status: InputSchemeUseType.AVAILABLE,
}
}
})
return Object.values(result)
}
/** Creates an array of every input scheme that is not currently in use by a robot */
public static availableInputSchemesByType(driveType?: DriveType): InputSchemeAvailability[] {
const allSchemes = this._availableInputSchemes
if (driveType == null) {
return allSchemes
}
return allSchemes.filter(entry => entry.scheme.supportedDrivetrains.includes(driveType))
}
/** Creates an array of every input scheme that is not currently in use by a robot */
public static availableInputSchemesByBrain(brainIndex: number): InputSchemeAvailability[] {
const driveType = SynthesisBrain.brainIndexMap.get(brainIndex)?.driveType
return this.availableInputSchemesByType(driveType)
}
/**
* Ensures the brain has an input scheme compatible with its current drivetrain.
*
* @returns the scheme now bound to the brain, or undefined if no compatible scheme is available.
*/
public static applyCompatibleScheme(brainIndex: number): InputScheme | undefined {
const driveType = SynthesisBrain.brainIndexMap.get(brainIndex)?.driveType
const current = InputSystem.brainIndexSchemeMap.get(brainIndex)
if (current && (driveType == null || current.supportedDrivetrains.includes(driveType))) {
return current
}
// Unbind the outgoing scheme before evaluating availability. Otherwise it still counts as in-use.
InputSystem.brainIndexSchemeMap.delete(brainIndex)
const next = this.availableInputSchemesByBrain(brainIndex).find(
entry => entry.status === InputSchemeUseType.AVAILABLE
)?.scheme
if (next) InputSystem.setBrainIndexSchemeMapping(brainIndex, next)
return next
}
/** @returns a random available robot name */
public static get randomAvailableName(): string {
const usedNames = this.allInputSchemes.map(s => s.schemeName)
const randomName = () => {
const index = Math.floor(random() * DefaultInputs.NAMES.length)
return DefaultInputs.NAMES[index]
}
let name = randomName()
while (usedNames.includes(name)) name = randomName()
return name
}
/** Save all schemes that have been customized to local storage via preferences */
public static saveSchemes(panelId?: string) {
const customizedSchemes = this.allInputSchemes.filter(s => {
return s.customized
})
PreferencesSystem.setUserPreference("InputSchemes", customizedSchemes)
PreferencesSystem.savePreferences()
EventSystem.dispatch("InputSchemeChanged", { panelId })
}
}
export default InputSchemeManager