Move 9-slice setup into DrawableBuilder (#1081)

This commit is contained in:
slprime authored and GitHub committed 2026-10-16 23:18:28 +03:00
1 parent ab168ad6ba
commit 7163630cb1
9 files changed
+128 -81

No files matched your search

@@ -102,20 +102,20 @@ public class GuiCraftingTree extends GuiContainer implements INEIGuiHandler, IGu
}
}
private static final int MARGIN = 4;
private static final int PADDING = 9;
private static final int PADDING_TOP = 22;
private static final DrawableResource BG_TEXTURE = new DrawableBuilder(
"nei:textures/gui/craftingtree.png",
0,
0,
64,
64).build();
64).setSlices(PADDING, PADDING, PADDING_TOP, PADDING).build();
private static final DateTimeFormatter SCREENSHOT_DATE_FORMAT = DateTimeFormatter
.ofPattern("yyyy-MM-dd_HH.mm.ss", Locale.ROOT);
private static final int MARGIN = 4;
private static final int PADDING = 9;
private static final int PADDING_TOP = 22;
private final LRUCache<String, String> handlerTitleCache = new LRUCache<>(100);
private GuiScreen prevGui;
@@ -338,15 +338,7 @@ public class GuiCraftingTree extends GuiContainer implements INEIGuiHandler, IGu
this.hoveredNode = canHover && hoveredBadgeHandler == null ? this.canvas.getNodeMouseOver(getMousePosition())
: null;
BG_TEXTURE.draw(
MARGIN,
MARGIN,
this.width - MARGIN * 2,
this.height - MARGIN * 2,
PADDING,
PADDING,
PADDING_TOP,
PADDING);
BG_TEXTURE.draw(MARGIN, MARGIN, this.width - MARGIN * 2, this.height - MARGIN * 2);
if (this.math.outputRecipes.isEmpty()) {
drawCenteredString(
@@ -242,7 +242,7 @@ class StatsPanel extends ScrollContainer {
26,
84,
46,
16).build();
16).setSlices(0, 0, 2, 0).build();
private static final DrawableResource SLOT_DEFAULT = new DrawableBuilder(
"nei:textures/gui/craftingtree.png",
@@ -290,8 +290,8 @@ class StatsPanel extends ScrollContainer {
public StatsPanel() {
final ScrollBar scrollbar = new ScrollBar().setTrackWidth(4)
.setThumbTexture(new DrawableBuilder("nei:textures/gui/craftingtree.png", 26, 100, 24, 4).build(), 0, 0)
.setTrackTexture(new DrawableBuilder("nei:textures/gui/craftingtree.png", 26, 104, 24, 4).build(), 0, 0)
.setThumbTexture(new DrawableBuilder("nei:textures/gui/craftingtree.png", 26, 100, 24, 4).build())
.setTrackTexture(new DrawableBuilder("nei:textures/gui/craftingtree.png", 26, 104, 24, 4).build())
.setTrackPadding(1, -5, 1, 0).setOverflowType(ScrollBar.OverflowType.AUTO);
setHorizontalScroll(scrollbar);
@@ -589,7 +589,7 @@ class StatsPanel extends ScrollContainer {
RenderHelper.enableGUIStandardItemLighting();
GL11.glDisable(GL11.GL_LIGHTING);
BG_TEXTURE.draw(this.x, this.y, this.w, this.h, 0, 0, 2, 0);
BG_TEXTURE.draw(this.x, this.y, this.w, this.h);
super.draw(mouseX, mouseY);
@@ -15,6 +15,10 @@ public class DrawableBuilder {
private int paddingBottom = 0;
private int paddingLeft = 0;
private int paddingRight = 0;
private int sliceLeft = 0;
private int sliceRight = 0;
private int sliceTop = 0;
private int sliceBottom = 0;
public DrawableBuilder(String resourceLocation, int u, int v, int width, int height) {
this.u = u;
@@ -38,6 +42,14 @@ public class DrawableBuilder {
return this;
}
public DrawableBuilder setSlices(int sliceLeft, int sliceRight, int sliceTop, int sliceBottom) {
this.sliceLeft = sliceLeft;
this.sliceRight = sliceRight;
this.sliceTop = sliceTop;
this.sliceBottom = sliceBottom;
return this;
}
public DrawableBuilder trim(int trimTop, int trimBottom, int trimLeft, int trimRight) {
this.u += trimLeft;
this.v += trimTop;
@@ -58,6 +70,10 @@ public class DrawableBuilder {
paddingLeft,
paddingRight,
textureWidth,
textureHeight);
textureHeight,
sliceLeft,
sliceRight,
sliceTop,
sliceBottom);
}
}
@@ -19,9 +19,42 @@ public class DrawableResource extends Image {
private final int paddingLeft;
private final int paddingRight;
private final int sliceLeft;
private final int sliceRight;
private final int sliceTop;
private final int sliceBottom;
public DrawableResource(ResourceLocation resourceLocation, int u, int v, int width, int height, int paddingTop,
int paddingBottom, int paddingLeft, int paddingRight, int textureWidth, int textureHeight) {
this(
resourceLocation,
u,
v,
width,
height,
paddingTop,
paddingBottom,
paddingLeft,
paddingRight,
textureWidth,
textureHeight,
0,
0,
0,
0);
}
// negative padding lets the texture overflow the box it is drawn into
public DrawableResource(ResourceLocation resourceLocation, int u, int v, int width, int height, int paddingTop,
int paddingBottom, int paddingLeft, int paddingRight, int textureWidth, int textureHeight, int sliceLeft,
int sliceRight, int sliceTop, int sliceBottom) {
super(u, v, width, height);
this.sliceLeft = sliceLeft;
this.sliceRight = sliceRight;
this.sliceTop = sliceTop;
this.sliceBottom = sliceBottom;
this.resourceLocation = resourceLocation;
this.textureWidth = textureWidth;
this.textureHeight = textureHeight;
@@ -59,14 +92,21 @@ public class DrawableResource extends Image {
if (!is2DTexture) GL11.glDisable(GL11.GL_TEXTURE_2D);
}
public void draw(int xOffset, int yOffset, int width, int height, int sliceLeft, int sliceRight, int sliceTop,
int sliceBottom) {
final int textureWidth = getWidth();
final int textureHeight = getHeight();
final int middleWidth = textureWidth - sliceLeft - sliceRight;
final int middleHeight = textureHeight - sliceTop - sliceBottom;
final int tileWidth = width - sliceLeft - sliceRight;
final int tileHeight = height - sliceTop - sliceBottom;
public void draw(int xOffset, int yOffset, int width, int height) {
if (width == getWidth() && height == getHeight()) {
draw(xOffset, yOffset);
return;
}
final int sliceLeft = this.sliceLeft;
final int sliceRight = this.sliceRight;
final int sliceTop = this.sliceTop;
final int sliceBottom = this.sliceBottom;
final int middleWidth = this.width - sliceLeft - sliceRight;
final int middleHeight = this.height - sliceTop - sliceBottom;
final int tileWidth = width - this.paddingLeft - this.paddingRight - sliceLeft - sliceRight;
final int tileHeight = height - this.paddingTop - this.paddingBottom - sliceTop - sliceBottom;
if (middleWidth > 0 && middleHeight > 0 && tileWidth > 0 && tileHeight > 0) {
final boolean is2DTexture = GL11.glGetBoolean(GL11.GL_TEXTURE_2D);
@@ -71,15 +71,23 @@ public abstract class GuiRecipe<H extends IRecipeHandler> extends GuiContainer i
0,
0,
176 + TRANSPARENCY_BORDER * 2,
166 + TRANSPARENCY_BORDER * 2).build();
166 + TRANSPARENCY_BORDER * 2)
.setSlices(
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER)
.build();
private static final int BUTTON_WIDTH = 13;
private static final int BUTTON_HEIGHT = 12;
protected static final ScrollBar VERTICAL_SCROLLBAR = new ScrollBar().setTrackWidth(14)
.setOverflowType(OverflowType.AUTO).setScrollPlace(ScrollPlace.END)
.setTrackTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 42, 90, 13, 21).build(), 9, 9)
.setThumbTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 22, 96, 6, 9).build(), 3, 2)
.setTrackTexture(
new DrawableBuilder("nei:textures/nei_sprites.png", 42, 90, 13, 21).setSlices(0, 0, 9, 9).build())
.setThumbTexture(
new DrawableBuilder("nei:textures/nei_sprites.png", 22, 96, 6, 9).setSlices(0, 0, 3, 2).build())
.setThumbPadding(1, 6, 0, 6);
public static final List<IRecipeFilterProvider> recipeFilterers = new LinkedList<>();
@@ -958,11 +966,7 @@ public abstract class GuiRecipe<H extends IRecipeHandler> extends GuiContainer i
this.guiLeft - TRANSPARENCY_BORDER,
this.guiTop - TRANSPARENCY_BORDER,
this.xSize + TRANSPARENCY_BORDER * 2,
this.ySize + TRANSPARENCY_BORDER * 2,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER);
this.ySize + TRANSPARENCY_BORDER * 2);
drawJEITabs(mouseX, mouseY);
}
@@ -55,11 +55,17 @@ public class GuiRecipeCatalyst extends ScrollContainer {
0,
0,
28 + TRANSPARENCY_BORDER * 2,
28 + TRANSPARENCY_BORDER * 2).setTextureSize(28 + TRANSPARENCY_BORDER * 2, 28 + TRANSPARENCY_BORDER * 2)
28 + TRANSPARENCY_BORDER * 2)
.setTextureSize(28 + TRANSPARENCY_BORDER * 2, 28 + TRANSPARENCY_BORDER * 2)
.setSlices(
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER)
.build();
private static final DrawableResource FG_TEXTURE = new DrawableBuilder("nei:textures/slot.png", 0, 0, 18, 18)
.setTextureSize(18, 18).build();
.setTextureSize(18, 18).setSlices(1, 1, 1, 1).build();
private static final ScrollBar VERTICAL_SCROLLBAR = ScrollBar.defaultVerticalBar()
.setOverflowType(ScrollBar.OverflowType.AUTO).setTrackPadding(0, BORDER_PADDING, -13, BORDER_PADDING)
@@ -155,20 +161,12 @@ public class GuiRecipeCatalyst extends ScrollContainer {
this.x - TRANSPARENCY_BORDER,
this.y - TRANSPARENCY_BORDER,
this.w + TRANSPARENCY_BORDER * 2,
this.h + TRANSPARENCY_BORDER * 2,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER);
this.h + TRANSPARENCY_BORDER * 2);
FG_TEXTURE.draw(
this.x + this.paddingInlineStart - 1,
this.y + this.paddingBlockStart - 1,
this.w - this.paddingInlineStart - this.paddingInlineEnd + 2,
this.h - this.paddingBlockStart - this.paddingBlockEnd + 2,
1,
1,
1,
1);
this.h - this.paddingBlockStart - this.paddingBlockEnd + 2);
super.draw(mx, my);
}
@@ -25,7 +25,13 @@ public class RecipeTooltipLineHandler implements ITooltipLineHandler {
0,
0,
176 + TRANSPARENCY_BORDER * 2,
166 + TRANSPARENCY_BORDER * 2).build();
166 + TRANSPARENCY_BORDER * 2)
.setSlices(
BG_PADDING + TRANSPARENCY_BORDER,
BG_PADDING + TRANSPARENCY_BORDER,
BG_PADDING + TRANSPARENCY_BORDER,
BG_PADDING + TRANSPARENCY_BORDER)
.build();
protected long lastUpdate = System.currentTimeMillis();
protected String recipeName = "";
@@ -105,11 +111,7 @@ public class RecipeTooltipLineHandler implements ITooltipLineHandler {
-TRANSPARENCY_BORDER,
-TRANSPARENCY_BORDER,
size.width + TRANSPARENCY_BORDER * 2,
size.height + TRANSPARENCY_BORDER * 2,
BG_PADDING + TRANSPARENCY_BORDER,
BG_PADDING + TRANSPARENCY_BORDER,
BG_PADDING + TRANSPARENCY_BORDER,
BG_PADDING + TRANSPARENCY_BORDER);
size.height + TRANSPARENCY_BORDER * 2);
GL11.glEnable(GL11.GL_ALPHA_TEST);
@@ -170,7 +170,13 @@ public class DebugHandlerWidget extends Widget implements IContainerTooltipHandl
0,
0,
176 + TRANSPARENCY_BORDER * 2,
166 + TRANSPARENCY_BORDER * 2).build();
166 + TRANSPARENCY_BORDER * 2)
.setSlices(
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER)
.build();
private static final int BORDER_PADDING = 6;
private static final int TRANSPARENCY_BORDER = 4;
@@ -379,11 +385,7 @@ public class DebugHandlerWidget extends Widget implements IContainerTooltipHandl
this.x - TRANSPARENCY_BORDER,
this.y - TRANSPARENCY_BORDER,
this.w + TRANSPARENCY_BORDER * 2,
this.h + TRANSPARENCY_BORDER * 2,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER,
BORDER_PADDING + TRANSPARENCY_BORDER);
this.h + TRANSPARENCY_BORDER * 2);
GuiDraw.drawRect(
this.x + INLINE_PADDING,
this.y + BORDER_PADDING,
@@ -53,28 +53,26 @@ public class ScrollBar {
private int trackPaddingBottom = 0;
private DrawableResource thumbTexture;
private int thumbSliceStart;
private int thumbSliceEnd;
private DrawableResource trackTexture;
private int trackSliceStart;
private int trackSliceEnd;
public static ScrollBar defaultScrollBar() {
return new ScrollBar();
}
public static ScrollBar defaultVerticalBar() {
return new ScrollBar().setTrackWidth(8)
.setTrackTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 29, 95, 8, 11).build(), 3, 4)
.setThumbTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 22, 96, 6, 9).build(), 3, 2)
return new ScrollBar().setTrackWidth(8).setTrackTexture(
new DrawableBuilder("nei:textures/nei_sprites.png", 29, 95, 8, 11).setSlices(0, 0, 3, 4).build())
.setThumbTexture(
new DrawableBuilder("nei:textures/nei_sprites.png", 22, 96, 6, 9).setSlices(0, 0, 3, 2).build())
.setThumbPadding(1, 1, 1, 1);
}
public static ScrollBar defaultHorizontalBar() {
return new ScrollBar().setTrackWidth(8).setOverflowType(OverflowType.SCROLL)
.setTrackTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 0, 97, 11, 8).build(), 3, 4)
.setThumbTexture(new DrawableBuilder("nei:textures/nei_sprites.png", 12, 98, 9, 6).build(), 3, 2)
.setTrackTexture(
new DrawableBuilder("nei:textures/nei_sprites.png", 0, 97, 11, 8).setSlices(3, 4, 0, 0).build())
.setThumbTexture(
new DrawableBuilder("nei:textures/nei_sprites.png", 12, 98, 9, 6).setSlices(3, 2, 0, 0).build())
.setThumbPadding(1, 1, 1, 1);
}
@@ -126,17 +124,13 @@ public class ScrollBar {
return getTrackWidth();
}
public ScrollBar setThumbTexture(DrawableResource texture, int sliceStart, int sliceEnd) {
public ScrollBar setThumbTexture(DrawableResource texture) {
this.thumbTexture = texture;
this.thumbSliceStart = sliceStart;
this.thumbSliceEnd = sliceEnd;
return this;
}
public ScrollBar setTrackTexture(DrawableResource texture, int sliceStart, int sliceEnd) {
public ScrollBar setTrackTexture(DrawableResource texture) {
this.trackTexture = texture;
this.trackSliceStart = sliceStart;
this.trackSliceEnd = sliceEnd;
return this;
}
@@ -379,13 +373,13 @@ public class ScrollBar {
final boolean disabled = getVisibleSize(container) >= getActualSize(container);
if (this.trackTexture != null) {
drawTexture(this.trackTexture, this.trackSliceStart, this.trackSliceEnd, trackBounds, false, opacity);
drawTexture(this.trackTexture, trackBounds, false, opacity);
} else {
drawTrackRect(container, trackBounds, opacity);
}
if (this.thumbTexture != null) {
drawTexture(this.thumbTexture, this.thumbSliceStart, this.thumbSliceEnd, thumbBounds, disabled, opacity);
drawTexture(this.thumbTexture, thumbBounds, disabled, opacity);
} else {
drawThumbRect(container, thumbBounds, selected, opacity);
}
@@ -417,8 +411,7 @@ public class ScrollBar {
NEIClientUtils.drawRect(bounds.x, bounds.y, bounds.w, bounds.h, thumbColor);
}
private void drawTexture(DrawableResource texture, int sliceStart, int sliceEnd, Rectangle4i bounds,
boolean disabled, float opacity) {
private void drawTexture(DrawableResource texture, Rectangle4i bounds, boolean disabled, float opacity) {
final float c = disabled ? 0.65f : 1f;
GL11.glColor4f(c, c, c, opacity);
@@ -426,9 +419,9 @@ public class ScrollBar {
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
if (this.scrollType == ScrollType.HORIZONTAL) {
texture.draw(bounds.x, bounds.y, bounds.w, texture.getHeight(), sliceStart, sliceEnd, 0, 0);
texture.draw(bounds.x, bounds.y, bounds.w, texture.getHeight());
} else {
texture.draw(bounds.x, bounds.y, texture.getWidth(), bounds.h, 0, 0, sliceStart, sliceEnd);
texture.draw(bounds.x, bounds.y, texture.getWidth(), bounds.h);
}
}