/
GuiIngredient.java
137 lines (113 loc) · 3.83 KB
/
GuiIngredient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package mezz.jei.gui.ingredients;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.client.gui.Gui;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import org.lwjgl.opengl.GL11;
import mezz.jei.gui.Focus;
import mezz.jei.gui.TooltipRenderer;
import mezz.jei.util.CycleTimer;
import mezz.jei.util.Log;
public class GuiIngredient<T> extends Gui implements IGuiIngredient<T> {
private final int xPosition;
private final int yPosition;
private final int width;
private final int height;
private final int padding;
@Nonnull
private final CycleTimer cycleTimer = new CycleTimer();
@Nonnull
private final List<T> contained = new ArrayList<>();
@Nonnull
private final IIngredientRenderer<T> ingredientRenderer;
@Nonnull
private final IIngredientHelper<T> ingredientHelper;
private boolean enabled;
private boolean visible;
public GuiIngredient(@Nonnull IIngredientRenderer<T> ingredientRenderer, @Nonnull IIngredientHelper<T> ingredientHelper, int xPosition, int yPosition, int width, int height, int padding) {
this.ingredientRenderer = ingredientRenderer;
this.ingredientHelper = ingredientHelper;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.width = width;
this.height = height;
this.padding = padding;
}
@Override
public void clear() {
visible = enabled = false;
contained.clear();
}
@Override
public boolean isMouseOver(int mouseX, int mouseY) {
return enabled && visible && (mouseX >= xPosition) && (mouseY >= yPosition) && (mouseX < xPosition + width) && (mouseY < yPosition + height);
}
@Override
@Nullable
public T get() {
return cycleTimer.getCycledItem(contained);
}
@Override
public void set(@Nonnull T contained, @Nonnull Focus focus) {
set(Collections.singleton(contained), focus);
}
@Override
public void set(@Nonnull Collection<T> contained, @Nonnull Focus focus) {
this.contained.clear();
contained = ingredientHelper.expandSubtypes(contained);
T match = ingredientHelper.getMatch(contained, focus);
if (match != null) {
this.contained.add(match);
} else {
this.contained.addAll(contained);
}
visible = enabled = !this.contained.isEmpty();
}
@Override
public void draw(@Nonnull Minecraft minecraft) {
draw(minecraft, true);
}
@Override
public void drawHovered(@Nonnull Minecraft minecraft, int mouseX, int mouseY) {
T value = get();
if (value == null) {
return;
}
draw(minecraft, false);
drawTooltip(minecraft, mouseX, mouseY, value);
}
private void draw(Minecraft minecraft, boolean cycleIcons) {
if (!visible) {
return;
}
cycleTimer.onDraw(cycleIcons);
T value = get();
if (value == null) {
return;
}
ingredientRenderer.draw(minecraft, xPosition + padding, yPosition + padding, value);
}
private void drawTooltip(@Nonnull Minecraft minecraft, int mouseX, int mouseY, @Nonnull T value) {
try {
GlStateManager.disableDepth();
this.zLevel = 0;
RenderHelper.disableStandardItemLighting();
GL11.glEnable(GL11.GL_BLEND);
drawRect(xPosition, yPosition, xPosition + width, yPosition + width, 0x7FFFFFFF);
this.zLevel = 0;
List<String> tooltip = ingredientRenderer.getTooltip(minecraft, value);
FontRenderer fontRenderer = ingredientRenderer.getFontRenderer(minecraft, value);
TooltipRenderer.drawHoveringText(minecraft, tooltip, mouseX, mouseY, fontRenderer);
GlStateManager.enableDepth();
} catch (RuntimeException e) {
Log.error("Exception when rendering tooltip on {}.\n{}", value, e);
}
}
}