Skip to content

Commit 36193be

Browse files
committed
Simplify KeyboardScreen
1 parent b689497 commit 36193be

2 files changed

Lines changed: 53 additions & 136 deletions

File tree

src/main/java/de/srendi/advancedperipherals/client/screens/KeyboardScreen.java

Lines changed: 42 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,42 @@
22

33
import com.mojang.blaze3d.vertex.PoseStack;
44
import dan200.computercraft.client.gui.ClientInputHandler;
5+
import dan200.computercraft.client.gui.widgets.WidgetTerminal;
6+
import dan200.computercraft.core.terminal.Terminal;
57
import dan200.computercraft.shared.computer.core.InputHandler;
68
import de.srendi.advancedperipherals.client.screens.base.BaseScreen;
79
import de.srendi.advancedperipherals.common.container.KeyboardContainer;
8-
import net.minecraft.SharedConstants;
10+
import net.minecraft.client.KeyMapping;
911
import net.minecraft.client.Minecraft;
1012
import net.minecraft.network.chat.Component;
1113
import net.minecraft.resources.ResourceLocation;
1214
import net.minecraft.world.entity.player.Inventory;
1315
import org.jetbrains.annotations.NotNull;
1416
import org.lwjgl.glfw.GLFW;
1517

16-
import java.util.BitSet;
17-
1818
/**
1919
* A simple screen but without any rendering calls. Used to unlock the mouse so we can freely write stuff
2020
* <p>
21-
* Char/key logic stolen from CC's WidgetTerminal
21+
* We just create a terminal which is used to forward all the key presses and mouse clicks but we don't render it.
2222
*/
2323
public class KeyboardScreen extends BaseScreen<KeyboardContainer> {
2424

2525
protected final InputHandler input;
26-
private final BitSet keysDown = new BitSet(256);
27-
28-
private float terminateTimer = -1;
29-
private float rebootTimer = -1;
30-
private float shutdownTimer = -1;
26+
private final Terminal terminalData;
3127

32-
private int lastMouseButton = -1;
33-
private int lastMouseX = -1;
34-
private int lastMouseY = -1;
28+
private WidgetTerminal terminal;
3529

3630
public KeyboardScreen(KeyboardContainer screenContainer, Inventory inv, Component titleIn) {
3731
super(screenContainer, inv, titleIn);
3832
input = new ClientInputHandler(menu);
33+
terminalData = new Terminal(0, 0, false);
3934
}
4035

4136
@Override
4237
public void render(@NotNull PoseStack poseStack, int x, int y, float partialTicks) {
4338
Minecraft minecraft = Minecraft.getInstance();
4439
float scale = 2f;
4540
int screenWidth = minecraft.getWindow().getGuiScaledWidth();
46-
int screenHeight = minecraft.getWindow().getGuiScaledHeight();
4741
// Make the text a bit smaller on small screens
4842
if (screenWidth <= 1080)
4943
scale = 1f;
@@ -52,12 +46,23 @@ public void render(@NotNull PoseStack poseStack, int x, int y, float partialTick
5246
String text = "Press ESC to close the Keyboard Screen.";
5347
float textX = (screenWidth / 2f - minecraft.font.width(text) * scale / 2f) / scale;
5448
minecraft.font.drawShadow(poseStack, text, textX, 1, 0xFFFFFF);
49+
}
50+
51+
@Override
52+
protected void init() {
53+
passEvents = true;
54+
KeyMapping.releaseAll();
5555

56-
// Prevents JEI/REI/EMI from rendering. Maybe not the best way, but it works for now.
57-
poseStack.scale(4f, 4f, 1);
58-
minecraft.font.draw(poseStack, "", screenWidth, screenHeight, 0xFFFFFF);
56+
super.init();
57+
minecraft.keyboardHandler.setSendRepeatsToGui(true);
58+
59+
terminal = addWidget(new WidgetTerminal(terminalData, new ClientInputHandler(menu), 0, 0));
60+
terminal.visible = false;
61+
terminal.active = false;
62+
setFocused(terminal);
5963
}
6064

65+
6166
@Override
6267
protected void renderBg(@NotNull PoseStack matrixStack, float partialTicks, int x, int y) {
6368
}
@@ -66,149 +71,52 @@ protected void renderBg(@NotNull PoseStack matrixStack, float partialTicks, int
6671
public void renderBackground(@NotNull PoseStack pPoseStack) {
6772
}
6873

69-
@Override
70-
public boolean charTyped(char ch, int modifiers) {
71-
if (ch >= 32 && ch <= 126 || ch >= 160 && ch <= 255) { // printable chars in byte range
72-
// Queue the "char" event
73-
input.queueEvent("char", new Object[]{Character.toString(ch)});
74-
}
75-
76-
return true;
77-
}
7874

7975
@Override
80-
public boolean keyPressed(int key, int scancode, int modifiers) {
81-
if (key == GLFW.GLFW_KEY_ESCAPE) {
82-
onClose();
83-
return true;
84-
}
85-
if ((modifiers & GLFW.GLFW_MOD_CONTROL) != 0) {
86-
switch (key) {
87-
case GLFW.GLFW_KEY_T:
88-
if (terminateTimer < 0) terminateTimer = 0;
89-
return true;
90-
case GLFW.GLFW_KEY_S:
91-
if (shutdownTimer < 0) shutdownTimer = 0;
92-
return true;
93-
case GLFW.GLFW_KEY_R:
94-
if (rebootTimer < 0) rebootTimer = 0;
95-
return true;
96-
97-
case GLFW.GLFW_KEY_V:
98-
// Ctrl+V for paste
99-
String clipboard = Minecraft.getInstance().keyboardHandler.getClipboard();
100-
if (clipboard != null) {
101-
// Clip to the first occurrence of \r or \n
102-
int newLineIndex1 = clipboard.indexOf("\r");
103-
int newLineIndex2 = clipboard.indexOf("\n");
104-
if (newLineIndex1 >= 0 && newLineIndex2 >= 0) {
105-
clipboard = clipboard.substring(0, Math.min(newLineIndex1, newLineIndex2));
106-
} else if (newLineIndex1 >= 0) {
107-
clipboard = clipboard.substring(0, newLineIndex1);
108-
} else if (newLineIndex2 >= 0) {
109-
clipboard = clipboard.substring(0, newLineIndex2);
110-
}
111-
112-
// Filter the string
113-
clipboard = SharedConstants.filterText(clipboard);
114-
if (!clipboard.isEmpty()) {
115-
// Clip to 512 characters and queue the event
116-
if (clipboard.length() > 512) clipboard = clipboard.substring(0, 512);
117-
input.queueEvent("paste", new Object[]{clipboard});
118-
}
119-
120-
return true;
121-
}
122-
}
123-
}
124-
125-
if (key >= 0 && terminateTimer < 0 && rebootTimer < 0 && shutdownTimer < 0) {
126-
// Queue the "key" event and add to the down set
127-
boolean repeat = keysDown.get(key);
128-
keysDown.set(key);
129-
input.keyDown(key, repeat);
130-
}
131-
132-
return true;
76+
public final void removed() {
77+
super.removed();
78+
minecraft.keyboardHandler.setSendRepeatsToGui(false);
13379
}
13480

13581
@Override
136-
public boolean keyReleased(int key, int scancode, int modifiers) {
137-
// Queue the "key_up" event and remove from the down set
138-
if (key >= 0 && keysDown.get(key)) {
139-
keysDown.set(key, false);
140-
input.keyUp(key);
141-
}
142-
143-
switch (key) {
144-
case GLFW.GLFW_KEY_T:
145-
terminateTimer = -1;
146-
break;
147-
case GLFW.GLFW_KEY_R:
148-
rebootTimer = -1;
149-
break;
150-
case GLFW.GLFW_KEY_S:
151-
shutdownTimer = -1;
152-
break;
153-
case GLFW.GLFW_KEY_LEFT_CONTROL:
154-
case GLFW.GLFW_KEY_RIGHT_CONTROL:
155-
terminateTimer = rebootTimer = shutdownTimer = -1;
156-
break;
157-
}
158-
159-
return true;
82+
public boolean mouseScrolled(double pMouseX, double pMouseY, double pDelta) {
83+
minecraft.player.getInventory().swapPaint(pDelta);
84+
return super.mouseScrolled(pMouseX, pMouseY, pDelta);
16085
}
16186

16287
@Override
163-
public boolean mouseClicked(double mouseX, double mouseY, int button) {
164-
input.mouseClick(button + 1, (int) mouseX, (int) mouseY);
165-
166-
lastMouseButton = button;
167-
lastMouseX = (int) mouseX;
168-
lastMouseY = (int) mouseY;
169-
170-
return true;
88+
public void onClose() {
89+
// Don't allow closing using standard keys like E. Closing using ESCAPE is still possible due to the keyPressed method
17190
}
17291

17392
@Override
174-
public boolean mouseReleased(double mouseX, double mouseY, int button) {
175-
if (lastMouseButton == button) {
176-
input.mouseUp(lastMouseButton + 1, (int) mouseX, (int) mouseY);
177-
lastMouseButton = -1;
178-
}
179-
93+
public boolean isPauseScreen() {
18094
return false;
18195
}
18296

18397
@Override
184-
public boolean mouseDragged(double mouseX, double mouseY, int button, double v2, double v3) {
185-
if (button == lastMouseButton && (mouseX != lastMouseX || mouseY != lastMouseY)) {
186-
input.mouseDrag(button + 1, (int) mouseX, (int) mouseY);
187-
lastMouseX = (int) mouseX;
188-
lastMouseY = (int) mouseY;
98+
public final boolean keyPressed(int key, int scancode, int modifiers) {
99+
if (key == GLFW.GLFW_KEY_ESCAPE) {
100+
super.onClose();
101+
return true;
102+
}
103+
// Forward the tab key to the terminal, rather than moving between controls.
104+
if (key == GLFW.GLFW_KEY_TAB && getFocused() != null && getFocused() == terminal) {
105+
return getFocused().keyPressed(key, scancode, modifiers);
189106
}
190107

191-
return false;
192-
}
193-
194-
@Override
195-
public boolean mouseScrolled(double mouseX, double mouseY, double delta) {
196-
input.mouseScroll(delta < 0 ? 1 : -1, (int) mouseX, (int) mouseY);
197-
198-
lastMouseX = (int) mouseX;
199-
lastMouseY = (int) mouseY;
200-
201-
return true;
108+
return super.keyPressed(key, scancode, modifiers);
202109
}
203110

111+
// We prevent jei by increasing the image size, even if we don't render it
204112
@Override
205113
public int getSizeX() {
206-
return 256;
114+
return 4096;
207115
}
208116

209117
@Override
210118
public int getSizeY() {
211-
return 256;
119+
return 4096;
212120
}
213121

214122
@Override

src/main/java/de/srendi/advancedperipherals/common/container/base/BaseContainer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.minecraft.world.inventory.Slot;
1111
import net.minecraft.world.item.ItemStack;
1212
import net.minecraft.world.level.Level;
13+
import net.minecraft.world.level.block.entity.BlockEntity;
1314
import net.minecraftforge.items.IItemHandler;
1415
import net.minecraftforge.items.SlotItemHandler;
1516
import net.minecraftforge.items.wrapper.InvWrapper;
@@ -23,8 +24,16 @@ public abstract class BaseContainer extends AbstractContainerMenu {
2324
protected BaseContainer(@Nullable MenuType<?> type, int id, Inventory inventory, BlockPos pos, Level world) {
2425
super(type, id);
2526
this.inventory = new InvWrapper(inventory);
26-
if (world != null)
27-
this.tileEntity = (PeripheralBlockEntity<?>) world.getBlockEntity(pos);
27+
if (world != null) {
28+
BlockEntity blockEntity = world.getBlockEntity(pos);
29+
// for player containers, pos is the position of the player
30+
// We don't actual need a block entity for player containers
31+
// But if a player stands for example on a mekanism cable, setting the tileEntity by casting and without a check
32+
// would prevent opening the screen
33+
if (blockEntity instanceof PeripheralBlockEntity<?> peripheralBlockEntity) {
34+
tileEntity = peripheralBlockEntity;
35+
}
36+
}
2837
}
2938

3039
@Override

0 commit comments

Comments
 (0)