| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.Lighting;
|
| 4 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 5 | import java.util.List;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.input.MouseButtonEvent;
|
| 11 | import net.minecraft.client.model.geom.ModelLayers;
|
| 12 | import net.minecraft.client.model.geom.ModelPart;
|
| 13 | import net.minecraft.client.model.object.banner.BannerFlagModel;
|
| 14 | import net.minecraft.client.renderer.RenderPipelines;
|
| 15 | import net.minecraft.client.renderer.Sheets;
|
| 16 | import net.minecraft.client.renderer.texture.TextureAtlasSprite;
|
| 17 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 18 | import net.minecraft.core.Holder;
|
| 19 | import net.minecraft.core.component.DataComponents;
|
| 20 | import net.minecraft.network.chat.Component;
|
| 21 | import net.minecraft.resources.Identifier;
|
| 22 | import net.minecraft.sounds.SoundEvents;
|
| 23 | import net.minecraft.util.Mth;
|
| 24 | import net.minecraft.world.entity.player.Inventory;
|
| 25 | import net.minecraft.world.inventory.LoomMenu;
|
| 26 | import net.minecraft.world.inventory.Slot;
|
| 27 | import net.minecraft.world.item.BannerItem;
|
| 28 | import net.minecraft.world.item.DyeColor;
|
| 29 | import net.minecraft.world.item.ItemStack;
|
| 30 | import net.minecraft.world.level.block.entity.BannerPattern;
|
| 31 | import net.minecraft.world.level.block.entity.BannerPatternLayers;
|
| 32 | import org.jspecify.annotations.Nullable;
|
| 33 |
|
| 34 | @Environment(EnvType.CLIENT)
|
| 35 | public class LoomScreen extends AbstractContainerScreen<LoomMenu> {
|
| 36 | private static final Identifier BANNER_SLOT_SPRITE = Identifier.withDefaultNamespace("container/slot/banner");
|
| 37 | private static final Identifier DYE_SLOT_SPRITE = Identifier.withDefaultNamespace("container/slot/dye");
|
| 38 | private static final Identifier PATTERN_SLOT_SPRITE = Identifier.withDefaultNamespace("container/slot/banner_pattern");
|
| 39 | private static final Identifier SCROLLER_SPRITE = Identifier.withDefaultNamespace("container/loom/scroller");
|
| 40 | private static final Identifier SCROLLER_DISABLED_SPRITE = Identifier.withDefaultNamespace("container/loom/scroller_disabled");
|
| 41 | private static final Identifier PATTERN_SELECTED_SPRITE = Identifier.withDefaultNamespace("container/loom/pattern_selected");
|
| 42 | private static final Identifier PATTERN_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("container/loom/pattern_highlighted");
|
| 43 | private static final Identifier PATTERN_SPRITE = Identifier.withDefaultNamespace("container/loom/pattern");
|
| 44 | private static final Identifier ERROR_SPRITE = Identifier.withDefaultNamespace("container/loom/error");
|
| 45 | private static final Identifier BG_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/loom.png");
|
| 46 | private static final int PATTERN_COLUMNS = 4;
|
| 47 | private static final int PATTERN_ROWS = 4;
|
| 48 | private static final int SCROLLER_WIDTH = 12;
|
| 49 | private static final int SCROLLER_HEIGHT = 15;
|
| 50 | private static final int PATTERN_IMAGE_SIZE = 14;
|
| 51 | private static final int SCROLLER_FULL_HEIGHT = 56;
|
| 52 | private static final int PATTERNS_X = 60;
|
| 53 | private static final int PATTERNS_Y = 13;
|
| 54 | private static final float BANNER_PATTERN_TEXTURE_SIZE = 64.0F;
|
| 55 | private static final float BANNER_PATTERN_WIDTH = 21.0F;
|
| 56 | private static final float BANNER_PATTERN_HEIGHT = 40.0F;
|
| 57 | private BannerFlagModel flag;
|
| 58 | private @Nullable BannerPatternLayers resultBannerPatterns;
|
| 59 | private ItemStack bannerStack = ItemStack.EMPTY;
|
| 60 | private ItemStack dyeStack = ItemStack.EMPTY;
|
| 61 | private ItemStack patternStack = ItemStack.EMPTY;
|
| 62 | private boolean displayPatterns;
|
| 63 | private boolean hasMaxPatterns;
|
| 64 | private float scrollOffs;
|
| 65 | private boolean scrolling;
|
| 66 | private int startRow;
|
| 67 |
|
| 68 | public LoomScreen(final LoomMenu menu, final Inventory inventory, final Component title) {
|
| 69 | super(menu, inventory, title);
|
| 70 | menu.registerUpdateListener(this::containerChanged);
|
| 71 | this.titleLabelY -= 2;
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | protected void init() {
|
| 76 | super.init();
|
| 77 | ModelPart modelPart = this.minecraft.getEntityModels().bakeLayer(ModelLayers.STANDING_BANNER_FLAG);
|
| 78 | this.flag = new BannerFlagModel(modelPart);
|
| 79 | }
|
| 80 |
|
| 81 | private int totalRowCount() {
|
| 82 | return Mth.positiveCeilDiv(this.menu.getSelectablePatterns().size(), 4);
|
| 83 | }
|
| 84 |
|
| 85 | @Override
|
| 86 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 87 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 88 | int xo = this.leftPos;
|
| 89 | int yo = this.topPos;
|
| 90 | graphics.blit(RenderPipelines.GUI_TEXTURED, BG_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
|
| 91 | Slot bannerSlot = this.menu.getBannerSlot();
|
| 92 | Slot dyeSlot = this.menu.getDyeSlot();
|
| 93 | Slot patternSlot = this.menu.getPatternSlot();
|
| 94 | Slot resultSlot = this.menu.getResultSlot();
|
| 95 | if (!bannerSlot.hasItem()) {
|
| 96 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, BANNER_SLOT_SPRITE, xo + bannerSlot.x, yo + bannerSlot.y, 16, 16);
|
| 97 | }
|
| 98 |
|
| 99 | if (!dyeSlot.hasItem()) {
|
| 100 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, DYE_SLOT_SPRITE, xo + dyeSlot.x, yo + dyeSlot.y, 16, 16);
|
| 101 | }
|
| 102 |
|
| 103 | if (!patternSlot.hasItem()) {
|
| 104 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, PATTERN_SLOT_SPRITE, xo + patternSlot.x, yo + patternSlot.y, 16, 16);
|
| 105 | }
|
| 106 |
|
| 107 | int sy = (int)(41.0F * this.scrollOffs);
|
| 108 | Identifier sprite = this.isScrollBarActive() ? SCROLLER_SPRITE : SCROLLER_DISABLED_SPRITE;
|
| 109 | int scrollerX = xo + 119;
|
| 110 | int scrollerY = yo + 13;
|
| 111 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, scrollerX, scrollerY + sy, 12, 15);
|
| 112 | if (mouseX >= scrollerX && mouseX < scrollerX + 12 && mouseY >= scrollerY && mouseY < scrollerY + 56) {
|
| 113 | if (this.isScrollBarActive()) {
|
| 114 | graphics.requestCursor(this.scrolling ? CursorTypes.RESIZE_NS : CursorTypes.POINTING_HAND);
|
| 115 | } else {
|
| 116 | graphics.requestCursor(CursorTypes.NOT_ALLOWED);
|
| 117 | }
|
| 118 | }
|
| 119 |
|
| 120 | if (this.resultBannerPatterns != null && !this.hasMaxPatterns) {
|
| 121 | DyeColor baseColor = ((BannerItem)resultSlot.getItem().getItem()).getColor();
|
| 122 | int x0 = xo + 141;
|
| 123 | int y0 = yo + 8;
|
| 124 | graphics.bannerPattern(this.flag, baseColor, this.resultBannerPatterns, x0, y0, x0 + 20, y0 + 40);
|
| 125 | } else if (this.hasMaxPatterns) {
|
| 126 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, ERROR_SPRITE, xo + resultSlot.x - 5, yo + resultSlot.y - 5, 26, 26);
|
| 127 | }
|
| 128 |
|
| 129 | if (this.displayPatterns) {
|
| 130 | int x = xo + 60;
|
| 131 | int y = yo + 13;
|
| 132 | List<Holder<BannerPattern>> selectablePatterns = this.menu.getSelectablePatterns();
|
| 133 |
|
| 134 | label82:
|
| 135 | for (int row = 0; row < 4; row++) {
|
| 136 | for (int column = 0; column < 4; column++) {
|
| 137 | int actualRow = row + this.startRow;
|
| 138 | int index = actualRow * 4 + column;
|
| 139 | if (index >= selectablePatterns.size()) {
|
| 140 | break label82;
|
| 141 | }
|
| 142 |
|
| 143 | int posX = x + column * 14;
|
| 144 | int posY = y + row * 14;
|
| 145 | Holder<BannerPattern> pattern = selectablePatterns.get(index);
|
| 146 | boolean isHighlighted = mouseX >= posX && mouseY >= posY && mouseX < posX + 14 && mouseY < posY + 14;
|
| 147 | Identifier buttonSprite;
|
| 148 | if (index == this.menu.getSelectedBannerPatternIndex()) {
|
| 149 | buttonSprite = PATTERN_SELECTED_SPRITE;
|
| 150 | } else if (isHighlighted) {
|
| 151 | buttonSprite = PATTERN_HIGHLIGHTED_SPRITE;
|
| 152 | DyeColor patternColor = this.dyeStack.getOrDefault(DataComponents.DYE, DyeColor.WHITE);
|
| 153 | graphics.setTooltipForNextFrame(Component.translatable(pattern.value().translationKey() + "." + patternColor.getName()), mouseX, mouseY);
|
| 154 | graphics.requestCursor(CursorTypes.POINTING_HAND);
|
| 155 | } else {
|
| 156 | buttonSprite = PATTERN_SPRITE;
|
| 157 | }
|
| 158 |
|
| 159 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, buttonSprite, posX, posY, 14, 14);
|
| 160 | TextureAtlasSprite bannerPatternSprite = graphics.getSprite(Sheets.getBannerSprite(pattern));
|
| 161 | this.extractBannerOnButton(graphics, posX, posY, bannerPatternSprite);
|
| 162 | }
|
| 163 | }
|
| 164 | }
|
| 165 |
|
| 166 | Minecraft.getInstance().gameRenderer.lighting().setupFor(Lighting.Entry.ITEMS_3D);
|
| 167 | }
|
| 168 |
|
| 169 | private boolean isScrollBarActive() {
|
| 170 | return this.displayPatterns && this.menu.getSelectablePatterns().size() > 16;
|
| 171 | }
|
| 172 |
|
| 173 | private void extractBannerOnButton(final GuiGraphicsExtractor graphics, final int posX, final int posY, final TextureAtlasSprite bannerPatternSprite) {
|
| 174 | graphics.pose().pushMatrix();
|
| 175 | graphics.pose().translate(posX + 4, posY + 2);
|
| 176 | float patternU0 = bannerPatternSprite.getU0();
|
| 177 | float patternU1 = patternU0 + (bannerPatternSprite.getU1() - bannerPatternSprite.getU0()) * 21.0F / 64.0F;
|
| 178 | float patternVSpan = bannerPatternSprite.getV1() - bannerPatternSprite.getV0();
|
| 179 | float patternV0 = bannerPatternSprite.getV0() + patternVSpan / 64.0F;
|
| 180 | float patternV1 = patternV0 + patternVSpan * 40.0F / 64.0F;
|
| 181 | int bannerWidth = 5;
|
| 182 | int bannerHeight = 10;
|
| 183 | graphics.fill(0, 0, 5, 10, DyeColor.GRAY.getTextureDiffuseColor());
|
| 184 | graphics.blit(bannerPatternSprite.atlasLocation(), 0, 0, 5, 10, patternU0, patternU1, patternV0, patternV1);
|
| 185 | graphics.pose().popMatrix();
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 190 | if (this.displayPatterns) {
|
| 191 | int xo = this.leftPos + 60;
|
| 192 | int yo = this.topPos + 13;
|
| 193 |
|
| 194 | for (int row = 0; row < 4; row++) {
|
| 195 | for (int column = 0; column < 4; column++) {
|
| 196 | double xx = event.x() - (xo + column * 14);
|
| 197 | double yy = event.y() - (yo + row * 14);
|
| 198 | int actualRow = row + this.startRow;
|
| 199 | int index = actualRow * 4 + column;
|
| 200 | if (xx >= 0.0 && yy >= 0.0 && xx < 14.0 && yy < 14.0 && this.menu.clickMenuButton(this.minecraft.player, index)) {
|
| 201 | Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_LOOM_SELECT_PATTERN, 1.0F));
|
| 202 | this.minecraft.gameMode.handleInventoryButtonClick(this.menu.containerId, index);
|
| 203 | return true;
|
| 204 | }
|
| 205 | }
|
| 206 | }
|
| 207 |
|
| 208 | xo = this.leftPos + 119;
|
| 209 | yo = this.topPos + 9;
|
| 210 | if (event.x() >= xo && event.x() < xo + 12 && event.y() >= yo && event.y() < yo + 56) {
|
| 211 | this.scrolling = true;
|
| 212 | }
|
| 213 | }
|
| 214 |
|
| 215 | return super.mouseClicked(event, doubleClick);
|
| 216 | }
|
| 217 |
|
| 218 | @Override
|
| 219 | public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
| 220 | int offscreenRows = this.totalRowCount() - 4;
|
| 221 | if (this.scrolling && this.displayPatterns && offscreenRows > 0) {
|
| 222 | int yscr = this.topPos + 13;
|
| 223 | int yscr2 = yscr + 56;
|
| 224 | this.scrollOffs = ((float)event.y() - yscr - 7.5F) / (yscr2 - yscr - 15.0F);
|
| 225 | this.scrollOffs = Mth.clamp(this.scrollOffs, 0.0F, 1.0F);
|
| 226 | this.startRow = Math.max((int)(this.scrollOffs * offscreenRows + 0.5), 0);
|
| 227 | return true;
|
| 228 | } else {
|
| 229 | return super.mouseDragged(event, dx, dy);
|
| 230 | }
|
| 231 | }
|
| 232 |
|
| 233 | @Override
|
| 234 | public boolean mouseReleased(final MouseButtonEvent event) {
|
| 235 | this.scrolling = false;
|
| 236 | return super.mouseReleased(event);
|
| 237 | }
|
| 238 |
|
| 239 | @Override
|
| 240 | public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
| 241 | if (super.mouseScrolled(x, y, scrollX, scrollY)) {
|
| 242 | return true;
|
| 243 | }
|
| 244 |
|
| 245 | int offscreenRows = this.totalRowCount() - 4;
|
| 246 | if (this.displayPatterns && offscreenRows > 0) {
|
| 247 | float scrolledDelta = (float)scrollY / offscreenRows;
|
| 248 | this.scrollOffs = Mth.clamp(this.scrollOffs - scrolledDelta, 0.0F, 1.0F);
|
| 249 | this.startRow = Math.max((int)(this.scrollOffs * offscreenRows + 0.5F), 0);
|
| 250 | }
|
| 251 |
|
| 252 | return true;
|
| 253 | }
|
| 254 |
|
| 255 | @Override
|
| 256 | protected boolean hasClickedOutside(final double mx, final double my, final int xo, final int yo) {
|
| 257 | return mx < xo || my < yo || mx >= xo + this.imageWidth || my >= yo + this.imageHeight;
|
| 258 | }
|
| 259 |
|
| 260 | private void containerChanged() {
|
| 261 | ItemStack resultStack = this.menu.getResultSlot().getItem();
|
| 262 | if (resultStack.isEmpty()) {
|
| 263 | this.resultBannerPatterns = null;
|
| 264 | } else {
|
| 265 | this.resultBannerPatterns = resultStack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
|
| 266 | }
|
| 267 |
|
| 268 | ItemStack bannerStack = this.menu.getBannerSlot().getItem();
|
| 269 | ItemStack dyeStack = this.menu.getDyeSlot().getItem();
|
| 270 | ItemStack patternStack = this.menu.getPatternSlot().getItem();
|
| 271 | BannerPatternLayers patterns = bannerStack.getOrDefault(DataComponents.BANNER_PATTERNS, BannerPatternLayers.EMPTY);
|
| 272 | this.hasMaxPatterns = patterns.layers().size() >= 6;
|
| 273 | if (this.hasMaxPatterns) {
|
| 274 | this.resultBannerPatterns = null;
|
| 275 | }
|
| 276 |
|
| 277 | if (!ItemStack.matches(bannerStack, this.bannerStack) || !ItemStack.matches(dyeStack, this.dyeStack) || !ItemStack.matches(patternStack, this.patternStack)) {
|
| 278 | this.displayPatterns = !bannerStack.isEmpty() && !dyeStack.isEmpty() && !this.hasMaxPatterns && !this.menu.getSelectablePatterns().isEmpty();
|
| 279 | }
|
| 280 |
|
| 281 | if (this.startRow >= this.totalRowCount()) {
|
| 282 | this.startRow = 0;
|
| 283 | this.scrollOffs = 0.0F;
|
| 284 | }
|
| 285 |
|
| 286 | this.bannerStack = bannerStack.copy();
|
| 287 | this.dyeStack = dyeStack.copy();
|
| 288 | this.patternStack = patternStack.copy();
|
| 289 | }
|
| 290 | }
|
| 291 |
|