From b773b7b8cc0f2825c36997ba07b8d0b6774c863b Mon Sep 17 00:00:00 2001 From: Sleepwalker <62907629+Sleepwalkerx@users.noreply.github.com> Date: Sat, 30 Sep 2023 20:11:34 +0300 Subject: [PATCH] Added mouse input handler and key input handle for JemiRecipe (#319) * handle mouse input and key input * create JemiWidget * removed changes * removed changes * removed unnecessary EmiDrawContext * optimization of calculations --- .../java/dev/emi/emi/jemi/JemiRecipe.java | 73 +++++++++++++++---- 1 file changed, 58 insertions(+), 15 deletions(-) diff --git a/xplat/src/main/java/dev/emi/emi/jemi/JemiRecipe.java b/xplat/src/main/java/dev/emi/emi/jemi/JemiRecipe.java index 49383e8a..68a12b9b 100644 --- a/xplat/src/main/java/dev/emi/emi/jemi/JemiRecipe.java +++ b/xplat/src/main/java/dev/emi/emi/jemi/JemiRecipe.java @@ -1,17 +1,13 @@ package dev.emi.emi.jemi; -import java.util.List; -import java.util.Optional; - -import org.jetbrains.annotations.Nullable; - import com.google.common.collect.Lists; - import dev.emi.emi.EmiUtil; import dev.emi.emi.api.recipe.EmiRecipe; import dev.emi.emi.api.recipe.EmiRecipeCategory; import dev.emi.emi.api.stack.EmiIngredient; import dev.emi.emi.api.stack.EmiStack; +import dev.emi.emi.api.widget.Bounds; +import dev.emi.emi.api.widget.Widget; import dev.emi.emi.api.widget.WidgetHolder; import dev.emi.emi.jemi.impl.JemiIngredientAcceptor; import dev.emi.emi.jemi.impl.JemiRecipeLayoutBuilder; @@ -20,12 +16,19 @@ import dev.emi.emi.jemi.widget.JemiSlotWidget; import dev.emi.emi.jemi.widget.JemiTankWidget; import dev.emi.emi.runtime.EmiDrawContext; +import dev.emi.emi.screen.EmiScreenManager; import mezz.jei.api.gui.IRecipeLayoutDrawable; import mezz.jei.api.recipe.RecipeIngredientRole; import mezz.jei.api.recipe.category.IRecipeCategory; import mezz.jei.library.focus.FocusGroup; +import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.tooltip.TooltipComponent; +import net.minecraft.client.util.InputUtil; import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; + +import java.util.List; +import java.util.Optional; public class JemiRecipe implements EmiRecipe { public List inputs = Lists.newArrayList(); @@ -115,15 +118,7 @@ public void addWidgets(WidgetHolder widgets) { jrsb.acceptor.coerceStacks(jrsb.tooltipCallback, jrsb.renderers); } if (opt.isPresent()) { - IRecipeLayoutDrawable drawable = opt.get(); - widgets.addDrawable(0, 0, getDisplayWidth(), getDisplayHeight(), (raw, mouseX, mouseY, delta) -> { - EmiDrawContext context = EmiDrawContext.wrap(raw); - category.getBackground().draw(context.raw()); - category.draw(recipe, drawable.getRecipeSlotsView(), context.raw(), mouseX, mouseY); - context.resetColor(); - }).tooltip((x, y) -> { - return category.getTooltipStrings(recipe, drawable.getRecipeSlotsView(), x, y).stream().map(t -> TooltipComponent.of(t.asOrderedText())).toList(); - }); + widgets.add(new JemiWidget(0, 0, getDisplayWidth(), getDisplayHeight(), opt.get())); for (JemiRecipeSlotBuilder sb : builder.slots) { JemiRecipeSlot slot = new JemiRecipeSlot(sb); if (slot.tankInfo != null && !slot.getIngredients(JemiUtil.getFluidType()).toList().isEmpty()) { @@ -134,4 +129,52 @@ public void addWidgets(WidgetHolder widgets) { } } } + + public class JemiWidget extends Widget { + + private final IRecipeLayoutDrawable recipeLayoutDrawable; + private final Bounds bounds; + private final int x, y; + + public JemiWidget(int x, int y, int w, int h, IRecipeLayoutDrawable recipeLayoutDrawable) { + this.recipeLayoutDrawable = recipeLayoutDrawable; + this.bounds = new Bounds(x, y, w, h); + this.x = x; + this.y = y; + } + + @Override + public Bounds getBounds() { + return bounds; + } + + @Override + public void render(DrawContext draw, int mouseX, int mouseY, float delta) { + EmiDrawContext context = EmiDrawContext.wrap(draw); + context.push(); + context.matrices().translate(x, y, 0); + category.getBackground().draw(context.raw()); + category.draw(recipe, recipeLayoutDrawable.getRecipeSlotsView(), context.raw(), mouseX, mouseY); + context.resetColor(); + context.pop(); + } + + @Override + public List getTooltip(int mouseX, int mouseY) { + return category.getTooltipStrings(recipe, recipeLayoutDrawable.getRecipeSlotsView(), x, y) + .stream() + .map(t -> TooltipComponent.of(t.asOrderedText())) + .toList(); + } + + @Override + public boolean mouseClicked(int mouseX, int mouseY, int button) { + return category.handleInput(recipe, mouseX, mouseY, InputUtil.Type.MOUSE.createFromCode(button)); + } + + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + return category.handleInput(recipe, EmiScreenManager.lastMouseX, EmiScreenManager.lastMouseY, InputUtil.fromKeyCode(keyCode, scanCode)); + } + } }