Skip to content

Commit b7f27fd

Browse files
authored
Merge pull request #3787 from mezz/1.20.1-backport
Backport features from 1.21.1
2 parents 4fa7b98 + 64b14c4 commit b7f27fd

File tree

79 files changed

+2164
-1043
lines changed

Some content is hidden

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

79 files changed

+2164
-1043
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.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import mezz.jei.api.gui.drawable.IDrawableStatic;
66

77
public record DrawableBlank(int width, int height) implements IDrawableStatic, IDrawableAnimated {
8+
public static final DrawableBlank EMPTY = new DrawableBlank(0, 0);
9+
810
@Override
911
public int getWidth() {
1012
return width;
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.get(0);
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+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package mezz.jei.common.gui.elements;
2+
3+
import mezz.jei.api.gui.drawable.IDrawable;
4+
import mezz.jei.common.util.StringUtil;
5+
import mezz.jei.core.util.Pair;
6+
import net.minecraft.client.Minecraft;
7+
import net.minecraft.client.gui.Font;
8+
import net.minecraft.client.gui.GuiGraphics;
9+
import net.minecraft.locale.Language;
10+
import net.minecraft.network.chat.FormattedText;
11+
import net.minecraft.util.FormattedCharSequence;
12+
13+
import java.util.List;
14+
15+
public class DrawableWrappedText implements IDrawable {
16+
private static final int lineSpacing = 2;
17+
18+
private final List<FormattedText> descriptionLines;
19+
private final int lineHeight;
20+
private final int width;
21+
private final int height;
22+
23+
public DrawableWrappedText(List<FormattedText> text, int maxWidth) {
24+
Minecraft minecraft = Minecraft.getInstance();
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();
29+
this.width = maxWidth;
30+
this.height = lineHeight * descriptionLines.size() - lineSpacing;
31+
}
32+
33+
@Override
34+
public int getWidth() {
35+
return width;
36+
}
37+
38+
@Override
39+
public int getHeight() {
40+
return height;
41+
}
42+
43+
@Override
44+
public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset) {
45+
Language language = Language.getInstance();
46+
Minecraft minecraft = Minecraft.getInstance();
47+
Font font = minecraft.font;
48+
49+
int yPos = 0;
50+
for (FormattedText descriptionLine : descriptionLines) {
51+
FormattedCharSequence charSequence = language.getVisualOrder(descriptionLine);
52+
guiGraphics.drawString(font, charSequence, xOffset, yPos + yOffset, 0xFF000000, false);
53+
yPos += lineHeight;
54+
}
55+
}
56+
}

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

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package mezz.jei.common.gui.elements;
22

3-
import net.minecraft.client.gui.GuiGraphics;
43
import mezz.jei.api.gui.drawable.IDrawable;
4+
import mezz.jei.api.gui.placement.IPlaceable;
5+
import mezz.jei.common.util.ImmutableRect2i;
6+
import net.minecraft.client.gui.GuiGraphics;
57

68
/**
79
* Draws with a built-in offset.
810
*/
9-
public class OffsetDrawable implements IDrawable {
11+
public class OffsetDrawable implements IDrawable, IPlaceable<OffsetDrawable> {
1012
public static IDrawable create(IDrawable drawable, int xOffset, int yOffset) {
1113
if (xOffset == 0 && yOffset == 0) {
1214
return drawable;
@@ -15,10 +17,10 @@ public static IDrawable create(IDrawable drawable, int xOffset, int yOffset) {
1517
}
1618

1719
private final IDrawable drawable;
18-
private final int xOffset;
19-
private final int yOffset;
20+
private int xOffset;
21+
private int yOffset;
2022

21-
private OffsetDrawable(IDrawable drawable, int xOffset, int yOffset) {
23+
public OffsetDrawable(IDrawable drawable, int xOffset, int yOffset) {
2224
this.drawable = drawable;
2325
this.xOffset = xOffset;
2426
this.yOffset = yOffset;
@@ -47,4 +49,15 @@ public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset) {
4749
public void draw(GuiGraphics guiGraphics) {
4850
this.drawable.draw(guiGraphics, this.xOffset, this.yOffset);
4951
}
52+
53+
@Override
54+
public OffsetDrawable setPosition(int xPos, int yPos) {
55+
this.xOffset = xPos;
56+
this.yOffset = yPos;
57+
return this;
58+
}
59+
60+
public ImmutableRect2i getArea() {
61+
return new ImmutableRect2i(xOffset, yOffset, getWidth(), getHeight());
62+
}
5063
}
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package mezz.jei.common.gui.elements;
2+
3+
import mezz.jei.api.gui.builder.ITooltipBuilder;
4+
import mezz.jei.api.gui.placement.HorizontalAlignment;
5+
import mezz.jei.api.gui.placement.VerticalAlignment;
6+
import mezz.jei.api.gui.widgets.IRecipeWidget;
7+
import mezz.jei.api.gui.widgets.ITextWidget;
8+
import mezz.jei.common.config.DebugConfig;
9+
import mezz.jei.common.util.ImmutableRect2i;
10+
import mezz.jei.common.util.StringUtil;
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 ImmutableRect2i availableArea;
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+
34+
private @Nullable List<FormattedText> wrappedText;
35+
private boolean truncated = false;
36+
37+
public TextWidget(List<FormattedText> text, int xPos, int yPos, int maxWidth, int maxHeight) {
38+
this.availableArea = new ImmutableRect2i(xPos, yPos, maxWidth, maxHeight);
39+
Minecraft minecraft = Minecraft.getInstance();
40+
this.font = minecraft.font;
41+
this.color = 0xFF000000;
42+
this.text = text;
43+
this.lineSpacing = 2;
44+
this.horizontalAlignment = HorizontalAlignment.LEFT;
45+
this.verticalAlignment = VerticalAlignment.TOP;
46+
}
47+
48+
private void invalidateCachedValues() {
49+
wrappedText = null;
50+
truncated = false;
51+
}
52+
53+
@Override
54+
public int getWidth() {
55+
return availableArea.width();
56+
}
57+
58+
@Override
59+
public int getHeight() {
60+
return availableArea.height();
61+
}
62+
63+
@Override
64+
public TextWidget setPosition(int xPos, int yPos) {
65+
this.availableArea = this.availableArea.setPosition(xPos, yPos);
66+
invalidateCachedValues();
67+
return this;
68+
}
69+
70+
@Override
71+
public TextWidget setTextAlignment(HorizontalAlignment horizontalAlignment) {
72+
if (this.horizontalAlignment.equals(horizontalAlignment)) {
73+
return this;
74+
}
75+
this.horizontalAlignment = horizontalAlignment;
76+
invalidateCachedValues();
77+
return this;
78+
}
79+
80+
@Override
81+
public TextWidget setTextAlignment(VerticalAlignment verticalAlignment) {
82+
if (this.verticalAlignment.equals(verticalAlignment)) {
83+
return this;
84+
}
85+
this.verticalAlignment = verticalAlignment;
86+
invalidateCachedValues();
87+
return this;
88+
}
89+
90+
@Override
91+
public ITextWidget setFont(Font font) {
92+
this.font = font;
93+
invalidateCachedValues();
94+
return this;
95+
}
96+
97+
@Override
98+
public ITextWidget setColor(int color) {
99+
this.color = color;
100+
invalidateCachedValues();
101+
return this;
102+
}
103+
104+
@Override
105+
public ITextWidget setLineSpacing(int lineSpacing) {
106+
this.lineSpacing = lineSpacing;
107+
invalidateCachedValues();
108+
return this;
109+
}
110+
111+
@Override
112+
public ITextWidget setShadow(boolean shadow) {
113+
this.shadow = shadow;
114+
invalidateCachedValues();
115+
return this;
116+
}
117+
118+
@Override
119+
public ScreenPosition getPosition() {
120+
return availableArea.getScreenPosition();
121+
}
122+
123+
private List<FormattedText> calculateWrappedText() {
124+
if (wrappedText != null) {
125+
return wrappedText;
126+
}
127+
int lineHeight = getLineHeight();
128+
int maxLines = availableArea.height() / lineHeight;
129+
if (maxLines * lineHeight + font.lineHeight <= availableArea.height()) {
130+
maxLines++;
131+
}
132+
Pair<List<FormattedText>, Boolean> result = StringUtil.splitLines(font, text, availableArea.width(), maxLines);
133+
this.wrappedText = result.first();
134+
this.truncated = result.second();
135+
return wrappedText;
136+
}
137+
138+
private int getLineHeight() {
139+
return font.lineHeight + lineSpacing;
140+
}
141+
142+
@Override
143+
public void drawWidget(GuiGraphics guiGraphics, double mouseX, double mouseY) {
144+
Language language = Language.getInstance();
145+
146+
final int lineHeight = getLineHeight();
147+
List<FormattedText> lines = calculateWrappedText();
148+
int yPos = getYPosStart(lineHeight, lines);
149+
for (FormattedText line : lines) {
150+
FormattedCharSequence charSequence = language.getVisualOrder(line);
151+
int xPos = getXPos(charSequence);
152+
guiGraphics.drawString(font, charSequence, xPos, yPos, color, shadow);
153+
yPos += lineHeight;
154+
}
155+
156+
if (DebugConfig.isDebugGuisEnabled()) {
157+
guiGraphics.fill(0,0, availableArea.width(), availableArea.height(), 0xAAAAAA00);
158+
}
159+
}
160+
161+
@Override
162+
public void getTooltip(ITooltipBuilder tooltip, double mouseX, double mouseY) {
163+
if (mouseX >= 0 && mouseX < availableArea.width() && mouseY >= 0 && mouseY < availableArea.height()) {
164+
calculateWrappedText();
165+
if (truncated) {
166+
tooltip.addAll(text);
167+
}
168+
}
169+
}
170+
171+
private int getXPos(FormattedCharSequence text) {
172+
return getXPos(font.width(text));
173+
}
174+
175+
private int getXPos(int lineWidth) {
176+
return horizontalAlignment.getXPos(this.availableArea.width(), lineWidth);
177+
}
178+
179+
private int getYPosStart(int lineHeight, List<FormattedText> text) {
180+
int linesHeight = (lineHeight * text.size()) - lineSpacing - 1;
181+
return verticalAlignment.getYPos(this.availableArea.height(), linesHeight);
182+
}
183+
}

0 commit comments

Comments
 (0)