not the best code quality, but it works how I wanted it to work

This commit is contained in:
GiantLuigi4 2023-11-22 22:52:08 -05:00
parent fe81c18b5b
commit f3f3cff079

View File

@ -5,6 +5,7 @@ import net.minecraft.client.Minecraft;
import net.minecraft.commands.arguments.EntityAnchorArgument; import net.minecraft.commands.arguments.EntityAnchorArgument;
import net.minecraft.util.Mth; import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.phys.Vec2;
import net.minecraft.world.phys.Vec3; import net.minecraft.world.phys.Vec3;
import net.minecraftforge.client.event.ViewportEvent; import net.minecraftforge.client.event.ViewportEvent;
import net.minecraftforge.event.TickEvent; import net.minecraftforge.event.TickEvent;
@ -24,20 +25,30 @@ public class KeyboardCamera {
private static double yCrd = -1; private static double yCrd = -1;
private static double nyCrd = -1; private static double nyCrd = -1;
private static double nextX = -1;
private static double nextY = -1;
protected static void updateCrd(JsonObject obj) { protected static void updateCrd(JsonObject obj) {
if (obj.getAsJsonPrimitive("exists").getAsBoolean()) { if (obj.getAsJsonPrimitive("exists").getAsBoolean()) {
ScreenData scr = tes.getScreen(side); ScreenData scr = tes.getScreen(side);
if (scr != null) { if (scr != null) {
nxCrd = obj.getAsJsonPrimitive("x").getAsDouble() + obj.getAsJsonPrimitive("w").getAsDouble() / 2; nextX = obj.getAsJsonPrimitive("x").getAsDouble() + obj.getAsJsonPrimitive("w").getAsDouble() / 2;
nyCrd = obj.getAsJsonPrimitive("y").getAsDouble() + obj.getAsJsonPrimitive("h").getAsDouble() / 2; nextY = obj.getAsJsonPrimitive("y").getAsDouble() + obj.getAsJsonPrimitive("h").getAsDouble() / 2;
nxCrd /= scr.resolution.x; nextX /= scr.resolution.x;
nxCrd *= scr.size.x; if (nextX > 1 || nextX < 0) {
nextX = -1;
} else nextX *= scr.size.x;
nyCrd /= scr.resolution.y; nextY /= scr.resolution.y;
nyCrd = 1 - nyCrd; nextY = 1 - nextY;
nyCrd *= scr.size.y; if (nextY > 1 || nextY < 0) {
nextY = -1;
} else nextY *= scr.size.y;
} }
} else {
nextX = -1;
nextY = -1;
} }
} }
@ -55,45 +66,45 @@ public class KeyboardCamera {
ScreenData scr = teTmp.getScreen(sdTmp); ScreenData scr = teTmp.getScreen(sdTmp);
if (scr != null) { if (scr != null) {
activeTask = WDRouter.INSTANCE.requestJson( //@formatter:off
scr.browser, "ActiveElement", """ activeTask = WDRouter.INSTANCE.requestJson(scr.browser, "ActiveElement", """
try { try {
let focusedElement = document.activeElement; let focusedElement = document.activeElement;
if (focusedElement == null) { if (focusedElement == null || focusedElement == document.body) {
window.cefQuery({ window.cefQuery({
request: 'WebDisplays_ActiveElement{exists: false}', request: 'WebDisplays_ActiveElement{exists: false}',
onSuccess: function(response) {}, onSuccess: function(response) {},
onFailure: function(error_code, error_message) {} onFailure: function(error_code, error_message) {}
}); });
} else { } else {
let bodyRect = document.body.getBoundingClientRect(); let bodyRect = document.body.getBoundingClientRect();
let elemRect = focusedElement.getBoundingClientRect(); let elemRect = focusedElement.getBoundingClientRect();
window.cefQuery({ window.cefQuery({
request: 'WebDisplays_ActiveElement{exists: true,'+ request: 'WebDisplays_ActiveElement{exists: true,'+
'x: ' + (elemRect.left) + ',' + 'x: ' + (elemRect.left) + ',' +
'y: ' + (elemRect.top) + ',' + 'y: ' + (elemRect.top) + ',' +
'w: ' + ((elemRect.right - elemRect.left)) + ',' + 'w: ' + ((elemRect.right - elemRect.left)) + ',' +
'h: ' + ((elemRect.bottom - elemRect.top)) + 'h: ' + ((elemRect.bottom - elemRect.top)) +
'}', '}',
onSuccess: function(response) {}, onSuccess: function(response) {},
onFailure: function(error_code, error_message) {} onFailure: function(error_code, error_message) {}
}); });
} }
} catch (err) { } catch (err) {
console.error(err); console.error(err);
window.cefQuery({ window.cefQuery({
request: 'WebDisplays_ActiveElement{exists: false}', request: 'WebDisplays_ActiveElement{exists: false}',
onSuccess: function(response) {}, onSuccess: function(response) {},
onFailure: function(error_code, error_message) {} onFailure: function(error_code, error_message) {}
}); });
} }""".replace("\n", "")
""".replace("\n", "") ).thenAccept((o1) -> {
).thenAccept((o1) -> { updateCrd(o1);
updateCrd(o1); activeTask = null;
activeTask = null; });
}); futureStart = System.currentTimeMillis();
futureStart = System.currentTimeMillis(); //@formatter:on
} }
} }
@ -126,34 +137,40 @@ public class KeyboardCamera {
// TODO: implement // TODO: implement
double coxCrd = Mth.lerp(0.5 * event.getPartialTick(), oxCrd, xCrd);
double cxCrd = Mth.lerp(0.15 * event.getPartialTick(), xCrd, nxCrd);
double coyCrd = Mth.lerp(0.5 * event.getPartialTick(), oyCrd, yCrd);
double focalX = tes.getBlockPos().getX() + double focalX = tes.getBlockPos().getX() +
side.right.x * (xCrd - 1) + side.up.x * yCrd; side.right.x * (coxCrd - 1) + side.up.x * coyCrd;
double focalY = tes.getBlockPos().getY() + double focalY = tes.getBlockPos().getY() +
side.right.y * (xCrd - 1) + side.up.y * yCrd; side.right.y * (coxCrd - 1) + side.up.y * coyCrd;
double focalZ = tes.getBlockPos().getZ() + double focalZ = tes.getBlockPos().getZ() +
side.right.z * (xCrd - 1) + side.up.z * yCrd; side.right.z * (coxCrd - 1) + side.up.z * coyCrd;
double pct = (delay - event.getPartialTick()) / 8f; //// double pct = 1 - event.getPartialTick();
pct *= pct; // double pct = 1;
focalX = Mth.lerp(pct, //// pct = pct / 5 + (1 / 4f);
focalX, // focalX = Mth.lerp(pct,
tes.getBlockPos().getX() + // focalX,
side.right.x * (oxCrd - 1) + side.up.x * oyCrd // tes.getBlockPos().getX() +
); // side.right.x * (coxCrd - 1) + side.up.x * coyCrd
focalY = Mth.lerp(pct, // );
focalY, // focalY = Mth.lerp(pct,
tes.getBlockPos().getY() + // focalY,
side.right.y * (oxCrd - 1) + side.up.y * oyCrd // tes.getBlockPos().getY() +
); // side.right.y * (coxCrd - 1) + side.up.y * coyCrd
focalZ = Mth.lerp(pct, // );
focalZ, // focalZ = Mth.lerp(pct,
tes.getBlockPos().getZ() + // focalZ,
side.right.z * (oxCrd - 1) + side.up.z * oyCrd // tes.getBlockPos().getZ() +
); // side.right.z * (coxCrd - 1) + side.up.z * coyCrd
// );
// focalX -= side.forward.x * 0.5f; // focalX += side.forward.x * 0.25f;
// focalY -= side.forward.y * 0.5f; // focalY += side.forward.y * 0.25f;
// focalZ -= side.forward.z * 0.5f; // focalZ += side.forward.z * 0.25f;
float[] angle = lookAt( float[] angle = lookAt(
event.getCamera().getEntity(), event.getCamera().getEntity(),
@ -161,21 +178,19 @@ public class KeyboardCamera {
new Vec3(focalX, focalY, focalZ) new Vec3(focalX, focalY, focalZ)
); );
double scl = 20; float scl = 20;
double mx = Minecraft.getInstance().mouseHandler.xpos(); double mx = Minecraft.getInstance().mouseHandler.xpos();
mx /= Minecraft.getInstance().getWindow().getWidth(); mx /= Minecraft.getInstance().getWindow().getWidth();
mx *= scl;
mx -= (scl / 2);
mx = signedSqrt(mx);
angle[1] += mx;
double my = Minecraft.getInstance().mouseHandler.ypos(); double my = Minecraft.getInstance().mouseHandler.ypos();
my /= Minecraft.getInstance().getWindow().getHeight(); my /= Minecraft.getInstance().getWindow().getHeight();
my *= scl;
my -= (scl / 2); Vec2 v2 = new Vec2((float) mx, (float) my);
my = signedSqrt(my); v2 = v2.normalized().scale(Mth.sqrt(v2.length()));
angle[0] += my;
angle[1] += (v2.x - 0.5f) * scl;
angle[0] += (v2.y - 0.5f) * scl;
float xRot = event.getYaw(); // left right float xRot = event.getYaw(); // left right
float yRot = event.getPitch(); // up down float yRot = event.getPitch(); // up down
@ -218,32 +233,34 @@ public class KeyboardCamera {
return; return;
} }
if (nxCrd == -1) { double anxx = nextX;
if (activeTask == null) { double anxy = nextY;
delay = 1;
pollElement(); if (
anxx == -1 || anxy == -1 ||
nxCrd == -1 || nyCrd == -1 ||
oxCrd == -1 || oyCrd == -1 ||
xCrd == -1 || yCrd == -1
) {
ScreenData data = tes.getScreen(side);
if (data == null)
return; return;
}
anxx = data.size.x / 2.0;
anxy = data.size.y / 2.0;
} }
if (oxCrd == xCrd && delay > 2) { if (activeTask == null)
delay = 2;
}
delay--;
if (delay == 0) {
oxCrd = xCrd;
xCrd = nxCrd;
oyCrd = yCrd;
yCrd = nyCrd;
pollElement(); pollElement();
} else if (delay < 0) {
oxCrd = xCrd; nxCrd = anxx;
xCrd = nxCrd; nyCrd = anxy;
oyCrd = yCrd;
yCrd = nyCrd; oxCrd = Mth.lerp(0.5, oxCrd, xCrd);
delay = 8; xCrd = Mth.lerp(0.15, xCrd, nxCrd);
}
oyCrd = Mth.lerp(0.5, oyCrd, yCrd);
yCrd = Mth.lerp(0.15, yCrd, nyCrd);
} }
} }
} }