Skip to content

Commit bdeb387

Browse files
committed
Cleanup and fix spacing on header rendering
1 parent c2ce6c8 commit bdeb387

3 files changed

Lines changed: 63 additions & 62 deletions

File tree

src/main/java/turing/tmb/client/TMBRenderer.java

Lines changed: 59 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@
3535
@Environment(EnvType.CLIENT)
3636
public class TMBRenderer {
3737
public static int currentPage = 0;
38-
public static int pages = 0;
38+
public static int pageCount = 0;
3939
public static int currentFavouritePage = 0;
40-
public static int favouritePages = 0;
40+
public static int favouritePageCount = 0;
4141
public static boolean show = true;
4242
public static boolean enabledRecipes = true;
4343
protected static boolean initialized;
4444
protected static final ButtonElement leftButton = new ButtonElement(0, 0, 0, 16, 16, "<");
4545
protected static final ButtonElement rightButton = new ButtonElement(1, 0, 0, 16, 16, ">");
46-
protected static final ButtonElement leftButton2 = new ButtonElement(0, 0, 0, 16, 16, "<");
47-
protected static final ButtonElement rightButton2 = new ButtonElement(1, 0, 0, 16, 16, ">");
46+
protected static final ButtonElement favoritesLeftButton = new ButtonElement(0, 0, 0, 16, 16, "<");
47+
protected static final ButtonElement favoritesRightButton = new ButtonElement(1, 0, 0, 16, 16, ">");
48+
protected static final int buttonWidth = leftButton.width + 2;
4849
public static TextFieldElement search;
4950
protected static TooltipElement tooltip;
5051
private static ITypedIngredient<?> hoveredItem = null;
@@ -55,56 +56,56 @@ public static void init(Minecraft mc) {
5556

5657
tooltip = new TooltipElement(mc);
5758
search = new TextFieldElement(null, Minecraft.getMinecraft().font, 0, 0, 120, 20, TMBOptions.lastTMBSearch.value, "Search...");
58-
show = !TMBOptions.isTMBHidden.value;//((IKeybinds) Minecraft.getMinecraft().gameSettings).toomanyblocks$getIsTMBHidden().value;
59-
enabledRecipes = TMBOptions.isRecipeViewEnabled.value;//((IKeybinds) Minecraft.getMinecraft().gameSettings).toomanyblocks$getIsRecipeViewEnabled().value;
59+
show = !TMBOptions.isTMBHidden.value;
60+
enabledRecipes = TMBOptions.isRecipeViewEnabled.value;
6061
}
6162

62-
public static void renderHeader(int mouseX, int mouseY, int width, int height, Minecraft mc, float pt, @Nullable IGuiProperties properties) {
63-
if (!show || !initialized) return;
63+
protected static void renderHeader(ButtonElement leftButton, ButtonElement rightButton, int currentPage, int pageCount, boolean favorites, int mouseX, int mouseY, int width, int height, Minecraft mc, @Nullable IGuiProperties properties) {
6464
int w = (int) (width / 3.5F);
6565
Screen currentScreen = mc.currentScreen;
66-
if (currentScreen instanceof ScreenContainerAbstract) {
67-
w = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - 16, w);
66+
if (currentScreen instanceof ScreenContainerAbstract screen) {
67+
w = Math.min(((width / 2) - (screen.xSize / 2)) - 16, w);
6868
} else if (properties != null) {
69-
w = Math.min(((width / 2) - properties.guiXSize() / 2) - 16, w);
69+
w = Math.min(((width / 2) - (properties.guiXSize() / 2)) - 16, w);
7070
}
71-
int startX = width - w;
71+
int minDistance = buttonWidth + buttonWidth + 50;
72+
int startX = favorites ? 0 : width - w;
7273
int startY = 4;
73-
String str = "Page " + (currentPage + 1) + " / " + (pages + 1);
74+
String str = "Page " + (currentPage + 1) + " / " + (pageCount + 1);
7475

75-
leftButton.xPosition = startX;
7676
leftButton.yPosition = startY;
77-
rightButton.xPosition = Math.min(startX + (18 * (w / 18)), width - 18);
7877
rightButton.yPosition = startY;
79-
mc.font.render(str, ((((leftButton.xPosition + leftButton.width) + (rightButton.xPosition + rightButton.width)) / 2) - mc.font.stringWidth(str) / 2) - 4, startY + 4).setColor(0xFFFFFF).call();
78+
int rightStartX = startX + (buttonWidth * (w / buttonWidth));
79+
if (favorites) {
80+
leftButton.xPosition = startX;
81+
rightButton.xPosition = Math.max(rightStartX, leftButton.xPosition + minDistance);
82+
} else {
83+
leftButton.xPosition = startX;
84+
rightButton.xPosition = Math.min(rightStartX, width - buttonWidth);
85+
leftButton.xPosition = Math.min(startX, rightButton.xPosition - minDistance);
86+
}
87+
int diff = (((rightButton.xPosition) - (leftButton.xPosition + buttonWidth)) / 2);
88+
mc.font.render(str, (leftButton.xPosition + buttonWidth + diff) - (mc.font.stringWidth(str) / 2), startY + 4).setColor(0xFFFFFF).call();
8089
leftButton.drawButton(mc, mouseX, mouseY);
8190
rightButton.drawButton(mc, mouseX, mouseY);
8291

83-
search.yPosition = height - search.height - 4;
84-
search.xPosition = width / 2 - search.width / 2;
85-
search.drawTextBox();
92+
if (!favorites) {
93+
search.xPosition = width / 2 - search.width / 2;
94+
search.yPosition = height - search.height - 4;
95+
search.drawTextBox();
96+
}
8697
}
8798

88-
public static void renderHeader2(int mouseX, int mouseY, int width, int height, Minecraft mc, float pt, @Nullable IGuiProperties properties) {
99+
public static void renderHeader(boolean favorites, int mouseX, int mouseY, int width, int height, Minecraft mc, @Nullable IGuiProperties properties) {
89100
if (!show || !initialized) return;
90-
int w = (int) (width / 3.5F);
91-
Screen currentScreen = mc.currentScreen;
92-
if (currentScreen instanceof ScreenContainerAbstract) {
93-
w = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - 16, w);
94-
} else if (properties != null) {
95-
w = Math.min(((width / 2) - properties.guiXSize() / 2) - 16, w);
96-
}
97-
int startX = 0;//width - w;
98-
int startY = 4;
99-
String str = "Page " + (currentFavouritePage + 1) + " / " + (favouritePages + 1);
100-
101-
leftButton2.xPosition = startX;
102-
leftButton2.yPosition = startY;
103-
rightButton2.xPosition = Math.min(startX + (18 * (w / 18)), width - 18);
104-
rightButton2.yPosition = startY;
105-
mc.font.render(str, ((((leftButton2.xPosition + leftButton2.width) + (rightButton2.xPosition + rightButton2.width)) / 2) - mc.font.stringWidth(str) / 2) - 4, startY + 4).setColor(0xFFFFFF).call();
106-
leftButton2.drawButton(mc, mouseX, mouseY);
107-
rightButton2.drawButton(mc, mouseX, mouseY);
101+
if (favorites && TMB.getRuntime().getFavourites().isEmpty()) return;
102+
renderHeader(
103+
favorites ? favoritesLeftButton : leftButton,
104+
favorites ? favoritesRightButton : rightButton,
105+
favorites ? currentFavouritePage : currentPage,
106+
favorites ? favouritePageCount : pageCount,
107+
favorites, mouseX, mouseY, width, height, mc, properties
108+
);
108109
}
109110

110111
public static void onTick() {
@@ -127,21 +128,21 @@ public static void mouseClicked(int mouseX, int mouseY, int width, int height, M
127128
change = -1;
128129
}
129130
currentPage += change;
130-
if (currentPage < 0) currentPage = pages;
131-
if (currentPage > pages) currentPage = 0;
131+
if (currentPage < 0) currentPage = pageCount;
132+
if (currentPage > pageCount) currentPage = 0;
132133
}
133134

134-
boolean left2 = leftButton2.mouseClicked(mc, mouseX, mouseY);
135-
boolean right2 = rightButton2.mouseClicked(mc, mouseX, mouseY);
135+
boolean left2 = favoritesLeftButton.mouseClicked(mc, mouseX, mouseY);
136+
boolean right2 = favoritesRightButton.mouseClicked(mc, mouseX, mouseY);
136137
if (left2 || right2) {
137138
mc.sndManager.playSound("random.click", SoundCategory.GUI_SOUNDS, 1.0F, 1.0F);
138139
int change = 1;
139140
if (left2) {
140141
change = -1;
141142
}
142143
currentFavouritePage += change;
143-
if (currentFavouritePage < 0) currentFavouritePage = favouritePages;
144-
if (currentFavouritePage > favouritePages) currentFavouritePage = 0;
144+
if (currentFavouritePage < 0) currentFavouritePage = favouritePageCount;
145+
if (currentFavouritePage > favouritePageCount) currentFavouritePage = 0;
145146
}
146147

147148
if(search == null) return;
@@ -157,8 +158,8 @@ public static void keyTyped(char c, int key, int mouseX, int mouseY) {
157158
private static void scroll(int direction) {
158159
int change = MathHelper.clamp(-direction, -1, 1);
159160
currentPage += change;
160-
if (currentPage < 0) currentPage = pages;
161-
if (currentPage > pages) currentPage = 0;
161+
if (currentPage < 0) currentPage = pageCount;
162+
if (currentPage > pageCount) currentPage = 0;
162163
}
163164

164165
@SuppressWarnings("unchecked")
@@ -178,21 +179,21 @@ public static void renderItems(int mouseX, int mouseY, int width, int height, Mi
178179
int startX = (int) (width / 3.5F);
179180

180181
if (currentScreen instanceof ScreenContainerAbstract) {
181-
startX = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - 18, startX);
182+
startX = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - buttonWidth, startX);
182183
} else if (properties != null) {
183-
startX = Math.min(((width / 2) - properties.guiXSize() / 2) - 18, startX);
184+
startX = Math.min(((width / 2) - properties.guiXSize() / 2) - buttonWidth, startX);
184185
}
185186

186-
int itemsX = (startX / 18);
187-
int itemsY = Math.min((height / 18) - 1, ((height - 28) / 18));
187+
int itemsX = (startX / buttonWidth);
188+
int itemsY = Math.min((height / buttonWidth) - 1, ((height - 28) / buttonWidth));
188189
int xOffset = 0;
189190
int yOffset = 1;
190191

191192
int itemsPerPage = itemsX * itemsY;
192193
if (itemsPerPage <= 0) return;
193-
pages = toDisplay.size() / itemsPerPage;
194+
pageCount = toDisplay.size() / itemsPerPage;
194195

195-
if (currentPage > pages) currentPage = pages;
196+
if (currentPage > pageCount) currentPage = pageCount;
196197

197198
List<ITypedIngredient<?>> pageList = toDisplay.stream().skip((long) itemsPerPage * currentPage).limit(itemsPerPage).collect(Collectors.toList());
198199

@@ -270,23 +271,23 @@ public static void renderItems2(int mouseX, int mouseY, int width, int height, M
270271
int startX = (int) (width / 3.5F);
271272

272273
if (currentScreen instanceof ScreenContainerAbstract) {
273-
startX = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - 18, startX);
274+
startX = Math.min(((width / 2) - ((ScreenContainerAbstract) (currentScreen)).xSize / 2) - buttonWidth, startX);
274275
} else if (properties != null) {
275-
startX = Math.min(((width / 2) - properties.guiXSize() / 2) - 18, startX);
276+
startX = Math.min(((width / 2) - properties.guiXSize() / 2) - buttonWidth, startX);
276277
}
277278

278-
int itemsX = (startX / 18);
279-
int itemsY = Math.min((height / 18) - 1, ((height - 28) / 18));
279+
int itemsX = (startX / buttonWidth);
280+
int itemsY = Math.min((height / buttonWidth) - 1, ((height - 28) / buttonWidth));
280281
int xOffset = 0;
281282
int yOffset = 1;
282283

283284
startX = 0;
284285

285286
int itemsPerPage = itemsX * itemsY;
286287
if (itemsPerPage <= 0) return;
287-
favouritePages = toDisplay.size() / itemsPerPage;
288+
favouritePageCount = toDisplay.size() / itemsPerPage;
288289

289-
if (currentFavouritePage > favouritePages) currentFavouritePage = favouritePages;
290+
if (currentFavouritePage > favouritePageCount) currentFavouritePage = favouritePageCount;
290291

291292
List<ITypedIngredient<?>> pageList = toDisplay.stream().skip((long) itemsPerPage * currentFavouritePage).limit(itemsPerPage).collect(Collectors.toList());
292293

src/main/java/turing/tmb/mixin/client/ScreenContainerAbstractMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public ScreenContainerAbstractMixin() {
2929

3030
@Inject(method = "render", at = @At("TAIL"))
3131
public void render(int mouseX, int mouseY, float pt, CallbackInfo ci) {
32-
TMBRenderer.renderHeader(mouseX, mouseY, width, height, mc, pt, null);
32+
TMBRenderer.renderHeader(false, mouseX, mouseY, width, height, mc, null);
3333
TMBRenderer.renderItems(mouseX, mouseY, width, height, mc, pt, null);
34-
TMBRenderer.renderHeader2(mouseX, mouseY, width, height, mc, pt, null);
34+
TMBRenderer.renderHeader(true, mouseX, mouseY, width, height, mc, null);
3535
TMBRenderer.renderItems2(mouseX, mouseY, width, height, mc, pt, null);
3636
}
3737

src/main/java/turing/tmb/mixin/client/ScreenMixin.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public void render(int mouseX, int mouseY, float partialTick, CallbackInfo ci) {
2929
if(GuiHelper.screenBlacklist.contains(t.getClass().getCanonicalName())) return;
3030
if (GuiHelper.extraScreens.containsKey(t.getClass().getCanonicalName())) {
3131
IGuiProperties properties = GuiHelper.extraScreens.get(t.getClass().getCanonicalName()).apply(t);
32-
TMBRenderer.renderHeader(mouseX, mouseY, width, height, mc, partialTick, properties);
32+
TMBRenderer.renderHeader(false, mouseX, mouseY, width, height, mc, properties);
3333
TMBRenderer.renderItems(mouseX, mouseY, width, height, mc, partialTick, properties);
34-
TMBRenderer.renderHeader2(mouseX, mouseY, width, height, mc, partialTick, properties);
34+
TMBRenderer.renderHeader(true, mouseX, mouseY, width, height, mc, properties);
3535
TMBRenderer.renderItems2(mouseX, mouseY, width, height, mc, partialTick, properties);
3636
}
3737
}

0 commit comments

Comments
 (0)