Skip to content

Commit

Permalink
Implement Remember Tab Scroll Position functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ACGaming committed Dec 14, 2024
1 parent 3f429cc commit 05062cc
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1711,6 +1711,10 @@ public static class AdvancementsCategory
@Config.Name("[09] Add Advancement Tab Title to Header")
@Config.Comment("Makes the focused Advancement Tab Title be added to the header, which otherwise is just 'Advancements' for every tab")
public boolean utAddFocusedTabTitleToHeader = true;

@Config.Name("[10] Remember Tab Scroll Position")
@Config.Comment("Remembers and restores the last advancement tab scroll position")
public boolean utRememberTabScrollPosition = false;
}

public static class ArmorCurveCategory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public class UTAdvancementInfo
private static final int HEIGHT_MULT = 28;
private static final int WIDTH_MULT = 32;

public static int lastScrollX = -1;
public static int lastScrollY = -1;
public static int lastSelectedTabIndex;

public static int utPageWidth(int externalWidth)
{
return utStepwiseWidth(utClampWidth(externalWidth - UTConfigTweaks.MISC.ADVANCEMENTS.utHorizontalMargin * 2));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package mod.acgaming.universaltweaks.tweaks.misc.advancements.guisize.mixin;

import net.minecraft.client.gui.advancements.GuiAdvancementTab;

import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(GuiAdvancementTab.class)
public interface UTGuiAdvancementTabAccessor
{
@Accessor("scrollX")
int getScrollX();

@Accessor("scrollY")
int getScrollY();

@Accessor("index")
int getIndex();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Constant;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyConstant;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

// Courtesy of WaitingIdly
@Mixin(GuiAdvancementTab.class)
Expand All @@ -39,6 +41,16 @@ private static int utAdjustIndividualMaxTabs(AdvancementTabType instance, Operat
@Final
private GuiScreenAdvancements screen;

@Shadow
private int scrollX;
@Shadow
private int scrollY;
@Shadow
@Final
private int index;
@Shadow
private boolean centered;

// update the size
@ModifyConstant(method = {"drawContents", "scroll", "drawToolTips"}, constant = @Constant(intValue = UTAdvancementInfo.DEFAULT_WIDTH - 18))
private int utDrawWidth(int original)
Expand Down Expand Up @@ -91,4 +103,19 @@ private float utRemoveFade(float original)
if (!UTConfigTweaks.MISC.ADVANCEMENTS.utAdvancementsToggle || !UTConfigTweaks.MISC.ADVANCEMENTS.utDisableFadeOnHover) return original;
return 0;
}

/**
* @reason restore the last tab scroll position
*/
@Inject(method = "drawContents", at = @At("HEAD"))
private void utRestoreTabScrollPosition(CallbackInfo ci)
{
if (!UTConfigTweaks.MISC.ADVANCEMENTS.utAdvancementsToggle || !UTConfigTweaks.MISC.ADVANCEMENTS.utRememberTabScrollPosition || UTAdvancementInfo.lastScrollX == -1 || UTAdvancementInfo.lastScrollY == -1 || UTAdvancementInfo.lastSelectedTabIndex != index || centered)
{
return;
}
scrollX = UTAdvancementInfo.lastScrollX;
scrollY = UTAdvancementInfo.lastScrollY;
centered = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public abstract class UTGuiScreenAdvancementsMixin extends GuiScreen
@Unique
private GuiButton buttonRight;


/**
* @reason ensure the maxPages field is set to 0 on gui size update, otherwise it is only updated if >0, meaning it will always linger at 1+
*/
Expand Down Expand Up @@ -315,6 +314,21 @@ private void utRenderLargerWindow(GuiScreenAdvancements instance, int left, int
utRenderTextureRepeating(left, top + texture_corner, texture_corner, bottom - top - texture_corner * 2, 0, texture_corner, texture_corner, texture_height - texture_corner * 2);
}

/**
* @reason remember the current tab scroll position
*/
@Inject(method = "onGuiClosed", at = @At("HEAD"))
private void utRememberTabScrollPosition(CallbackInfo ci)
{
if (!UTConfigTweaks.MISC.ADVANCEMENTS.utAdvancementsToggle || !UTConfigTweaks.MISC.ADVANCEMENTS.utRememberTabScrollPosition || selectedTab == null)
{
return;
}
UTAdvancementInfo.lastScrollX = (((UTGuiAdvancementTabAccessor) selectedTab).getScrollX());
UTAdvancementInfo.lastScrollY = (((UTGuiAdvancementTabAccessor) selectedTab).getScrollY());
UTAdvancementInfo.lastSelectedTabIndex = (((UTGuiAdvancementTabAccessor) selectedTab).getIndex());
}

@Unique
private void utRenderTextureRepeating(int x, int y, int width, int height, int textureX, int textureY, int textureWidth, int textureHeight)
{
Expand All @@ -330,5 +344,4 @@ private void utRenderTextureRepeating(int x, int y, int width, int height, int t
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"refmap": "universaltweaks.refmap.json",
"minVersion": "0.8",
"compatibilityLevel": "JAVA_8",
"client": ["UTAdvancementTabTypeMixin", "UTGuiAdvancementTabMixin", "UTGuiScreenAdvancementsMixin"]
"client": ["UTAdvancementTabTypeMixin", "UTGuiAdvancementTabMixin", "UTGuiAdvancementTabAccessor", "UTGuiScreenAdvancementsMixin"]
}

0 comments on commit 05062cc

Please sign in to comment.