/
GuiIngredient.java
181 lines (152 loc) · 5.63 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package mezz.jei.gui.ingredients;
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;
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 javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.awt.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
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;
@Nonnull
private final List<T> displayIngredients = new ArrayList<>(); // ingredients, taking focus into account
@Nonnull
private final List<T> allIngredients = new ArrayList<>(); // all ingredients, ignoring focus
@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, int itemCycleOffset) {
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;
this.cycleTimer = new CycleTimer(itemCycleOffset);
}
@Override
public void clear() {
enabled = false;
displayIngredients.clear();
}
@Override
public boolean isMouseOver(int xOffset, int yOffset, int mouseX, int mouseY) {
return enabled && (mouseX >= xOffset + xPosition) && (mouseY >= yOffset + yPosition) && (mouseX < xOffset + xPosition + width) && (mouseY < yOffset + yPosition + height);
}
@Nullable
public T getIngredient() {
return cycleTimer.getCycledItem(displayIngredients);
}
@Override
public Focus getFocus() {
T ingredient = getIngredient();
if (ingredient == null) {
return null;
}
return ingredientHelper.createFocus(ingredient);
}
@Nonnull
@Override
public List<T> getAllIngredients() {
return allIngredients;
}
@Override
public void set(@Nonnull T ingredient, @Nonnull Focus focus) {
set(Collections.singleton(ingredient), focus);
}
@Override
public void set(@Nonnull Collection<T> ingredients, @Nonnull Focus focus) {
this.displayIngredients.clear();
this.allIngredients.clear();
ingredients = ingredientHelper.expandSubtypes(ingredients);
T match = null;
if ((isInput() && focus.getMode() == Focus.Mode.INPUT) || (!isInput() && focus.getMode() == Focus.Mode.OUTPUT)) {
match = ingredientHelper.getMatch(ingredients, focus);
}
if (match != null) {
this.displayIngredients.add(match);
} else {
this.displayIngredients.addAll(ingredients);
}
this.ingredientRenderer.setIngredients(ingredients);
this.allIngredients.addAll(ingredients);
enabled = !this.displayIngredients.isEmpty();
}
public void setTooltipCallback(@Nullable ITooltipCallback<T> tooltipCallback) {
this.tooltipCallback = tooltipCallback;
}
@Override
public void draw(@Nonnull Minecraft minecraft, int xOffset, int yOffset) {
cycleTimer.onDraw();
T value = getIngredient();
ingredientRenderer.draw(minecraft, xOffset + xPosition + padding, yOffset + yPosition + padding, value);
}
@Override
public void drawHovered(@Nonnull Minecraft minecraft, int xOffset, int yOffset, int mouseX, int mouseY) {
T value = getIngredient();
if (value == null) {
return;
}
draw(minecraft, xOffset, yOffset);
drawTooltip(minecraft, xOffset, yOffset, mouseX, mouseY, value);
}
@Override
public void drawHighlight(@Nonnull Minecraft minecraft, Color color, int xOffset, int yOffset) {
int x = xPosition + xOffset + padding;
int y = yPosition + yOffset + padding;
GlStateManager.disableLighting();
drawRect(x, y, x + width - padding * 2, y + height - padding * 2, color.getRGB());
GlStateManager.color(1f, 1f, 1f, 1f);
}
private void drawTooltip(@Nonnull Minecraft minecraft, int xOffset, int yOffset, int mouseX, int mouseY, @Nonnull T value) {
try {
GlStateManager.disableDepth();
RenderHelper.disableStandardItemLighting();
drawRect(xOffset + xPosition + padding,
yOffset + yPosition + padding,
xOffset + xPosition + width - padding,
yOffset + yPosition + height - padding,
0x7FFFFFFF);
GlStateManager.color(1f, 1f, 1f, 1f);
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, xOffset + mouseX, yOffset + mouseY, fontRenderer);
GlStateManager.enableDepth();
} catch (RuntimeException e) {
Log.error("Exception when rendering tooltip on {}.", value, e);
}
}
@Override
public boolean isInput() {
return input;
}
}