| 1 | package net.minecraft.client.gui.screens.advancements;
|
| 2 |
|
| 3 | import com.google.common.collect.Maps;
|
| 4 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 5 | import java.util.Map;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.advancements.AdvancementHolder;
|
| 9 | import net.minecraft.advancements.AdvancementNode;
|
| 10 | import net.minecraft.advancements.DisplayInfo;
|
| 11 | import net.minecraft.client.Minecraft;
|
| 12 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 13 | import net.minecraft.client.renderer.RenderPipelines;
|
| 14 | import net.minecraft.network.chat.Component;
|
| 15 | import net.minecraft.resources.Identifier;
|
| 16 | import net.minecraft.util.Mth;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.item.ItemStackTemplate;
|
| 19 | import org.jspecify.annotations.Nullable;
|
| 20 |
|
| 21 | @Environment(EnvType.CLIENT)
|
| 22 | public class AdvancementTab {
|
| 23 | private final Minecraft minecraft;
|
| 24 | private final AdvancementsScreen screen;
|
| 25 | private final AdvancementTabType type;
|
| 26 | private final int index;
|
| 27 | private final Identifier background;
|
| 28 | private final ItemStack icon;
|
| 29 | private final Component title;
|
| 30 | private final AdvancementWidget root;
|
| 31 | private final Map<AdvancementHolder, AdvancementWidget> widgets = Maps.newLinkedHashMap();
|
| 32 | private double scrollX;
|
| 33 | private double scrollY;
|
| 34 | private int minX = Integer.MAX_VALUE;
|
| 35 | private int minY = Integer.MAX_VALUE;
|
| 36 | private int maxX = Integer.MIN_VALUE;
|
| 37 | private int maxY = Integer.MIN_VALUE;
|
| 38 | private float fade;
|
| 39 | private boolean centered;
|
| 40 | private @Nullable AdvancementWidget hovered;
|
| 41 |
|
| 42 | public AdvancementTab(
|
| 43 | final Minecraft minecraft,
|
| 44 | final AdvancementsScreen screen,
|
| 45 | final AdvancementTabType type,
|
| 46 | final int index,
|
| 47 | final AdvancementWidget root,
|
| 48 | final ItemStackTemplate icon,
|
| 49 | final Component title,
|
| 50 | final Identifier background
|
| 51 | ) {
|
| 52 | this.minecraft = minecraft;
|
| 53 | this.screen = screen;
|
| 54 | this.type = type;
|
| 55 | this.index = index;
|
| 56 | this.background = background;
|
| 57 | this.icon = icon.create();
|
| 58 | this.title = title;
|
| 59 | this.root = root;
|
| 60 | this.addWidget(root);
|
| 61 | }
|
| 62 |
|
| 63 | public void copyPosition(final AdvancementTab source) {
|
| 64 | this.scrollX = source.scrollX;
|
| 65 | this.scrollY = source.scrollY;
|
| 66 | this.minX = source.minX;
|
| 67 | this.minY = source.minY;
|
| 68 | this.maxX = source.maxX;
|
| 69 | this.maxY = source.maxY;
|
| 70 | this.centered = source.centered;
|
| 71 | }
|
| 72 |
|
| 73 | public AdvancementTabType getType() {
|
| 74 | return this.type;
|
| 75 | }
|
| 76 |
|
| 77 | public int getIndex() {
|
| 78 | return this.index;
|
| 79 | }
|
| 80 |
|
| 81 | public AdvancementHolder getRootAdvancement() {
|
| 82 | return this.root.getAdvancement();
|
| 83 | }
|
| 84 |
|
| 85 | public Component getTitle() {
|
| 86 | return this.title;
|
| 87 | }
|
| 88 |
|
| 89 | public void tick(final int relativeMouseX, final int relativeMouseY) {
|
| 90 | boolean hovering = false;
|
| 91 | if (relativeMouseX > 0 && relativeMouseX < 234 && relativeMouseY > 0 && relativeMouseY < 113) {
|
| 92 | int intScrollX = Mth.floor(this.scrollX);
|
| 93 | int intScrollY = Mth.floor(this.scrollY);
|
| 94 |
|
| 95 | for (AdvancementWidget widget : this.widgets.values()) {
|
| 96 | if (widget.isMouseOver(intScrollX, intScrollY, relativeMouseX, relativeMouseY)) {
|
| 97 | hovering = true;
|
| 98 | this.hovered = widget;
|
| 99 | break;
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | if (hovering) {
|
| 105 | this.fade = Mth.clamp(this.fade + 0.06F, 0.0F, 0.3F);
|
| 106 | } else {
|
| 107 | this.fade = Mth.clamp(this.fade - 0.12F, 0.0F, 1.0F);
|
| 108 | if (this.hovered != null) {
|
| 109 | this.hovered = null;
|
| 110 | }
|
| 111 | }
|
| 112 | }
|
| 113 |
|
| 114 | public void extractTab(final GuiGraphicsExtractor graphics, final int xo, final int yo, final int mouseX, final int mouseY, final boolean selected) {
|
| 115 | int tabX = xo + this.type.getX(this.index);
|
| 116 | int tabY = yo + this.type.getY(this.index);
|
| 117 | this.type.extractRenderState(graphics, tabX, tabY, selected, this.index);
|
| 118 | if (!selected && mouseX > tabX && mouseY > tabY && mouseX < tabX + this.type.getWidth() && mouseY < tabY + this.type.getHeight()) {
|
| 119 | graphics.requestCursor(CursorTypes.POINTING_HAND);
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public void extractIcon(final GuiGraphicsExtractor graphics, final int xo, final int yo) {
|
| 124 | this.type.extractIcon(graphics, xo, yo, this.index, this.icon);
|
| 125 | }
|
| 126 |
|
| 127 | public void extractContents(final GuiGraphicsExtractor graphics, final int windowLeft, final int windowTop) {
|
| 128 | if (!this.centered) {
|
| 129 | this.scrollX = 117 - (this.maxX + this.minX) / 2;
|
| 130 | this.scrollY = 56 - (this.maxY + this.minY) / 2;
|
| 131 | this.centered = true;
|
| 132 | }
|
| 133 |
|
| 134 | graphics.enableScissor(windowLeft, windowTop, windowLeft + 234, windowTop + 113);
|
| 135 | graphics.pose().pushMatrix();
|
| 136 | graphics.pose().translate(windowLeft, windowTop);
|
| 137 | int intScrollX = Mth.floor(this.scrollX);
|
| 138 | int intScrollY = Mth.floor(this.scrollY);
|
| 139 | int left = intScrollX % 16;
|
| 140 | int top = intScrollY % 16;
|
| 141 |
|
| 142 | for (int x = -1; x <= 15; x++) {
|
| 143 | for (int y = -1; y <= 8; y++) {
|
| 144 | graphics.blit(RenderPipelines.GUI_TEXTURED, this.background, left + 16 * x, top + 16 * y, 0.0F, 0.0F, 16, 16, 16, 16);
|
| 145 | }
|
| 146 | }
|
| 147 |
|
| 148 | this.root.extractConnectivity(graphics, intScrollX, intScrollY, true);
|
| 149 | this.root.extractConnectivity(graphics, intScrollX, intScrollY, false);
|
| 150 | this.root.extractRenderState(graphics, intScrollX, intScrollY);
|
| 151 | graphics.pose().popMatrix();
|
| 152 | graphics.disableScissor();
|
| 153 | }
|
| 154 |
|
| 155 | public void extractTooltips(final GuiGraphicsExtractor graphics, final int xo, final int yo) {
|
| 156 | graphics.fill(0, 0, 234, 113, Mth.floor(this.fade * 255.0F) << 24);
|
| 157 | if (this.hovered != null) {
|
| 158 | int intScrollX = Mth.floor(this.scrollX);
|
| 159 | int intScrollY = Mth.floor(this.scrollY);
|
| 160 | this.hovered.extractHover(graphics, intScrollX, intScrollY, this.fade, xo, yo, this.screen.width);
|
| 161 | }
|
| 162 | }
|
| 163 |
|
| 164 | public boolean isMouseOver(final int xo, final int yo, final double mx, final double my) {
|
| 165 | return this.type.isMouseOver(xo, yo, this.index, mx, my);
|
| 166 | }
|
| 167 |
|
| 168 | public static @Nullable AdvancementTab create(final Minecraft minecraft, final AdvancementsScreen screen, int index, final AdvancementNode root) {
|
| 169 | AdvancementWidget rootWidget = AdvancementWidget.createWidget(minecraft, root);
|
| 170 | if (rootWidget == null) {
|
| 171 | return null;
|
| 172 | }
|
| 173 |
|
| 174 | DisplayInfo rootDisplay = rootWidget.getDisplay();
|
| 175 | if (rootDisplay.background().isEmpty()) {
|
| 176 | return null;
|
| 177 | }
|
| 178 |
|
| 179 | Identifier background = rootDisplay.background().get().texturePath();
|
| 180 |
|
| 181 | for (AdvancementTabType type : AdvancementTabType.values()) {
|
| 182 | if (index < type.getMax()) {
|
| 183 | return new AdvancementTab(minecraft, screen, type, index, rootWidget, rootDisplay.icon(), rootDisplay.title(), background);
|
| 184 | }
|
| 185 |
|
| 186 | index -= type.getMax();
|
| 187 | }
|
| 188 |
|
| 189 | return null;
|
| 190 | }
|
| 191 |
|
| 192 | public void scroll(final double x, final double y) {
|
| 193 | if (this.canScrollHorizontally()) {
|
| 194 | this.scrollX = Mth.clamp(this.scrollX + x, -(this.maxX - 234), 0.0);
|
| 195 | }
|
| 196 |
|
| 197 | if (this.canScrollVertically()) {
|
| 198 | this.scrollY = Mth.clamp(this.scrollY + y, -(this.maxY - 113), 0.0);
|
| 199 | }
|
| 200 | }
|
| 201 |
|
| 202 | public boolean canScrollHorizontally() {
|
| 203 | return this.maxX - this.minX > 234;
|
| 204 | }
|
| 205 |
|
| 206 | public boolean canScrollVertically() {
|
| 207 | return this.maxY - this.minY > 113;
|
| 208 | }
|
| 209 |
|
| 210 | public void addAdvancement(final AdvancementNode node) {
|
| 211 | AdvancementWidget widget = AdvancementWidget.createWidget(this.minecraft, node);
|
| 212 | if (widget != null) {
|
| 213 | this.addWidget(widget);
|
| 214 | }
|
| 215 | }
|
| 216 |
|
| 217 | private void addWidget(final AdvancementWidget widget) {
|
| 218 | this.widgets.put(widget.getAdvancement(), widget);
|
| 219 | int x0 = widget.getX();
|
| 220 | int x1 = x0 + 28;
|
| 221 | int y0 = widget.getY();
|
| 222 | int y1 = y0 + 27;
|
| 223 | this.minX = Math.min(this.minX, x0);
|
| 224 | this.maxX = Math.max(this.maxX, x1);
|
| 225 | this.minY = Math.min(this.minY, y0);
|
| 226 | this.maxY = Math.max(this.maxY, y1);
|
| 227 |
|
| 228 | for (AdvancementWidget other : this.widgets.values()) {
|
| 229 | other.attachToParent(this);
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | public @Nullable AdvancementWidget getWidget(final AdvancementHolder advancement) {
|
| 234 | return this.widgets.get(advancement);
|
| 235 | }
|
| 236 |
|
| 237 | public AdvancementsScreen getScreen() {
|
| 238 | return this.screen;
|
| 239 | }
|
| 240 | }
|
| 241 |
|