Skip to content

Commit

Permalink
[VirtualInput] Created VirtualCameraAngleInput
Browse files Browse the repository at this point in the history
  • Loading branch information
ScribbleTAS committed Jan 19, 2024
1 parent d520216 commit 0c6a116
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,6 @@ public int playback_redirectKeyboardGetEventKey() {
return TASmodClient.virtual.getEventKeyboardKey();
}

/**
* Runs everytime {@link #playback_redirectKeyboardNext()} has an event ready. Redirects {@link Keyboard#getEventCharacter()}
* @return The character for the current event in {@link VirtualInput2}
*/
@Redirect(method = "runTickKeyboard", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventCharacter()C", remap = false))
public char playback_redirectKeyboardGetEventCharacter() {
return TASmodClient.virtual.getEventKeyboardCharacter();
}

/**
* Runs everytime {@link #playback_redirectKeyboardNext()} has an event ready. Redirects {@link Keyboard#getEventKeyState()}
* @return Whether the key is down in {@link VirtualInput2}
Expand All @@ -106,6 +97,15 @@ public boolean playback_redirectGetEventState() {
return TASmodClient.virtual.getEventKeyboardState();
}

/**
* Runs everytime {@link #playback_redirectKeyboardNext()} has an event ready. Redirects {@link Keyboard#getEventCharacter()}
* @return The character for the current event in {@link VirtualInput2}
*/
@Redirect(method = "runTickKeyboard", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventCharacter()C", remap = false))
public char playback_redirectKeyboardGetEventCharacter() {
return TASmodClient.virtual.getEventKeyboardCharacter();
}

/**
* Runs everytime {@link #playback_redirectKeyboardNext()} has an event ready. Redirects {@link Keyboard#isKeyDown(int)}
* @return Whether the key is down in {@link VirtualInput2}
Expand All @@ -115,6 +115,21 @@ public boolean playback_redirectIsKeyDown(int keyCode) {
return TASmodClient.virtual.isKeyDown(keyCode);
}

@Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKey()I", remap = false))
public int playback_redirectGetEventKeyDPK() {
return TASmodClient.virtual.getEventKeyboardKey();
}

@Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKeyState()Z", remap = false))
public boolean playback_redirectGetEventKeyStateDPK() {
return TASmodClient.virtual.getEventKeyboardState();
}

@Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventCharacter()C", remap = false))
public char playback_redirectGetEventCharacterDPK() {
return TASmodClient.virtual.getEventKeyboardCharacter();
}

// ============================ Mouse

@Inject(method = "runTickMouse", at = @At(value = "HEAD"))
Expand Down Expand Up @@ -146,18 +161,4 @@ public int playback_redirectGetEventDWheel() {
return TASmodClient.virtual.getEventMouseScrollWheel();
}

@Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKey()I", remap = false))
public int playback_redirectGetEventKeyDPK() {
return TASmodClient.virtual.getEventKeyboardKey();
}

@Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventCharacter()C", remap = false))
public char playback_redirectGetEventCharacterDPK() {
return TASmodClient.virtual.getEventKeyboardCharacter();
}

@Redirect(method = "dispatchKeypresses", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Keyboard;getEventKeyState()Z", remap = false))
public boolean playback_redirectGetEventKeyStateDPK() {
return TASmodClient.virtual.getEventKeyboardState();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public boolean isParent() {
}

public void getCameraAngleSubticks(Queue<VirtualCameraAngle2> reference) {
reference.addAll(subtickList);
if(isParent()) {
reference.addAll(subtickList);
}
}

@Override
Expand Down
95 changes: 54 additions & 41 deletions src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput2.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,52 @@
/**
* Main component for redirecting inputs.<br>
* <br>
* This class mimics the LWJGL classes {@link org.lwjgl.input.Keyboard} and {@link org.lwjgl.input.Mouse}.<br>
* This class mimics the LWJGL classes {@link org.lwjgl.input.Keyboard} and
* {@link org.lwjgl.input.Mouse}.<br>
* <br>
*/
public class VirtualInput2 {
private final VirtualKeyboardInput keyboardInput;
private final VirtualMouseInput mouseInput;
private final VirtualCameraAngleInput cameraAngleInput;

public VirtualInput2() {
this(new VirtualKeyboard2(), new VirtualMouse2(), new VirtualCameraAngle(0, 0));
}

/**
* Creates a virtual input with pre-loaded values
*
* @param preloadedKeyboard
* @param preloadedMouse
* @param preloadedCamera
*/
public VirtualInput2(VirtualKeyboard2 preloadedKeyboard, VirtualMouse2 preloadedMouse, VirtualCameraAngle preloadedCamera) {
keyboardInput = new VirtualKeyboardInput(preloadedKeyboard);
mouseInput = new VirtualMouseInput(preloadedMouse);
this.cameraAngle = preloadedCamera;
cameraAngleInput = new VirtualCameraAngleInput(preloadedCamera);
}

/**
* Updates the logic for {@link #keyboardInput}, {@link #mouseInput} and {@link #cameraAngle}<br>
* Updates the logic for {@link #keyboardInput}, {@link #mouseInput} and
* {@link #cameraAngle}<br>
* Runs every frame
*
* @see MixinMinecraft#playback_injectRunGameLoop(CallbackInfo)
* @param currentScreen The current screen from Minecraft.class.
* Used for checking if the mouse logic should be adapted to GUIScreens
* @param currentScreen The current screen from Minecraft.class. Used for
* checking if the mouse logic should be adapted to
* GUIScreens
*/
public void update(GuiScreen currentScreen){
public void update(GuiScreen currentScreen) {
while (Keyboard.next()) {
keyboardInput.updateNextKeyboard(
Keyboard.getEventKey(),
Keyboard.getEventKeyState(),
Keyboard.getEventCharacter());
keyboardInput.updateNextKeyboard(Keyboard.getEventKey(), Keyboard.getEventKeyState(), Keyboard.getEventCharacter());
}
while (Mouse.next()) {
if(currentScreen == null) {
mouseInput.updateNextMouse(
Mouse.getEventButton(),
Mouse.getEventButtonState(),
Mouse.getEventDWheel(),
null,
null);
if (currentScreen == null) {
mouseInput.updateNextMouse(Mouse.getEventButton(), Mouse.getEventButtonState(), Mouse.getEventDWheel(), null, null);
} else {
Ducks.GuiScreenDuck screen = (Ducks.GuiScreenDuck) currentScreen;
mouseInput.updateNextMouse(
Mouse.getEventButton(),
Mouse.getEventButtonState(),
Mouse.getEventDWheel(),
screen.calcX(Mouse.getEventX()),
screen.calcY(Mouse.getEventY()));
mouseInput.updateNextMouse(Mouse.getEventButton(), Mouse.getEventButtonState(), Mouse.getEventDWheel(), screen.calcX(Mouse.getEventX()), screen.calcY(Mouse.getEventY()));
}
}
}
Expand All @@ -79,14 +72,15 @@ public VirtualMouseInput getMouseInput() {
return mouseInput;
}

public VirtualCameraAngle getCameraAngle() {
return cameraAngle;
public VirtualCameraAngleInput getCameraAngle() {
return cameraAngleInput;
}

/**
* Subclass of {@link VirtualInput} handling keyboard logic.<br>
* <br>
* Vanilla keyboard handling looks something like this:
*
* <pre>
* public void runTickKeyboard() { // Executed every tick in runTick()
* while({@linkplain Keyboard#next()}){
Expand All @@ -98,7 +92,10 @@ public VirtualCameraAngle getCameraAngle() {
* }
* }
* </pre>
* After redirecting the calls in {@link MixinMinecraft}, the resulting logic now looks like this:
*
* After redirecting the calls in {@link MixinMinecraft}, the resulting logic
* now looks like this:
*
* <pre>
* public void runTickKeyboard() {
* {@linkplain #nextKeyboardTick()}
Expand All @@ -116,7 +113,8 @@ public VirtualCameraAngle getCameraAngle() {
private static class VirtualKeyboardInput {
/**
* The keyboard "state" that is currently recognized by the game,<br>
* meaning it is a direct copy of the vanilla keybindings. Updated every <em>tick</em>.<br>
* meaning it is a direct copy of the vanilla keybindings. Updated every
* <em>tick</em>.<br>
* Updated in {@link #nextKeyboardTick()}
*/
private final VirtualKeyboard2 currentKeyboard;
Expand All @@ -128,25 +126,29 @@ private static class VirtualKeyboardInput {
private final VirtualKeyboard2 nextKeyboard = new VirtualKeyboard2();
/**
* Queue for keyboard events.<br>
* Is filled in {@link #nextKeyboardTick()} and read in {@link #nextKeyboardSubtick()}
* Is filled in {@link #nextKeyboardTick()} and read in
* {@link #nextKeyboardSubtick()}
*/
private final Queue<VirtualKeyboardEvent> keyboardEventQueue = new ConcurrentLinkedQueue<VirtualKeyboardEvent>();
/**
* The current keyboard event where the vanilla keybindings are reading from.<br>
* Updated in {@link #nextKeyboardSubtick()} and read out in {@link #getEventKeyboardKey()}, {@link #getEventKeyboardState()} and {@link #getEventKeyboardCharacter()}
* The current keyboard event where the vanilla keybindings are reading
* from.<br>
* Updated in {@link #nextKeyboardSubtick()} and read out in
* {@link #getEventKeyboardKey()}, {@link #getEventKeyboardState()} and
* {@link #getEventKeyboardCharacter()}
*/
private VirtualKeyboardEvent currentKeyboardEvent = new VirtualKeyboardEvent();


public VirtualKeyboardInput(VirtualKeyboard2 preloadedKeyboard){
public VirtualKeyboardInput(VirtualKeyboard2 preloadedKeyboard) {
currentKeyboard = preloadedKeyboard;
}

/**
* Updates the next keyboard
*
* @see VirtualInput2#update(GuiScreen)
* @param keycode The keycode of this event
* @param keystate The keystate of this event
* @param keycode The keycode of this event
* @param keystate The keystate of this event
* @param character The character of this event
*/
public void updateNextKeyboard(int keycode, boolean keystate, char character) {
Expand All @@ -155,7 +157,9 @@ public void updateNextKeyboard(int keycode, boolean keystate, char character) {

/**
* Runs when the next keyboard tick is about to occur.<br>
* Used to load {@link #nextKeyboard} into {@link #currentKeyboard}, creating {@link VirtualKeyboardEvent}s in the process.
* Used to load {@link #nextKeyboard} into {@link #currentKeyboard}, creating
* {@link VirtualKeyboardEvent}s in the process.
*
* @see MixinMinecraft#playback_injectRunTickKeyboard(org.spongepowered.asm.mixin.injection.callback.CallbackInfo)
*/
public void nextKeyboardTick() {
Expand All @@ -164,14 +168,16 @@ public void nextKeyboardTick() {
}

/**
* Runs in a while loop. Used for updating {@link #currentKeyboardEvent} and ending the while loop.
* Runs in a while loop. Used for updating {@link #currentKeyboardEvent} and
* ending the while loop.
*
* @see MixinMinecraft#playback_redirectKeyboardNext()
* @return If a keyboard event is in {@link #keyboardEventQueue}
*/
public boolean nextKeyboardSubtick() {
return (currentKeyboardEvent = keyboardEventQueue.poll()) != null;
}

/**
* @return The keycode of {@link #currentKeyboardEvent}
*/
Expand Down Expand Up @@ -200,11 +206,11 @@ private static class VirtualMouseInput {
private final Queue<VirtualMouseEvent> mouseEventQueue = new ConcurrentLinkedQueue<>();
private VirtualMouseEvent currentMouseEvent = new VirtualMouseEvent();

public VirtualMouseInput(){
public VirtualMouseInput() {
this(new VirtualMouse2());
}

public VirtualMouseInput(VirtualMouse2 preloadedMouse){
public VirtualMouseInput(VirtualMouse2 preloadedMouse) {
currentMouse = preloadedMouse;
}

Expand Down Expand Up @@ -243,6 +249,13 @@ public int getEventCursorY() {

}

private VirtualCameraAngle cameraAngle;

private static class VirtualCameraAngleInput {

private VirtualCameraAngle cameraAngle;

public VirtualCameraAngleInput(VirtualCameraAngle preloadedCamera) {
cameraAngle = preloadedCamera;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ private char getOrMinChar(Character charr){
* should be the one from tick 16
* @param reference The queue to fill. Passed in by reference.
*/
public void getVirtualEvents(VirtualKeyboard2 nextKeyboard, Queue<VirtualKeyboardEvent> reference) {
getSubticks().forEach(keyboard -> {
keyboard.getDifference(nextKeyboard, reference);
});
}
public void getVirtualEvents(VirtualKeyboard2 nextKeyboard, Queue<VirtualKeyboardEvent> reference) {
if (isParent()) {
getSubticks().forEach(keyboard -> {
keyboard.getDifference(nextKeyboard, reference);
});
}
}

/**
* Add a character to the {@link #charList}<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,11 @@ public void getDifference(VirtualMouse2 nextPeripheral, Queue<VirtualMouseEvent>
}

public void getVirtualEvents(VirtualMouse2 nextPeripheral, Queue<VirtualMouseEvent> reference) {
getSubticks().forEach(mouse -> {
mouse.getDifference(nextPeripheral, reference);
});
if(isParent()) {
getSubticks().forEach(mouse -> {
mouse.getDifference(nextPeripheral, reference);
});
}
}

@Override
Expand Down

0 comments on commit 0c6a116

Please sign in to comment.