diff options
Diffstat (limited to 'src/main')
5 files changed, 41 insertions, 20 deletions
diff --git a/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java b/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java index e88c144..ec70d60 100644 --- a/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java +++ b/src/main/java/dev/isxander/yacl3/gui/YACLScreen.java @@ -43,7 +43,7 @@ public class YACLScreen extends Screen { private final Screen parent; public final TabManager tabManager = new TabManager(this::addRenderableWidget, this::removeWidget); - public TabNavigationBar tabNavigationBar; + public ScrollableNavigationBar tabNavigationBar; public ScreenRectangle tabArea; public Component saveButtonMessage; @@ -67,7 +67,7 @@ public class YACLScreen extends Screen { tabArea = new ScreenRectangle(0, 24 - 1, this.width, this.height - 24 + 1); int currentTab = tabNavigationBar != null - ? tabNavigationBar.tabs.indexOf(tabManager.getCurrentTab()) + ? tabNavigationBar.getTabs().indexOf(tabManager.getCurrentTab()) : 0; if (currentTab == -1) currentTab = 0; diff --git a/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java b/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java index 5829202..f458557 100644 --- a/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java +++ b/src/main/java/dev/isxander/yacl3/gui/tab/ScrollableNavigationBar.java @@ -23,11 +23,14 @@ public class ScrollableNavigationBar extends TabNavigationBar { private int scrollOffset; private int maxScrollOffset; + private final TabNavigationBarAccessor accessor; + public ScrollableNavigationBar(int width, TabManager tabManager, Iterable<? extends Tab> tabs) { super(width, tabManager, ImmutableList.copyOf(tabs)); + this.accessor = (TabNavigationBarAccessor) this; // add tab tooltips to the tab buttons - for (TabButton tabButton : this.tabButtons) { + for (TabButton tabButton : accessor.getTabButtons()) { if (tabButton.tab() instanceof TabExt tab) { tabButton.setTooltip(tab.getTooltip()); } @@ -36,20 +39,21 @@ public class ScrollableNavigationBar extends TabNavigationBar { @Override public void arrangeElements() { - int noScrollWidth = this.width - NAVBAR_MARGIN*2; + ImmutableList<TabButton> tabButtons = accessor.getTabButtons(); + int noScrollWidth = accessor.getWidth() - NAVBAR_MARGIN*2; int allTabsWidth = 0; // first pass: set the width of each tab button - for (TabButton tabButton : this.tabButtons) { + for (TabButton tabButton : tabButtons) { int buttonWidth = font.width(tabButton.getMessage()) + 20; allTabsWidth += buttonWidth; tabButton.setWidth(buttonWidth); } if (allTabsWidth < noScrollWidth) { - int equalWidth = noScrollWidth / this.tabButtons.size(); - var smallTabs = this.tabButtons.stream().filter(btn -> btn.getWidth() < equalWidth).toList(); - var bigTabs = this.tabButtons.stream().filter(btn -> btn.getWidth() >= equalWidth).toList(); + int equalWidth = noScrollWidth / tabButtons.size(); + var smallTabs = tabButtons.stream().filter(btn -> btn.getWidth() < equalWidth).toList(); + var bigTabs = tabButtons.stream().filter(btn -> btn.getWidth() >= equalWidth).toList(); int leftoverWidth = noScrollWidth - bigTabs.stream().mapToInt(AbstractWidget::getWidth).sum(); int equalWidthForSmallTabs = leftoverWidth / smallTabs.size(); for (TabButton tabButton : smallTabs) { @@ -64,7 +68,7 @@ public class ScrollableNavigationBar extends TabNavigationBar { layout.setY(0); scrollOffset = 0; - layout.setX(Math.max((this.width - allTabsWidth) / 2, NAVBAR_MARGIN)); + layout.setX(Math.max((accessor.getWidth() - allTabsWidth) / 2, NAVBAR_MARGIN)); this.maxScrollOffset = Math.max(0, allTabsWidth - noScrollWidth); } @@ -113,8 +117,12 @@ public class ScrollableNavigationBar extends TabNavigationBar { protected void ensureVisible(TabButton tabButton) { if (tabButton.getX() < NAVBAR_MARGIN) { this.setScrollOffset(this.scrollOffset - (NAVBAR_MARGIN - tabButton.getX())); - } else if (tabButton.getX() + tabButton.getWidth() > this.width - NAVBAR_MARGIN) { - this.setScrollOffset(this.scrollOffset + (tabButton.getX() + tabButton.getWidth() - (this.width - NAVBAR_MARGIN))); + } else if (tabButton.getX() + tabButton.getWidth() > accessor.getWidth() - NAVBAR_MARGIN) { + this.setScrollOffset(this.scrollOffset + (tabButton.getX() + tabButton.getWidth() - (accessor.getWidth() - NAVBAR_MARGIN))); } } + + public ImmutableList<Tab> getTabs() { + return accessor.getTabs(); + } } diff --git a/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java b/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java index bd5ada0..f22af36 100644 --- a/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java +++ b/src/main/java/dev/isxander/yacl3/mixin/ContainerEventHandlerMixin.java @@ -1,7 +1,9 @@ -/*? if !forge {*/ package dev.isxander.yacl3.mixin; import net.minecraft.client.gui.components.events.ContainerEventHandler; +import org.spongepowered.asm.mixin.Mixin; + +/*? if !forge {*/ import net.minecraft.client.gui.components.events.GuiEventListener; import net.minecraft.client.gui.components.tabs.TabNavigationBar; import net.minecraft.client.gui.navigation.FocusNavigationEvent; @@ -9,13 +11,11 @@ import net.minecraft.client.gui.navigation.ScreenAxis; import net.minecraft.client.gui.navigation.ScreenDirection; import net.minecraft.client.gui.navigation.ScreenRectangle; import org.jetbrains.annotations.Nullable; -import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Redirect; import java.util.List; - @Mixin(ContainerEventHandler.class) public interface ContainerEventHandlerMixin { // This mixin is used to prevent the tab bar from being focused when navigating left or right @@ -30,7 +30,7 @@ public interface ContainerEventHandlerMixin { } } /*?} else {*//* -@Mixin(targets = {}) +@Mixin(ContainerEventHandler.class) public class ContainerEventHandlerMixin { } diff --git a/src/main/java/dev/isxander/yacl3/mixin/TabNavigationBarAccessor.java b/src/main/java/dev/isxander/yacl3/mixin/TabNavigationBarAccessor.java index 388407b..f0da81d 100644 --- a/src/main/java/dev/isxander/yacl3/mixin/TabNavigationBarAccessor.java +++ b/src/main/java/dev/isxander/yacl3/mixin/TabNavigationBarAccessor.java @@ -1,8 +1,13 @@ package dev.isxander.yacl3.mixin; +import com.google.common.collect.ImmutableList; +import net.minecraft.client.gui.components.TabButton; +import net.minecraft.client.gui.components.tabs.Tab; +import net.minecraft.client.gui.components.tabs.TabManager; import net.minecraft.client.gui.components.tabs.TabNavigationBar; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.gen.Invoker; @Mixin(TabNavigationBar.class) public interface TabNavigationBarAccessor { @@ -13,4 +18,17 @@ public interface TabNavigationBarAccessor { @Accessor net.minecraft.client.gui.layouts.GridLayout getLayout(); /*?}*/ + + @Accessor + int getWidth(); + + @Accessor + TabManager getTabManager(); + + @Accessor + ImmutableList<Tab> getTabs(); + + @Accessor + ImmutableList<TabButton> getTabButtons(); + } diff --git a/src/main/resources/yacl.accesswidener b/src/main/resources/yacl.accesswidener index 7c0e2ca..e7bf3a4 100644 --- a/src/main/resources/yacl.accesswidener +++ b/src/main/resources/yacl.accesswidener @@ -4,9 +4,4 @@ extendable method net/minecraft/client/gui/components/AbstractSelectionList chil extendable method net/minecraft/client/gui/components/AbstractSelectionList getEntryAtPosition (DD)Lnet/minecraft/client/gui/components/AbstractSelectionList$Entry; accessible class net/minecraft/client/gui/components/AbstractSelectionList$Entry accessible method net/minecraft/client/gui/components/tabs/TabNavigationBar <init> (ILnet/minecraft/client/gui/components/tabs/TabManager;Ljava/lang/Iterable;)V -accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar layout Lnet/minecraft/client/gui/layouts/GridLayout; -accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar width I -accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar tabManager Lnet/minecraft/client/gui/components/tabs/TabManager; -accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar tabs Lcom/google/common/collect/ImmutableList; -accessible field net/minecraft/client/gui/components/tabs/TabNavigationBar tabButtons Lcom/google/common/collect/ImmutableList; accessible method net/minecraft/client/gui/components/Tooltip <init> (Lnet/minecraft/network/chat/Component;Lnet/minecraft/network/chat/Component;)V |