mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-07-17 17:32:55 +00:00
Add pasting support to the standalone emulator
- Move paste normalisation code to StringUtil, so it can be shared by emulators. - Add paste support to the emulator.
This commit is contained in:
parent
784e623776
commit
87345c6b2e
@ -8,8 +8,8 @@ import com.mojang.blaze3d.vertex.Tesselator;
|
|||||||
import dan200.computercraft.client.render.RenderTypes;
|
import dan200.computercraft.client.render.RenderTypes;
|
||||||
import dan200.computercraft.client.render.text.FixedWidthFontRenderer;
|
import dan200.computercraft.client.render.text.FixedWidthFontRenderer;
|
||||||
import dan200.computercraft.core.terminal.Terminal;
|
import dan200.computercraft.core.terminal.Terminal;
|
||||||
|
import dan200.computercraft.core.util.StringUtil;
|
||||||
import dan200.computercraft.shared.computer.core.InputHandler;
|
import dan200.computercraft.shared.computer.core.InputHandler;
|
||||||
import net.minecraft.SharedConstants;
|
|
||||||
import net.minecraft.client.Minecraft;
|
import net.minecraft.client.Minecraft;
|
||||||
import net.minecraft.client.gui.GuiGraphics;
|
import net.minecraft.client.gui.GuiGraphics;
|
||||||
import net.minecraft.client.gui.components.AbstractWidget;
|
import net.minecraft.client.gui.components.AbstractWidget;
|
||||||
@ -112,26 +112,8 @@ public class TerminalWidget extends AbstractWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void paste() {
|
private void paste() {
|
||||||
var clipboard = Minecraft.getInstance().keyboardHandler.getClipboard();
|
var clipboard = StringUtil.normaliseClipboardString(Minecraft.getInstance().keyboardHandler.getClipboard());
|
||||||
|
if (!clipboard.isEmpty()) computer.queueEvent("paste", new Object[]{ clipboard });
|
||||||
// Clip to the first occurrence of \r or \n
|
|
||||||
var newLineIndex1 = clipboard.indexOf('\r');
|
|
||||||
var 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);
|
|
||||||
computer.queueEvent("paste", new Object[]{ clipboard });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -8,18 +8,48 @@ public final class StringUtil {
|
|||||||
private StringUtil() {
|
private StringUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String normaliseLabel(String label) {
|
private static boolean isAllowed(char c) {
|
||||||
var length = Math.min(32, label.length());
|
return (c >= ' ' && c <= '~') || (c >= 161 && c <= 172) || (c >= 174 && c <= 255);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String removeSpecialCharacters(String text, int length) {
|
||||||
var builder = new StringBuilder(length);
|
var builder = new StringBuilder(length);
|
||||||
for (var i = 0; i < length; i++) {
|
for (var i = 0; i < length; i++) {
|
||||||
var c = label.charAt(i);
|
var c = text.charAt(i);
|
||||||
if ((c >= ' ' && c <= '~') || (c >= 161 && c <= 172) || (c >= 174 && c <= 255)) {
|
builder.append(isAllowed(c) ? c : '?');
|
||||||
builder.append(c);
|
|
||||||
} else {
|
|
||||||
builder.append('?');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return builder.toString();
|
return builder.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static String normaliseLabel(String text) {
|
||||||
|
return removeSpecialCharacters(text, Math.min(32, text.length()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Normalise a string from the clipboard, suitable for pasting into a computer.
|
||||||
|
* <p>
|
||||||
|
* This removes special characters and strips to the first line of text.
|
||||||
|
*
|
||||||
|
* @param clipboard The text from the clipboard.
|
||||||
|
* @return The normalised clipboard text.
|
||||||
|
*/
|
||||||
|
public static String normaliseClipboardString(String clipboard) {
|
||||||
|
// Clip to the first occurrence of \r or \n
|
||||||
|
var newLineIndex1 = clipboard.indexOf('\r');
|
||||||
|
var newLineIndex2 = clipboard.indexOf('\n');
|
||||||
|
|
||||||
|
int length;
|
||||||
|
if (newLineIndex1 >= 0 && newLineIndex2 >= 0) {
|
||||||
|
length = Math.min(newLineIndex1, newLineIndex2);
|
||||||
|
} else if (newLineIndex1 >= 0) {
|
||||||
|
length = newLineIndex1;
|
||||||
|
} else if (newLineIndex2 >= 0) {
|
||||||
|
length = newLineIndex2;
|
||||||
|
} else {
|
||||||
|
length = clipboard.length();
|
||||||
|
}
|
||||||
|
|
||||||
|
return removeSpecialCharacters(clipboard, Math.min(length, 512));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,6 +8,7 @@ import dan200.computercraft.core.apis.handles.ArrayByteChannel;
|
|||||||
import dan200.computercraft.core.apis.transfer.TransferredFile;
|
import dan200.computercraft.core.apis.transfer.TransferredFile;
|
||||||
import dan200.computercraft.core.apis.transfer.TransferredFiles;
|
import dan200.computercraft.core.apis.transfer.TransferredFiles;
|
||||||
import dan200.computercraft.core.computer.Computer;
|
import dan200.computercraft.core.computer.Computer;
|
||||||
|
import dan200.computercraft.core.util.StringUtil;
|
||||||
import org.lwjgl.glfw.GLFW;
|
import org.lwjgl.glfw.GLFW;
|
||||||
import org.lwjgl.glfw.GLFWDropCallback;
|
import org.lwjgl.glfw.GLFWDropCallback;
|
||||||
import org.lwjgl.glfw.GLFWKeyCallbackI;
|
import org.lwjgl.glfw.GLFWKeyCallbackI;
|
||||||
@ -54,16 +55,24 @@ public class InputState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void onKeyEvent(int key, int action, int modifiers) {
|
public void onKeyEvent(long window, int key, int action, int modifiers) {
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case GLFW.GLFW_PRESS, GLFW.GLFW_REPEAT -> keyPressed(key, modifiers);
|
case GLFW.GLFW_PRESS, GLFW.GLFW_REPEAT -> keyPressed(window, key, modifiers);
|
||||||
case GLFW.GLFW_RELEASE -> keyReleased(key);
|
case GLFW.GLFW_RELEASE -> keyReleased(key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void keyPressed(int key, int modifiers) {
|
private void keyPressed(long window, int key, int modifiers) {
|
||||||
if (key == GLFW.GLFW_KEY_ESCAPE) return;
|
if (key == GLFW.GLFW_KEY_ESCAPE) return;
|
||||||
|
|
||||||
|
if (key == GLFW.GLFW_KEY_V && modifiers == GLFW.GLFW_MOD_CONTROL) {
|
||||||
|
var string = GLFW.glfwGetClipboardString(window);
|
||||||
|
if (string != null) {
|
||||||
|
var clipboard = StringUtil.normaliseClipboardString(string);
|
||||||
|
if (!clipboard.isEmpty()) computer.queueEvent("paste", new Object[]{ clipboard });
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if ((modifiers & GLFW.GLFW_MOD_CONTROL) != 0) {
|
if ((modifiers & GLFW.GLFW_MOD_CONTROL) != 0) {
|
||||||
switch (key) {
|
switch (key) {
|
||||||
|
@ -204,7 +204,7 @@ public class Main {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add all our callbacks
|
// Add all our callbacks
|
||||||
glfwSetKeyCallback(window, (w, key, scancode, action, mods) -> inputState.onKeyEvent(key, action, mods));
|
glfwSetKeyCallback(window, (w, key, scancode, action, mods) -> inputState.onKeyEvent(w, key, action, mods));
|
||||||
glfwSetCharModsCallback(window, (w, codepoint, mods) -> inputState.onCharEvent(codepoint));
|
glfwSetCharModsCallback(window, (w, codepoint, mods) -> inputState.onCharEvent(codepoint));
|
||||||
glfwSetDropCallback(window, (w, count, files) -> inputState.onFileDrop(count, files));
|
glfwSetDropCallback(window, (w, count, files) -> inputState.onFileDrop(count, files));
|
||||||
glfwSetMouseButtonCallback(window, (w, button, action, mods) -> inputState.onMouseClick(button, action));
|
glfwSetMouseButtonCallback(window, (w, button, action, mods) -> inputState.onMouseClick(button, action));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user