Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for GuiScreen and for dragging ghost items
- Loading branch information
Showing
27 changed files
with
747 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,4 @@ curse_project_id=238222 | |
|
||
version_major=4 | ||
version_minor=8 | ||
version_patch=3 | ||
version_patch=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/api/java/mezz/jei/api/gui/IGhostIngredientHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package mezz.jei.api.gui; | ||
|
||
import net.minecraft.client.gui.GuiScreen; | ||
|
||
import java.awt.Rectangle; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
/** | ||
* Lets mods accept ghost ingredients from JEI. | ||
* These ingredients are dragged from the ingredient list on to your gui, and are useful | ||
* for setting recipes or anything else that does not need the real ingredient to exist. | ||
* @since JEI 4.8.4 | ||
*/ | ||
public interface IGhostIngredientHandler<T extends GuiScreen> { | ||
/** | ||
* Called when a player wants to drag an ingredient on to your gui. | ||
* Return the targets that can accept the ingredient. | ||
* | ||
* This is called when a player hovers over an ingredient with doStart=false, | ||
* and called again when they pick up the ingredient with doStart=true. | ||
*/ | ||
<I> List<Target<I>> getTargets(T gui, I ingredient, boolean doStart); | ||
|
||
/** | ||
* Called when the player is done dragging an ingredient. | ||
* If the drag succeeded, {@link Target#accept(Object)} was called before this. | ||
* Otherwise, the player failed to drag an ingredient to a {@link Target}. | ||
*/ | ||
void onComplete(); | ||
|
||
/** | ||
* @return true if JEI should highlight the targets for the player. | ||
* false to handle highlighting yourself. | ||
*/ | ||
default boolean shouldHighlightTargets() { | ||
return true; | ||
} | ||
|
||
interface Target<I> extends Consumer<I> { | ||
/** | ||
* @return the area (in screen coordinates) where the ingredient can be dropped. | ||
*/ | ||
Rectangle getArea(); | ||
|
||
/** | ||
* Called with the ingredient when it is dropped on the target. | ||
*/ | ||
@Override | ||
void accept(I ingredient); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mezz.jei.api.gui; | ||
|
||
import net.minecraft.client.gui.GuiScreen; | ||
|
||
/** | ||
* Defines the properties of a gui so that JEI can draw next to it. | ||
* @since JEI 4.8.4 | ||
*/ | ||
public interface IGuiProperties { | ||
Class<? extends GuiScreen> getGuiClass(); | ||
int getGuiLeft(); | ||
int getGuiTop(); | ||
int getGuiXSize(); | ||
int getGuiYSize(); | ||
int getScreenWidth(); | ||
int getScreenHeight(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package mezz.jei.api.gui; | ||
|
||
import mezz.jei.api.IModRegistry; | ||
import net.minecraft.client.gui.GuiScreen; | ||
import net.minecraft.client.gui.inventory.GuiContainer; | ||
|
||
import java.util.function.Function; | ||
|
||
/** | ||
* Creates {@link IGuiProperties} from a {@link GuiScreen} so JEI can draw next to it. | ||
* By default, JEI already handles this for all {@link GuiContainer}. | ||
* Register a {@link IGuiScreenHandler} with JEI by using {@link IModRegistry#addGuiScreenHandler(Class, IGuiScreenHandler)} | ||
* @since JEI 4.8.4 | ||
*/ | ||
@FunctionalInterface | ||
public interface IGuiScreenHandler<T extends GuiScreen> extends Function<T, IGuiProperties> { | ||
@Override | ||
IGuiProperties apply(T guiScreen); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/main/java/mezz/jei/gui/ghost/GhostIngredientDrag.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package mezz.jei.gui.ghost; | ||
|
||
import mezz.jei.api.gui.IGhostIngredientHandler; | ||
import mezz.jei.api.gui.IGhostIngredientHandler.Target; | ||
import mezz.jei.api.ingredients.IIngredientRenderer; | ||
import net.minecraft.client.Minecraft; | ||
import net.minecraft.client.gui.Gui; | ||
import net.minecraft.client.renderer.GlStateManager; | ||
import net.minecraft.client.renderer.RenderItem; | ||
import org.lwjgl.opengl.GL11; | ||
|
||
import javax.annotation.Nullable; | ||
import java.awt.Color; | ||
import java.awt.Rectangle; | ||
import java.util.List; | ||
|
||
public class GhostIngredientDrag<T> { | ||
private static final Color targetColor = new Color(19, 201, 10, 64); | ||
private static final Color hoverColor = new Color(76, 201, 25, 128); | ||
|
||
private final IGhostIngredientHandler<?> handler; | ||
private final List<Target<T>> targets; | ||
private final IIngredientRenderer<T> ingredientRenderer; | ||
private final T ingredient; | ||
@Nullable | ||
private final Rectangle origin; | ||
|
||
public GhostIngredientDrag(IGhostIngredientHandler<?> handler, List<Target<T>> targets, IIngredientRenderer<T> ingredientRenderer, T ingredient, @Nullable Rectangle origin) { | ||
this.handler = handler; | ||
this.targets = targets; | ||
this.ingredientRenderer = ingredientRenderer; | ||
this.ingredient = ingredient; | ||
this.origin = origin; | ||
} | ||
|
||
public void drawTargets(int mouseX, int mouseY) { | ||
if (handler.shouldHighlightTargets()) { | ||
@SuppressWarnings("unchecked") | ||
List<Target<Object>> targets = (List<Target<Object>>) (Object) this.targets; | ||
drawTargets(mouseX, mouseY, targets); | ||
} | ||
} | ||
|
||
public void drawItem(Minecraft minecraft, int mouseX, int mouseY) { | ||
if (origin != null) { | ||
int originX = origin.x + (origin.width / 2); | ||
int originY = origin.y + (origin.height / 2); | ||
int xDist = originX - mouseX; | ||
int yDist = originY - mouseY; | ||
float lineWidth = 2; | ||
if (minecraft.currentScreen != null) { | ||
long distanceSq = xDist * xDist + yDist * yDist; | ||
int screenDim = minecraft.currentScreen.width * minecraft.currentScreen.height; | ||
float percentOfDim = Math.min(1, distanceSq / (float) screenDim); | ||
lineWidth = 1 + ((1 - (percentOfDim)) * 3); | ||
} | ||
GL11.glDisable(GL11.GL_TEXTURE_2D); | ||
GlStateManager.disableDepth(); | ||
GL11.glLineWidth(lineWidth); | ||
GL11.glEnable(GL11.GL_LINE_SMOOTH); | ||
GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST); | ||
GL11.glBegin(GL11.GL_LINES); | ||
GL11.glColor4f(targetColor.getRed() / 255f, targetColor.getGreen() / 255f, targetColor.getBlue() / 255f, targetColor.getAlpha() / 255f); | ||
GL11.glVertex3f(mouseX, mouseY, 150); | ||
GL11.glVertex3f(originX, originY, 150); | ||
GL11.glEnd(); | ||
GlStateManager.enableDepth(); | ||
GL11.glEnable(GL11.GL_TEXTURE_2D); | ||
} | ||
|
||
RenderItem renderItem = minecraft.getRenderItem(); | ||
renderItem.zLevel += 150.0F; | ||
ingredientRenderer.render(minecraft, mouseX - 8, mouseY - 8, ingredient); | ||
renderItem.zLevel -= 150.0F; | ||
} | ||
|
||
public static void drawTargets(int mouseX, int mouseY, List<Target<Object>> targets) { | ||
GlStateManager.disableLighting(); | ||
GlStateManager.disableDepth(); | ||
for (Target target : targets) { | ||
Rectangle area = target.getArea(); | ||
Color color; | ||
if (area.contains(mouseX, mouseY)) { | ||
color = hoverColor; | ||
} else { | ||
color = targetColor; | ||
} | ||
Gui.drawRect(area.x, area.y, area.x + area.width, area.y + area.height, color.getRGB()); | ||
} | ||
GlStateManager.color(1f, 1f, 1f, 1f); | ||
} | ||
|
||
public boolean onClick(int mouseX, int mouseY) { | ||
for (Target<T> target : targets) { | ||
Rectangle area = target.getArea(); | ||
if (area.contains(mouseX, mouseY)) { | ||
target.accept(ingredient); | ||
handler.onComplete(); | ||
return true; | ||
} | ||
} | ||
handler.onComplete(); | ||
return false; | ||
} | ||
|
||
public IIngredientRenderer<T> getIngredientRenderer() { | ||
return ingredientRenderer; | ||
} | ||
|
||
public T getIngredient() { | ||
return ingredient; | ||
} | ||
|
||
@Nullable | ||
public Rectangle getOrigin() { | ||
return origin; | ||
} | ||
} |
Oops, something went wrong.