Skip to content

Commit

Permalink
Simplify implementation of recipe categories. Add lots of helper meth…
Browse files Browse the repository at this point in the history
…ods for widgets
  • Loading branch information
mezz committed Sep 27, 2024
1 parent 7b71ce6 commit 5142066
Show file tree
Hide file tree
Showing 48 changed files with 1,133 additions and 849 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package mezz.jei.common.gui.elements;

import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.api.gui.drawable.IDrawableAnimated;
import net.minecraft.client.gui.GuiGraphics;

import java.util.List;

public class DrawableCombined implements IDrawableAnimated {
private final List<IDrawable> drawables;
private final int width;
private final int height;

public DrawableCombined(IDrawable... drawables) {
this(List.of(drawables));
}

public DrawableCombined(List<IDrawable> drawables) {
IDrawable first = drawables.getFirst();
this.width = first.getWidth();
this.height = first.getHeight();
for (int i = 1; i < drawables.size(); i++) {
IDrawable drawable = drawables.get(i);
if (drawable.getWidth() != width || drawable.getHeight() != height) {
throw new IllegalArgumentException("Drawables must have the same width and height. Expected " + width + " x " + height + " but got " + drawable.getWidth() + " x " + drawable.getHeight());
}
}
this.drawables = drawables;
}

@Override
public int getWidth() {
return width;
}

@Override
public int getHeight() {
return height;
}

@Override
public void draw(GuiGraphics guiGraphics) {
for (IDrawable drawable : drawables) {
drawable.draw(guiGraphics);
}
}

@Override
public void draw(GuiGraphics guiGraphics, int xOffset, int yOffset) {
for (IDrawable drawable : drawables) {
drawable.draw(guiGraphics, xOffset, yOffset);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import mezz.jei.api.gui.drawable.IDrawable;
import mezz.jei.common.util.StringUtil;
import mezz.jei.core.util.Pair;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -21,8 +22,10 @@ public class DrawableWrappedText implements IDrawable {

public DrawableWrappedText(List<FormattedText> text, int maxWidth) {
Minecraft minecraft = Minecraft.getInstance();
this.lineHeight = minecraft.font.lineHeight + lineSpacing;
this.descriptionLines = StringUtil.splitLines(text, maxWidth);
Font font = minecraft.font;
this.lineHeight = font.lineHeight + lineSpacing;
Pair<List<FormattedText>, Boolean> result = StringUtil.splitLines(font, text, maxWidth, Integer.MAX_VALUE);
this.descriptionLines = result.first();
this.width = maxWidth;
this.height = lineHeight * descriptionLines.size() - lineSpacing;
}
Expand Down
185 changes: 185 additions & 0 deletions Common/src/main/java/mezz/jei/common/gui/elements/TextWidget.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package mezz.jei.common.gui.elements;

import mezz.jei.api.gui.builder.ITooltipBuilder;
import mezz.jei.api.gui.widgets.IRecipeWidget;
import mezz.jei.api.gui.widgets.ITextWidget;
import mezz.jei.common.config.DebugConfig;
import mezz.jei.common.util.HorizontalAlignment;
import mezz.jei.common.util.ImmutableRect2i;
import mezz.jei.common.util.StringUtil;
import mezz.jei.common.util.VerticalAlignment;
import mezz.jei.core.util.Pair;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.navigation.ScreenPosition;
import net.minecraft.locale.Language;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.util.FormattedCharSequence;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class TextWidget implements ITextWidget, IRecipeWidget {
private final List<FormattedText> text;
private final ImmutableRect2i area;

private HorizontalAlignment horizontalAlignment;
private VerticalAlignment verticalAlignment;
private Font font;
private int color;
private boolean shadow;
private int lineSpacing;
private List<FormattedText> tooltipText = List.of();
private @Nullable List<FormattedText> wrappedText;

public TextWidget(List<FormattedText> text, int xPos, int yPos, int maxWidth, int maxHeight) {
this.area = new ImmutableRect2i(xPos, yPos, maxWidth, maxHeight);
Minecraft minecraft = Minecraft.getInstance();
this.font = minecraft.font;
this.color = 0xFF000000;
this.text = text;
this.lineSpacing = 2;
this.horizontalAlignment = HorizontalAlignment.LEFT;
this.verticalAlignment = VerticalAlignment.TOP;
}

@Override
public ITextWidget alignHorizontalLeft() {
this.horizontalAlignment = HorizontalAlignment.LEFT;
return this;
}

@Override
public ITextWidget alignHorizontalRight() {
this.horizontalAlignment = HorizontalAlignment.RIGHT;
return this;
}

@Override
public ITextWidget alignHorizontalCenter() {
this.horizontalAlignment = HorizontalAlignment.CENTER;
return this;
}

@Override
public ITextWidget alignVerticalTop() {
this.verticalAlignment = VerticalAlignment.TOP;
return this;
}

@Override
public ITextWidget alignVerticalCenter() {
this.verticalAlignment = VerticalAlignment.CENTER;
return this;
}

@Override
public ITextWidget alignVerticalBottom() {
this.verticalAlignment = VerticalAlignment.BOTTOM;
return this;
}

@Override
public ITextWidget setFont(Font font) {
this.font = font;
return this;
}

@Override
public ITextWidget setColor(int color) {
this.color = color;
return this;
}

@Override
public ITextWidget setLineSpacing(int lineSpacing) {
this.lineSpacing = lineSpacing;
return this;
}

@Override
public ITextWidget setShadow(boolean shadow) {
this.shadow = shadow;
return this;
}

@Override
public ScreenPosition getPosition() {
return area.getScreenPosition();
}

private List<FormattedText> calculateWrappedText() {
if (wrappedText != null) {
return wrappedText;
}
int lineHeight = getLineHeight();
int maxLines = area.height() / lineHeight;
if (maxLines * lineHeight + font.lineHeight <= area.height()) {
maxLines++;
}
Pair<List<FormattedText>, Boolean> result = StringUtil.splitLines(font, text, area.width(), maxLines);
this.wrappedText = result.first();
boolean truncated = result.second();
if (truncated) {
this.tooltipText = text;
} else {
this.tooltipText = List.of();
}
return wrappedText;
}

private int getLineHeight() {
return font.lineHeight + lineSpacing;
}

@Override
public void drawWidget(GuiGraphics guiGraphics, double mouseX, double mouseY) {
Language language = Language.getInstance();

final int lineHeight = getLineHeight();
List<FormattedText> lines = calculateWrappedText();
int yPos = getYPosStart(lineHeight, lines);
for (FormattedText line : lines) {
FormattedCharSequence charSequence = language.getVisualOrder(line);
int xPos = getXPos(charSequence);
guiGraphics.drawString(font, charSequence, xPos, yPos, color, shadow);
yPos += lineHeight;
}

if (DebugConfig.isDebugGuisEnabled()) {
guiGraphics.fill(0,0, area.width(), area.height(), 0xAAAAAA00);
}
}

@Override
public void getTooltip(ITooltipBuilder tooltip, double mouseX, double mouseY) {
if (mouseX >= 0 && mouseX < area.width() && mouseY >= 0 && mouseY < area.height()) {
calculateWrappedText();
tooltip.addAll(tooltipText);
}
}

private int getXPos(FormattedCharSequence text) {
return switch (horizontalAlignment) {
case LEFT -> 0;
case RIGHT -> this.area.width() - font.width(text);
case CENTER -> Math.round((this.area.width() - font.width(text)) / 2f);
};
}

private int getYPosStart(int lineHeight, List<FormattedText> text) {
if (verticalAlignment == VerticalAlignment.TOP) {
return 0;
}

int linesHeight = (lineHeight * text.size()) - lineSpacing - 1;
if (verticalAlignment == VerticalAlignment.BOTTOM) {
return area.height() - linesHeight;
} else if (verticalAlignment == VerticalAlignment.CENTER) {
return Math.round((area.height() - linesHeight) / 2f);
} else {
throw new IllegalArgumentException("Unknown verticalAlignment " + verticalAlignment);
}
}
}
4 changes: 2 additions & 2 deletions Common/src/main/java/mezz/jei/common/util/MathUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.client.gui.Font;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.renderer.Rect2i;
import net.minecraft.network.chat.FormattedText;
import net.minecraft.util.FormattedCharSequence;
import net.minecraft.world.phys.Vec2;
import org.joml.Matrix4f;
import org.joml.Vector3f;
Expand Down Expand Up @@ -87,7 +87,7 @@ public static ImmutableRect2i centerTextArea(ImmutableRect2i outer, Font fontRen
return centerArea(outer, width, height);
}

public static ImmutableRect2i centerTextArea(ImmutableRect2i outer, Font fontRenderer, FormattedText text) {
public static ImmutableRect2i centerTextArea(ImmutableRect2i outer, Font fontRenderer, FormattedCharSequence text) {
int width = fontRenderer.width(text);
int height = fontRenderer.lineHeight;
return centerArea(outer, width, height);
Expand Down
Loading

0 comments on commit 5142066

Please sign in to comment.