/
GuiIngredient.java
163 lines (134 loc) · 4.64 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package mezz.jei.gui.ingredients;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.Color;
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.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.RenderHelper;
import mezz.jei.api.gui.ITooltipCallback;
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 slotIndex;
private final boolean input;
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;
@Nullable
private ITooltipCallback<T> tooltipCallback;
private boolean enabled;
public GuiIngredient(@Nonnull IIngredientRenderer<T> ingredientRenderer, @Nonnull IIngredientHelper<T> ingredientHelper, int slotIndex, boolean input, int xPosition, int yPosition, int width, int height, int padding) {
this.ingredientRenderer = ingredientRenderer;
this.ingredientHelper = ingredientHelper;
this.slotIndex = slotIndex;
this.input = input;
this.xPosition = xPosition;
this.yPosition = yPosition;
this.width = width;
this.height = height;
this.padding = padding;
}
@Override
public void clear() {
enabled = false;
contained.clear();
}
@Override
public boolean isMouseOver(int mouseX, int mouseY) {
return enabled && (mouseX >= xPosition) && (mouseY >= yPosition) && (mouseX < xPosition + width) && (mouseY < yPosition + height);
}
@Override
@Nullable
public T get() {
return cycleTimer.getCycledItem(contained);
}
@Nonnull
@Override
public List<T> getAll() {
return 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 = null;
if ((isInput() && focus.getMode() == Focus.Mode.INPUT) || (!isInput() && focus.getMode() == Focus.Mode.OUTPUT)) {
match = ingredientHelper.getMatch(contained, focus);
}
if (match != null) {
this.contained.add(match);
} else {
this.contained.addAll(contained);
}
enabled = !this.contained.isEmpty();
}
public void setTooltipCallback(@Nonnull ITooltipCallback<T> tooltipCallback) {
this.tooltipCallback = tooltipCallback;
}
@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);
}
@Override
public void drawHighlight(@Nonnull Minecraft minecraft, Color color, int xOffset, int yOffset) {
int x = xPosition + xOffset;
int y = yPosition + yOffset;
GuiScreen.drawRect(x, y, x + width, y + height, color.getRGB());
}
private void draw(Minecraft minecraft, boolean cycleIcons) {
cycleTimer.onDraw(cycleIcons);
T value = get();
ingredientRenderer.draw(minecraft, xPosition + padding, yPosition + padding, value);
}
private void drawTooltip(@Nonnull Minecraft minecraft, int mouseX, int mouseY, @Nonnull T value) {
try {
GlStateManager.disableDepth();
RenderHelper.disableStandardItemLighting();
drawRect(xPosition, yPosition, xPosition + width, yPosition + height, 0x7FFFFFFF);
List<String> tooltip = ingredientRenderer.getTooltip(minecraft, value);
if (tooltipCallback != null) {
tooltipCallback.onTooltip(slotIndex, input, value, tooltip);
}
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 {}.", value, e);
}
}
public boolean isInput() {
return input;
}
}