Skip to content

Commit

Permalink
Improved Scrollbar Range Behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
IntegerLimit committed Dec 27, 2024
1 parent d60f819 commit 5cbe59b
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import net.minecraft.util.math.Vec3d;

import com.projecturanus.betterp2p.client.gui.widget.WidgetScrollBar;

public interface AccessibleInfoList {

void labs$setPlayerPos(Vec3d pos);

void labs$properlyResetScrollbar(WidgetScrollBar scrollBar, int numEntries);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import com.projecturanus.betterp2p.client.gui.GuiAdvancedMemoryCard;
import com.projecturanus.betterp2p.client.gui.InfoList;
import com.projecturanus.betterp2p.client.gui.InfoWrapper;
import com.projecturanus.betterp2p.client.gui.widget.GuiScale;
import com.projecturanus.betterp2p.client.gui.widget.WidgetScrollBar;
import com.projecturanus.betterp2p.client.gui.widget.WidgetTypeSelector;
import com.projecturanus.betterp2p.item.BetterMemoryCardModes;

Expand Down Expand Up @@ -45,6 +47,13 @@ public abstract class GuiAdvancedMemoryCardMixin extends GuiScreen implements Ac
@Final
private InfoList infos;

@Shadow
@Final
private WidgetScrollBar scrollBar;

@Shadow
private GuiScale scale;

@Override
@Unique
public BetterMemoryCardModes labs$getMode() {
Expand Down Expand Up @@ -74,6 +83,33 @@ private void setupInfoListPlayerPos(CallbackInfo ci) {
((AccessibleInfoList) (Object) infos).labs$setPlayerPos(mc.player.getPositionVector());
}

@Inject(method = "initGui", at = @At("TAIL"), remap = true)
private void properlySetScrollbarInit(CallbackInfo ci) {
labs$properlyResetScrollbar();
}

@Inject(method = "mouseClicked",
at = @At(value = "INVOKE",
target = "Lcom/projecturanus/betterp2p/client/gui/InfoList;refilter()V",
shift = At.Shift.AFTER,
remap = false),
require = 1,
remap = true)
private void properlyResetScrollbarFilterClear(int mouseX, int mouseY, int mouseButton, CallbackInfo ci) {
labs$properlyResetScrollbar();
}

@Inject(method = "keyTyped",
at = @At(value = "INVOKE",
target = "Lcom/projecturanus/betterp2p/client/gui/InfoList;refilter()V",
shift = At.Shift.AFTER,
remap = false),
require = 1,
remap = true)
private void properlyResetScrollbarFilterTyped(char typedChar, int keyCode, CallbackInfo ci) {
labs$properlyResetScrollbar();
}

@Inject(method = "refreshOverlay", at = @At("HEAD"))
private void fillLabsCache(CallbackInfo ci) {
LabsClientCache.inputLoc.clear();
Expand All @@ -92,4 +128,10 @@ private void fillLabsCache(CallbackInfo ci) {
.forEach(pair -> pair.getFirst()
.add(new Pair<>(pair.getSecond().getPos(), pair.getSecond().getFacing())));
}

@Unique
private void labs$properlyResetScrollbar() {
((AccessibleInfoList) (Object) infos).labs$properlyResetScrollbar(scrollBar,
scale.getSize().invoke(height - 75));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

/**
* Handles updating infos' distance to player, and allows for custom sorting.
* Improves handling of scrollbar after refresh, reload, or refilter.
*/
@Mixin(value = InfoList.class, remap = false)
public abstract class InfoListMixin implements AccessibleInfoList {
Expand Down Expand Up @@ -57,18 +58,53 @@ public abstract class InfoListMixin implements AccessibleInfoList {
calcDistFor(masterMap.values());
}

@Unique
@Override
public void labs$properlyResetScrollbar(WidgetScrollBar scrollBar, int numEntries) {
// numEntries is the amount of entries visible
// So we subtract from the total size (if 16 entries, but 5 are visible, we only need 11 scrolls)
var size = Math.max(0, labs$getThis().getFiltered().size() - numEntries);
// pageSize seems to be unused, or set to either 1 or 23
scrollBar.setRange(0, size, 23);

// Reset scroll position
scrollBar.setCurrentScroll(0);
}

@Inject(method = "rebuild", at = @At("HEAD"))
private void calcDistanceInRebuild(Collection<InfoWrapper> updateList, WidgetScrollBar scrollbar, int numEntries,
CallbackInfo ci) {
calcDistFor(updateList);
}

@Inject(method = "rebuild",
at = @At(value = "INVOKE",
target = "Lcom/projecturanus/betterp2p/client/gui/widget/WidgetScrollBar;setRange(III)V"),
require = 1,
cancellable = true)
private void replaceOldRebuildScrollbar(Collection<InfoWrapper> updateList, WidgetScrollBar scrollbar,
int numEntries, CallbackInfo ci) {
ci.cancel();
labs$properlyResetScrollbar(scrollbar, numEntries);
}

@Inject(method = "update", at = @At("HEAD"))
private void calcDistanceInUpdate(Collection<InfoWrapper> updateList, WidgetScrollBar scrollbar, int numEntries,
CallbackInfo ci) {
calcDistFor(updateList);
}

@Inject(method = "update",
at = @At(value = "INVOKE",
target = "Lcom/projecturanus/betterp2p/client/gui/widget/WidgetScrollBar;setRange(III)V"),
require = 1,
cancellable = true)
private void replaceOldUpdateScrollbar(Collection<InfoWrapper> updateList, WidgetScrollBar scrollbar,
int numEntries, CallbackInfo ci) {
ci.cancel();
labs$properlyResetScrollbar(scrollbar, numEntries);
}

@Unique
private void calcDistFor(Iterable<InfoWrapper> infos) {
for (InfoWrapper info : infos) {
Expand Down

0 comments on commit 5cbe59b

Please sign in to comment.