1 | package net.gamelog.suntrot.screen;
|
2 |
|
3 | import net.gamelog.suntrot.Suntrot;
|
4 | import net.gamelog.suntrot.recipe.SewingRecipe;
|
5 | import net.minecraft.client.MinecraftClient;
|
6 | import net.minecraft.client.gui.DrawContext;
|
7 | import net.minecraft.client.gui.screen.ingame.HandledScreen;
|
8 | import net.minecraft.client.sound.PositionedSoundInstance;
|
9 | import net.minecraft.entity.player.PlayerInventory;
|
10 | import net.minecraft.screen.slot.Slot;
|
11 | import net.minecraft.sound.SoundEvents;
|
12 | import net.minecraft.text.Text;
|
13 | import net.minecraft.util.Identifier;
|
14 | import net.minecraft.util.math.MathHelper;
|
15 |
|
16 | import java.util.List;
|
17 |
|
18 | public class SewingScreen extends HandledScreen<SewingScreenHandler> {
|
19 | private static final Identifier TEXTURE = new Identifier(Suntrot.MOD_ID, "textures/gui/container/sewing_station.png");
|
20 | private static final int SCROLLBAR_WIDTH = 12;
|
21 | private static final int SCROLLBAR_HEIGHT = 15;
|
22 | private static final int RECIPE_LIST_COLUMNS = 4;
|
23 | private static final int RECIPE_LIST_ROWS = 3;
|
24 | private static final int RECIPE_ENTRY_WIDTH = 16;
|
25 | private static final int RECIPE_ENTRY_HEIGHT = 18;
|
26 | private static final int SCROLLBAR_AREA_HEIGHT = 54;
|
27 | private static final int RECIPE_LIST_OFFSET_X = 52;
|
28 | private static final int RECIPE_LIST_OFFSET_Y = 14;
|
29 | private float scrollAmount;
|
30 | private boolean mouseClicked;
|
31 | private int scrollOffset;
|
32 | private boolean canCraft;
|
33 |
|
34 | public SewingScreen(SewingScreenHandler handler, PlayerInventory inventory, Text title) {
|
35 | super(handler, inventory, title);
|
36 | handler.setContentsChangedListener(this::onInventoryChange);
|
37 | this.titleY--;
|
38 | }
|
39 |
|
40 | @Override
|
41 | public void render(DrawContext context, int mouseX, int mouseY, float delta) {
|
42 | super.render(context, mouseX, mouseY, delta);
|
43 | this.drawMouseoverTooltip(context, mouseX, mouseY);
|
44 | }
|
45 |
|
46 | @Override
|
47 | protected void drawBackground(DrawContext context, float delta, int mouseX, int mouseY) {
|
48 | this.renderBackground(context);
|
49 | int i = this.x;
|
50 | int j = this.y;
|
51 | context.drawTexture(TEXTURE, i, j, 0, 0, this.backgroundWidth, this.backgroundHeight);
|
52 | Slot slot = this.handler.getDyeSlot();
|
53 | if (!slot.hasStack()) {
|
54 | context.drawTexture(TEXTURE, i + slot.x, j + slot.y, this.backgroundWidth, 15, 16, 16);
|
55 | }
|
56 |
|
57 |
|
58 |
|
59 |
|
60 | int k = (int)(41.0F * this.scrollAmount);
|
61 | context.drawTexture(TEXTURE, i + 119, j + SCROLLBAR_HEIGHT + k, 176 + (this.shouldScroll() ? 0 : SCROLLBAR_WIDTH), 0, SCROLLBAR_WIDTH, SCROLLBAR_HEIGHT);
|
62 | int l = this.x + RECIPE_LIST_OFFSET_X;
|
63 | int m = this.y + RECIPE_LIST_OFFSET_Y;
|
64 | int n = this.scrollOffset + SCROLLBAR_WIDTH;
|
65 | this.renderRecipeBackground(context, mouseX, mouseY, l, m, n);
|
66 | this.renderRecipeIcons(context, l, m, n);
|
67 | }
|
68 |
|
69 | @Override
|
70 | protected void drawMouseoverTooltip(DrawContext context, int x, int y) {
|
71 | super.drawMouseoverTooltip(context, x, y);
|
72 | if (this.canCraft) {
|
73 | int i = this.x + RECIPE_LIST_OFFSET_X;
|
74 | int j = this.y + RECIPE_LIST_OFFSET_Y;
|
75 | int k = this.scrollOffset + SCROLLBAR_WIDTH;
|
76 | List<SewingRecipe> list = this.handler.getAvailableRecipes();
|
77 |
|
78 | for (int l = this.scrollOffset; l < k && l < this.handler.getAvailableRecipeCount(); l++) {
|
79 | int m = l - this.scrollOffset;
|
80 | int n = i + m % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH;
|
81 | int o = j + m / RECIPE_LIST_COLUMNS * RECIPE_ENTRY_HEIGHT + 2;
|
82 | if (x >= n && x < n + RECIPE_ENTRY_WIDTH && y >= o && y < o + RECIPE_ENTRY_HEIGHT) {
|
83 | context.drawItemTooltip(this.textRenderer, ((SewingRecipe)list.get(l)).getOutput(this.client.world.getRegistryManager()), x, y);
|
84 | }
|
85 | }
|
86 | }
|
87 | }
|
88 |
|
89 | private void renderRecipeBackground(DrawContext context, int mouseX, int mouseY, int x, int y, int scrollOffset) {
|
90 | for (int i = this.scrollOffset; i < scrollOffset && i < this.handler.getAvailableRecipeCount(); i++) {
|
91 | int j = i - this.scrollOffset;
|
92 | int k = x + j % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH;
|
93 | int l = j / RECIPE_LIST_COLUMNS;
|
94 | int m = y + l * RECIPE_ENTRY_HEIGHT + 2;
|
95 | int n = this.backgroundHeight;
|
96 | if (i == this.handler.getSelectedRecipe()) {
|
97 | n += RECIPE_ENTRY_HEIGHT;
|
98 | } else if (mouseX >= k && mouseY >= m && mouseX < k + RECIPE_ENTRY_WIDTH && mouseY < m + RECIPE_ENTRY_HEIGHT) {
|
99 | n += 36;
|
100 | }
|
101 |
|
102 | context.drawTexture(TEXTURE, k, m - 1, 0, n, RECIPE_ENTRY_WIDTH, RECIPE_ENTRY_HEIGHT);
|
103 | }
|
104 | }
|
105 |
|
106 | private void renderRecipeIcons(DrawContext context, int x, int y, int scrollOffset) {
|
107 | List<SewingRecipe> list = this.handler.getAvailableRecipes();
|
108 |
|
109 | for (int i = this.scrollOffset; i < scrollOffset && i < this.handler.getAvailableRecipeCount(); i++) {
|
110 | int j = i - this.scrollOffset;
|
111 | int k = x + j % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH;
|
112 | int l = j / RECIPE_LIST_COLUMNS;
|
113 | int m = y + l * RECIPE_ENTRY_HEIGHT + 2;
|
114 | context.drawItem(((SewingRecipe)list.get(i)).getOutput(this.client.world.getRegistryManager()), k, m);
|
115 | }
|
116 | }
|
117 |
|
118 | @Override
|
119 | public boolean mouseClicked(double mouseX, double mouseY, int button) {
|
120 | this.mouseClicked = false;
|
121 | if (this.canCraft) {
|
122 | int i = this.x + RECIPE_LIST_OFFSET_X;
|
123 | int j = this.y + RECIPE_LIST_OFFSET_Y;
|
124 | int k = this.scrollOffset + SCROLLBAR_WIDTH;
|
125 |
|
126 | for (int l = this.scrollOffset; l < k; l++) {
|
127 | int m = l - this.scrollOffset;
|
128 | double d = mouseX - (double)(i + m % RECIPE_LIST_COLUMNS * RECIPE_ENTRY_WIDTH);
|
129 | double e = mouseY - (double)(j + m / RECIPE_LIST_COLUMNS * RECIPE_ENTRY_HEIGHT);
|
130 | if (d >= 0.0 && e >= 0.0 && d < RECIPE_ENTRY_WIDTH && e < RECIPE_ENTRY_HEIGHT && this.handler.onButtonClick(this.client.player, l)) {
|
131 | MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_STONECUTTER_SELECT_RECIPE, 1.0F));
|
132 | this.client.interactionManager.clickButton(this.handler.syncId, l);
|
133 | return true;
|
134 | }
|
135 | }
|
136 |
|
137 | i = this.x + 119;
|
138 | j = this.y + 9;
|
139 | if (mouseX >= (double)i && mouseX < (double)(i + SCROLLBAR_WIDTH) && mouseY >= (double)j && mouseY < (double)(j + SCROLLBAR_AREA_HEIGHT)) {
|
140 | this.mouseClicked = true;
|
141 | }
|
142 | }
|
143 |
|
144 | return super.mouseClicked(mouseX, mouseY, button);
|
145 | }
|
146 |
|
147 | @Override
|
148 | public boolean mouseDragged(double mouseX, double mouseY, int button, double deltaX, double deltaY) {
|
149 | if (this.mouseClicked && this.shouldScroll()) {
|
150 | int i = this.y + RECIPE_LIST_OFFSET_Y;
|
151 | int j = i + SCROLLBAR_AREA_HEIGHT;
|
152 | this.scrollAmount = ((float)mouseY - (float)i - 7.5F) / ((float)(j - i) - SCROLLBAR_HEIGHT);
|
153 | this.scrollAmount = MathHelper.clamp(this.scrollAmount, 0.0F, 1.0F);
|
154 | this.scrollOffset = (int)((double)(this.scrollAmount * (float)this.getMaxScroll()) + 0.5) * RECIPE_LIST_COLUMNS;
|
155 | return true;
|
156 | } else {
|
157 | return super.mouseDragged(mouseX, mouseY, button, deltaX, deltaY);
|
158 | }
|
159 | }
|
160 |
|
161 | @Override
|
162 | public boolean mouseScrolled(double mouseX, double mouseY, double amount) {
|
163 | if (this.shouldScroll()) {
|
164 | int i = this.getMaxScroll();
|
165 | float f = (float)amount / (float)i;
|
166 | this.scrollAmount = MathHelper.clamp(this.scrollAmount - f, 0.0F, 1.0F);
|
167 | this.scrollOffset = (int)((double)(this.scrollAmount * (float)i) + 0.5) * RECIPE_LIST_COLUMNS;
|
168 | }
|
169 |
|
170 | return true;
|
171 | }
|
172 |
|
173 | private boolean shouldScroll() {
|
174 | return this.canCraft && this.handler.getAvailableRecipeCount() > SCROLLBAR_WIDTH;
|
175 | }
|
176 |
|
177 | protected int getMaxScroll() {
|
178 | return (this.handler.getAvailableRecipeCount() + RECIPE_LIST_COLUMNS - 1) / RECIPE_LIST_COLUMNS - RECIPE_LIST_ROWS;
|
179 | }
|
180 |
|
181 | private void onInventoryChange() {
|
182 | this.canCraft = this.handler.canCraft();
|
183 | if (!this.canCraft) {
|
184 | this.scrollAmount = 0.0F;
|
185 | this.scrollOffset = 0;
|
186 | }
|
187 | }
|
188 | }
|