aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoringlettronald <inglettronald@gmail.com>2023-06-12 18:05:47 -0500
committeringlettronald <inglettronald@gmail.com>2023-06-12 18:05:47 -0500
commit517cfe7ab7fdc3c95754af2e142ff91c542b82fe (patch)
tree72adb6e23047b1cd7320b6fbd0d487734eaca5c9
parent0e5f1d3dc9b2845e142e3504026b98bbf6f5ff41 (diff)
downloadDulkirMod-Fabric-517cfe7ab7fdc3c95754af2e142ff91c542b82fe.tar.gz
DulkirMod-Fabric-517cfe7ab7fdc3c95754af2e142ff91c542b82fe.tar.bz2
DulkirMod-Fabric-517cfe7ab7fdc3c95754af2e142ff91c542b82fe.zip
Scrollable tooltips Implementation is now mostly fixed, some minor TODO's left
-rw-r--r--src/main/java/com/dulkirfabric/mixin/ItemStackMixin.java27
-rw-r--r--src/main/java/com/dulkirfabric/mixin/render/DrawContextMixin.java19
-rw-r--r--src/main/kotlin/com/dulkirfabric/events/TooltipRenderChangeEvent.kt5
-rw-r--r--src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt6
-rw-r--r--src/main/kotlin/com/dulkirfabric/features/TooltipImpl.kt52
-rw-r--r--src/main/kotlin/com/dulkirfabric/util/ItemChangeHandler.kt13
-rw-r--r--src/main/resources/dulkirmod-fabric.mixins.json3
7 files changed, 111 insertions, 14 deletions
diff --git a/src/main/java/com/dulkirfabric/mixin/ItemStackMixin.java b/src/main/java/com/dulkirfabric/mixin/ItemStackMixin.java
new file mode 100644
index 0000000..5fdd7f5
--- /dev/null
+++ b/src/main/java/com/dulkirfabric/mixin/ItemStackMixin.java
@@ -0,0 +1,27 @@
+package com.dulkirfabric.mixin;
+
+import com.dulkirfabric.util.ItemChangeHandler;
+import net.minecraft.client.item.TooltipContext;
+import net.minecraft.entity.player.PlayerEntity;
+import net.minecraft.item.Item;
+import net.minecraft.item.ItemStack;
+import net.minecraft.text.Text;
+import org.jetbrains.annotations.Nullable;
+import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Shadow;
+import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
+
+import java.util.List;
+
+@Mixin(ItemStack.class)
+public abstract class ItemStackMixin {
+ @Shadow public abstract Item getItem();
+
+ @Inject(method = "getTooltip", at = @At("HEAD"))
+ private void onGetTooltip(@Nullable PlayerEntity player, TooltipContext context, CallbackInfoReturnable<List<Text>> cir) {
+ ItemChangeHandler.INSTANCE.handle(this.getItem().getName().getString());
+ }
+}
diff --git a/src/main/java/com/dulkirfabric/mixin/render/DrawContextMixin.java b/src/main/java/com/dulkirfabric/mixin/render/DrawContextMixin.java
index 9b6cac3..1bef0f5 100644
--- a/src/main/java/com/dulkirfabric/mixin/render/DrawContextMixin.java
+++ b/src/main/java/com/dulkirfabric/mixin/render/DrawContextMixin.java
@@ -1,16 +1,29 @@
package com.dulkirfabric.mixin.render;
+import com.dulkirfabric.events.TooltipRenderChangeEvent;
import com.dulkirfabric.features.TooltipImpl;
+import com.dulkirfabric.util.ItemChangeHandler;
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation;
+import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
+import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.gui.tooltip.TooltipPositioner;
+import net.minecraft.client.util.math.MatrixStack;
import org.joml.Vector2ic;
+import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
+import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
+import org.spongepowered.asm.mixin.injection.Inject;
+import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
+
+import java.util.List;
@Mixin(DrawContext.class)
public class DrawContextMixin {
+ @Shadow @Final private MatrixStack matrices;
+ List<TooltipComponent> prevComponents;
@WrapOperation(method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;IILnet/minecraft/client/gui/tooltip/TooltipPositioner;)V",
at = @At(target = "Lnet/minecraft/client/gui/tooltip/TooltipPositioner;getPosition(IIIIII)Lorg/joml/Vector2ic;", value = "INVOKE"))
@@ -18,4 +31,10 @@ public class DrawContextMixin {
Vector2ic v = operation.call(positionerInstance, sw, sh, mx, my, tw, th);
return TooltipImpl.INSTANCE.calculatePos(v);
}
+
+ @Inject(method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;IILnet/minecraft/client/gui/tooltip/TooltipPositioner;)V",
+ at = @At(target = "Lnet/minecraft/client/util/math/MatrixStack;push()V", value = "INVOKE"))
+ public void onPush(TextRenderer textRenderer, List<TooltipComponent> components, int x, int y, TooltipPositioner positioner, CallbackInfo ci) {
+ TooltipImpl.INSTANCE.applyScale(matrices);
+ }
}
diff --git a/src/main/kotlin/com/dulkirfabric/events/TooltipRenderChangeEvent.kt b/src/main/kotlin/com/dulkirfabric/events/TooltipRenderChangeEvent.kt
new file mode 100644
index 0000000..ae399da
--- /dev/null
+++ b/src/main/kotlin/com/dulkirfabric/events/TooltipRenderChangeEvent.kt
@@ -0,0 +1,5 @@
+package com.dulkirfabric.events
+
+import com.dulkirfabric.events.base.Event
+
+object TooltipRenderChangeEvent: Event() \ No newline at end of file
diff --git a/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt b/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt
index af76c44..6863479 100644
--- a/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt
+++ b/src/main/kotlin/com/dulkirfabric/features/RenderBoxTest.kt
@@ -14,5 +14,11 @@ object RenderBoxTest {
event.context, Box(0.0, 0.0, 0.0, 1.0, 1.0, 1.0),
Color(255, 255, 255, 255), 10f, true
)
+
+ WorldRenderUtils.drawBox(
+ event.context, Box(2.0, 0.0, 0.0, 3.0, 1.0, 1.0),
+ Color(1, 255, 1, 255), 10f, false
+ )
+
}
} \ No newline at end of file
diff --git a/src/main/kotlin/com/dulkirfabric/features/TooltipImpl.kt b/src/main/kotlin/com/dulkirfabric/features/TooltipImpl.kt
index 100b880..14bc6c5 100644
--- a/src/main/kotlin/com/dulkirfabric/features/TooltipImpl.kt
+++ b/src/main/kotlin/com/dulkirfabric/features/TooltipImpl.kt
@@ -3,36 +3,45 @@ package com.dulkirfabric.features
import com.dulkirfabric.DulkirModFabric.mc
import com.dulkirfabric.events.ClientTickEvent
import com.dulkirfabric.events.MouseScrollEvent
+import com.dulkirfabric.events.TooltipRenderChangeEvent
import meteordevelopment.orbit.EventHandler
import net.minecraft.client.MinecraftClient
import net.minecraft.client.util.InputUtil
+import net.minecraft.client.util.math.MatrixStack
import org.joml.Vector2i
import org.joml.Vector2ic
import org.lwjgl.glfw.GLFW
object TooltipImpl {
- var scaleBuffer = 1f
- var horizontalBuffer = 0.0
- var verticalBuffer = 0.0
-
- var tickScale = 0f
- var tickHorizontal = 0
- var tickVertical = 0
-
- var prevTickX = 0
- var prevTickY = 0
+ private var scaleBuffer = 1f
+ private var horizontalBuffer = 0.0
+ private var verticalBuffer = 0.0
+ private var tickScale = 0f
+ private var tickHorizontal = 0
+ private var tickVertical = 0
+ private var prevTickX = 0
+ private var prevTickY = 0
+ private var prevScale = 1f
+ private var frameScale = 1f
+ private var frameX = 0
+ private var frameY = 0
fun calculatePos(v: Vector2ic): Vector2ic {
// calculate the position of the tooltip based on the scroll amount
val partialTicks = MinecraftClient.getInstance().tickDelta
- val newVec = v.add(prevTickX + ((tickHorizontal - prevTickX) * partialTicks).toInt(),
- prevTickY + ((tickVertical - prevTickY) * partialTicks).toInt(), Vector2i())
+ frameX = v.x() + prevTickX + ((tickHorizontal - prevTickX) * partialTicks).toInt()
+ frameY = v.y() + prevTickY + ((tickVertical - prevTickY) * partialTicks).toInt()
+ val newVec = v.add(-v.x() + frameX, -v.y() + frameY, Vector2i())
return newVec
}
- fun applyScale() {
-
+ fun applyScale(matrices: MatrixStack) {
+ frameScale = prevScale + (tickScale - prevScale) * MinecraftClient.getInstance().tickDelta
+ matrices.scale(frameScale, frameScale, 1f)
+ val newX = frameX / frameScale
+ val newY = frameY / frameScale
+ matrices.translate(newX - frameX, newY - frameY, 0f)
}
@EventHandler
@@ -40,6 +49,7 @@ object TooltipImpl {
// flushes the buffer to a scroll amount this tick, will be interpolated in calculatePos
prevTickX = tickHorizontal
prevTickY = tickVertical
+ prevScale = tickScale
tickHorizontal = horizontalBuffer.toInt()
tickVertical = verticalBuffer.toInt()
tickScale = scaleBuffer
@@ -47,6 +57,7 @@ object TooltipImpl {
@EventHandler
fun onScroll(event: MouseScrollEvent) {
+ // TODO: ignore input in config screen
if (event.verticalScrollAmount == 0.0) return
val handle = MinecraftClient.getInstance().window.handle
if (InputUtil.isKeyPressed(handle, GLFW.GLFW_KEY_LEFT_SHIFT)) {
@@ -57,4 +68,17 @@ object TooltipImpl {
verticalBuffer += (mc.window.height / 108) * event.verticalScrollAmount
}
}
+
+ @EventHandler
+ fun onChange(event: TooltipRenderChangeEvent) {
+ scaleBuffer = 1f
+ horizontalBuffer = 0.0
+ verticalBuffer = 0.0
+ tickScale = 1f
+ tickHorizontal = 0
+ tickVertical = 0
+ prevTickX = 0
+ prevTickY = 0
+ prevScale = 1f
+ }
} \ No newline at end of file
diff --git a/src/main/kotlin/com/dulkirfabric/util/ItemChangeHandler.kt b/src/main/kotlin/com/dulkirfabric/util/ItemChangeHandler.kt
new file mode 100644
index 0000000..7a98f7f
--- /dev/null
+++ b/src/main/kotlin/com/dulkirfabric/util/ItemChangeHandler.kt
@@ -0,0 +1,13 @@
+package com.dulkirfabric.util
+
+import com.dulkirfabric.events.TooltipRenderChangeEvent
+
+object ItemChangeHandler {
+ var prevName = ""
+ fun handle(name: String) {
+ if (name != prevName) {
+ prevName = name
+ TooltipRenderChangeEvent.post()
+ }
+ }
+} \ No newline at end of file
diff --git a/src/main/resources/dulkirmod-fabric.mixins.json b/src/main/resources/dulkirmod-fabric.mixins.json
index ec83ba1..759e752 100644
--- a/src/main/resources/dulkirmod-fabric.mixins.json
+++ b/src/main/resources/dulkirmod-fabric.mixins.json
@@ -13,5 +13,8 @@
"render.KeyboardMixin",
"render.PerspectiveMixin",
"render.ScreenMixin"
+ ],
+ "mixins": [
+ "ItemStackMixin"
]
} \ No newline at end of file