1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-09-06 12:27:56 +00:00

Move computer events to a single point

This abstraction never made much sense on InputHandler, as we only leave
the default methods on ServerComputer.

We now add a new class (ComputerEvents), which has a series of *static*
methods, that can queue an event on a ComputerEvents.Receiver object.
This is a bit of an odd indirection (why not just make them instance
methods on Receiver?!), but I don't really want those methods leaking
everywhere.
This commit is contained in:
Jonathan Coates
2025-01-18 19:18:09 +00:00
parent 6739c4c6c0
commit 938eb38ad5
7 changed files with 112 additions and 69 deletions

View File

@@ -8,6 +8,7 @@ import dan200.computercraft.core.apis.handles.ArrayByteChannel;
import dan200.computercraft.core.apis.transfer.TransferredFile;
import dan200.computercraft.core.apis.transfer.TransferredFiles;
import dan200.computercraft.core.computer.Computer;
import dan200.computercraft.core.computer.ComputerEvents;
import dan200.computercraft.core.util.StringUtil;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWDropCallback;
@@ -92,7 +93,7 @@ public class InputState {
// Queue the "key" event and add to the down set
var repeat = keysDown.get(key);
keysDown.set(key);
computer.queueEvent("key", new Object[]{ key, repeat });
ComputerEvents.keyDown(computer, key, repeat);
}
}
@@ -100,7 +101,7 @@ public class InputState {
// Queue the "key_up" event and remove from the down set
if (key >= 0 && keysDown.get(key)) {
keysDown.set(key, false);
computer.queueEvent("key_up", new Object[]{ key });
ComputerEvents.keyUp(computer, key);
}
switch (key) {
@@ -115,12 +116,12 @@ public class InputState {
public void onMouseClick(int button, int action) {
switch (action) {
case GLFW.GLFW_PRESS -> {
computer.queueEvent("mouse_click", new Object[]{ button + 1, lastMouseX + 1, lastMouseY + 1 });
ComputerEvents.mouseClick(computer, button + 1, lastMouseX + 1, lastMouseY + 1);
lastMouseButton = button;
}
case GLFW.GLFW_RELEASE -> {
if (button == lastMouseButton) {
computer.queueEvent("mouse_click", new Object[]{ button + 1, lastMouseX + 1, lastMouseY + 1 });
ComputerEvents.mouseUp(computer, button + 1, lastMouseX + 1, lastMouseY + 1);
lastMouseButton = -1;
}
}
@@ -133,13 +134,13 @@ public class InputState {
lastMouseX = mouseX;
lastMouseY = mouseY;
if (lastMouseButton != -1) {
computer.queueEvent("mouse_drag", new Object[]{ lastMouseButton + 1, mouseX + 1, mouseY + 1 });
ComputerEvents.mouseDrag(computer, lastMouseButton + 1, mouseX + 1, mouseY + 1);
}
}
public void onMouseScroll(double yOffset) {
if (yOffset != 0) {
computer.queueEvent("mouse_scroll", new Object[]{ yOffset < 0 ? 1 : -1, lastMouseX + 1, lastMouseY + 1 });
ComputerEvents.mouseScroll(computer, yOffset < 0 ? 1 : -1, lastMouseX + 1, lastMouseY + 1);
}
}