Skip to content

Commit e45f39e

Browse files
committed
Scissoring to clip preview to bounds
1 parent 268d5f1 commit e45f39e

1 file changed

Lines changed: 75 additions & 1 deletion

File tree

src/main/java/com/robotgryphon/compactcrafting/compat/jei/JeiMiniaturizationCraftingCategory.java

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.mojang.blaze3d.matrix.MatrixStack;
44
import com.mojang.blaze3d.systems.RenderSystem;
5+
import com.mojang.blaze3d.vertex.IVertexBuilder;
56
import com.robotgryphon.compactcrafting.CompactCrafting;
67
import com.robotgryphon.compactcrafting.client.render.RenderTickCounter;
78
import com.robotgryphon.compactcrafting.client.render.RenderTypesExtensions;
@@ -17,6 +18,7 @@
1718
import mezz.jei.api.ingredients.IIngredients;
1819
import mezz.jei.api.recipe.category.IRecipeCategory;
1920
import net.minecraft.block.BlockState;
21+
import net.minecraft.client.MainWindow;
2022
import net.minecraft.client.Minecraft;
2123
import net.minecraft.client.audio.SimpleSound;
2224
import net.minecraft.client.audio.SoundHandler;
@@ -34,6 +36,7 @@
3436
import net.minecraft.util.math.AxisAlignedBB;
3537
import net.minecraft.util.math.BlockPos;
3638
import net.minecraft.util.math.vector.Quaternion;
39+
import net.minecraft.util.math.vector.Vector3d;
3740
import net.minecraft.util.text.IFormattableTextComponent;
3841
import net.minecraft.util.text.TextFormatting;
3942
import net.minecraft.util.text.TranslationTextComponent;
@@ -84,6 +87,7 @@ public JeiMiniaturizationCraftingCategory(IGuiHelper guiHelper) {
8487
this.blocks = Minecraft.getInstance().getBlockRendererDispatcher();
8588
}
8689

90+
//region JEI implementation requirements
8791
@Override
8892
public ResourceLocation getUid() {
8993
return UID;
@@ -108,7 +112,9 @@ public IDrawable getBackground() {
108112
public IDrawable getIcon() {
109113
return this.icon;
110114
}
115+
//endregion
111116

117+
//region Recipe Slots and Items
112118
@Override
113119
public void setIngredients(MiniaturizationRecipe recipe, IIngredients ing) {
114120
List<ItemStack> inputs = new ArrayList<>();
@@ -217,6 +223,7 @@ private void addOutputSlots(MiniaturizationRecipe recipe, int GUTTER_X, int OFFS
217223
guiItemStacks.set(outputOffset, output);
218224
}
219225
}
226+
//endregion
220227

221228
@Override
222229
public boolean handleClick(MiniaturizationRecipe recipe, double mouseX, double mouseY, int mouseButton) {
@@ -258,6 +265,7 @@ public boolean handleClick(MiniaturizationRecipe recipe, double mouseX, double m
258265
return false;
259266
}
260267

268+
//region Rendering help
261269
private void drawScaledTexture(
262270
MatrixStack matrixStack,
263271
ResourceLocation texture, Rectangle bounds,
@@ -277,10 +285,58 @@ private void drawScaledTexture(
277285
// }
278286
}
279287

288+
private void addColoredVertex(IVertexBuilder renderer, Color color, Vector3d position) {
289+
renderer.pos(position.getX(), position.getY(), position.getZ())
290+
.color(color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha())
291+
.lightmap(0, 240)
292+
.normal(1, 0, 0)
293+
.endVertex();
294+
}
295+
296+
private void drawRectOutline(IVertexBuilder renderer, Color color, Rectangle rect) {
297+
Vector3d bottomLeft = new Vector3d((float) rect.getMinX(), (float) rect.getMinY(), 0);
298+
Vector3d bottomRight = bottomLeft.add(rect.width, 0, 0);
299+
Vector3d topRight = bottomLeft.add(rect.width, rect.height, 0);
300+
Vector3d topLeft = bottomLeft.add(0, rect.height, 0);
301+
302+
addColoredVertex(renderer, color, bottomLeft);
303+
addColoredVertex(renderer, color, bottomRight);
304+
305+
addColoredVertex(renderer, color, bottomRight);
306+
addColoredVertex(renderer, color, topRight);
307+
308+
addColoredVertex(renderer, color, topRight);
309+
addColoredVertex(renderer, color, topLeft);
310+
311+
addColoredVertex(renderer, color, topLeft);
312+
addColoredVertex(renderer, color, bottomLeft);
313+
}
314+
//endregion
315+
280316
@Override
281317
public void draw(MiniaturizationRecipe recipe, MatrixStack mx, double mouseX, double mouseY) {
282318
AxisAlignedBB dims = recipe.getDimensions();
283319

320+
Screen curr = Minecraft.getInstance().currentScreen;
321+
322+
MainWindow mainWindow = Minecraft.getInstance().getMainWindow();
323+
int scaledWidth = mainWindow.getScaledWidth();
324+
int scaledHeight = mainWindow.getScaledHeight();
325+
326+
int winWidth = (9 * 18) + 10;
327+
328+
int slotHeight = (18 * 3) - 8;
329+
330+
int scissorX = (curr.width / 2) - (background.getWidth() / 2) + 15;
331+
int scissorY = (curr.height / 2) - (background.getHeight() / 2) + slotHeight;
332+
333+
double guiScaleFactor = mainWindow.getGuiScaleFactor();
334+
Rectangle scissorBounds = new Rectangle(
335+
scissorX, scissorY,
336+
winWidth - 22,
337+
(int) (background.getHeight() - slotHeight - 27)
338+
);
339+
284340
//region JEI controls
285341
mx.push();
286342
mx.translate(0, 0, 500);
@@ -314,12 +370,27 @@ public void draw(MiniaturizationRecipe recipe, MatrixStack mx, double mouseX, do
314370
//endregion
315371

316372
try {
373+
AbstractGui.fill(
374+
mx,
375+
// scissorBounds.x, scissorBounds.y,
376+
14, 0,
377+
scissorBounds.width + 16,
378+
scissorBounds.height + 1,
379+
Color.darkGray.getRGB()
380+
);
381+
317382
IRenderTypeBuffer.Impl buffers = IRenderTypeBuffer.getImpl(Tessellator.getInstance().getBuffer());
318383

384+
RenderSystem.enableScissor(
385+
(int) (scissorBounds.x * guiScaleFactor),
386+
(int) (scissorBounds.y * guiScaleFactor),
387+
(int) (scissorBounds.width * guiScaleFactor),
388+
(int) (scissorBounds.height * guiScaleFactor));
389+
319390
mx.push();
320391

321392
mx.translate(
322-
background.getWidth() / 2,
393+
(background.getWidth() / 2) + 6,
323394
70,
324395
400);
325396

@@ -360,9 +431,12 @@ public void draw(MiniaturizationRecipe recipe, MatrixStack mx, double mouseX, do
360431
layer.ifPresent(l -> renderRecipeLayer(recipe, mx, buffers, l, y));
361432
}
362433

434+
363435
mx.pop();
364436

365437
buffers.finish();
438+
439+
RenderSystem.disableScissor();
366440
} catch (Exception ex) {
367441
CompactCrafting.LOGGER.warn(ex);
368442
}

0 commit comments

Comments
 (0)