Skip to content

Commit

Permalink
Fix scrolling the item list when mouse is in the gaps between elements
Browse files Browse the repository at this point in the history
  • Loading branch information
mezz committed Aug 7, 2017
1 parent 99e6a96 commit 9fd1680
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 84 deletions.
77 changes: 11 additions & 66 deletions src/main/java/mezz/jei/gui/overlay/IngredientGrid.java
@@ -1,16 +1,7 @@
package mezz.jei.gui.overlay;

import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import mezz.jei.Internal;
import mezz.jei.JustEnoughItems;
import mezz.jei.api.gui.IAdvancedGuiHandler;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.config.Config;
import mezz.jei.gui.TooltipRenderer;
Expand All @@ -26,15 +17,19 @@
import mezz.jei.render.GuiIngredientFast;
import mezz.jei.render.GuiIngredientFastList;
import mezz.jei.runtime.JeiRuntime;
import mezz.jei.util.MathUtil;
import mezz.jei.util.Translator;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.EntityPlayerSP;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.Collection;
import java.util.List;

/**
* An ingredient grid displays a rectangular area of clickable recipe ingredients.
*/
Expand All @@ -43,53 +38,14 @@ public abstract class IngredientGrid implements IShowsRecipeFocuses, IPaged {
private static final int INGREDIENT_WIDTH = GuiItemStackGroup.getWidth(INGREDIENT_PADDING);
private static final int INGREDIENT_HEIGHT = GuiItemStackGroup.getHeight(INGREDIENT_PADDING);

private Set<Rectangle> guiAreas = Collections.emptySet();
private Rectangle area = new Rectangle();
protected final GuiIngredientFastList guiIngredientList;

public IngredientGrid(IIngredientRegistry ingredientRegistry) {
this.guiIngredientList = new GuiIngredientFastList(ingredientRegistry);
}

private static boolean intersects(Collection<Rectangle> areas, Rectangle comparisonArea) {
for (Rectangle area : areas) {
if (area.intersects(comparisonArea)) {
return true;
}
}
return false;
}

private static boolean isMouseOverGuiArea(Collection<Rectangle> guiAreas, int mouseX, int mouseY) {
for (Rectangle guiArea : guiAreas) {
if (guiArea.contains(mouseX, mouseY)) {
return true;
}
}
return false;
}

private static Set<Rectangle> getGuiAreas() {
final GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen;
if (currentScreen instanceof GuiContainer) {
final GuiContainer guiContainer = (GuiContainer) currentScreen;
final JeiRuntime jeiRuntime = Internal.getRuntime();
if (jeiRuntime != null) {
final Set<Rectangle> allGuiExtraAreas = new HashSet<>();
final List<IAdvancedGuiHandler<GuiContainer>> activeAdvancedGuiHandlers = jeiRuntime.getActiveAdvancedGuiHandlers(guiContainer);
for (IAdvancedGuiHandler<GuiContainer> advancedGuiHandler : activeAdvancedGuiHandlers) {
final List<Rectangle> guiExtraAreas = advancedGuiHandler.getGuiExtraAreas(guiContainer);
if (guiExtraAreas != null) {
allGuiExtraAreas.addAll(guiExtraAreas);
}
}
return allGuiExtraAreas;
}
}
return Collections.emptySet();
}

public void updateBounds(Rectangle area) {
public void updateBounds(Rectangle area, Collection<Rectangle> exclusionAreas) {
final int columns = area.width / INGREDIENT_WIDTH;
final int rows = area.height / INGREDIENT_HEIGHT;

Expand All @@ -101,24 +57,23 @@ public void updateBounds(Rectangle area) {
this.area = new Rectangle(x, y, width, height);
this.guiIngredientList.clear();

this.guiAreas = getGuiAreas();
for (int row = 0; row < rows; row++) {
int y1 = y + (row * INGREDIENT_HEIGHT);
for (int column = 0; column < columns; column++) {
int x1 = x + (column * INGREDIENT_WIDTH);
GuiIngredientFast guiIngredientFast = new GuiIngredientFast(x1, y1, INGREDIENT_PADDING);
Rectangle stackArea = guiIngredientFast.getArea();
final boolean blocked = intersects(this.guiAreas, stackArea);
final boolean blocked = MathUtil.intersects(exclusionAreas, stackArea);
guiIngredientFast.setBlocked(blocked);
this.guiIngredientList.add(guiIngredientFast);
}
}
}

public void updateLayout() {
public void updateLayout(Collection<Rectangle> guiExclusionAreas) {
for (GuiIngredientFast guiIngredientFast : this.guiIngredientList.getAllGuiIngredients()) {
Rectangle stackArea = guiIngredientFast.getArea();
final boolean blocked = intersects(this.guiAreas, stackArea);
final boolean blocked = MathUtil.intersects(guiExclusionAreas, stackArea);
guiIngredientFast.setBlocked(blocked);
}
}
Expand Down Expand Up @@ -168,8 +123,7 @@ private static boolean shouldShowDeleteItemTooltip(Minecraft minecraft) {
}

public boolean isMouseOver(int mouseX, int mouseY) {
return area.contains(mouseX, mouseY) &&
!isMouseOverGuiArea(this.guiAreas, mouseX, mouseY);
return area.contains(mouseX, mouseY);
}

public boolean handleMouseClicked(int mouseX, int mouseY) {
Expand Down Expand Up @@ -222,15 +176,6 @@ public boolean canSetFocusWithMouse() {
return true;
}

public boolean updateGuiAreas() {
final Set<Rectangle> guiAreas = getGuiAreas();
if (!guiAreas.equals(this.guiAreas)) {
this.guiAreas = guiAreas;
return true;
}
return false;
}

public abstract int getPageCount();

public abstract int getPageNum();
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/mezz/jei/gui/overlay/IngredientGridAll.java
@@ -1,15 +1,17 @@
package mezz.jei.gui.overlay;

import java.util.ArrayList;
import java.util.List;

import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.config.SessionData;
import mezz.jei.gui.ingredients.IIngredientListElement;
import mezz.jei.ingredients.IngredientFilter;
import mezz.jei.render.GuiIngredientFast;
import mezz.jei.util.MathUtil;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
* Displays all known recipe ingredients.
*/
Expand All @@ -22,8 +24,8 @@ public IngredientGridAll(IIngredientRegistry ingredientRegistry, IngredientFilte
}

@Override
public void updateLayout() {
super.updateLayout();
public void updateLayout(Collection<Rectangle> guiExclusionAreas) {
super.updateLayout(guiExclusionAreas);
List<IIngredientListElement> ingredientList = ingredientFilter.getIngredientList();
this.guiIngredientList.set(SessionData.getFirstItemIndex(), ingredientList);
}
Expand Down
65 changes: 52 additions & 13 deletions src/main/java/mezz/jei/gui/overlay/IngredientListOverlay.java
@@ -1,13 +1,10 @@
package mezz.jei.gui.overlay;

import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.Collection;
import java.util.List;

import com.google.common.collect.ImmutableList;
import mezz.jei.Internal;
import mezz.jei.api.IIngredientListOverlay;
import mezz.jei.api.IItemListOverlay;
import mezz.jei.api.gui.IAdvancedGuiHandler;
import mezz.jei.api.ingredients.IIngredientRegistry;
import mezz.jei.config.Config;
import mezz.jei.config.KeyBindings;
Expand All @@ -20,14 +17,25 @@
import mezz.jei.input.IMouseHandler;
import mezz.jei.input.IPaged;
import mezz.jei.input.IShowsRecipeFocuses;
import mezz.jei.runtime.JeiRuntime;
import mezz.jei.util.ErrorUtil;
import mezz.jei.util.Log;
import mezz.jei.util.MathUtil;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;

import javax.annotation.Nullable;
import java.awt.Rectangle;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class IngredientListOverlay implements IItemListOverlay, IIngredientListOverlay, IPaged, IMouseHandler, IShowsRecipeFocuses {
private static final int BORDER_PADDING = 2;
private static final int BUTTON_SIZE = 20;
Expand All @@ -52,6 +60,8 @@ private static boolean isSearchBarCentered(GuiProperties guiProperties) {
private final PageNavigation navigation;
private final IngredientGrid contents;
private final GuiTextFieldFilter searchField;
private Set<Rectangle> guiExclusionAreas = Collections.emptySet();
private Rectangle displayArea = new Rectangle();

// properties of the gui we're beside
@Nullable
Expand Down Expand Up @@ -120,13 +130,13 @@ public void updateScreen(@Nullable GuiScreen guiScreen) {
final int y = BORDER_PADDING;
final int width = guiProperties.getScreenWidth() - x - (2 * BORDER_PADDING);
final int height = guiProperties.getScreenHeight() - y - (2 * BORDER_PADDING);
Rectangle displayArea = new Rectangle(x, y, width, height);
displayArea = new Rectangle(x, y, width, height);

final boolean searchBarCentered = isSearchBarCentered(guiProperties);
final int searchHeight = searchBarCentered ? 0 : SEARCH_HEIGHT + 4;

Rectangle contentsArea = new Rectangle(displayArea.x, displayArea.y + NAVIGATION_HEIGHT + 2, displayArea.width, displayArea.height - NAVIGATION_HEIGHT - searchHeight);
this.contents.updateBounds(contentsArea);
this.contents.updateBounds(contentsArea, this.guiExclusionAreas);

// update area to match contents size
contentsArea = this.contents.getArea();
Expand All @@ -153,7 +163,7 @@ public void updateScreen(@Nullable GuiScreen guiScreen) {
}

private void updateLayout() {
this.contents.updateLayout();
this.contents.updateLayout(this.guiExclusionAreas);

int pageNum = this.contents.getPageNum();
int pageCount = this.contents.getPageCount();
Expand All @@ -163,7 +173,7 @@ private void updateLayout() {
}

public void drawScreen(Minecraft minecraft, int mouseX, int mouseY, float partialTicks) {
if (this.contents.updateGuiAreas()) {
if (this.updateGuiExclusionAreas()) {
updateLayout();
}

Expand All @@ -175,6 +185,15 @@ public void drawScreen(Minecraft minecraft, int mouseX, int mouseY, float partia
this.configButton.draw(minecraft, mouseX, mouseY, partialTicks);
}

public boolean updateGuiExclusionAreas() {
final Set<Rectangle> guiAreas = getGuiAreas();
if (!guiAreas.equals(this.guiExclusionAreas)) {
this.guiExclusionAreas = guiAreas;
return true;
}
return false;
}

public void drawTooltips(Minecraft minecraft, int mouseX, int mouseY) {
this.contents.drawTooltips(minecraft, mouseX, mouseY);
this.configButton.drawTooltips(minecraft, mouseX, mouseY);
Expand Down Expand Up @@ -214,10 +233,10 @@ public boolean hasPrevious() {

@Override
public boolean isMouseOver(int mouseX, int mouseY) {
return this.navigation.isMouseOver(mouseX, mouseY) ||
this.contents.isMouseOver(mouseX, mouseY) ||
this.searchField.isMouseOver(mouseX, mouseY) ||
this.configButton.isMouseOver(mouseX, mouseY);
if (displayArea.contains(mouseX, mouseY) || searchField.isMouseOver(mouseX, mouseY) || configButton.isMouseOver(mouseX, mouseY)) {
return !MathUtil.contains(guiExclusionAreas, mouseX, mouseY);
}
return false;
}

@Override
Expand Down Expand Up @@ -368,4 +387,24 @@ public ImmutableList<Object> getVisibleIngredients() {

return visibleIngredients.build();
}

private static Set<Rectangle> getGuiAreas() {
final GuiScreen currentScreen = Minecraft.getMinecraft().currentScreen;
if (currentScreen instanceof GuiContainer) {
final GuiContainer guiContainer = (GuiContainer) currentScreen;
final JeiRuntime jeiRuntime = Internal.getRuntime();
if (jeiRuntime != null) {
final Set<Rectangle> allGuiExtraAreas = new HashSet<>();
final List<IAdvancedGuiHandler<GuiContainer>> activeAdvancedGuiHandlers = jeiRuntime.getActiveAdvancedGuiHandlers(guiContainer);
for (IAdvancedGuiHandler<GuiContainer> advancedGuiHandler : activeAdvancedGuiHandlers) {
final List<Rectangle> guiExtraAreas = advancedGuiHandler.getGuiExtraAreas(guiContainer);
if (guiExtraAreas != null) {
allGuiExtraAreas.addAll(guiExtraAreas);
}
}
return allGuiExtraAreas;
}
}
return Collections.emptySet();
}
}
21 changes: 21 additions & 0 deletions src/main/java/mezz/jei/util/MathUtil.java
@@ -1,5 +1,8 @@
package mezz.jei.util;

import java.awt.Rectangle;
import java.util.Collection;

public final class MathUtil {
private MathUtil() {

Expand All @@ -19,4 +22,22 @@ public static int clamp(int value, int min, int max) {
return value;
}
}

public static boolean intersects(Collection<Rectangle> areas, Rectangle comparisonArea) {
for (Rectangle area : areas) {
if (area.intersects(comparisonArea)) {
return true;
}
}
return false;
}

public static boolean contains(Collection<Rectangle> areas, int x, int y) {
for (Rectangle guiArea : areas) {
if (guiArea.contains(x, y)) {
return true;
}
}
return false;
}
}

0 comments on commit 9fd1680

Please sign in to comment.