From fd8b36e43530fb4e150e428ea887142cc6e0cbc8 Mon Sep 17 00:00:00 2001 From: deirn Date: Mon, 18 Sep 2023 16:10:53 +0700 Subject: [PATCH] make contributor credit multi column --- .../waila/gui/screen/CreditsScreen.java | 60 ++++++++-- .../waila/gui/widget/CategoryEntry.java | 1 + .../resources/assets/waila/credits.json | 111 ++++++++++-------- 3 files changed, 108 insertions(+), 64 deletions(-) diff --git a/src/main/java/mcp/mobius/waila/gui/screen/CreditsScreen.java b/src/main/java/mcp/mobius/waila/gui/screen/CreditsScreen.java index a9220398f..7d581dadf 100644 --- a/src/main/java/mcp/mobius/waila/gui/screen/CreditsScreen.java +++ b/src/main/java/mcp/mobius/waila/gui/screen/CreditsScreen.java @@ -5,6 +5,7 @@ import java.util.LinkedHashMap; import java.util.List; +import com.google.common.collect.Lists; import com.google.gson.Gson; import mcp.mobius.waila.Waila; import mcp.mobius.waila.buildconst.Tl; @@ -17,6 +18,7 @@ import net.minecraft.client.gui.screens.Screen; import net.minecraft.network.chat.CommonComponents; import net.minecraft.network.chat.Component; +import net.minecraft.network.chat.MutableComponent; import org.jetbrains.annotations.NotNull; import static mcp.mobius.waila.util.DisplayUtil.createButton; @@ -38,17 +40,21 @@ protected void init() { try { var credits = new Gson().fromJson(minecraft.getResourceManager().getResource(Waila.id("credits.json")).get().openAsReader(), CreditMap.class); - var listWidget = new ListWidget(minecraft, width, height, 32, height - 32, minecraft.font.lineHeight + 6); - credits.forEach((key, list) -> { + + credits.forEach((key, category) -> { var children = listWidget.children(); - children.add(new Entry(Component.translatable(Tl.Gui.CREDITS + "." + key).withStyle(ChatFormatting.GRAY))); - for (var person : list) { - children.add(new Entry(Component.literal(" " + person))); + + children.add(new CreditLine(1, List.of(Component.translatable(Tl.Gui.CREDITS + "." + key).withStyle(ChatFormatting.GRAY)))); + + for (var chunk : Lists.partition(category.values.stream().map(Component::literal).toList(), category.width)) { + children.add(new CreditLine(category.width, chunk)); } - children.add(new Entry(Component.empty())); + + children.add(new CreditLine(1, List.of())); }); + listWidget.init(); addRenderableWidget(listWidget); } catch (IOException e) { e.printStackTrace(); @@ -69,16 +75,35 @@ public void onClose() { minecraft.setScreen(parent); } - private static class CreditMap extends LinkedHashMap> { + private static class CreditMap extends LinkedHashMap { + + } + + private static class CreditCategory { + + int width = 0; + List values = List.of(); } - private static class ListWidget extends ContainerObjectSelectionList { + private static class ListWidget extends ContainerObjectSelectionList { private ListWidget(Minecraft client, int width, int height, int top, int bottom, int itemHeight) { super(client, width, height, top, bottom, itemHeight); + } + private void init() { setRenderBackground(false); + + var totalHeight = (children().size() - 1) * itemHeight; + if (totalHeight < height) { + setRenderHeader(true, (height - totalHeight) / 2 - y0); + } + } + + @Override + public int getRowWidth() { + return Math.min(width - 20, 360); } @Override @@ -88,12 +113,14 @@ protected int getScrollbarPosition() { } - private static class Entry extends ContainerObjectSelectionList.Entry { + private class CreditLine extends ContainerObjectSelectionList.Entry { - private final Component component; + private final int column; + private final List components; - private Entry(Component component) { - this.component = component; + private CreditLine(int column, List components) { + this.column = column; + this.components = components; } @Override @@ -108,7 +135,14 @@ private Entry(Component component) { @Override public void render(@NotNull GuiGraphics ctx, int index, int rowTop, int rowLeft, int width, int height, int mouseX, int mouseY, boolean hovered, float deltaTime) { - ctx.drawString(Minecraft.getInstance().font, component, rowLeft, rowTop + 3, 0xFFFFFF); + if (components.isEmpty()) return; + + var columnWidth = width / column; + + for (var i = 0; i < components.size(); i++) { + var component = components.get(i); + ctx.drawCenteredString(minecraft.font, component, rowLeft + (columnWidth * i) + (columnWidth / 2), rowTop + 3, 0xFFFFFF); + } } } diff --git a/src/main/java/mcp/mobius/waila/gui/widget/CategoryEntry.java b/src/main/java/mcp/mobius/waila/gui/widget/CategoryEntry.java index ae16e2077..f996f4ef1 100644 --- a/src/main/java/mcp/mobius/waila/gui/widget/CategoryEntry.java +++ b/src/main/java/mcp/mobius/waila/gui/widget/CategoryEntry.java @@ -12,6 +12,7 @@ public CategoryEntry(String title) { @Override public void render(@NotNull GuiGraphics ctx, int index, int rowTop, int rowLeft, int width, int height, int mouseX, int mouseY, boolean hovered, float deltaTime) { +// ctx.drawString(client.font, category, rowLeft, rowTop + height - client.font.lineHeight, 0xFFFFFF); ctx.drawCenteredString(client.font, category, rowLeft + width / 2, rowTop + height - client.font.lineHeight, 0xFFFFFF); } diff --git a/src/resources/resources/assets/waila/credits.json b/src/resources/resources/assets/waila/credits.json index 86b45d743..14cdc1e30 100644 --- a/src/resources/resources/assets/waila/credits.json +++ b/src/resources/resources/assets/waila/credits.json @@ -1,55 +1,64 @@ { - "original_mod_by": [ - "ProfMobius" - ], + "original_mod_by": { + "width" : 1, + "values": [ + "ProfMobius" + ] + }, - "authors" : [ - "ProfMobius", - "TehNut", - "deirn" - ], + "authors" : { + "width" : 1, + "values": [ + "ProfMobius", + "TehNut", + "deirn" + ] + }, - "contributors" : [ - "3TUSK", - "AlexChevios", - "Artoria2e5", - "crafteverywhere", - "CrimsonEdgeHope", - "cs127", - "Eracer", - "Exopandora", - "Foxgreat", - "gigabit101", - "Girafi", - "GodGun968", - "gyular", - "InsomniaKitten", - "irunatbullets", - "josephcsible", - "Lordmau5", - "luni3359", - "Lyaiya", - "magik6k", - "MelnCat", - "Motschen", - "MrNinja93", - "odininon", - "Pancham138", - "Parker8283", - "plusls", - "raphydaphy", - "Regnander", - "RCXcrafter", - "rogi27", - "shadowfacts", - "Shadows-of-Fire", - "SinusoidalC", - "StarskyXIII", - "Sven792", - "tambry", - "Trent87", - "Vexatos", - "viliml", - "X00LA" - ] + "contributors" : { + "width" : 3, + "values": [ + "3TUSK", + "AlexChevios", + "Artoria2e5", + "crafteverywhere", + "CrimsonEdgeHope", + "cs127", + "Eracer", + "Exopandora", + "Foxgreat", + "gigabit101", + "Girafi", + "GodGun968", + "gyular", + "InsomniaKitten", + "irunatbullets", + "josephcsible", + "Lordmau5", + "luni3359", + "Lyaiya", + "magik6k", + "MelnCat", + "Motschen", + "MrNinja93", + "odininon", + "Pancham138", + "Parker8283", + "plusls", + "raphydaphy", + "Regnander", + "RCXcrafter", + "rogi27", + "shadowfacts", + "Shadows-of-Fire", + "SinusoidalC", + "StarskyXIII", + "Sven792", + "tambry", + "Trent87", + "Vexatos", + "viliml", + "X00LA" + ] + } }