| 1 | package net.minecraft.client.gui.components.tabs;
|
| 2 |
|
| 3 | import com.google.common.collect.ImmutableList;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.gui.ActiveTextCollector;
|
| 8 | import net.minecraft.client.gui.Font;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.components.TabButton;
|
| 11 | import net.minecraft.client.gui.components.WidgetSprites;
|
| 12 | import net.minecraft.client.gui.screens.Screen;
|
| 13 | import net.minecraft.client.renderer.RenderPipelines;
|
| 14 | import net.minecraft.resources.Identifier;
|
| 15 | import net.minecraft.util.Mth;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class MenuTabBar extends TabNavigationBar {
|
| 19 | private static final int HEIGHT = 24;
|
| 20 | private static final int MAX_WIDTH = 400;
|
| 21 | private static final int MARGIN = 14;
|
| 22 |
|
| 23 | public MenuTabBar(
|
| 24 | final int x,
|
| 25 | final int y,
|
| 26 | final int width,
|
| 27 | final int height,
|
| 28 | final TabManager tabManager,
|
| 29 | final ImmutableList<TabButton> tabButtons,
|
| 30 | final ImmutableList<Tab> tabs
|
| 31 | ) {
|
| 32 | super(x, y, width, height, tabManager, tabButtons, tabs);
|
| 33 | }
|
| 34 |
|
| 35 | public static MenuTabBar.Builder builder(final TabManager tabManager, final int width) {
|
| 36 | return new MenuTabBar.Builder(tabManager, width);
|
| 37 | }
|
| 38 |
|
| 39 | @Override
|
| 40 | protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 41 | graphics.blit(
|
| 42 | RenderPipelines.GUI_TEXTURED,
|
| 43 | Screen.HEADER_SEPARATOR,
|
| 44 | 0,
|
| 45 | this.layout.getY() + this.layout.getHeight() - 2,
|
| 46 | 0.0F,
|
| 47 | 0.0F,
|
| 48 | this.tabButtons.getFirst().getX(),
|
| 49 | 2,
|
| 50 | 32,
|
| 51 | 2
|
| 52 | );
|
| 53 | int afterLastTab = this.tabButtons.get(this.tabButtons.size() - 1).getRight();
|
| 54 | graphics.blit(
|
| 55 | RenderPipelines.GUI_TEXTURED, Screen.HEADER_SEPARATOR, afterLastTab, this.layout.getY() + this.layout.getHeight() - 2, 0.0F, 0.0F, this.width, 2, 32, 2
|
| 56 | );
|
| 57 | super.extractWidgetRenderState(graphics, mouseX, mouseY, a);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public void arrangeElements(final int width) {
|
| 62 | this.width = width;
|
| 63 | int tabsWidth = Math.min(400, width) - 28;
|
| 64 | int tabWidth = Mth.roundToward(tabsWidth / this.tabs.size(), 2);
|
| 65 |
|
| 66 | for (TabButton button : this.tabButtons) {
|
| 67 | button.setWidth(tabWidth);
|
| 68 | }
|
| 69 |
|
| 70 | this.layout.arrangeElements();
|
| 71 | this.layout.setX(Mth.roundToward((width - tabsWidth) / 2, 2));
|
| 72 | this.layout.setY(0);
|
| 73 | }
|
| 74 |
|
| 75 | @Environment(EnvType.CLIENT)
|
| 76 | public static class Builder extends TabNavigationBar.Builder {
|
| 77 | private Builder(final TabManager tabManager, final int width) {
|
| 78 | super(tabManager, 0, 0, width, 24);
|
| 79 | }
|
| 80 |
|
| 81 | public MenuTabBar.Builder addTab(final Tab tab) {
|
| 82 | super.addTab(new MenuTabBar.MenuTabButton(this.tabManager, tab, 0, this.height), tab);
|
| 83 | return this;
|
| 84 | }
|
| 85 |
|
| 86 | public MenuTabBar.Builder addTabs(final Tab... tabs) {
|
| 87 | for (Tab tab : tabs) {
|
| 88 | this.addTab(tab);
|
| 89 | }
|
| 90 |
|
| 91 | return this;
|
| 92 | }
|
| 93 |
|
| 94 | public MenuTabBar build() {
|
| 95 | return new MenuTabBar(this.x, this.y, this.width, this.height, this.tabManager, ImmutableList.copyOf(this.tabButtons), ImmutableList.copyOf(this.tabs));
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | @Environment(EnvType.CLIENT)
|
| 100 | public static class MenuTabButton extends TabButton {
|
| 101 | private static final WidgetSprites SPRITES = new WidgetSprites(
|
| 102 | Identifier.withDefaultNamespace("widget/tab_selected"),
|
| 103 | Identifier.withDefaultNamespace("widget/tab"),
|
| 104 | Identifier.withDefaultNamespace("widget/tab_selected_highlighted"),
|
| 105 | Identifier.withDefaultNamespace("widget/tab_highlighted")
|
| 106 | );
|
| 107 | private static final int SELECTED_OFFSET = 3;
|
| 108 | private static final int TEXT_MARGIN = 1;
|
| 109 | private static final int UNDERLINE_HEIGHT = 1;
|
| 110 | private static final int UNDERLINE_MARGIN_X = 4;
|
| 111 | private static final int UNDERLINE_MARGIN_BOTTOM = 2;
|
| 112 |
|
| 113 | public MenuTabButton(final TabManager tabManager, final Tab tab, final int width, final int height) {
|
| 114 | super(tabManager, tab, width, height);
|
| 115 | }
|
| 116 |
|
| 117 | @Override
|
| 118 | protected void extractWidgetRenderState(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 119 | graphics.blitSprite(
|
| 120 | RenderPipelines.GUI_TEXTURED, SPRITES.get(this.isSelected(), this.isHoveredOrFocused()), this.getX(), this.getY(), this.width, this.height
|
| 121 | );
|
| 122 | Font font = Minecraft.getInstance().font;
|
| 123 | int underlineColor = this.active ? -1 : -6250336;
|
| 124 | if (this.isSelected()) {
|
| 125 | this.renderMenuBackground(graphics, this.getX() + 2, this.getY() + 2, this.getRight() - 2, this.getBottom());
|
| 126 | this.renderFocusUnderline(graphics, font, underlineColor);
|
| 127 | }
|
| 128 |
|
| 129 | this.renderLabel(graphics.textRendererForWidget(this, GuiGraphicsExtractor.HoveredTextEffects.NONE));
|
| 130 | this.handleCursor(graphics);
|
| 131 | }
|
| 132 |
|
| 133 | protected void renderMenuBackground(final GuiGraphicsExtractor graphics, final int x0, final int y0, final int x1, final int y1) {
|
| 134 | Screen.extractMenuBackgroundTexture(graphics, Screen.MENU_BACKGROUND, x0, y0, 0.0F, 0.0F, x1 - x0, y1 - y0);
|
| 135 | }
|
| 136 |
|
| 137 | private void renderLabel(final ActiveTextCollector output) {
|
| 138 | int left = this.getX() + 1;
|
| 139 | int top = this.getY() + (this.isSelected() ? 0 : 3);
|
| 140 | int right = this.getX() + this.getWidth() - 1;
|
| 141 | int bottom = this.getY() + this.getHeight();
|
| 142 | output.acceptScrollingWithDefaultCenter(this.getMessage(), left, right, top, bottom);
|
| 143 | }
|
| 144 |
|
| 145 | private void renderFocusUnderline(final GuiGraphicsExtractor graphics, final Font font, final int color) {
|
| 146 | int width = Math.min(font.width(this.getMessage()), this.getWidth() - 4);
|
| 147 | int left = this.getX() + (this.getWidth() - width) / 2;
|
| 148 | int top = this.getY() + this.getHeight() - 2;
|
| 149 | graphics.fill(left, top, left + width, top + 1, color);
|
| 150 | }
|
| 151 | }
|
| 152 | }
|
| 153 |
|