Skip to content

Commit ff3848d

Browse files
authored
Make Multiblock Materials recipe category have scrollable/paged views to allow for more blocks to be shown (#1023)
1 parent a1a031a commit ff3848d

File tree

6 files changed

+242
-13
lines changed

6 files changed

+242
-13
lines changed

src/client/java/aztech/modern_industrialization/compat/viewer/abstraction/ViewerCategory.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,11 @@ public interface LayoutBuilder {
104104

105105
SlotBuilder outputSlot(int x, int y);
106106

107+
void invisibleInput(ItemStack item);
108+
107109
void invisibleOutput(ItemStack item);
110+
111+
void scrollableSlots(int cols, int rows, List<ItemStack> stacks);
108112
}
109113

110114
public interface SlotBuilder {
@@ -125,6 +129,43 @@ default SlotBuilder item(ItemStack stack) {
125129
SlotBuilder removeBackground();
126130

127131
SlotBuilder markCatalyst();
132+
133+
class NoOp implements SlotBuilder {
134+
@Override
135+
public SlotBuilder variant(TransferVariant<?> variant) {
136+
return this;
137+
}
138+
139+
@Override
140+
public SlotBuilder fluid(FluidVariant fluid, long amount, float probability) {
141+
return this;
142+
}
143+
144+
@Override
145+
public SlotBuilder fluid(FluidIngredient ingredient, long amount, float probability) {
146+
return this;
147+
}
148+
149+
@Override
150+
public SlotBuilder item(ItemStack stack, float probability) {
151+
return this;
152+
}
153+
154+
@Override
155+
public SlotBuilder ingredient(Ingredient ingredient, long amount, float probability) {
156+
return this;
157+
}
158+
159+
@Override
160+
public SlotBuilder removeBackground() {
161+
return this;
162+
}
163+
164+
@Override
165+
public SlotBuilder markCatalyst() {
166+
return this;
167+
}
168+
}
128169
}
129170

130171
public interface WidgetList {
@@ -147,6 +188,8 @@ default void rectangle(Rectangle rectangle, int fillColor) {
147188
void drawable(Consumer<GuiGraphics> widget);
148189

149190
void tooltip(int x, int y, int w, int h, List<Component> tooltip);
191+
192+
void scrollableSlots(int cols, int rows, List<ItemStack> stacks);
150193
}
151194

152195
public enum TextAlign {
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2020 Azercoco & Technici4n
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package aztech.modern_industrialization.compat.viewer.abstraction;
25+
26+
import java.util.List;
27+
import net.minecraft.world.item.ItemStack;
28+
29+
public class ViewerPageManager {
30+
public final List<ItemStack> stacks;
31+
public final int pageSize;
32+
33+
public int currentPage;
34+
35+
public ViewerPageManager(List<ItemStack> stacks, int pageSize) {
36+
this.stacks = stacks;
37+
this.pageSize = pageSize;
38+
}
39+
40+
public int totalPages() {
41+
return (stacks.size() - 1) / pageSize + 1;
42+
}
43+
44+
public void scroll(int delta) {
45+
currentPage += delta;
46+
int totalPages = totalPages();
47+
if (currentPage < 0) {
48+
currentPage = totalPages - 1;
49+
}
50+
if (currentPage >= totalPages) {
51+
currentPage = 0;
52+
}
53+
}
54+
55+
public ItemStack getStack(int index) {
56+
index += pageSize * currentPage;
57+
if (index < stacks.size()) {
58+
return stacks.get(index);
59+
}
60+
return ItemStack.EMPTY;
61+
}
62+
}

src/client/java/aztech/modern_industrialization/compat/viewer/impl/emi/ViewerCategoryEmi.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import aztech.modern_industrialization.MI;
2727
import aztech.modern_industrialization.compat.viewer.abstraction.ViewerCategory;
28+
import aztech.modern_industrialization.compat.viewer.abstraction.ViewerPageManager;
2829
import aztech.modern_industrialization.machines.gui.MachineScreen;
2930
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.fluid.FluidVariant;
3031
import aztech.modern_industrialization.thirdparty.fabrictransfer.api.item.ItemVariant;
@@ -35,6 +36,7 @@
3536
import dev.emi.emi.api.render.EmiRenderable;
3637
import dev.emi.emi.api.stack.EmiIngredient;
3738
import dev.emi.emi.api.stack.EmiStack;
39+
import dev.emi.emi.api.widget.SlotWidget;
3840
import dev.emi.emi.api.widget.WidgetHolder;
3941
import java.util.ArrayList;
4042
import java.util.List;
@@ -86,13 +88,26 @@ public ViewerCategory.SlotBuilder outputSlot(int x, int y) {
8688
return ing;
8789
}
8890

91+
@Override
92+
public void invisibleInput(ItemStack item) {
93+
var ing = new IngredientBuilder(0, 0);
94+
ing.item(item);
95+
ing.isVisible = false;
96+
inputs.add(ing);
97+
}
98+
8999
@Override
90100
public void invisibleOutput(ItemStack item) {
91101
var ing = new IngredientBuilder(0, 0);
92102
ing.item(item);
93103
ing.isVisible = false;
94104
outputs.add(ing);
95105
}
106+
107+
@Override
108+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
109+
stacks.forEach(this::invisibleInput);
110+
}
96111
});
97112
}
98113

@@ -308,6 +323,36 @@ public void tooltip(int x, int y, int w, int h, List<Component> tooltip) {
308323
widgets.addDrawable(x - 4, y - 4, w, h, (matrices, mouseX, mouseY, delta) -> {
309324
}).tooltip((mouseX, mouseY) -> mapped);
310325
}
326+
327+
@Override
328+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
329+
int offsetX = (widgets.getWidth() / 2) - ((cols * 18) / 2);
330+
int offsetY = 20;
331+
int pageHeight = (widgets.getHeight() - 21) / 18;
332+
int pageSize = cols * rows;
333+
ViewerPageManager pages = new ViewerPageManager(stacks, pageSize);
334+
if (pageSize < stacks.size()) {
335+
widgets.addButton(2, 2, 12, 12, 0, 0, () -> true, (mx, my, button) -> {
336+
pages.scroll(-1);
337+
});
338+
widgets.addButton(widgets.getWidth() - 14, 2, 12, 12, 12, 0, () -> true, (mx, my, button) -> {
339+
pages.scroll(1);
340+
});
341+
}
342+
int index = 0;
343+
for (; index < stacks.size() && index / cols <= pageHeight; index++) {
344+
final int finalIndex = index;
345+
widgets.add(new SlotWidget(EmiStack.EMPTY, index % cols * 18 + offsetX, index / cols * 18 + offsetY) {
346+
@Override
347+
public EmiIngredient getStack() {
348+
return EmiStack.of(pages.getStack(finalIndex));
349+
}
350+
});
351+
}
352+
for (; index < pageSize; index++) {
353+
widgets.addSlot(EmiStack.EMPTY, index % cols * 18 + offsetX, index / cols * 18 + offsetY);
354+
}
355+
}
311356
});
312357

313358
// Inputs and outputs

src/client/java/aztech/modern_industrialization/compat/viewer/impl/jei/ViewerCategoryJei.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
import mezz.jei.api.gui.drawable.IDrawable;
4040
import mezz.jei.api.gui.drawable.IDrawableStatic;
4141
import mezz.jei.api.gui.ingredient.IRecipeSlotsView;
42+
import mezz.jei.api.gui.placement.HorizontalAlignment;
43+
import mezz.jei.api.gui.placement.VerticalAlignment;
44+
import mezz.jei.api.gui.widgets.IRecipeExtrasBuilder;
4245
import mezz.jei.api.helpers.IJeiHelpers;
4346
import mezz.jei.api.recipe.IFocusGroup;
4447
import mezz.jei.api.recipe.RecipeIngredientRole;
@@ -89,12 +92,25 @@ public ViewerCategory.SlotBuilder outputSlot(int x, int y) {
8992
return slot(RecipeIngredientRole.OUTPUT, x, y);
9093
}
9194

95+
@Override
96+
public void invisibleInput(ItemStack stack) {
97+
builder.addInvisibleIngredients(RecipeIngredientRole.INPUT)
98+
.addItemStack(stack);
99+
}
100+
92101
@Override
93102
public void invisibleOutput(ItemStack stack) {
94103
builder.addInvisibleIngredients(RecipeIngredientRole.OUTPUT)
95104
.addItemStack(stack);
96105
}
97106

107+
@Override
108+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
109+
for (var stack : stacks) {
110+
builder.addInputSlot().addItemStack(stack);
111+
}
112+
}
113+
98114
private ViewerCategory.SlotBuilder slot(RecipeIngredientRole role, int x, int y) {
99115
var slotBuilder = builder.addSlot(role, x - 4, y - 4)
100116
.setStandardSlotBackground();
@@ -183,6 +199,36 @@ public ViewerCategory.SlotBuilder markCatalyst() {
183199
});
184200
}
185201

202+
@Override
203+
public void createRecipeExtras(IRecipeExtrasBuilder builder, D recipe, IFocusGroup focuses) {
204+
wrapped.buildLayout(recipe, new ViewerCategory.LayoutBuilder() {
205+
@Override
206+
public ViewerCategory.SlotBuilder inputSlot(int x, int y) {
207+
return new ViewerCategory.SlotBuilder.NoOp();
208+
}
209+
210+
@Override
211+
public ViewerCategory.SlotBuilder outputSlot(int x, int y) {
212+
return new ViewerCategory.SlotBuilder.NoOp();
213+
}
214+
215+
@Override
216+
public void invisibleInput(ItemStack stack) {
217+
}
218+
219+
@Override
220+
public void invisibleOutput(ItemStack stack) {
221+
}
222+
223+
@Override
224+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
225+
var slots = builder.getRecipeSlots().getSlots(RecipeIngredientRole.INPUT);
226+
var scrollWidget = builder.addScrollGridWidget(slots, cols, rows);
227+
scrollWidget.setPosition(0, 0, getWidth(), getHeight(), HorizontalAlignment.CENTER, VerticalAlignment.BOTTOM);
228+
}
229+
});
230+
}
231+
186232
@Override
187233
public void draw(D recipe, IRecipeSlotsView recipeSlotsView, GuiGraphics guiGraphics, double mouseX, double mouseY) {
188234
guiGraphics.pose().pushPose();
@@ -222,6 +268,10 @@ public void drawable(Consumer<GuiGraphics> widget) {
222268
@Override
223269
public void tooltip(int x, int y, int w, int h, List<Component> tooltip) {
224270
}
271+
272+
@Override
273+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
274+
}
225275
});
226276

227277
guiGraphics.pose().popPose();
@@ -263,6 +313,10 @@ public void tooltip(int x, int y, int w, int h, List<Component> tooltip) {
263313
tooltips.addAll(tooltip);
264314
}
265315
}
316+
317+
@Override
318+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
319+
}
266320
});
267321
}
268322

src/client/java/aztech/modern_industrialization/compat/viewer/impl/rei/ViewerCategoryRei.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,39 @@ public ViewerCategory.SlotBuilder outputSlot(int x, int y) {
116116
return ing;
117117
}
118118

119+
@Override
120+
public void invisibleInput(ItemStack item) {
121+
var ing = new IngredientBuilder(0, 0, true);
122+
ing.item(item);
123+
ing.isVisible = false;
124+
inputs.add(ing);
125+
}
126+
119127
@Override
120128
public void invisibleOutput(ItemStack item) {
121129
var ing = new IngredientBuilder(0, 0, false);
122130
ing.item(item);
123131
ing.isVisible = false;
124132
outputs.add(ing);
125133
}
134+
135+
@Override
136+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
137+
int x = (wrapped.width / 2) - (18 * cols) / 2 + 1;
138+
int y = wrapped.height - (rows * 18) - 3;
139+
int index = 0;
140+
for (int row = 0; row < rows; row++) {
141+
for (int col = 0; col < cols; col++) {
142+
int ix = x + (col * 18);
143+
int iy = y + (row * 18);
144+
var slot = this.inputSlot(ix, iy);
145+
if (index < stacks.size()) {
146+
slot.item(stacks.get(index));
147+
}
148+
index++;
149+
}
150+
}
151+
}
126152
});
127153
}
128154

@@ -317,6 +343,10 @@ public void tooltip(int x, int y, int w, int h, List<Component> tooltip) {
317343
}
318344
}));
319345
}
346+
347+
@Override
348+
public void scrollableSlots(int cols, int rows, List<ItemStack> stacks) {
349+
}
320350
});
321351

322352
// Inputs and outputs

0 commit comments

Comments
 (0)