Skip to content

Commit

Permalink
save scroll pos, sort tabs, drag to scroll
Browse files Browse the repository at this point in the history
  • Loading branch information
char3210 committed Dec 12, 2023
1 parent 08ce90f commit 100168a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package de.guntram.mcmod.advancementinfo.duck;

public interface IClientAdvancementManager {
int getTabScrollPos();
void setTabScrollPos(int pos);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import de.guntram.mcmod.advancementinfo.AdvancementStep;
import de.guntram.mcmod.advancementinfo.IteratorReceiver;
import de.guntram.mcmod.advancementinfo.duck.IAdvancementsScreen;
import de.guntram.mcmod.advancementinfo.mixin.accessors.AdvancementScreenAccessor;
import de.guntram.mcmod.advancementinfo.duck.IClientAdvancementManager;
import de.guntram.mcmod.advancementinfo.mixin.accessors.AdvancementTabAccessor;
import de.guntram.mcmod.advancementinfo.mixin.accessors.AdvancementWidgetAccessor;

import java.util.*;
Expand Down Expand Up @@ -47,9 +48,10 @@ public abstract class AdvancementScreenMixin extends Screen implements IAdvancem
public AdvancementScreenMixin() { super(null); }

private int scrollPos;
private boolean movingTabScroll;
private double tabScrollPos;
private double targetTabScrollPos;
private long lastUpdateNanos;
private double vel;
private int currentInfoWidth = config.infoWidth.calculate(width);
private TextFieldWidget search;
@Shadow @Final private ClientAdvancementManager advancementHandler;
Expand All @@ -59,6 +61,8 @@ public abstract class AdvancementScreenMixin extends Screen implements IAdvancem

@Shadow @Final private Map<AdvancementEntry, AdvancementTab> tabs;

@Shadow private boolean movingTab;

@ModifyConstant(method="render", constant=@Constant(intValue = 252), require=1)
private int getRenderLeft(int orig) { return width - config.marginX*2; }

Expand Down Expand Up @@ -88,6 +92,18 @@ private void initSearchField(CallbackInfo ci) {
this.search = new TextFieldWidget(textRenderer, width-config.marginX-currentInfoWidth+9, config.marginY+18, currentInfoWidth-18, 17, ScreenTexts.EMPTY);
}

@Inject(method="init", at=@At("RETURN"))
private void initTabScroll(CallbackInfo ci) {
targetTabScrollPos = ((IClientAdvancementManager)advancementHandler).getTabScrollPos();
tabScrollPos = targetTabScrollPos;
}

@Inject(method="removed", at=@At("RETURN"))
private void saveScrollPos(CallbackInfo ci) {
((IClientAdvancementManager)advancementHandler).setTabScrollPos((int)targetTabScrollPos);
}

// is this necessary? no
@Inject(method = "render", at=@At("HEAD"))
public void updateScrollPhys(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci) {
double dt = client.getLastFrameDuration();
Expand Down Expand Up @@ -229,7 +245,16 @@ public void rememberClickedWidget(double x, double y, int button, CallbackInfoRe
// public void debugRootAdded(Advancement root, CallbackInfo ci) {
// System.out.println("root added to screen; display="+root.getDisplay()+", id="+root.getId().toString());
// }


@Inject(method="onRootAdded", at=@At(value="INVOKE", target = "Ljava/util/Map;put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;", shift = At.Shift.AFTER))
public void sortTabs(PlacedAdvancement root, CallbackInfo ci) {
ArrayList<AdvancementTab> advancementTabs = new ArrayList<>(tabs.values());
advancementTabs.sort(Comparator.comparing(tab -> tab.getTitle().getString()));
for (int i = 0; i < advancementTabs.size(); i++) {
((AdvancementTabAccessor) advancementTabs.get(i)).setIndex(i);
}
}

// @Inject(method="mouseScrolled", at=@At("HEAD"), cancellable = true)
/**
* @author
Expand All @@ -247,12 +272,44 @@ public boolean mouseScrolled(double X, double Y, double horizontalAmount, double
}
} else {
targetTabScrollPos -= verticalAmount * 4;
targetTabScrollPos = MathHelper.clamp(targetTabScrollPos, 0, tabs.size() * 32);
targetTabScrollPos = clampTabScroll(targetTabScrollPos);
}
// System.out.println("scrollpos is now "+scrollPos+", needed lines "+AdvancementInfo.cachedClickListLineCount+", shown "+((height-2*config.marginY-45)/textRenderer.fontHeight - 1));
return false;
}

private double clampTabScroll(double val) {
return MathHelper.clamp(val, 0, Math.max(tabs.size() * 32 - (width - 2 * config.marginX), 0));
}

@Inject(method="mouseDragged", at=@At("HEAD"), cancellable = true)
public void onTabsDrag(double mouseX, double mouseY, int button, double deltaX, double deltaY, CallbackInfoReturnable<Boolean> cir) {
if ((!movingTabScroll && mouseY > config.marginY) || button != 0 || movingTab) {
movingTabScroll = false;
return;
}

movingTabScroll = true;
tabScrollPos -= deltaX;
tabScrollPos = clampTabScroll(tabScrollPos);
targetTabScrollPos = tabScrollPos;
vel = -deltaX;
cir.setReturnValue(false);
}

// how do you do this in a compatible way
// i guess mixin to the base class and instanceof?? doesn't seem very performant or maybe it is idk
@Override
public boolean mouseReleased(double mouseX, double mouseY, int button) {
movingTab = false;
movingTabScroll = false;
if (vel != 0) {
targetTabScrollPos = clampTabScroll(tabScrollPos + vel * vel * Math.signum(vel));
vel = 0;
}
return super.mouseReleased(mouseX, mouseY, button);
}

@Inject(method="keyPressed", at=@At("HEAD"), cancellable = true)
public void redirectKeysToSearch(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable<Boolean> cir) {
if (search.isActive()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package de.guntram.mcmod.advancementinfo.mixin;

import de.guntram.mcmod.advancementinfo.duck.IClientAdvancementManager;
import net.minecraft.client.network.ClientAdvancementManager;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(ClientAdvancementManager.class)
public class ClientAdvancementManagerMixin implements IClientAdvancementManager {
private int tabScrollPos;

@Override
public int getTabScrollPos() {
return tabScrollPos;
}

public void setTabScrollPos(int tabScrollPos) {
this.tabScrollPos = tabScrollPos;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package de.guntram.mcmod.advancementinfo.mixin.accessors;

import net.minecraft.client.gui.screen.advancement.AdvancementTab;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Mutable;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(AdvancementTab.class)
public interface AdvancementTabAccessor {
@Mutable
@Accessor
void setIndex(int i);
}
2 changes: 2 additions & 0 deletions src/main/resources/mixins.advancementinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
"AdvancementTabMixin",
"AdvancementTabTypeMixin",
"AdvancementWidgetMixin",
"accessors.AdvancementTabAccessor",
"accessors.AdvancementTabTypeAccessor",
"accessors.AdvancementWidgetAccessor"
],
"injectors": {
"defaultRequire": 1
},
"mixins": [
"ClientAdvancementManagerMixin",
"accessors.AdvancementScreenAccessor"
]
}

0 comments on commit 100168a

Please sign in to comment.