-
-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathBindController.java
More file actions
177 lines (147 loc) · 6.92 KB
/
Copy pathBindController.java
File metadata and controls
177 lines (147 loc) · 6.92 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
package dev.isxander.controlify.gui.controllers;
import dev.isxander.controlify.Controlify;
import dev.isxander.controlify.bindings.ControlifyBindings;
import dev.isxander.controlify.bindings.input.*;
import dev.isxander.controlify.controller.*;
import dev.isxander.controlify.controller.input.ControllerStateView;
import dev.isxander.controlify.controller.input.HatState;
import dev.isxander.controlify.controller.input.InputComponent;
import dev.isxander.controlify.gui.screen.BindConsumerScreen;
import dev.isxander.controlify.screenop.ComponentProcessor;
import dev.isxander.controlify.screenop.ScreenProcessor;
import dev.isxander.controlify.utils.MinecraftUtil;
import dev.isxander.yacl3.api.Controller;
import dev.isxander.yacl3.api.Option;
import dev.isxander.yacl3.api.utils.Dimension;
import dev.isxander.yacl3.gui.YACLScreen;
import dev.isxander.yacl3.gui.controllers.ControllerWidget;
import net.minecraft.ChatFormatting;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.input.KeyEvent;
import net.minecraft.client.input.MouseButtonEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.Identifier;
import org.lwjgl.glfw.GLFW;
import java.util.ArrayList;
import java.util.List;
public class BindController implements Controller<Input> {
private final Option<Input> option;
public final ControllerEntity controller;
private boolean conflicting;
public BindController(Option<Input> option, ControllerEntity controller) {
this.option = option;
this.controller = controller;
}
@Override
public Option<Input> option() {
return this.option;
}
@Override
public Component formatValue() {
return Component.empty();
}
public void setConflicting(boolean conflicting) {
this.conflicting = conflicting;
}
public boolean getConflicting() {
return this.conflicting;
}
@Override
public BindControllerElement provideWidget(YACLScreen yaclScreen, Dimension<Integer> dimension) {
return new BindControllerElement(this, yaclScreen, dimension);
}
public static class BindControllerElement extends ControllerWidget<BindController> implements ComponentProcessor {
public boolean awaitingControllerInput = false;
private final Component awaitingText = Component.translatable("controlify.gui.bind_input_awaiting").withStyle(ChatFormatting.ITALIC);
public BindControllerElement(BindController control, YACLScreen screen, Dimension<Integer> dim) {
super(control, screen, dim);
}
@Override
protected void extractValueText(GuiGraphicsExtractor graphics, int mouseX, int mouseY, float a) {
if (awaitingControllerInput) {
graphics.text(textRenderer, awaitingText, getDimension().xLimit() - textRenderer.width(awaitingText) - getXPadding(), (int)(getDimension().centerY() - textRenderer.lineHeight / 2f), 0xFFFFFFFF, true);
} else {
var bind = control.option().pendingValue();
if (EmptyInput.equals(bind)) return;
Component text = Controlify.instance().inputFontMapper()
.getComponentFromBind(control.controller.info().type().namespace(), bind);
int width = textRenderer.width(text);
graphics.text(textRenderer, text, getDimension().xLimit() - width - 1, (int)(getDimension().centerY() - textRenderer.lineHeight / 2f + 1), -1, false);
}
}
@Override
public boolean keyPressed(KeyEvent keyEvent) {
if (isFocused() && keyEvent.key() == GLFW.GLFW_KEY_ENTER) {
openConsumerScreen();
return true;
}
return false;
}
@Override
public boolean mouseClicked(MouseButtonEvent mouseButtonEvent, boolean doubleClick) {
if (getDimension().isPointInside((int) mouseButtonEvent.x(), (int) mouseButtonEvent.y())) {
openConsumerScreen();
return true;
}
return false;
}
private void openConsumerScreen() {
awaitingControllerInput = true;
MinecraftUtil.setScreen(new BindConsumerScreen(this::getPressedBinds, control.option(), this, MinecraftUtil.getScreen()));
}
@Override
public boolean overrideControllerButtons(ScreenProcessor<?> screen, ControllerEntity controller) {
if (controller != control.controller) return true;
if (ControlifyBindings.GUI_PRESS.on(controller).justPressed()) {
openConsumerScreen();
return true;
}
return false;
}
@Override
protected int getHoveredControlWidth() {
return getUnhoveredControlWidth();
}
@Override
protected int getUnhoveredControlWidth() {
if (awaitingControllerInput)
return textRenderer.width(awaitingText);
Component text = Controlify.instance().inputFontMapper()
.getComponentFromBind(control.controller.info().type().namespace(), control.option().pendingValue());
return textRenderer.width(text);
}
@Override
protected int getValueColor() {
return control.conflicting ? 0xFFFF5555 : super.getValueColor();
}
public List<Input> getPressedBinds() {
InputComponent input = control.controller.input().orElseThrow();
ControllerStateView state = input.stateNow();
ControllerStateView prevState = input.stateThen();
List<Input> pressedBinds = new ArrayList<>();
for (Identifier button : state.getButtons()) {
if (state.isButtonDown(button) && !prevState.isButtonDown(button)) {
pressedBinds.add(new ButtonInput(button));
} else if (!state.isButtonDown(button) && prevState.isButtonDown(button)) {
return List.of(EmptyInput.INSTANCE);
}
}
for (Identifier axis : state.getAxes()) {
if (state.getAxisState(axis) > 0.5f && prevState.getAxisState(axis) <= 0.5f) {
pressedBinds.add(new AxisInput(axis));
} else if (state.getAxisState(axis) <= 0.5f && prevState.getAxisState(axis) > 0.5f) {
return List.of(EmptyInput.INSTANCE);
}
}
for (Identifier hat : state.getHats()) {
HatState hatState = state.getHatState(hat);
if (hatState != HatState.CENTERED && prevState.getHatState(hat) == HatState.CENTERED) {
pressedBinds.add(new HatInput(hat, hatState));
} else if (hatState == HatState.CENTERED && prevState.getHatState(hat) != HatState.CENTERED) {
return List.of(EmptyInput.INSTANCE);
}
}
return pressedBinds;
}
}
}