1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
package dev.isxander.yacl3.gui.tab;
import com.google.common.collect.ImmutableList;
import dev.isxander.yacl3.mixin.TabNavigationBarAccessor;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.AbstractWidget;
import net.minecraft.client.gui.components.TabButton;
import net.minecraft.client.gui.components.events.GuiEventListener;
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 net.minecraft.client.gui.layouts.Layout;
import net.minecraft.util.Mth;
import org.jetbrains.annotations.Nullable;
public class ScrollableNavigationBar extends TabNavigationBar {
private static final int NAVBAR_MARGIN = 28;
private static final Font font = Minecraft.getInstance().font;
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 : accessor.yacl$getTabButtons()) {
if (tabButton.tab() instanceof TabExt tab) {
tabButton.setTooltip(tab.getTooltip());
}
}
}
@Override
public void arrangeElements() {
ImmutableList<TabButton> tabButtons = accessor.yacl$getTabButtons();
int noScrollWidth = accessor.yacl$getWidth() - NAVBAR_MARGIN*2;
int allTabsWidth = 0;
// first pass: set the width of each tab button
for (TabButton tabButton : tabButtons) {
int buttonWidth = font.width(tabButton.getMessage()) + 20;
allTabsWidth += buttonWidth;
tabButton.setWidth(buttonWidth);
}
if (allTabsWidth < noScrollWidth) {
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) {
tabButton.setWidth(equalWidthForSmallTabs);
}
allTabsWidth = noScrollWidth;
}
Layout layout = ((TabNavigationBarAccessor) this).yacl$getLayout();
layout.arrangeElements();
layout.setY(0);
scrollOffset = 0;
layout.setX(Math.max((accessor.yacl$getWidth() - allTabsWidth) / 2, NAVBAR_MARGIN));
this.maxScrollOffset = Math.max(0, allTabsWidth - noScrollWidth);
}
@Override
public void render(GuiGraphics graphics, int mouseX, int mouseY, float delta) {
graphics.pose().pushPose();
// render option list BELOW the navbar without need to scissor
graphics.pose().translate(0, 0, 10);
super.render(graphics, mouseX, mouseY, delta);
graphics.pose().popPose();
}
@Override
public boolean mouseScrolled(double mouseX, double mouseY, /*? if >1.20.2 {*/ double horizontal, /*?}*/ double vertical) {
this.setScrollOffset(this.scrollOffset - (int)(vertical*15));
return true;
}
@Override
public boolean isMouseOver(double mouseX, double mouseY) {
return mouseY <= 24;
}
public void setScrollOffset(int scrollOffset) {
Layout layout = ((TabNavigationBarAccessor) this).yacl$getLayout();
layout.setX(layout.getX() + this.scrollOffset);
this.scrollOffset = Mth.clamp(scrollOffset, 0, maxScrollOffset);
layout.setX(layout.getX() - this.scrollOffset);
}
public int getScrollOffset() {
return scrollOffset;
}
@Override
public void setFocused(@Nullable GuiEventListener child) {
super.setFocused(child);
if (child instanceof TabButton tabButton) {
this.ensureVisible(tabButton);
}
}
protected void ensureVisible(TabButton tabButton) {
if (tabButton.getX() < NAVBAR_MARGIN) {
this.setScrollOffset(this.scrollOffset - (NAVBAR_MARGIN - tabButton.getX()));
} else if (tabButton.getX() + tabButton.getWidth() > accessor.yacl$getWidth() - NAVBAR_MARGIN) {
this.setScrollOffset(this.scrollOffset + (tabButton.getX() + tabButton.getWidth() - (accessor.yacl$getWidth() - NAVBAR_MARGIN)));
}
}
public ImmutableList<Tab> getTabs() {
return accessor.yacl$getTabs();
}
public TabManager getTabManager() {
return accessor.yacl$getTabManager();
}
}
|