-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathBindConsumerScreen.java
More file actions
177 lines (146 loc) · 6.15 KB
/
BindConsumerScreen.java
File metadata and controls
177 lines (146 loc) · 6.15 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.screen;
import dev.isxander.controlify.bindings.input.Input;
import dev.isxander.controlify.controller.ControllerEntity;
import dev.isxander.controlify.gui.controllers.BindController;
import dev.isxander.controlify.screenop.ScreenProcessor;
import dev.isxander.controlify.screenop.ScreenProcessorProvider;
import dev.isxander.controlify.utils.render.CGuiPose;
import dev.isxander.yacl3.api.Option;
import dev.isxander.yacl3.api.utils.Dimension;
import net.minecraft.client.gui.GuiGraphicsExtractor;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
import java.util.Optional;
public class BindConsumerScreen extends Screen implements ScreenProcessorProvider {
private final BindConsumer bindConsumer;
private final Option<Input> option;
private final Screen backgroundScreen;
private final BindController.BindControllerElement widgetToFocus;
private final ScreenProcessorImpl screenProcessor = new ScreenProcessorImpl(this);
private int ticksTillClose;
private int ticksTillInput;
public BindConsumerScreen(BindConsumer bindConsumer, Option<Input> option, BindController.BindControllerElement widgetToFocus, Screen backgroundScreen) {
super(Component.empty());
this.bindConsumer = bindConsumer;
this.option = option;
this.widgetToFocus = widgetToFocus;
this.backgroundScreen = backgroundScreen;
this.ticksTillInput = 5;
}
@Override
public void extractRenderState(GuiGraphicsExtractor GuiGraphicsExtractor, int mouseX, int mouseY, float tickDelta) {
Dimension<Integer> dim = widgetToFocus.getDimension();
var pose = CGuiPose.ofPush(GuiGraphicsExtractor);
// text renders with z > 0 so push everything back so text doesn't pop through fill
//? if <1.21.6
/*GuiGraphicsExtractor.pose().translate(0, 0, -20);*/
//? if >=1.21.10 {
backgroundScreen.extractRenderStateWithTooltipAndSubtitles(GuiGraphicsExtractor, dim.centerX(), dim.centerY(), tickDelta);
//?} else {
/*backgroundScreen.renderWithTooltip(GuiGraphicsExtractor, dim.centerX(), dim.centerY(), tickDelta);
*///?}
pose.pop();
pose.push();
pose.nextLayer(1000f);
// darken everything except the widget
GuiGraphicsExtractor.fill(0, 0, width, dim.y() - 1, 0x80000000);
GuiGraphicsExtractor.fill(0, dim.y(), dim.x() - 1, height, 0x80000000);
GuiGraphicsExtractor.fill(dim.xLimit() + 1, dim.y() - 1, width, height, 0x80000000);
GuiGraphicsExtractor.fill(dim.x(), dim.yLimit() + 1, dim.xLimit(), height, 0x80000000);
pose.pop();
super.extractRenderState(GuiGraphicsExtractor, mouseX, mouseY, tickDelta);
}
@Override
public void extractBackground(GuiGraphicsExtractor GuiGraphicsExtractor, int i, int j, float f) {
// do not render background
}
@Override
public void tick() {
if (ticksTillClose > 0) {
ticksTillClose--;
if (ticksTillClose == 0) {
widgetToFocus.awaitingControllerInput = false;
// don't call setScreen because will cause background to re-init
minecraft.screen = backgroundScreen;
}
}
if (ticksTillInput > 0) {
ticksTillInput--;
if (ticksTillInput > 0) {
return;
}
}
// tick runs after all controller input ticks
Optional<Input> pressedBind = bindConsumer.getPressedBind();
if (pressedBind.isPresent()) {
option.requestSet(pressedBind.get());
returnToBackground();
}
}
@Override
//? if >=1.21.9 {
public boolean keyPressed(net.minecraft.client.input.KeyEvent keyEvent) {
boolean consumed = super.keyPressed(keyEvent);
//?} else {
/*public boolean keyPressed(int keyCode, int scanCode, int modifiers) {
boolean consumed = super.keyPressed(keyCode, scanCode, modifiers);
*///?}
if (consumed) return true;
if (ticksTillInput > 0) return false;
returnToBackground();
return true;
}
@Override
//? if >=1.21.9 {
public boolean mouseClicked(net.minecraft.client.input.MouseButtonEvent mouseButtonEvent, boolean doubleClick) {
boolean consumed = super.mouseClicked(mouseButtonEvent, doubleClick);
//?} else {
/*public boolean mouseClicked(double mouseX, double mouseY, int button) {
boolean consumed = super.mouseClicked(mouseX, mouseY, button);
*///?}
if (consumed) return true;
if (ticksTillInput > 0) return false;
returnToBackground();
return true;
}
@Override
//? if >=1.21.9 {
public boolean mouseDragged(net.minecraft.client.input.MouseButtonEvent mouseButtonEvent, double dx, double dy) {
boolean consumed = super.mouseDragged(mouseButtonEvent, dx, dy);
//?} else {
/*public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
boolean consumed = super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
*///?}
if (consumed) return true;
if (ticksTillInput > 0) return false;
returnToBackground();
return true;
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double amount, double d) {
boolean consumed = super.mouseScrolled(mouseX, mouseY, amount, d);
if (consumed) return true;
if (ticksTillInput > 0) return false;
returnToBackground();
return true;
}
private void returnToBackground() {
ticksTillClose = 5;
}
@Override
public ScreenProcessor<?> screenProcessor() {
return screenProcessor;
}
public interface BindConsumer {
Optional<Input> getPressedBind();
}
private static class ScreenProcessorImpl extends ScreenProcessor<BindConsumerScreen> {
public ScreenProcessorImpl(BindConsumerScreen screen) {
super(screen);
}
@Override
public void onControllerUpdate(ControllerEntity controller) {
// prevent all other controller input logic
}
}
}