Skip to content

Commit

Permalink
use crunched util method
Browse files Browse the repository at this point in the history
  • Loading branch information
sisby-folk committed Jan 5, 2024
1 parent 4bf61fe commit e1b41a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/main/java/folk/sisby/inventory_tabs/tabs/Tab.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package folk.sisby.inventory_tabs.tabs;

import folk.sisby.inventory_tabs.util.DrawUtil;
import folk.sisby.inventory_tabs.util.WidgetPosition;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
Expand All @@ -16,11 +17,13 @@
public interface Tab {
Identifier TABS_TEXTURE = new Identifier("textures/gui/container/creative_inventory/tabs.png");
int TAB_TEXTURE_WIDTH = 26;
int TAB_TEXTURE_HEIGHT = 32;
int TAB_TEXTURE_HEIGHT_SELECTED = 32;
int TAB_TEXTURE_HEIGHT_UNSELECTED = 30;
int TAB_TEXTURE_U = 26;
int TAB_TEXTURE_V_UNSELECTED = 2;
int TAB_TEXTURE_V_UNSELECTED_INVERTED = 64;
int TAB_TEXTURE_V_SELECTED = 32;
int TAB_TEXTURE_V_INVERTED_OFFSET = 64;
int TAB_TEXTURE_V_SELECTED_INVERTED = 96;

/**
* Opens the screen associated with the tab.
Expand Down Expand Up @@ -62,12 +65,12 @@ default int getPriority() {

default void renderBackground(DrawContext drawContext, WidgetPosition pos, int width, int height, boolean current) {
int y = pos.y + (pos.up ? -height + 4 : height - 4);
if (!current) drawContext.drawNineSlicedTexture(TABS_TEXTURE, pos.x, y, width, height, 6, TAB_TEXTURE_WIDTH, TAB_TEXTURE_HEIGHT, TAB_TEXTURE_U, TAB_TEXTURE_V_UNSELECTED + (pos.up ? 0 : TAB_TEXTURE_V_INVERTED_OFFSET));
if (!current) DrawUtil.drawCrunched(drawContext, TABS_TEXTURE, pos.x, y, width, height, TAB_TEXTURE_WIDTH, TAB_TEXTURE_HEIGHT_UNSELECTED, TAB_TEXTURE_U, pos.up ? TAB_TEXTURE_V_UNSELECTED : TAB_TEXTURE_V_UNSELECTED_INVERTED);
}

default void renderForeground(DrawContext drawContext, WidgetPosition pos, int width, int height, double mouseX, double mouseY, boolean current) {
int y = pos.y + (pos.up ? -height + 4 : height - 4);
if (current) drawContext.drawNineSlicedTexture(TABS_TEXTURE, pos.x, y, width, height, 6, TAB_TEXTURE_WIDTH, TAB_TEXTURE_HEIGHT, TAB_TEXTURE_U, TAB_TEXTURE_V_SELECTED + (pos.up ? 0 : TAB_TEXTURE_V_INVERTED_OFFSET));
if (current) DrawUtil.drawCrunched(drawContext, TABS_TEXTURE, pos.x, y, width, height, TAB_TEXTURE_WIDTH, TAB_TEXTURE_HEIGHT_SELECTED, TAB_TEXTURE_U, pos.up ? TAB_TEXTURE_V_SELECTED : TAB_TEXTURE_V_SELECTED_INVERTED);
int itemPadding = Math.max(0, (width - 16) / 2);
drawContext.drawItem(getTabIcon(), pos.x + itemPadding, y + itemPadding);
if (new Rect2i(pos.x + itemPadding, y + itemPadding, 16, 16).contains((int) mouseX, (int) mouseY)) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/folk/sisby/inventory_tabs/util/DrawUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package folk.sisby.inventory_tabs.util;

import net.minecraft.client.gui.DrawContext;
import net.minecraft.util.Identifier;

public class DrawUtil {
public static void drawCrunched(DrawContext context, Identifier texture, int x, int y, int width, int height, int sourceWidth, int sourceHeight, int u, int v) {
int leftWidth = width / 2 + (width % 2);
int topHeight = height / 2 + (height % 2);
int rightWidth = width - leftWidth;
int bottomHeight = height - topHeight;

context.drawTexture(texture, x, y, u, v, leftWidth, topHeight); // Top Left
context.drawTexture(texture, x + leftWidth, y, u + sourceWidth - rightWidth, v, rightWidth, topHeight); // Top Right
context.drawTexture(texture, x, y + topHeight, u, v + sourceHeight - bottomHeight, leftWidth, bottomHeight); // Bottom Left
context.drawTexture(texture, x + leftWidth, y + topHeight, u + sourceWidth - rightWidth, v + sourceHeight - bottomHeight, rightWidth, bottomHeight); // Bottom Right
}
}

0 comments on commit e1b41a9

Please sign in to comment.