Skip to content

Commit

Permalink
more robust triggering of associated entry from tooltip
Browse files Browse the repository at this point in the history
  • Loading branch information
gliscowo committed May 18, 2024
1 parent e81aec1 commit c40a0d2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.15.1

# Mod Properties
mod_version = 0.1.8
mod_version = 0.1.9
maven_group = io.wispforest
archives_base_name = lavender

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@
import io.wispforest.owo.ui.container.Containers;
import io.wispforest.owo.ui.container.FlowLayout;
import io.wispforest.owo.ui.core.*;
import io.wispforest.owo.ui.util.Delta;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
Expand All @@ -17,6 +20,7 @@

public class AssociatedEntryTooltipComponent implements TooltipComponent {

public static float entryTriggerProgress = 0f;
public static @Nullable WeakReference<ItemStack> tooltipStack = null;

private final FlowLayout layout;
Expand Down Expand Up @@ -61,4 +65,11 @@ public int getHeight() {
public int getWidth(TextRenderer textRenderer) {
return this.layout.width();
}

static {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (Screen.hasAltDown()) return;
entryTriggerProgress += Delta.compute(entryTriggerProgress, 0f, .125f);
});
}
}
18 changes: 10 additions & 8 deletions src/main/java/io/wispforest/lavender/mixin/DrawContextMixin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.wispforest.lavender.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import io.wispforest.lavender.book.LavenderBookItem;
import io.wispforest.lavender.book.BookLoader;
import io.wispforest.lavender.client.AssociatedEntryTooltipComponent;
Expand All @@ -12,21 +14,20 @@
import net.minecraft.client.gui.tooltip.TooltipComponent;
import net.minecraft.client.gui.tooltip.TooltipPositioner;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Unique;
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.ArrayList;
import java.util.List;

import static io.wispforest.lavender.client.AssociatedEntryTooltipComponent.entryTriggerProgress;

@Mixin(DrawContext.class)
public class DrawContextMixin {

@Unique
private static float entryTriggerProgress = 0f;

@Inject(method = "drawTooltip(Lnet/minecraft/client/font/TextRenderer;Ljava/util/List;IILnet/minecraft/client/gui/tooltip/TooltipPositioner;)V", at = @At("HEAD"))
private void injectTooltipComponents(TextRenderer textRenderer, List<TooltipComponent> components, int x, int y, TooltipPositioner positioner, CallbackInfo ci) {
private void injectTooltipComponents(TextRenderer textRenderer, List<TooltipComponent> components, int x, int y, TooltipPositioner positioner, CallbackInfo ci, @Local(argsOnly = true) LocalRef<List<TooltipComponent>> componentsRef) {
var client = MinecraftClient.getInstance();

if (AssociatedEntryTooltipComponent.tooltipStack != null && AssociatedEntryTooltipComponent.tooltipStack.get() != null) {
Expand All @@ -51,8 +52,11 @@ private void injectTooltipComponents(TextRenderer textRenderer, List<TooltipComp

if (bookIndex == -1) return;

components = new ArrayList<>(components);
components.add(new AssociatedEntryTooltipComponent(LavenderBookItem.itemOf(book), associatedEntry, entryTriggerProgress));
entryTriggerProgress += Delta.compute(entryTriggerProgress, Screen.hasAltDown() ? 1.35f : 0f, client.getLastFrameDuration() * .125);
componentsRef.set(components);

entryTriggerProgress += Delta.compute(entryTriggerProgress, Screen.hasAltDown() ? 1.35f : 0f, client.getLastFrameDuration() * .125f);

if (entryTriggerProgress >= .95) {
LavenderBookScreen.pushEntry(book, associatedEntry);
Expand All @@ -68,7 +72,5 @@ private void injectTooltipComponents(TextRenderer textRenderer, List<TooltipComp
return;
}
}

entryTriggerProgress += Delta.compute(entryTriggerProgress, 0f, client.getLastFrameDuration() * .125);
}
}

0 comments on commit c40a0d2

Please sign in to comment.