Skip to content

Commit

Permalink
fixed one line scrolling with smooth scrolling
Browse files Browse the repository at this point in the history
  • Loading branch information
ImToggle committed Jul 16, 2024
1 parent f31f153 commit d252bd5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<ChatLine> 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()) {
Expand All @@ -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);
}
}
}
8 changes: 2 additions & 6 deletions src/main/kotlin/org/polyfrost/chatting/chat/ChatWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d252bd5

Please sign in to comment.