-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathInputBindingImpl.java
More file actions
350 lines (294 loc) · 11.1 KB
/
Copy pathInputBindingImpl.java
File metadata and controls
350 lines (294 loc) · 11.1 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package dev.isxander.controlify.bindings;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.api.bind.InputBinding;
import dev.isxander.controlify.bindings.input.Input;
import dev.isxander.controlify.bindings.output.*;
import dev.isxander.controlify.controller.input.ControllerStateView;
import dev.isxander.controlify.controller.input.InputComponent;
import dev.isxander.controlify.utils.ResizableRingBuffer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class InputBindingImpl implements InputBinding {
private final Identifier id;
private final Component name, description, category;
/**
* Fallback bound input used only when inputComponent is null (i.e., bindings not attached to a controller).
* When inputComponent is present, the bound input is read/written directly from the config.
*/
private Input fallbackBoundInput;
private final Supplier<Input> defaultBindSupplier;
private final Set<BindContext> contexts;
private final @Nullable Identifier radialIcon;
private final @Nullable InputComponent inputComponent;
private final Identifier controllerType;
private final ResizableRingBuffer<Float> stateHistory;
private final Set<StateAccessImpl> borrowedAccesses;
private boolean suppressed;
private final AnalogueOutput analogueNow;
private final AnalogueOutput analoguePrev;
private final DigitalOutput digitalNow;
private final DigitalOutput digitalThen;
private final DigitalOutput justPressed;
private final DigitalOutput justReleased;
private final DigitalOutput justTapped;
private final GuiPressOutput guiPressOutput;
private final Map<Identifier, DigitalOutput> digitalOutputs;
private final Map<Identifier, AnalogueOutput> analogueOutputs;
private int fakePressState = -1;
public InputBindingImpl(
@Nullable InputComponent inputComponent,
Identifier controllerType,
Identifier id,
Component name,
Component description,
Component category,
Supplier<Input> defaultBindSupplier,
Set<BindContext> contexts,
@Nullable Identifier radialIcon
) {
this.inputComponent = inputComponent;
this.controllerType = controllerType;
this.id = id;
this.name = name;
this.description = description;
this.category = category;
this.stateHistory = new ResizableRingBuffer<>(2, () -> 0f);
this.fallbackBoundInput = defaultBindSupplier.get();
this.defaultBindSupplier = defaultBindSupplier;
this.contexts = contexts;
this.radialIcon = radialIcon;
this.borrowedAccesses = new HashSet<>();
this.digitalOutputs = new HashMap<>();
this.analogueOutputs = new HashMap<>();
this.analogueNow = addAnalogueOutput(ANALOGUE_NOW, new SimpleAnalogueOutput(this, 0));
this.analoguePrev = addAnalogueOutput(ANALOGUE_PREV, new SimpleAnalogueOutput(this, 1));
this.digitalNow = addDigitalOutput(DIGITAL_NOW, new SimpleDigitalOutput(this, 0));
this.digitalThen = addDigitalOutput(DIGITAL_PREV, new SimpleDigitalOutput(this, 1));
this.justPressed = addDigitalOutput(JUST_PRESSED, new JustPressedOutput(this));
this.justReleased = addDigitalOutput(JUST_RELEASED, new JustReleasedOutput(this));
this.justTapped = addDigitalOutput(JUST_TAPPED, new JustTappedOutput(this));
this.guiPressOutput = addDigitalOutput(GUI_PRESSED, new GuiPressOutput(this));
}
@Override
public Identifier id() {
return this.id;
}
@Override
public Component name() {
return this.name;
}
@Override
public Component description() {
return this.description;
}
@Override
public Component category() {
return this.category;
}
@Override
public Component inputGlyph() {
return Controlify.instance().inputFontMapper().getComponentFromInputs(
controllerType,
boundInput().getRelevantInputs()
);
}
@Override
public StateAccess createStateAccess(int historyRequired) {
return createStateAccess(historyRequired, null);
}
@Override
public StateAccess createStateAccess(int historyRequired, Consumer<StateAccess> pushEvent) {
if (historyRequired > this.stateHistory.size()) {
this.stateHistory.setSize(historyRequired);
}
StateAccessImpl access = new StateAccessImpl(historyRequired, pushEvent);
borrowedAccesses.add(access);
return access;
}
@Override
public void returnStateAccess(StateAccess stateAccess) {
if (stateAccess instanceof StateAccessImpl accessImpl) {
boolean removed = this.borrowedAccesses.remove(accessImpl);
accessImpl.retire();
if (removed) {
OptionalInt newMaxSize = this.borrowedAccesses.stream().mapToInt(StateAccessImpl::maxHistory).max();
newMaxSize.ifPresent(this.stateHistory::setSize);
}
} else {
throw new IllegalStateException("Unknown implementation of state access");
}
}
@Override
public void pushState(ControllerStateView state) {
if (!this.contexts.isEmpty()) {
Set<BindContext> thisTickContexts = Controlify.instance().thisTickBindContexts();
this.suppressed = this.contexts.stream().noneMatch(thisTickContexts::contains);
} else {
this.suppressed = false;
}
float analogue = this.boundInput().state(state);
switch (fakePressState) {
case 0 -> analogue = 0;
case 1,2 -> analogue = 1;
case 3 -> analogue = 0;
}
if (fakePressState >= 0) {
if (fakePressState == 3)
fakePressState = -1;
else
fakePressState++;
}
this.stateHistory.push(analogue);
borrowedAccesses.forEach(StateAccessImpl::onPush);
}
@Override
public void fakePress() {
fakePressState = 0;
}
@Override
public void limitActivity() {
if (fakePressState == -1)
fakePressState = 3;
}
@Override
public void setBoundInput(Input input) {
if (inputComponent != null) {
var settings = inputComponent.settings();
// If keepDefaultBindings is false and the input equals the default,
// remove from config so it won't be serialized (allowing future default changes to take effect)
if (!settings.keepDefaultBindings && input.equals(defaultInput())) {
settings.bindings.bindings.remove(id);
} else {
// Write directly to the config - all bindings sharing the same config will see this change
settings.bindings.bindings.put(id, input);
}
} else {
// No inputComponent means this binding isn't attached to a controller, use fallback
this.fallbackBoundInput = input;
}
Controlify.instance().config().markDirty();
}
@Override
public Input boundInput() {
if (inputComponent != null) {
// Read directly from the config - ensures all bindings sharing the same config see the same value
Input configInput = inputComponent.settings().bindings.bindings.get(id);
if (configInput != null) {
return configInput;
}
// Not in config means it's at the default (when keepDefaultBindings is false)
// Return the dynamic default so all controllers stay in sync
return defaultInput();
}
// Fallback: use local value for bindings without inputComponent
return this.fallbackBoundInput;
}
@Override
public Input defaultInput() {
return this.defaultBindSupplier.get();
}
@Override
public Set<BindContext> contexts() {
return this.contexts;
}
@Override
public Optional<Identifier> radialIcon() {
return Optional.ofNullable(this.radialIcon);
}
@Override
public float analogueNow() {
return this.analogueNow.get();
}
@Override
public float analoguePrev() {
return this.analoguePrev.get();
}
@Override
public boolean digitalNow() {
return this.digitalNow.get();
}
@Override
public boolean digitalPrev() {
return this.digitalThen.get();
}
@Override
public boolean justPressed() {
return this.justPressed.get();
}
@Override
public boolean justReleased() {
return this.justReleased.get();
}
@Override
public boolean justTapped() {
return this.justTapped.get();
}
@Override
public GuiPressOutput guiPressed() {
return this.guiPressOutput;
}
@Override
public <T extends DigitalOutput> T addDigitalOutput(Identifier id, T output) {
this.digitalOutputs.put(id, output);
return output;
}
@Override
public <T extends DigitalOutput> T getDigitalOutput(Identifier id) {
return (T) this.digitalOutputs.get(id);
}
@Override
public <T extends AnalogueOutput> T addAnalogueOutput(Identifier id, T output) {
this.analogueOutputs.put(id, output);
return output;
}
@Override
public <T extends AnalogueOutput> T getAnalogueOutput(Identifier id) {
return (T) this.analogueOutputs.get(id);
}
private class StateAccessImpl implements StateAccess {
private final int maxHistory;
private boolean valid;
private final @Nullable Consumer<StateAccess> pushListener;
public StateAccessImpl(int maxHistory, @Nullable Consumer<StateAccess> pushListener) {
this.maxHistory = maxHistory;
this.valid = true;
this.pushListener = pushListener;
}
@Override
public float analogue(int history) {
if (!valid) throw new IllegalStateException("Tried to access state from returned access!");
if (history > maxHistory) throw new IllegalStateException("Overflowing history!");
return InputBindingImpl.this.stateHistory.tail(history);
}
@Override
public boolean digital(int history) {
float threshold = inputComponent != null
? inputComponent.settings().buttonActivationThreshold
: 0.5f;
return analogue(history) > threshold;
}
@Override
public boolean isSuppressed() {
return suppressed;
}
@Override
public boolean isValid() {
return valid;
}
@Override
public int maxHistory() {
return this.maxHistory;
}
public void onPush() {
if (this.pushListener != null)
this.pushListener.accept(this);
}
public void retire() {
this.valid = false;
}
}
}