From 5212756a4913160a4519066fb928955dea99d009 Mon Sep 17 00:00:00 2001 From: Integer Limit <103940576+IntegerLimit@users.noreply.github.com> Date: Sat, 28 Dec 2024 16:21:46 +1100 Subject: [PATCH] Fix Mouse Interactions with Scrollbar --- .../mixin/betterp2p/WidgetScrollBarMixin.java | 61 +++++++++++++++++++ .../resources/mixins.nomilabs.betterp2p.json | 1 + 2 files changed, 62 insertions(+) create mode 100644 src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetScrollBarMixin.java diff --git a/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetScrollBarMixin.java b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetScrollBarMixin.java new file mode 100644 index 0000000..ecb59a9 --- /dev/null +++ b/src/main/java/com/nomiceu/nomilabs/mixin/betterp2p/WidgetScrollBarMixin.java @@ -0,0 +1,61 @@ +package com.nomiceu.nomilabs.mixin.betterp2p; + +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 com.projecturanus.betterp2p.client.gui.widget.WidgetScrollBar; + +/** + * Fixes clicking scrollbar. + */ +@Mixin(value = WidgetScrollBar.class, remap = false) +public abstract class WidgetScrollBarMixin { + + @Shadow + protected abstract int getRange(); + + @Shadow + private int displayX; + + @Shadow + private int width; + + @Shadow + private int displayY; + + @Shadow + private int height; + + @Shadow + private int currentScroll; + + @Shadow + private int minScroll; + + @Shadow + private boolean moving; + + @Shadow + protected abstract void applyRange(); + + @Inject(method = "click", at = @At("HEAD"), cancellable = true) + private void properlyHandleClick(int x, int y, CallbackInfo ci) { + ci.cancel(); + if (getRange() == 0) { + return; + } + + if (y > displayY && y <= displayY + height) { + if (x > displayX && x <= displayX + width) { + currentScroll = y - displayY; + currentScroll = minScroll + currentScroll * 2 * getRange() / height; + currentScroll = (currentScroll + 1) >> 1; + applyRange(); + moving = true; + } + } + } +} diff --git a/src/main/resources/mixins.nomilabs.betterp2p.json b/src/main/resources/mixins.nomilabs.betterp2p.json index dd922b3..82de40e 100644 --- a/src/main/resources/mixins.nomilabs.betterp2p.json +++ b/src/main/resources/mixins.nomilabs.betterp2p.json @@ -20,6 +20,7 @@ "RenderHandlerMixin", "WidgetP2PColumnMixin", "WidgetP2PDeviceMixin", + "WidgetScrollBarMixin", "WidgetTypeSelectorMixin" ], "server": []