Skip to content

Commit

Permalink
Simplify JEI mouse click implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
pupnewfster committed Feb 28, 2024
1 parent b02dd97 commit 1a40a85
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/main/java/mekanism/client/gui/GuiMekanism.java
Expand Up @@ -40,6 +40,7 @@
import net.minecraft.client.gui.components.events.ContainerEventHandler;
import net.minecraft.client.gui.components.events.GuiEventListener;
import net.minecraft.client.gui.navigation.FocusNavigationEvent;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
Expand Down Expand Up @@ -206,6 +207,12 @@ public GuiEventListener getFocused() {
@Override
public void setFocused(@Nullable GuiEventListener focused) {
}

@NotNull
@Override
public ScreenRectangle getRectangle() {
return GuiMekanism.this.getRectangle();
}
};
ComponentPath componentPath = handleNavigation.apply(handlerWithWindows);
if (componentPath == null) {
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/mekanism/client/gui/element/GuiElement.java
Expand Up @@ -30,6 +30,7 @@
import net.minecraft.client.gui.navigation.FocusNavigationEvent.ArrowNavigation;
import net.minecraft.client.gui.navigation.FocusNavigationEvent.InitialFocus;
import net.minecraft.client.gui.navigation.FocusNavigationEvent.TabNavigation;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.sounds.SimpleSoundInstance;
import net.minecraft.client.sounds.SoundManager;
Expand Down Expand Up @@ -147,10 +148,15 @@ public final int getGuiHeight() {
return guiObj.getYSize();
}

@NotNull
@Override
public ScreenRectangle getRectangle() {
return super.getRectangle();
}

@NotNull
@Override
public List<GuiElement> children() {
//TODO - 1.20.4: Override getRectangle?? At the very least for GuiMekanism so that elements that stick out are included
return children;
}

Expand Down
30 changes: 15 additions & 15 deletions src/main/java/mekanism/client/jei/BaseRecipeCategory.java
Expand Up @@ -6,13 +6,10 @@
import java.util.EnumMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import mekanism.api.chemical.ChemicalStack;
import mekanism.api.providers.IItemProvider;
import mekanism.client.gui.GuiUtils;
import mekanism.client.gui.IGuiWrapper;
import mekanism.client.gui.element.GuiElement;
import mekanism.client.gui.element.GuiTexturedElement;
import mekanism.client.gui.element.bar.GuiBar.IBarInfoHandler;
import mekanism.client.gui.element.gauge.GaugeOverlay;
import mekanism.client.gui.element.gauge.GuiGauge;
Expand Down Expand Up @@ -62,7 +59,7 @@ protected static IDrawable createIcon(IGuiHelper helper, IItemProvider provider)
return helper.createDrawableIngredient(VanillaTypes.ITEM_STACK, provider.getItemStack());
}

private final List<GuiTexturedElement> guiElements = new ArrayList<>();
private final List<GuiElement> guiElements = new ArrayList<>();
private final Component component;
private final IGuiHelper guiHelper;
private final IDrawable background;
Expand Down Expand Up @@ -97,28 +94,31 @@ protected BaseRecipeCategory(IGuiHelper helper, RecipeType<RECIPE> recipeType, C
this.background = new NOOPDrawable(width, height);
}

protected <ELEMENT extends GuiTexturedElement> ELEMENT addElement(ELEMENT element) {
protected <ELEMENT extends GuiElement> ELEMENT addElement(ELEMENT element) {
guiElements.add(element);
return element;
}

@NotNull
@Override
public List<GuiTexturedElement> children() {
public List<GuiElement> children() {
return guiElements;
}

@NotNull
@Override
public ScreenRectangle getRectangle() {
return new ScreenRectangle(getGuiLeft(), getGuiTop(), getXSize(), getYSize());
}

@Override
public boolean handleInput(RECIPE recipe, double mouseX, double mouseY, InputConstants.Key input) {
//TODO: Evaluate implementing isDragging, setDragging in some fashion or another
Predicate<GuiTexturedElement> predicate = switch (input.getType()) {
case KEYSYM -> child -> child.keyPressed(input.getValue(), -1, 0);
case SCANCODE -> child -> child.keyPressed(-1, input.getValue(), 0);
case MOUSE -> child -> child.mouseClicked(mouseX, mouseY, input.getValue());
return switch (input.getType()) {
case KEYSYM -> keyPressed(input.getValue(), -1, 0);
case SCANCODE -> keyPressed(-1, input.getValue(), 0);
//Shift by the offset as JEI doesn't realize we are actually starting at negatives
case MOUSE -> mouseClicked(mouseX, mouseY, input.getValue());
};
GuiTexturedElement targetedChild = GuiUtils.findChild(children(), predicate);
setFocused(targetedChild);
return targetedChild != null;
}

/**
Expand Down Expand Up @@ -186,7 +186,7 @@ protected void renderElements(RECIPE recipe, IRecipeSlotsView recipeSlotsView, G
//Note: We don't care that onRenderForeground updates the maxZOffset in the mekanism gui as that is just used for rendering windows
// and as our categories don't support windows we don't need to worry about that
int zOffset = 200;
for (GuiTexturedElement element : guiElements) {
for (GuiElement element : guiElements) {
pose.pushPose();
element.onRenderForeground(guiGraphics, x, y, zOffset, zOffset);
pose.popPose();
Expand Down

0 comments on commit 1a40a85

Please sign in to comment.