package net.gamelog.suntrot.screen; import net.gamelog.suntrot.Suntrot; import net.gamelog.suntrot.recipe.SewingRecipe; import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.sound.PositionedSoundInstance; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.screen.slot.Slot; import net.minecraft.sound.SoundEvents; import net.minecraft.text.Text; import net.minecraft.util.Identifier; import net.minecraft.util.math.MathHelper; import java.util.List; public class SewingScreen extends HandledScreen { private static final Identifier TEXTURE = new Identifier(Suntrot.MOD_ID, "textures/gui/container/sewing_station.png"); private static final int SCROLLBAR_WIDTH = 12; private static final int SCROLLBAR_HEIGHT = 15; private static final int RECIPE_LIST_COLUMNS = 4; private static final int RECIPE_LIST_ROWS = 3; private static final int RECIPE_ENTRY_WIDTH = 16; private static final int RECIPE_ENTRY_HEIGHT = 18; private static final int SCROLLBAR_AREA_HEIGHT = 54; private static final int RECIPE_LIST_OFFSET_X = 52; private static final int RECIPE_LIST_OFFSET_Y = 14; private float scrollAmount; private boolean mouseClicked; private int scrollOffset; private boolean canCraft; public SewingScreen(SewingScreenHandler handler, PlayerInventory inventory, Text title) { super(handler, inventory, title); handler.setContentsChangedListener(this::onInventoryChange); this.titleY--; } @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { super.render(context, mouseX, mouseY, delta); this.drawMouseoverTooltip(context, mouseX, mouseY); } @Override protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) { this.renderBackground(context); int i = this.x; int j = this.y; context.drawTexture(TEXTURE, i, j, 0, 0, this.backgroundWidth, this.backgroundHeight); Slot slot = this.handler.getDyeSlot(); if (!slot.hasStack()) { context.drawTexture(TEXTURE, i + slot.x, j + slot.y, this.backgroundWidth, 15, 16, 16); } int k = (int)(41.0F * this.scrollAmount); context.drawTexture(TEXTURE, i + 119, j + SCROLLBAR_HEIGHT + k, 176 + (this.shouldScroll() ? 0 : SCROLLBAR_WIDTH), 0, SCROLLBAR_WIDTH, SCROLLBAR_HEIGHT); int l = this.x + RECIPE_LIST_OFFSET_X; int m = this.y + RECIPE_LIST_OFFSET_Y; int n = this.scrollOffset + SCROLLBAR_WIDTH; this.renderRecipeBackground(context, mouseX, mouseY, l, m, n); this.renderRecipeIcons(context, l, m, n); } @Override protected void drawMouseoverTooltip(DrawContext context, int x, int y) { super.drawMouseoverTooltip(context, x, y); if (this.canCraft) { int i = this.x + RECIPE_LIST_OFFSET_X; int j = this.y + RECIPE_LIST_OFFSET_Y; int k = this.scrollOffset + SCROLLBAR_WIDTH; List list = this.handler.getAvailableRecipes(); for (int l = this.scrollOffset; l < k && l < this.handler.getAvailableRecipeCount(); l++) { int m = l - this.scrollOffset; int n = i + m % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH; int o = j + m / RECIPE_LIST_COLUMNS * RECIPE_ENTRY_HEIGHT + 2; if (x >= n && x < n + RECIPE_ENTRY_WIDTH && y >= o && y < o + RECIPE_ENTRY_HEIGHT) { context.drawItemTooltip(this.textRenderer, ((SewingRecipe)list.get(l)).getOutput(this.client.world.getRegistryManager()), x, y); } } } } private void renderRecipeBackground(DrawContext context, int mouseX, int mouseY, int x, int y, int scrollOffset) { for (int i = this.scrollOffset; i < scrollOffset && i < this.handler.getAvailableRecipeCount(); i++) { int j = i - this.scrollOffset; int k = x + j % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH; int l = j / RECIPE_LIST_COLUMNS; int m = y + l * RECIPE_ENTRY_HEIGHT + 2; int n = this.backgroundHeight; if (i == this.handler.getSelectedRecipe()) { n += RECIPE_ENTRY_HEIGHT; } else if (mouseX >= k && mouseY >= m && mouseX < k + RECIPE_ENTRY_WIDTH && mouseY < m + RECIPE_ENTRY_HEIGHT) { n += 36; } context.drawTexture(TEXTURE, k, m - 1, 0, n, RECIPE_ENTRY_WIDTH, RECIPE_ENTRY_HEIGHT); } } private void renderRecipeIcons(DrawContext context, int x, int y, int scrollOffset) { List list = this.handler.getAvailableRecipes(); for (int i = this.scrollOffset; i < scrollOffset && i < this.handler.getAvailableRecipeCount(); i++) { int j = i - this.scrollOffset; int k = x + j % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH; int l = j / RECIPE_LIST_COLUMNS; int m = y + l * RECIPE_ENTRY_HEIGHT + 2; context.drawItem(((SewingRecipe)list.get(i)).getOutput(this.client.world.getRegistryManager()), k, m); } } @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { this.mouseClicked = false; if (this.canCraft) { int i = this.x + RECIPE_LIST_OFFSET_X; int j = this.y + RECIPE_LIST_OFFSET_Y; int k = this.scrollOffset + SCROLLBAR_WIDTH; for (int l = this.scrollOffset; l < k; l++) { int m = l - this.scrollOffset; double d = mouseX - (double)(i + m % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH); double e = mouseY - (double)(j + m / RECIPE_LIST_COLUMNS * RECIPE_ENTRY_HEIGHT); if (d >= 0.0 && e >= 0.0 && d < RECIPE_ENTRY_WIDTH && e < RECIPE_ENTRY_HEIGHT && this.handler.onButtonClick(this.client.player, l)) { MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_STONECUTTER_SELECT_RECIPE, 1.0F)); this.client.interactionManager.clickButton(this.handler.syncId, l); return true; } } i = this.x + 119; j = this.y + 9; if (mouseX >= (double)i && mouseX < (double)(i + SCROLLBAR_WIDTH) && mouseY >= (double)j && mouseY < (double)(j + SCROLLBAR_AREA_HEIGHT)) { this.mouseClicked = true; } } return super.mouseClicked(mouseX, mouseY, button); } @Override public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) { if (this.mouseClicked && this.shouldScroll()) { int i = this.y + RECIPE_LIST_OFFSET_Y; int j = i + SCROLLBAR_AREA_HEIGHT; this.scrollAmount = ((float)mouseY - (float)i - 7.5F) / ((float)(j - i) - SCROLLBAR_HEIGHT); this.scrollAmount = MathHelper.clamp(this.scrollAmount, 0.0F, 1.0F); this.scrollOffset = (int)((double)(this.scrollAmount * (float)this.getMaxScroll()) + 0.5) * RECIPE_LIST_COLUMNS; return true; } else { return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY); } } @Override public boolean mouseScrolled(double mouseX, double mouseY, double amount) { if (this.shouldScroll()) { int i = this.getMaxScroll(); float f = (float)amount / (float)i; this.scrollAmount = MathHelper.clamp(this.scrollAmount - f, 0.0F, 1.0F); this.scrollOffset = (int)((double)(this.scrollAmount * (float)i) + 0.5) * RECIPE_LIST_COLUMNS; } return true; } private boolean shouldScroll() { return this.canCraft && this.handler.getAvailableRecipeCount() > SCROLLBAR_WIDTH; } protected int getMaxScroll() { return (this.handler.getAvailableRecipeCount() + RECIPE_LIST_COLUMNS - 1) / RECIPE_LIST_COLUMNS - RECIPE_LIST_ROWS; } private void onInventoryChange() { this.canCraft = this.handler.canCraft(); if (!this.canCraft) { this.scrollAmount = 0.0F; this.scrollOffset = 0; } } }