Skip to content

Commit 5142066

Browse files
committed
Simplify implementation of recipe categories. Add lots of helper methods for widgets
1 parent 7b71ce6 commit 5142066

File tree

48 files changed

+1133
-849
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1133
-849
lines changed

Common/src/main/java/mezz/jei/common/gui/elements/DrawableAnimatedRecipeArrow.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

Common/src/main/java/mezz/jei/common/gui/elements/DrawableAnimatedRecipeFlame.java

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package mezz.jei.common.gui.elements;
2+
3+
import mezz.jei.api.gui.drawable.IDrawable;
4+
import mezz.jei.api.gui.drawable.IDrawableAnimated;
5+
import net.minecraft.client.gui.GuiGraphics;
6+
7+
import java.util.List;
8+
9+
public class DrawableCombined implements IDrawableAnimated {
10+
private final List<IDrawable> drawables;
11+
private final int width;
12+
private final int height;
13+
14+
public DrawableCombined(IDrawable... drawables) {
15+
this(List.of(drawables));
16+
}
17+
18+
public DrawableCombined(List<IDrawable> drawables) {
19+
IDrawable first = drawables.getFirst();
20+
this.width = first.getWidth();
21+
this.height = first.getHeight();
22+
for (int i = 1; i < drawables.size(); i++) {
23+
IDrawable drawable = drawables.get(i);
24+
if (drawable.getWidth() != width || drawable.getHeight() != height) {
25+
throw new IllegalArgumentException("Drawables must have the same width and height. Expected " + width + " x " + height + " but got " + drawable.getWidth() + " x " + drawable.getHeight());
26+
}
27+
}
28+
this.drawables = drawables;
29+
}
30+
31+
@Override
32+
public int getWidth() {
33+
return width;
34+
}
35+
36+
@Override
37+
public int getHeight() {
38+
return height;
39+
}
40+
41+
@Override
42+
public void draw(GuiGraphics guiGraphics) {
43+
for (IDrawable drawable : drawables) {
44+
drawable.draw(guiGraphics);
45+
}
46+
}
47+
48+
@Override
49+
public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset) {
50+
for (IDrawable drawable : drawables) {
51+
drawable.draw(guiGraphics, xOffset, yOffset);
52+
}
53+
}
54+
}

Common/src/main/java/mezz/jei/common/gui/elements/DrawableWrappedText.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import mezz.jei.api.gui.drawable.IDrawable;
44
import mezz.jei.common.util.StringUtil;
5+
import mezz.jei.core.util.Pair;
56
import net.minecraft.client.Minecraft;
67
import net.minecraft.client.gui.Font;
78
import net.minecraft.client.gui.GuiGraphics;
@@ -21,8 +22,10 @@ public class DrawableWrappedText implements IDrawable {
2122

2223
public DrawableWrappedText(List<FormattedText> text, int maxWidth) {
2324
Minecraft minecraft = Minecraft.getInstance();
24-
this.lineHeight = minecraft.font.lineHeight + lineSpacing;
25-
this.descriptionLines = StringUtil.splitLines(text, maxWidth);
25+
Font font = minecraft.font;
26+
this.lineHeight = font.lineHeight + lineSpacing;
27+
Pair<List<FormattedText>, Boolean> result = StringUtil.splitLines(font, text, maxWidth, Integer.MAX_VALUE);
28+
this.descriptionLines = result.first();
2629
this.width = maxWidth;
2730
this.height = lineHeight * descriptionLines.size() - lineSpacing;
2831
}
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
package mezz.jei.common.gui.elements;
2+
3+
import mezz.jei.api.gui.builder.ITooltipBuilder;
4+
import mezz.jei.api.gui.widgets.IRecipeWidget;
5+
import mezz.jei.api.gui.widgets.ITextWidget;
6+
import mezz.jei.common.config.DebugConfig;
7+
import mezz.jei.common.util.HorizontalAlignment;
8+
import mezz.jei.common.util.ImmutableRect2i;
9+
import mezz.jei.common.util.StringUtil;
10+
import mezz.jei.common.util.VerticalAlignment;
11+
import mezz.jei.core.util.Pair;
12+
import net.minecraft.client.Minecraft;
13+
import net.minecraft.client.gui.Font;
14+
import net.minecraft.client.gui.GuiGraphics;
15+
import net.minecraft.client.gui.navigation.ScreenPosition;
16+
import net.minecraft.locale.Language;
17+
import net.minecraft.network.chat.FormattedText;
18+
import net.minecraft.util.FormattedCharSequence;
19+
import org.jetbrains.annotations.Nullable;
20+
21+
import java.util.List;
22+
23+
public class TextWidget implements ITextWidget, IRecipeWidget {
24+
private final List<FormattedText> text;
25+
private final ImmutableRect2i area;
26+
27+
private HorizontalAlignment horizontalAlignment;
28+
private VerticalAlignment verticalAlignment;
29+
private Font font;
30+
private int color;
31+
private boolean shadow;
32+
private int lineSpacing;
33+
private List<FormattedText> tooltipText = List.of();
34+
private @Nullable List<FormattedText> wrappedText;
35+
36+
public TextWidget(List<FormattedText> text, int xPos, int yPos, int maxWidth, int maxHeight) {
37+
this.area = new ImmutableRect2i(xPos, yPos, maxWidth, maxHeight);
38+
Minecraft minecraft = Minecraft.getInstance();
39+
this.font = minecraft.font;
40+
this.color = 0xFF000000;
41+
this.text = text;
42+
this.lineSpacing = 2;
43+
this.horizontalAlignment = HorizontalAlignment.LEFT;
44+
this.verticalAlignment = VerticalAlignment.TOP;
45+
}
46+
47+
@Override
48+
public ITextWidget alignHorizontalLeft() {
49+
this.horizontalAlignment = HorizontalAlignment.LEFT;
50+
return this;
51+
}
52+
53+
@Override
54+
public ITextWidget alignHorizontalRight() {
55+
this.horizontalAlignment = HorizontalAlignment.RIGHT;
56+
return this;
57+
}
58+
59+
@Override
60+
public ITextWidget alignHorizontalCenter() {
61+
this.horizontalAlignment = HorizontalAlignment.CENTER;
62+
return this;
63+
}
64+
65+
@Override
66+
public ITextWidget alignVerticalTop() {
67+
this.verticalAlignment = VerticalAlignment.TOP;
68+
return this;
69+
}
70+
71+
@Override
72+
public ITextWidget alignVerticalCenter() {
73+
this.verticalAlignment = VerticalAlignment.CENTER;
74+
return this;
75+
}
76+
77+
@Override
78+
public ITextWidget alignVerticalBottom() {
79+
this.verticalAlignment = VerticalAlignment.BOTTOM;
80+
return this;
81+
}
82+
83+
@Override
84+
public ITextWidget setFont(Font font) {
85+
this.font = font;
86+
return this;
87+
}
88+
89+
@Override
90+
public ITextWidget setColor(int color) {
91+
this.color = color;
92+
return this;
93+
}
94+
95+
@Override
96+
public ITextWidget setLineSpacing(int lineSpacing) {
97+
this.lineSpacing = lineSpacing;
98+
return this;
99+
}
100+
101+
@Override
102+
public ITextWidget setShadow(boolean shadow) {
103+
this.shadow = shadow;
104+
return this;
105+
}
106+
107+
@Override
108+
public ScreenPosition getPosition() {
109+
return area.getScreenPosition();
110+
}
111+
112+
private List<FormattedText> calculateWrappedText() {
113+
if (wrappedText != null) {
114+
return wrappedText;
115+
}
116+
int lineHeight = getLineHeight();
117+
int maxLines = area.height() / lineHeight;
118+
if (maxLines * lineHeight + font.lineHeight <= area.height()) {
119+
maxLines++;
120+
}
121+
Pair<List<FormattedText>, Boolean> result = StringUtil.splitLines(font, text, area.width(), maxLines);
122+
this.wrappedText = result.first();
123+
boolean truncated = result.second();
124+
if (truncated) {
125+
this.tooltipText = text;
126+
} else {
127+
this.tooltipText = List.of();
128+
}
129+
return wrappedText;
130+
}
131+
132+
private int getLineHeight() {
133+
return font.lineHeight + lineSpacing;
134+
}
135+
136+
@Override
137+
public void drawWidget(GuiGraphics guiGraphics, double mouseX, double mouseY) {
138+
Language language = Language.getInstance();
139+
140+
final int lineHeight = getLineHeight();
141+
List<FormattedText> lines = calculateWrappedText();
142+
int yPos = getYPosStart(lineHeight, lines);
143+
for (FormattedText line : lines) {
144+
FormattedCharSequence charSequence = language.getVisualOrder(line);
145+
int xPos = getXPos(charSequence);
146+
guiGraphics.drawString(font, charSequence, xPos, yPos, color, shadow);
147+
yPos += lineHeight;
148+
}
149+
150+
if (DebugConfig.isDebugGuisEnabled()) {
151+
guiGraphics.fill(0,0, area.width(), area.height(), 0xAAAAAA00);
152+
}
153+
}
154+
155+
@Override
156+
public void getTooltip(ITooltipBuilder tooltip, double mouseX, double mouseY) {
157+
if (mouseX >= 0 && mouseX < area.width() && mouseY >= 0 && mouseY < area.height()) {
158+
calculateWrappedText();
159+
tooltip.addAll(tooltipText);
160+
}
161+
}
162+
163+
private int getXPos(FormattedCharSequence text) {
164+
return switch (horizontalAlignment) {
165+
case LEFT -> 0;
166+
case RIGHT -> this.area.width() - font.width(text);
167+
case CENTER -> Math.round((this.area.width() - font.width(text)) / 2f);
168+
};
169+
}
170+
171+
private int getYPosStart(int lineHeight, List<FormattedText> text) {
172+
if (verticalAlignment == VerticalAlignment.TOP) {
173+
return 0;
174+
}
175+
176+
int linesHeight = (lineHeight * text.size()) - lineSpacing - 1;
177+
if (verticalAlignment == VerticalAlignment.BOTTOM) {
178+
return area.height() - linesHeight;
179+
} else if (verticalAlignment == VerticalAlignment.CENTER) {
180+
return Math.round((area.height() - linesHeight) / 2f);
181+
} else {
182+
throw new IllegalArgumentException("Unknown verticalAlignment " + verticalAlignment);
183+
}
184+
}
185+
}

Common/src/main/java/mezz/jei/common/util/MathUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import net.minecraft.client.gui.Font;
44
import net.minecraft.client.gui.navigation.ScreenRectangle;
55
import net.minecraft.client.renderer.Rect2i;
6-
import net.minecraft.network.chat.FormattedText;
6+
import net.minecraft.util.FormattedCharSequence;
77
import net.minecraft.world.phys.Vec2;
88
import org.joml.Matrix4f;
99
import org.joml.Vector3f;
@@ -87,7 +87,7 @@ public static ImmutableRect2i centerTextArea(ImmutableRect2i outer, Font fontRen
8787
return centerArea(outer, width, height);
8888
}
8989

90-
public static ImmutableRect2i centerTextArea(ImmutableRect2i outer, Font fontRenderer, FormattedText text) {
90+
public static ImmutableRect2i centerTextArea(ImmutableRect2i outer, Font fontRenderer, FormattedCharSequence text) {
9191
int width = fontRenderer.width(text);
9292
int height = fontRenderer.lineHeight;
9393
return centerArea(outer, width, height);

0 commit comments

Comments
 (0)