From d252bd59e06954ee800fb04d1fe1c30bb961e6b9 Mon Sep 17 00:00:00 2001 From: ImToggle <98242902+ImToggle@users.noreply.github.com> Date: Tue, 16 Jul 2024 17:58:10 +0800 Subject: [PATCH] fixed one line scrolling with smooth scrolling --- .../chatting/mixin/GuiNewChatMixin.java | 2 +- .../mixin/GuiNewChatMixin_Scrolling.java | 47 ++++++------------- .../org/polyfrost/chatting/chat/ChatWindow.kt | 8 +--- 3 files changed, 17 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java index 1a2beb2..093e702 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin.java @@ -183,7 +183,7 @@ private void disableScissor(int updateCounter, CallbackInfo ci) { GL11.glDisable(GL11.GL_SCISSOR_TEST); } - @ModifyArgs(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V"), slice = @Slice(from = @At(value = "INVOKE", target = "Lnet/minecraft/util/MathHelper;clamp_double(DDD)D"), to = @At(value = "INVOKE", target = "Lnet/minecraft/client/renderer/GlStateManager;enableBlend()V"))) + @ModifyArgs(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V", ordinal = 0)) private void captureDrawRect(Args args, int updateCounter) { args.set(4, ColorUtils.getColor(0, 0, 0, 0)); if (mc.currentScreen instanceof GuiChat) { diff --git a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java index 4a28af7..975dd07 100644 --- a/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java +++ b/src/main/java/org/polyfrost/chatting/mixin/GuiNewChatMixin_Scrolling.java @@ -2,14 +2,11 @@ import cc.polyfrost.oneconfig.gui.animations.Animation; import cc.polyfrost.oneconfig.gui.animations.DummyAnimation; -import cc.polyfrost.oneconfig.utils.MathUtils; import org.polyfrost.chatting.chat.ChatScrollingHook; -import net.minecraft.client.gui.ChatLine; import net.minecraft.client.gui.Gui; import net.minecraft.client.gui.GuiNewChat; import org.polyfrost.chatting.config.ChattingConfig; import org.polyfrost.chatting.utils.EaseOutQuad; -import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.Unique; @@ -18,41 +15,34 @@ import org.spongepowered.asm.mixin.injection.Redirect; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import java.util.List; - @Mixin(GuiNewChat.class) public abstract class GuiNewChatMixin_Scrolling extends Gui { @Shadow private int scrollPos; - @Shadow - @Final - private List drawnChatLines; - - @Shadow - public abstract int getLineCount(); - - @Shadow - private boolean isScrolled; - - @Shadow public abstract boolean getChatOpen(); - - @Shadow public abstract void resetScroll(); @Unique private Animation chatting$scrollingAnimation = new DummyAnimation(0f); @Inject(method = "drawChat", at = @At("HEAD")) private void chatting$scrollingAnimationStart(int updateCounter, CallbackInfo ci) { - if (chatting$scrollingAnimation.isFinished()) { - if (!getChatOpen()) resetScroll(); - if (scrollPos == 0) { - isScrolled = false; + boolean shouldSmooth = ChatScrollingHook.INSTANCE.getShouldSmooth(); + if (shouldSmooth) ChatScrollingHook.INSTANCE.setShouldSmooth(false); + if (ChattingConfig.INSTANCE.getSmoothScrolling()) { + if (chatting$scrollingAnimation.getEnd() != scrollPos) { + if (Math.abs(chatting$scrollingAnimation.getEnd() - scrollPos) > 1 && shouldSmooth) { + chatting$scrollingAnimation = new EaseOutQuad((int) (ChattingConfig.INSTANCE.getScrollingSpeed() * 1000), chatting$scrollingAnimation.get(), scrollPos, false); + } else { + chatting$scrollingAnimation = new DummyAnimation(scrollPos); + } } - } else { - scrollPos = (int) chatting$scrollingAnimation.get(); } } + @Redirect(method = "drawChat", at = @At(value = "FIELD", target = "Lnet/minecraft/client/gui/GuiNewChat;scrollPos:I")) + private int redirectPos(GuiNewChat instance) { + return ChattingConfig.INSTANCE.getSmoothScrolling() ? (int) chatting$scrollingAnimation.get() : scrollPos; + } + @Redirect(method = "drawChat", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/gui/GuiNewChat;drawRect(IIIII)V", ordinal = 1)) private void redirectScrollBar(int left, int top, int right, int bottom, int color) { if (!ChattingConfig.INSTANCE.getRemoveScrollBar()) { @@ -67,13 +57,4 @@ private void redirectScrollBar2(int left, int top, int right, int bottom, int co } } - @Inject(method = "scroll", at = @At("HEAD"), cancellable = true) - private void injectScroll(int amount, CallbackInfo ci) { - if (ChattingConfig.INSTANCE.getSmoothScrolling() && amount != 0 && ChatScrollingHook.INSTANCE.getShouldSmooth()) { - ci.cancel(); - ChatScrollingHook.INSTANCE.setShouldSmooth(false); - int result = (int) MathUtils.clamp(scrollPos + amount, 0, Math.max(drawnChatLines.size() - getLineCount() - 1, 0)); - chatting$scrollingAnimation = new EaseOutQuad((int) (ChattingConfig.INSTANCE.getScrollingSpeed() * 1000), scrollPos, result, false); - } - } } diff --git a/src/main/kotlin/org/polyfrost/chatting/chat/ChatWindow.kt b/src/main/kotlin/org/polyfrost/chatting/chat/ChatWindow.kt index ea8a838..b6e9a54 100644 --- a/src/main/kotlin/org/polyfrost/chatting/chat/ChatWindow.kt +++ b/src/main/kotlin/org/polyfrost/chatting/chat/ChatWindow.kt @@ -246,13 +246,9 @@ class ChatWindow : BasicHud(true, 2f, 1080 - 27f - 45f - 12f, return isEnabled && (shouldShow() || Platform.getGuiPlatform().isInChat) && (isGuiIngame xor isCachingIgnored) } - fun getPaddingX(): Float { - return paddingX - } + fun getPaddingX() = paddingX - fun getPaddingY(): Float { - return paddingY - } + fun getPaddingY() = paddingY override fun shouldDrawBackground(): Boolean { return HudCore.editing