-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathKeyboardScreen.java
More file actions
218 lines (182 loc) · 7.36 KB
/
KeyboardScreen.java
File metadata and controls
218 lines (182 loc) · 7.36 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
package de.srendi.advancedperipherals.client.screens;
import com.mojang.blaze3d.vertex.PoseStack;
import dan200.computercraft.client.gui.ClientInputHandler;
import dan200.computercraft.shared.computer.core.InputHandler;
import de.srendi.advancedperipherals.client.screens.base.BaseScreen;
import de.srendi.advancedperipherals.common.container.KeyboardContainer;
import net.minecraft.SharedConstants;
import net.minecraft.client.Minecraft;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
import org.jetbrains.annotations.NotNull;
import org.lwjgl.glfw.GLFW;
import java.util.BitSet;
/**
* A simple screen but without any rendering calls. Used to unlock the mouse so we can freely write stuff
* <p>
* Char/key logic stolen from CC's WidgetTerminal
*/
public class KeyboardScreen extends BaseScreen<KeyboardContainer> {
protected final InputHandler input;
private final BitSet keysDown = new BitSet(256);
private float terminateTimer = -1;
private float rebootTimer = -1;
private float shutdownTimer = -1;
private int lastMouseButton = -1;
private int lastMouseX = -1;
private int lastMouseY = -1;
public KeyboardScreen(KeyboardContainer screenContainer, Inventory inv, Component titleIn) {
super(screenContainer, inv, titleIn);
input = new ClientInputHandler(menu);
}
@Override
public void render(@NotNull PoseStack poseStack, int x, int y, float partialTicks) {
Minecraft minecraft = Minecraft.getInstance();
float scale = 2f;
int screenWidth = minecraft.getWindow().getGuiScaledWidth();
int screenHeight = minecraft.getWindow().getGuiScaledHeight();
// Make the text a bit smaller on small screens
if (screenWidth <= 1080)
scale = 1f;
poseStack.scale(scale, scale, 1);
String text = "Press ESC to close the Keyboard Screen.";
float textX = (screenWidth / 2f - minecraft.font.width(text) * scale / 2f) / scale;
minecraft.font.drawShadow(poseStack, text, textX, 1, 0xFFFFFF);
// Prevents JEI/REI/EMI from rendering. Maybe not the best way, but it works for now.
poseStack.scale(4f, 4f, 1);
minecraft.font.draw(poseStack, "", screenWidth, screenHeight, 0xFFFFFF);
}
@Override
protected void renderBg(@NotNull PoseStack matrixStack, float partialTicks, int x, int y) {
}
@Override
public void renderBackground(@NotNull PoseStack pPoseStack) {
}
@Override
public boolean charTyped(char ch, int modifiers) {
if (ch >= 32 && ch <= 126 || ch >= 160 && ch <= 255) { // printable chars in byte range
// Queue the "char" event
input.queueEvent("char", new Object[]{Character.toString(ch)});
}
return true;
}
@Override
public boolean keyPressed(int key, int scancode, int modifiers) {
if (key == GLFW.GLFW_KEY_ESCAPE) {
onClose();
return true;
}
if ((modifiers & GLFW.GLFW_MOD_CONTROL) != 0) {
switch (key) {
case GLFW.GLFW_KEY_T:
if (terminateTimer < 0) terminateTimer = 0;
return true;
case GLFW.GLFW_KEY_S:
if (shutdownTimer < 0) shutdownTimer = 0;
return true;
case GLFW.GLFW_KEY_R:
if (rebootTimer < 0) rebootTimer = 0;
return true;
case GLFW.GLFW_KEY_V:
// Ctrl+V for paste
String clipboard = Minecraft.getInstance().keyboardHandler.getClipboard();
if (clipboard != null) {
// Clip to the first occurrence of \r or \n
int newLineIndex1 = clipboard.indexOf("\r");
int newLineIndex2 = clipboard.indexOf("\n");
if (newLineIndex1 >= 0 && newLineIndex2 >= 0) {
clipboard = clipboard.substring(0, Math.min(newLineIndex1, newLineIndex2));
} else if (newLineIndex1 >= 0) {
clipboard = clipboard.substring(0, newLineIndex1);
} else if (newLineIndex2 >= 0) {
clipboard = clipboard.substring(0, newLineIndex2);
}
// Filter the string
clipboard = SharedConstants.filterText(clipboard);
if (!clipboard.isEmpty()) {
// Clip to 512 characters and queue the event
if (clipboard.length() > 512) clipboard = clipboard.substring(0, 512);
input.queueEvent("paste", new Object[]{clipboard});
}
return true;
}
}
}
if (key >= 0 && terminateTimer < 0 && rebootTimer < 0 && shutdownTimer < 0) {
// Queue the "key" event and add to the down set
boolean repeat = keysDown.get(key);
keysDown.set(key);
input.keyDown(key, repeat);
}
return true;
}
@Override
public boolean keyReleased(int key, int scancode, int modifiers) {
// Queue the "key_up" event and remove from the down set
if (key >= 0 && keysDown.get(key)) {
keysDown.set(key, false);
input.keyUp(key);
}
switch (key) {
case GLFW.GLFW_KEY_T:
terminateTimer = -1;
break;
case GLFW.GLFW_KEY_R:
rebootTimer = -1;
break;
case GLFW.GLFW_KEY_S:
shutdownTimer = -1;
break;
case GLFW.GLFW_KEY_LEFT_CONTROL:
case GLFW.GLFW_KEY_RIGHT_CONTROL:
terminateTimer = rebootTimer = shutdownTimer = -1;
break;
}
return true;
}
@Override
public boolean mouseClicked(double mouseX, double mouseY, int button) {
input.mouseClick(button + 1, (int) mouseX, (int) mouseY);
lastMouseButton = button;
lastMouseX = (int) mouseX;
lastMouseY = (int) mouseY;
return true;
}
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
if (lastMouseButton == button) {
input.mouseUp(lastMouseButton + 1, (int) mouseX, (int) mouseY);
lastMouseButton = -1;
}
return false;
}
@Override
public boolean mouseDragged(double mouseX, double mouseY, int button, double v2, double v3) {
if (button == lastMouseButton && (mouseX != lastMouseX || mouseY != lastMouseY)) {
input.mouseDrag(button + 1, (int) mouseX, (int) mouseY);
lastMouseX = (int) mouseX;
lastMouseY = (int) mouseY;
}
return false;
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
input.mouseScroll(delta < 0 ? 1 : -1, (int) mouseX, (int) mouseY);
lastMouseX = (int) mouseX;
lastMouseY = (int) mouseY;
return true;
}
@Override
public int getSizeX() {
return 256;
}
@Override
public int getSizeY() {
return 256;
}
@Override
public ResourceLocation getTexture() {
return null;
}
}