Skip to content

Commit 729743d

Browse files
committed
[VirtualInput] Move interpolation code into it's own class
1 parent 01b4c5e commit 729743d

File tree

7 files changed

+141
-111
lines changed

7 files changed

+141
-111
lines changed

src/main/java/com/minecrafttas/mctcommon/events/EventListenerRegistry.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,9 @@ public static Object fireEvent(Class<? extends EventListenerRegistry.EventBase>
252252
toThrow = null; // Reset toThrow as the correct method was found
253253
method.setAccessible(true);
254254
try {
255-
returnValue = method.invoke(eventListener, eventParams); // Call the method
255+
Object newReturnValue = method.invoke(eventListener, eventParams); // Call the method
256+
if (newReturnValue != null)
257+
returnValue = newReturnValue;
256258
} catch (IllegalAccessException | InvocationTargetException e) {
257259
throw new EventException(eventClass, e);
258260
} catch (IllegalArgumentException e) {

src/main/java/com/minecrafttas/tasmod/TASmodClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ private void registerEventListeners() {
198198
EventListenerRegistry.register(TASmodAPIRegistry.PLAYBACK_FILE_COMMAND);
199199
EventListenerRegistry.register(new LoggerMarkers());
200200
EventListenerRegistry.register(savestateHandlerClient);
201+
202+
EventListenerRegistry.register(virtual.interpolationHandler);
201203
}
202204

203205
@Override

src/main/java/com/minecrafttas/tasmod/gui/InfoHud.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ public boolean checkInit() {
390390
if (Minecraft.getMinecraft().currentScreen == this)
391391
return "Mouse Position";
392392

393-
Integer xCursor = TASmodClient.virtual.MOUSE.getInterpolatedX(0, false);
394-
Integer yCursor = TASmodClient.virtual.MOUSE.getInterpolatedY(0, false);
393+
Integer xCursor = TASmodClient.virtual.interpolationHandler.getInterpolatedX(0, false);
394+
Integer yCursor = TASmodClient.virtual.interpolationHandler.getInterpolatedY(0, false);
395395

396396
if (Minecraft.getMinecraft().currentScreen != null)
397397
return String.format("Mouse Cursor: %s %s", xCursor == null ? "null" : xCursor, yCursor == null ? "null" : yCursor);

src/main/java/com/minecrafttas/tasmod/mixin/playbackhooks/MixinEntityRenderer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public void playback_updateOverlay(CallbackInfo ci) {
201201
* @return The redirected yaw
202202
*/
203203
private float redirectCam(float pitch, float yaw) {
204-
Triple<Float, Float, Float> interpolated = TASmodClient.virtual.CAMERA_ANGLE.getInterpolatedState(Minecraft.getMinecraft().timer.renderPartialTicks, pitch, yaw, TASmodClient.controller.isPlayingback());
204+
Triple<Float, Float, Float> interpolated = TASmodClient.virtual.interpolationHandler.getInterpolatedState(Minecraft.getMinecraft().timer.renderPartialTicks, pitch, yaw, TASmodClient.controller.isPlayingback());
205205
float pitch2 = interpolated.getLeft();
206206
float yaw2 = interpolated.getMiddle();
207207
// Update pitch
@@ -214,13 +214,13 @@ private float redirectCam(float pitch, float yaw) {
214214

215215
@Redirect(method = "updateCameraAndRender", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;getX()I", remap = false))
216216
public int redirect_updateCameraAndRendererX() {
217-
int interpolatedX = TASmodClient.virtual.MOUSE.getInterpolatedX(Minecraft.getMinecraft().timer.renderPartialTicks, TASmodClient.controller.isPlayingback());
217+
int interpolatedX = TASmodClient.virtual.interpolationHandler.getInterpolatedX(Minecraft.getMinecraft().timer.renderPartialTicks, TASmodClient.controller.isPlayingback());
218218
return interpolatedX;
219219
}
220220

221221
@Redirect(method = "updateCameraAndRender", at = @At(value = "INVOKE", target = "Lorg/lwjgl/input/Mouse;getY()I", remap = false))
222222
public int redirect_updateCameraAndRendererY() {
223-
int interpolatedY = TASmodClient.virtual.MOUSE.getInterpolatedY(Minecraft.getMinecraft().timer.renderPartialTicks, TASmodClient.controller.isPlayingback());
223+
int interpolatedY = TASmodClient.virtual.interpolationHandler.getInterpolatedY(Minecraft.getMinecraft().timer.renderPartialTicks, TASmodClient.controller.isPlayingback());
224224
return interpolatedY;
225225
}
226226
}

src/main/java/com/minecrafttas/tasmod/virtual/VirtualInput.java

Lines changed: 2 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
package com.minecrafttas.tasmod.virtual;
22

3-
import java.util.ArrayList;
43
import java.util.List;
54
import java.util.Queue;
65
import java.util.concurrent.ConcurrentLinkedQueue;
76

8-
import org.apache.commons.lang3.tuple.Triple;
97
import org.apache.logging.log4j.Logger;
108
import org.lwjgl.input.Keyboard;
119
import org.lwjgl.input.Mouse;
1210
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1311

1412
import com.minecrafttas.mctcommon.events.EventListenerRegistry;
15-
import com.minecrafttas.tasmod.TASmodClient;
1613
import com.minecrafttas.tasmod.events.EventVirtualInput;
1714
import com.minecrafttas.tasmod.mixin.playbackhooks.MixinEntityRenderer;
1815
import com.minecrafttas.tasmod.mixin.playbackhooks.MixinMinecraft;
1916
import com.minecrafttas.tasmod.util.Ducks;
20-
import com.minecrafttas.tasmod.util.Ducks.GuiScreenDuck;
2117
import com.minecrafttas.tasmod.util.Ducks.SubtickDuck;
2218
import com.minecrafttas.tasmod.util.LoggerMarkers;
2319
import com.minecrafttas.tasmod.util.PointerNormalizer;
@@ -26,7 +22,6 @@
2622

2723
import net.minecraft.client.Minecraft;
2824
import net.minecraft.client.gui.GuiScreen;
29-
import net.minecraft.util.math.MathHelper;
3025

3126
/**
3227
* Main component for redirecting inputs.<br>
@@ -51,6 +46,8 @@ public class VirtualInput {
5146
*/
5247
public final VirtualCameraAngleInput CAMERA_ANGLE;
5348

49+
public final VirtualInterpolationHandler interpolationHandler = new VirtualInterpolationHandler();
50+
5451
/**
5552
* Creates a new virtual input with an empty {@link VirtualKeyboardInput}, {@link VirtualMouseInput} and {@link VirtualCameraAngleInput}
5653
* @param logger The logger instance
@@ -426,8 +423,6 @@ public class VirtualMouseInput {
426423
*/
427424
private VirtualMouseEvent currentMouseEvent = new VirtualMouseEvent();
428425

429-
private final List<VirtualMouse> mousePointerInterpolationStates = new ArrayList<>();
430-
431426
/**
432427
* Constructor to preload the {@link #currentMouse} with an existing mouse
433428
* @param preloadedMouse The new {@link #currentMouse}
@@ -460,8 +455,6 @@ public void updateNextMouse(int keycode, boolean keystate, int scrollwheel, int
460455
*/
461456
public void nextMouseTick() {
462457
nextMouse.deepCopyFrom((VirtualMouse) EventListenerRegistry.fireEvent(EventVirtualInput.EventVirtualMouseTick.class, nextMouse));
463-
mousePointerInterpolationStates.clear();
464-
TASmodClient.controller.getNextMouse().getStates(mousePointerInterpolationStates);
465458
currentMouse.getVirtualEvents(nextMouse, mouseEventQueue);
466459
currentMouse.moveFrom(nextMouse);
467460
}
@@ -551,60 +544,6 @@ public boolean willKeyBeDown(int keycode) {
551544
return nextMouse.isKeyDown(keycode);
552545
}
553546

554-
/**
555-
* Gets the absolute coordinates of the camera angle
556-
*
557-
* @param partialTick The partial ticks of the timer
558-
* @param pitch The original pitch of the camera
559-
* @param yaw The original yaw of the camera
560-
* @param enable Whether the custom interpolation is enabled. Enabled during playback.
561-
* @return Integer coordinate of
562-
*/
563-
public int getInterpolatedX(float partialTick, boolean enable) {
564-
565-
int interpolatedPointerX = nextMouse.getCursorX();
566-
567-
if (enable && !mousePointerInterpolationStates.isEmpty()) {
568-
int index = (int) MathHelper.clampedLerp(0, mousePointerInterpolationStates.size() - 1, partialTick); // Get interpolate index
569-
570-
VirtualMouse interpolatedCamera = mousePointerInterpolationStates.get(index);
571-
572-
interpolatedPointerX = interpolatedCamera.getCursorX();
573-
574-
}
575-
Minecraft mc = Minecraft.getMinecraft();
576-
GuiScreenDuck gui = (GuiScreenDuck) mc.currentScreen;
577-
578-
if (gui != null) {
579-
interpolatedPointerX = gui.rescaleX(PointerNormalizer.reapplyScalingX(interpolatedPointerX));
580-
}
581-
582-
return interpolatedPointerX;
583-
}
584-
585-
public int getInterpolatedY(float partialTick, boolean enable) {
586-
587-
int interpolatedPointerY = nextMouse.getCursorY();
588-
589-
if (enable && !mousePointerInterpolationStates.isEmpty()) {
590-
int index = (int) MathHelper.clampedLerp(0, mousePointerInterpolationStates.size() - 1, partialTick); // Get interpolate index
591-
592-
VirtualMouse interpolatedCamera = mousePointerInterpolationStates.get(index);
593-
594-
interpolatedPointerY = interpolatedCamera.getCursorY();
595-
596-
}
597-
598-
Minecraft mc = Minecraft.getMinecraft();
599-
GuiScreenDuck gui = (GuiScreenDuck) mc.currentScreen;
600-
601-
if (gui != null) {
602-
interpolatedPointerY = gui.rescaleY(PointerNormalizer.reapplyScalingY(interpolatedPointerY));
603-
}
604-
605-
return interpolatedPointerY;
606-
}
607-
608547
/**
609548
* Clears the {@link #nextMouse}
610549
*/
@@ -676,11 +615,6 @@ public class VirtualCameraAngleInput {
676615
* and updates {@link #currentCameraAngle} in {@link #nextCameraTick()}
677616
*/
678617
private final VirtualCameraAngle nextCameraAngle = new VirtualCameraAngle();
679-
/**
680-
* States of the {@link #nextCameraAngle} made during the tick.<br>
681-
* Is updated in {@link #nextCameraTick()}
682-
*/
683-
private final List<VirtualCameraAngle> cameraAngleInterpolationStates = new ArrayList<>();
684618

685619
/**
686620
* Constructor to preload the {@link #currentCameraAngle} with an existing
@@ -728,8 +662,6 @@ public void updateNextCameraAngle(float pitchDelta, float yawDelta, boolean upda
728662
*/
729663
public void nextCameraTick() {
730664
nextCameraAngle.deepCopyFrom((VirtualCameraAngle) EventListenerRegistry.fireEvent(EventVirtualInput.EventVirtualCameraAngleTick.class, nextCameraAngle));
731-
cameraAngleInterpolationStates.clear();
732-
nextCameraAngle.getStates(cameraAngleInterpolationStates);
733665
currentCameraAngle.moveFrom(nextCameraAngle);
734666
}
735667

@@ -765,32 +697,6 @@ public Float getCurrentYaw() {
765697
return currentCameraAngle.getYaw();
766698
}
767699

768-
/**
769-
* Gets the absolute coordinates of the camera angle
770-
*
771-
* @param partialTick The partial ticks of the timer
772-
* @param pitch The original pitch of the camera
773-
* @param yaw The original yaw of the camera
774-
* @param enable Whether the custom interpolation is enabled. Enabled during playback.
775-
* @return A triple of pitch, yaw and roll, as left, middle and right respectively
776-
*/
777-
public Triple<Float, Float, Float> getInterpolatedState(float partialTick, float pitch, float yaw, boolean enable) {
778-
779-
float interpolatedPitch = nextCameraAngle.getPitch() == null ? pitch : nextCameraAngle.getPitch();
780-
float interpolatedYaw = nextCameraAngle.getYaw() == null ? yaw : nextCameraAngle.getYaw() + 180;
781-
782-
if (enable && !cameraAngleInterpolationStates.isEmpty()) {
783-
int index = (int) MathHelper.clampedLerp(0, cameraAngleInterpolationStates.size() - 1, partialTick); // Get interpolate index
784-
785-
VirtualCameraAngle interpolatedCamera = cameraAngleInterpolationStates.get(index);
786-
787-
interpolatedPitch = interpolatedCamera.getPitch() == null ? 0 : interpolatedCamera.getPitch();
788-
interpolatedYaw = interpolatedCamera.getYaw() == null ? 0 : interpolatedCamera.getYaw() + 180;
789-
790-
}
791-
return Triple.of(interpolatedPitch, interpolatedYaw, 0f);
792-
}
793-
794700
/**
795701
* Clears the {@link #nextCameraAngle}
796702
*/
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package com.minecrafttas.tasmod.virtual;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.apache.commons.lang3.tuple.Triple;
7+
8+
import com.minecrafttas.tasmod.TASmodClient;
9+
import com.minecrafttas.tasmod.events.EventVirtualInput;
10+
import com.minecrafttas.tasmod.util.Ducks.GuiScreenDuck;
11+
import com.minecrafttas.tasmod.util.PointerNormalizer;
12+
13+
import net.minecraft.client.Minecraft;
14+
import net.minecraft.util.math.MathHelper;
15+
16+
public class VirtualInterpolationHandler implements EventVirtualInput.EventVirtualMouseTick, EventVirtualInput.EventVirtualCameraAngleTick {
17+
18+
private final List<VirtualMouse> mousePointerInterpolationStates = new ArrayList<>();
19+
/**
20+
* States of the {@link #nextCameraAngle} made during the tick.<br>
21+
* Is updated in {@link #nextCameraTick()}
22+
*/
23+
private final List<VirtualCameraAngle> cameraAngleInterpolationStates = new ArrayList<>();
24+
25+
private VirtualMouse nextMouse = new VirtualMouse();
26+
private VirtualCameraAngle nextCameraAngle = new VirtualCameraAngle();
27+
28+
public int getInterpolatedX(float partialTick, boolean enable) {
29+
30+
int interpolatedPointerX = nextMouse.getCursorX();
31+
32+
if (enable && !mousePointerInterpolationStates.isEmpty()) {
33+
int index = (int) MathHelper.clampedLerp(0, mousePointerInterpolationStates.size() - 1, partialTick); // Get interpolate index
34+
35+
VirtualMouse interpolatedCamera = mousePointerInterpolationStates.get(index);
36+
37+
interpolatedPointerX = interpolatedCamera.getCursorX();
38+
39+
}
40+
Minecraft mc = Minecraft.getMinecraft();
41+
GuiScreenDuck gui = (GuiScreenDuck) mc.currentScreen;
42+
43+
if (gui != null) {
44+
interpolatedPointerX = gui.rescaleX(PointerNormalizer.reapplyScalingX(interpolatedPointerX));
45+
}
46+
47+
return interpolatedPointerX;
48+
}
49+
50+
public int getInterpolatedY(float partialTick, boolean enable) {
51+
52+
int interpolatedPointerY = nextMouse.getCursorY();
53+
54+
if (enable && !mousePointerInterpolationStates.isEmpty()) {
55+
int index = (int) MathHelper.clampedLerp(0, mousePointerInterpolationStates.size() - 1, partialTick); // Get interpolate index
56+
57+
VirtualMouse interpolatedCamera = mousePointerInterpolationStates.get(index);
58+
59+
interpolatedPointerY = interpolatedCamera.getCursorY();
60+
61+
}
62+
63+
Minecraft mc = Minecraft.getMinecraft();
64+
GuiScreenDuck gui = (GuiScreenDuck) mc.currentScreen;
65+
66+
if (gui != null) {
67+
interpolatedPointerY = gui.rescaleY(PointerNormalizer.reapplyScalingY(interpolatedPointerY));
68+
}
69+
70+
return interpolatedPointerY;
71+
}
72+
73+
/**
74+
* Gets the absolute coordinates of the camera angle
75+
*
76+
* @param partialTick The partial ticks of the timer
77+
* @param pitch The original pitch of the camera
78+
* @param yaw The original yaw of the camera
79+
* @param enable Whether the custom interpolation is enabled. Enabled during playback.
80+
* @return A triple of pitch, yaw and roll, as left, middle and right respectively
81+
*/
82+
public Triple<Float, Float, Float> getInterpolatedState(float partialTick, float pitch, float yaw, boolean enable) {
83+
84+
float interpolatedPitch = nextCameraAngle.getPitch() == null ? pitch : nextCameraAngle.getPitch();
85+
float interpolatedYaw = nextCameraAngle.getYaw() == null ? yaw : nextCameraAngle.getYaw() + 180;
86+
87+
if (enable && !cameraAngleInterpolationStates.isEmpty()) {
88+
int index = (int) MathHelper.clampedLerp(0, cameraAngleInterpolationStates.size() - 1, partialTick); // Get interpolate index
89+
90+
VirtualCameraAngle interpolatedCamera = cameraAngleInterpolationStates.get(index);
91+
92+
interpolatedPitch = interpolatedCamera.getPitch() == null ? 0 : interpolatedCamera.getPitch();
93+
interpolatedYaw = interpolatedCamera.getYaw() == null ? 0 : interpolatedCamera.getYaw() + 180;
94+
95+
}
96+
return Triple.of(interpolatedPitch, interpolatedYaw, 0f);
97+
}
98+
99+
@Override
100+
public VirtualMouse onVirtualMouseTick(VirtualMouse vmouse) {
101+
this.nextMouse = vmouse;
102+
mousePointerInterpolationStates.clear();
103+
TASmodClient.controller.getNextMouse().getStates(mousePointerInterpolationStates);
104+
return null;
105+
}
106+
107+
@Override
108+
public VirtualCameraAngle onVirtualCameraTick(VirtualCameraAngle vcamera) {
109+
this.nextCameraAngle = vcamera;
110+
cameraAngleInterpolationStates.clear();
111+
nextCameraAngle.getStates(cameraAngleInterpolationStates);
112+
return null;
113+
}
114+
}

0 commit comments

Comments
 (0)