Skip to content
This repository has been archived by the owner on Aug 6, 2024. It is now read-only.

Commit

Permalink
feat: user input
Browse files Browse the repository at this point in the history
  • Loading branch information
ajh123 committed Feb 11, 2024
1 parent 148a309 commit 65b9d07
Show file tree
Hide file tree
Showing 8 changed files with 388 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class RealisticComputersClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientPlayNetworking.registerGlobalReceiver(ModNetworking.OPEN_VNC_SCREEN, (client, handler, buf, responseSender) -> {
ClientPlayNetworking.registerGlobalReceiver(ModNetworking.VNC_SCREEN_OPEN, (client, handler, buf, responseSender) -> {
BlockPos terminalPosition = buf.readBlockPos();

client.execute(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public void close() {
this.client.setScreen(this.parent);
PacketByteBuf buf = PacketByteBufs.create();
buf.writeBlockPos(this.terminalPosition);
ClientPlayNetworking.send(ModNetworking.CLOSE_VNC_SCREEN, buf);
ClientPlayNetworking.send(ModNetworking.VNC_SCREEN_CLOSE, buf);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public VNCScreenLayout() {

screenWidget = new VNCScreenWidget();
root.add(screenWidget, 0, 0, 19, 12);
requestFocus(screenWidget);

root.validate(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
import io.github.cottonmc.cotton.gui.widget.WWidget;
import io.github.cottonmc.cotton.gui.widget.data.InputResult;
import io.github.cottonmc.cotton.gui.widget.data.Texture;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.math.ColorHelper;
import uk.minersonline.RealisticComputers.ModNetworking;


public class VNCScreenWidget extends WWidget {
private Texture image = null;
Expand All @@ -25,10 +31,81 @@ public boolean canResize() {
return true;
}

@Override
public boolean canFocus() {
return true;
}

public void setImage(Texture image) {
if (this.image != null) {
MinecraftClient.getInstance().getTextureManager().destroyTexture(this.image.image());
}
this.image = image;
}

@Override
public InputResult onKeyPressed(int ch, int key, int modifiers) {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeInt(ch);
buf.writeInt(modifiers);
buf.writeBoolean(true);
ClientPlayNetworking.send(ModNetworking.VNC_KEY, buf);
return super.onKeyPressed(ch, key, modifiers);
}

@Override
public InputResult onKeyReleased(int ch, int key, int modifiers) {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeInt(ch);
buf.writeInt(modifiers);
buf.writeBoolean(false);
ClientPlayNetworking.send(ModNetworking.VNC_KEY, buf);
return super.onKeyReleased(ch, key, modifiers);
}

@Override
public InputResult onMouseMove(int x, int y) {
requestFocus();
PacketByteBuf buf = PacketByteBufs.create();
buf.writeInt(x);
buf.writeInt(y);
ClientPlayNetworking.send(ModNetworking.VNC_MOUSE_MOVE, buf);
return super.onMouseMove(x, y);
}

@Override
public InputResult onMouseUp(int x, int y, int button) {
requestFocus();
PacketByteBuf buf = PacketByteBufs.create();
buf.writeInt(x);
buf.writeInt(y);
if (button == 0) {
buf.writeInt(1);
} else if (button == 1) {
buf.writeInt(3);
} else {
buf.writeInt(2);
}
buf.writeBoolean(false);
ClientPlayNetworking.send(ModNetworking.VNC_MOUSE_MOVE, buf);
return super.onMouseUp(x, y, button);
}

@Override
public InputResult onMouseDown(int x, int y, int button) {
requestFocus();
PacketByteBuf buf = PacketByteBufs.create();
buf.writeInt(x);
buf.writeInt(y);
if (button == 0) {
buf.writeInt(1);
} else if (button == 1) {
buf.writeInt(3);
} else {
buf.writeInt(2);
}
buf.writeBoolean(true);
ClientPlayNetworking.send(ModNetworking.VNC_MOUSE_MOVE, buf);
return super.onMouseDown(x, y, button);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.BlockPos;
import org.lwjgl.glfw.GLFW;
import uk.minersonline.RealisticComputers.utils.KeyUtils;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -13,12 +17,15 @@
public class ModNetworking {
public static Map<ServerPlayerEntity, BlockPos> openScreens = new HashMap<>();
public static Map<ServerPlayerEntity, VNCSession> openSessions = new HashMap<>();
public static final Identifier OPEN_VNC_SCREEN = new Identifier(MOD_ID, "vnc_screen_open");
public static final Identifier CLOSE_VNC_SCREEN = new Identifier(MOD_ID, "vnc_screen_close");
public static final Identifier VNC_SCREEN_OPEN = new Identifier(MOD_ID, "vnc_screen_open");
public static final Identifier VNC_SCREEN_CLOSE = new Identifier(MOD_ID, "vnc_screen_close");
public static final Identifier VNC_SCREEN_DATA = new Identifier(MOD_ID, "vnc_screen_data");
public static final Identifier VNC_MOUSE_MOVE = new Identifier(MOD_ID, "vnc_mouse_move");
public static final Identifier VNC_MOUSE_CLICK = new Identifier(MOD_ID, "vnc_mouse_click");
public static final Identifier VNC_KEY = new Identifier(MOD_ID, "vnc_key");

public static void serverInit() {
ServerPlayNetworking.registerGlobalReceiver(CLOSE_VNC_SCREEN, (server, player, handler, buf, responseSender) -> {
ServerPlayNetworking.registerGlobalReceiver(VNC_SCREEN_CLOSE, (server, player, handler, buf, responseSender) -> {
VNCSession session = openSessions.get(player);
session.end();
try {
Expand All @@ -32,5 +39,63 @@ public static void serverInit() {
openScreens.put(player, null);
RealisticComputers.LOGGER.info(player.getDisplayName().getString() + " has closed the vnc terminal at " + terminalPosition);
});

ServerPlayNetworking.registerGlobalReceiver(VNC_MOUSE_MOVE, (server, player, handler, buf, responseSender) -> {
VNCSession session = openSessions.get(player);
if (session != null) {
int x = buf.readInt();
int y = buf.readInt();
session.sendMousePos(x, y);
}
});

ServerPlayNetworking.registerGlobalReceiver(VNC_MOUSE_CLICK, (server, player, handler, buf, responseSender) -> {
VNCSession session = openSessions.get(player);
if (session != null) {
int x = buf.readInt();
int y = buf.readInt();
int btn = buf.readInt();
boolean pressed = buf.readBoolean();
session.sendMouseClick(x, y, btn, pressed);
}
});

ServerPlayNetworking.registerGlobalReceiver(VNC_KEY, (server, player, handler, buf, responseSender) -> {
VNCSession session = openSessions.get(player);
if (session != null) {
int key = buf.readInt();
int glfwModifiers = buf.readInt();
boolean pressed = buf.readBoolean();

KeyEvent keyPressEvent;
boolean isChar = KeyUtils.isCharacterKey(key);
int modifiers = KeyUtils.getModifiers(glfwModifiers);
char type = KeyEvent.CHAR_UNDEFINED;
if (isChar) {
type = KeyUtils.getCharacterFromKeyCode(key, (glfwModifiers & GLFW.GLFW_MOD_SHIFT) != 0);
}
Component source = new Component() {};
if (pressed) {
keyPressEvent = new KeyEvent(
source,
KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
modifiers,
KeyUtils.convertKey(key),
type
);
} else {
keyPressEvent = new KeyEvent(
source,
KeyEvent.KEY_RELEASED,
System.currentTimeMillis(),
modifiers,
KeyUtils.convertKey(key),
type
);
}
session.handleKey(keyPressEvent);
}
});
}
}
14 changes: 14 additions & 0 deletions src/main/java/uk/minersonline/RealisticComputers/VNCSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import uk.minersonline.RealisticComputers.utils.ImageUtils;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.IOException;

public class VNCSession extends Thread {
Expand Down Expand Up @@ -59,4 +60,17 @@ public void end() {
client.stop();
running = false;
}

public void sendMousePos(int x, int y) {
client.moveMouse(x, y);
}

public void sendMouseClick(int x, int y, int btn, boolean pressed) {
client.moveMouse(x, y);
client.updateMouseButton(btn, pressed);
}

public void handleKey(KeyEvent event) {
client.handleKeyEvent(event);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEnt
if (!world.isClient()) {
PacketByteBuf buf = PacketByteBufs.create();
buf.writeBlockPos(pos);
ServerPlayNetworking.send((ServerPlayerEntity) player, ModNetworking.OPEN_VNC_SCREEN, buf);
ServerPlayNetworking.send((ServerPlayerEntity) player, ModNetworking.VNC_SCREEN_OPEN, buf);
ModNetworking.openScreens.put((ServerPlayerEntity) player, pos);
VNCSession session = new VNCSession((ServerPlayerEntity) player);
session.start();
Expand Down
Loading

0 comments on commit 65b9d07

Please sign in to comment.