Skip to content

Commit

Permalink
make contributor credit multi column
Browse files Browse the repository at this point in the history
  • Loading branch information
deirn committed Sep 18, 2023
1 parent 60020d2 commit fd8b36e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 64 deletions.
60 changes: 47 additions & 13 deletions src/main/java/mcp/mobius/waila/gui/screen/CreditsScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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();
Expand All @@ -69,16 +75,35 @@ public void onClose() {
minecraft.setScreen(parent);
}

private static class CreditMap extends LinkedHashMap<String, List<String>> {
private static class CreditMap extends LinkedHashMap<String, CreditCategory> {

}

private static class CreditCategory {

int width = 0;
List<String> values = List.of();

}

private static class ListWidget extends ContainerObjectSelectionList<Entry> {
private static class ListWidget extends ContainerObjectSelectionList<CreditLine> {

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
Expand All @@ -88,12 +113,14 @@ protected int getScrollbarPosition() {

}

private static class Entry extends ContainerObjectSelectionList.Entry<Entry> {
private class CreditLine extends ContainerObjectSelectionList.Entry<CreditLine> {

private final Component component;
private final int column;
private final List<MutableComponent> components;

private Entry(Component component) {
this.component = component;
private CreditLine(int column, List<MutableComponent> components) {
this.column = column;
this.components = components;
}

@Override
Expand All @@ -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);
}
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
111 changes: 60 additions & 51 deletions src/resources/resources/assets/waila/credits.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
}

0 comments on commit fd8b36e

Please sign in to comment.