| 1 | package net.minecraft.client.gui.screens.inventory;
|
| 2 |
|
| 3 | import com.mojang.blaze3d.platform.cursor.CursorTypes;
|
| 4 | import net.fabricmc.api.EnvType;
|
| 5 | import net.fabricmc.api.Environment;
|
| 6 | import net.minecraft.client.Minecraft;
|
| 7 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 8 | import net.minecraft.client.input.MouseButtonEvent;
|
| 9 | import net.minecraft.client.renderer.RenderPipelines;
|
| 10 | import net.minecraft.client.resources.sounds.SimpleSoundInstance;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.resources.Identifier;
|
| 13 | import net.minecraft.sounds.SoundEvents;
|
| 14 | import net.minecraft.util.Mth;
|
| 15 | import net.minecraft.util.context.ContextMap;
|
| 16 | import net.minecraft.world.entity.player.Inventory;
|
| 17 | import net.minecraft.world.inventory.StonecutterMenu;
|
| 18 | import net.minecraft.world.item.crafting.StonecutterRecipe;
|
| 19 | import net.minecraft.world.item.crafting.SelectableRecipe.SingleInputSet;
|
| 20 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 21 | import net.minecraft.world.item.crafting.display.SlotDisplayContext;
|
| 22 |
|
| 23 | @Environment(EnvType.CLIENT)
|
| 24 | public class StonecutterScreen extends AbstractContainerScreen<StonecutterMenu> {
|
| 25 | private static final Identifier SCROLLER_SPRITE = Identifier.withDefaultNamespace("container/stonecutter/scroller");
|
| 26 | private static final Identifier SCROLLER_DISABLED_SPRITE = Identifier.withDefaultNamespace("container/stonecutter/scroller_disabled");
|
| 27 | private static final Identifier RECIPE_SELECTED_SPRITE = Identifier.withDefaultNamespace("container/stonecutter/recipe_selected");
|
| 28 | private static final Identifier RECIPE_HIGHLIGHTED_SPRITE = Identifier.withDefaultNamespace("container/stonecutter/recipe_highlighted");
|
| 29 | private static final Identifier RECIPE_SPRITE = Identifier.withDefaultNamespace("container/stonecutter/recipe");
|
| 30 | private static final Identifier BG_LOCATION = Identifier.withDefaultNamespace("textures/gui/container/stonecutter.png");
|
| 31 | private static final int SCROLLER_WIDTH = 12;
|
| 32 | private static final int SCROLLER_HEIGHT = 15;
|
| 33 | private static final int RECIPES_COLUMNS = 4;
|
| 34 | private static final int RECIPES_ROWS = 3;
|
| 35 | private static final int RECIPES_IMAGE_SIZE_WIDTH = 16;
|
| 36 | private static final int RECIPES_IMAGE_SIZE_HEIGHT = 18;
|
| 37 | private static final int SCROLLER_FULL_HEIGHT = 54;
|
| 38 | private static final int RECIPES_X = 52;
|
| 39 | private static final int RECIPES_Y = 14;
|
| 40 | private float scrollOffs;
|
| 41 | private boolean scrolling;
|
| 42 | private int startIndex;
|
| 43 | private boolean displayRecipes;
|
| 44 |
|
| 45 | public StonecutterScreen(final StonecutterMenu menu, final Inventory inventory, final Component title) {
|
| 46 | super(menu, inventory, title);
|
| 47 | menu.registerUpdateListener(this::containerChanged);
|
| 48 | this.titleLabelY--;
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public void extractBackground(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY, final float a) {
|
| 53 | super.extractBackground(graphics, mouseX, mouseY, a);
|
| 54 | int xo = this.leftPos;
|
| 55 | int yo = this.topPos;
|
| 56 | graphics.blit(RenderPipelines.GUI_TEXTURED, BG_LOCATION, xo, yo, 0.0F, 0.0F, this.imageWidth, this.imageHeight, 256, 256);
|
| 57 | int sy = (int)(41.0F * this.scrollOffs);
|
| 58 | Identifier sprite = this.isScrollBarActive() ? SCROLLER_SPRITE : SCROLLER_DISABLED_SPRITE;
|
| 59 | int scrollerXStart = xo + 119;
|
| 60 | int scrollerYStart = yo + 15;
|
| 61 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, scrollerXStart, scrollerYStart + sy, 12, 15);
|
| 62 | if (mouseX >= scrollerXStart && mouseY >= scrollerYStart && mouseX < scrollerXStart + 12 && mouseY < scrollerYStart + 54) {
|
| 63 | if (this.isScrollBarActive()) {
|
| 64 | graphics.requestCursor(this.scrolling ? CursorTypes.RESIZE_NS : CursorTypes.POINTING_HAND);
|
| 65 | } else {
|
| 66 | graphics.requestCursor(CursorTypes.NOT_ALLOWED);
|
| 67 | }
|
| 68 | }
|
| 69 |
|
| 70 | int x = this.leftPos + 52;
|
| 71 | int y = this.topPos + 14;
|
| 72 | int endIndex = this.startIndex + 12;
|
| 73 | this.extractButtons(graphics, mouseX, mouseY, x, y, endIndex);
|
| 74 | this.extractRecipes(graphics, x, y, endIndex);
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | protected void extractTooltip(final GuiGraphicsExtractor graphics, final int mouseX, final int mouseY) {
|
| 79 | super.extractTooltip(graphics, mouseX, mouseY);
|
| 80 | if (this.displayRecipes) {
|
| 81 | int edgeLeft = this.leftPos + 52;
|
| 82 | int edgeTop = this.topPos + 14;
|
| 83 | int endIndex = this.startIndex + 12;
|
| 84 | SingleInputSet<StonecutterRecipe> visibleRecipes = this.menu.getVisibleRecipes();
|
| 85 |
|
| 86 | for (int index = this.startIndex; index < endIndex && index < visibleRecipes.size(); index++) {
|
| 87 | int posIndex = index - this.startIndex;
|
| 88 | int itemLeft = edgeLeft + posIndex % 4 * 16;
|
| 89 | int itemRight = edgeTop + posIndex / 4 * 18 + 2;
|
| 90 | if (mouseX >= itemLeft && mouseX < itemLeft + 16 && mouseY >= itemRight && mouseY < itemRight + 18) {
|
| 91 | ContextMap context = SlotDisplayContext.fromLevel(this.minecraft.level);
|
| 92 | SlotDisplay buttonIcon = visibleRecipes.entries().get(index).recipe().optionDisplay();
|
| 93 | graphics.setTooltipForNextFrame(this.font, buttonIcon.resolveForFirstStack(context), mouseX, mouseY);
|
| 94 | }
|
| 95 | }
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | private void extractButtons(final GuiGraphicsExtractor graphics, final int xm, final int ym, final int x, final int y, final int endIndex) {
|
| 100 | for (int index = this.startIndex; index < endIndex && index < this.menu.getNumberOfVisibleRecipes(); index++) {
|
| 101 | int posIndex = index - this.startIndex;
|
| 102 | int posX = x + posIndex % 4 * 16;
|
| 103 | int row = posIndex / 4;
|
| 104 | int posY = y + row * 18 + 2;
|
| 105 | Identifier sprite;
|
| 106 | if (index == this.menu.getSelectedRecipeIndex()) {
|
| 107 | sprite = RECIPE_SELECTED_SPRITE;
|
| 108 | } else if (xm >= posX && ym >= posY && xm < posX + 16 && ym < posY + 18) {
|
| 109 | sprite = RECIPE_HIGHLIGHTED_SPRITE;
|
| 110 | } else {
|
| 111 | sprite = RECIPE_SPRITE;
|
| 112 | }
|
| 113 |
|
| 114 | int textureY = posY - 1;
|
| 115 | graphics.blitSprite(RenderPipelines.GUI_TEXTURED, sprite, posX, textureY, 16, 18);
|
| 116 | if (xm >= posX && ym >= textureY && xm < posX + 16 && ym < textureY + 18) {
|
| 117 | graphics.requestCursor(CursorTypes.POINTING_HAND);
|
| 118 | }
|
| 119 | }
|
| 120 | }
|
| 121 |
|
| 122 | private void extractRecipes(final GuiGraphicsExtractor graphics, final int x, final int y, final int endIndex) {
|
| 123 | SingleInputSet<StonecutterRecipe> visibleRecipes = this.menu.getVisibleRecipes();
|
| 124 | ContextMap context = SlotDisplayContext.fromLevel(this.minecraft.level);
|
| 125 |
|
| 126 | for (int index = this.startIndex; index < endIndex && index < visibleRecipes.size(); index++) {
|
| 127 | int posIndex = index - this.startIndex;
|
| 128 | int posX = x + posIndex % 4 * 16;
|
| 129 | int row = posIndex / 4;
|
| 130 | int posY = y + row * 18 + 2;
|
| 131 | SlotDisplay buttonIcon = visibleRecipes.entries().get(index).recipe().optionDisplay();
|
| 132 | graphics.item(buttonIcon.resolveForFirstStack(context), posX, posY);
|
| 133 | }
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public boolean mouseClicked(final MouseButtonEvent event, final boolean doubleClick) {
|
| 138 | if (this.displayRecipes) {
|
| 139 | int xo = this.leftPos + 52;
|
| 140 | int yo = this.topPos + 14;
|
| 141 | int endIndex = this.startIndex + 12;
|
| 142 |
|
| 143 | for (int index = this.startIndex; index < endIndex; index++) {
|
| 144 | int posIndex = index - this.startIndex;
|
| 145 | double xx = event.x() - (xo + posIndex % 4 * 16);
|
| 146 | double yy = event.y() - (yo + posIndex / 4 * 18);
|
| 147 | if (xx >= 0.0 && yy >= 0.0 && xx < 16.0 && yy < 18.0 && this.menu.clickMenuButton(this.minecraft.player, index)) {
|
| 148 | Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_STONECUTTER_SELECT_RECIPE, 1.0F));
|
| 149 | this.minecraft.gameMode.handleInventoryButtonClick(this.menu.containerId, index);
|
| 150 | return true;
|
| 151 | }
|
| 152 | }
|
| 153 |
|
| 154 | xo = this.leftPos + 119;
|
| 155 | yo = this.topPos + 9;
|
| 156 | if (event.x() >= xo && event.x() < xo + 12 && event.y() >= yo && event.y() < yo + 54) {
|
| 157 | this.scrolling = true;
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | return super.mouseClicked(event, doubleClick);
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public boolean mouseDragged(final MouseButtonEvent event, final double dx, final double dy) {
|
| 166 | if (this.scrolling && this.isScrollBarActive()) {
|
| 167 | int yscr = this.topPos + 14;
|
| 168 | int yscr2 = yscr + 54;
|
| 169 | this.scrollOffs = ((float)event.y() - yscr - 7.5F) / (yscr2 - yscr - 15.0F);
|
| 170 | this.scrollOffs = Mth.clamp(this.scrollOffs, 0.0F, 1.0F);
|
| 171 | this.startIndex = (int)(this.scrollOffs * this.getOffscreenRows() + 0.5) * 4;
|
| 172 | return true;
|
| 173 | } else {
|
| 174 | return super.mouseDragged(event, dx, dy);
|
| 175 | }
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | public boolean mouseReleased(final MouseButtonEvent event) {
|
| 180 | this.scrolling = false;
|
| 181 | return super.mouseReleased(event);
|
| 182 | }
|
| 183 |
|
| 184 | @Override
|
| 185 | public boolean mouseScrolled(final double x, final double y, final double scrollX, final double scrollY) {
|
| 186 | if (super.mouseScrolled(x, y, scrollX, scrollY)) {
|
| 187 | return true;
|
| 188 | }
|
| 189 |
|
| 190 | if (this.isScrollBarActive()) {
|
| 191 | int offscreenRows = this.getOffscreenRows();
|
| 192 | float scrolledDelta = (float)scrollY / offscreenRows;
|
| 193 | this.scrollOffs = Mth.clamp(this.scrollOffs - scrolledDelta, 0.0F, 1.0F);
|
| 194 | this.startIndex = (int)(this.scrollOffs * offscreenRows + 0.5) * 4;
|
| 195 | }
|
| 196 |
|
| 197 | return true;
|
| 198 | }
|
| 199 |
|
| 200 | private boolean isScrollBarActive() {
|
| 201 | return this.displayRecipes && this.menu.getNumberOfVisibleRecipes() > 12;
|
| 202 | }
|
| 203 |
|
| 204 | protected int getOffscreenRows() {
|
| 205 | return (this.menu.getNumberOfVisibleRecipes() + 4 - 1) / 4 - 3;
|
| 206 | }
|
| 207 |
|
| 208 | private void containerChanged() {
|
| 209 | this.displayRecipes = this.menu.hasInputItem();
|
| 210 | this.scrollOffs = 0.0F;
|
| 211 | this.startIndex = 0;
|
| 212 | }
|
| 213 | }
|
| 214 |
|