Skip to content

Commit

Permalink
Added mouse input handler and key input handle for JemiRecipe (#319)
Browse files Browse the repository at this point in the history
* handle mouse input and key input

* create JemiWidget

* removed changes

* removed changes

* removed unnecessary EmiDrawContext

* optimization of calculations
  • Loading branch information
Sleepwalkerx authored Sep 30, 2023
1 parent 3ca0029 commit b773b7b
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions xplat/src/main/java/dev/emi/emi/jemi/JemiRecipe.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<T> implements EmiRecipe {
public List<EmiIngredient> inputs = Lists.newArrayList();
Expand Down Expand Up @@ -115,15 +118,7 @@ public void addWidgets(WidgetHolder widgets) {
jrsb.acceptor.coerceStacks(jrsb.tooltipCallback, jrsb.renderers);
}
if (opt.isPresent()) {
IRecipeLayoutDrawable<T> 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()) {
Expand All @@ -134,4 +129,52 @@ public void addWidgets(WidgetHolder widgets) {
}
}
}

public class JemiWidget extends Widget {

private final IRecipeLayoutDrawable<T> recipeLayoutDrawable;
private final Bounds bounds;
private final int x, y;

public JemiWidget(int x, int y, int w, int h, IRecipeLayoutDrawable<T> 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<TooltipComponent> 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));
}
}
}

0 comments on commit b773b7b

Please sign in to comment.