From 917af90f37a6470d1ce25f0cb9e85019235f07b0 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 8 Nov 2023 14:51:54 +0000 Subject: [PATCH] Revamp the tribe selection screen so it can handle more options --- .../unicopia/client/gui/TextBlock.java | 2 +- .../unicopia/client/gui/TribeButton.java | 6 +- .../client/gui/TribeConfirmationScreen.java | 57 +++++++++--- .../client/gui/TribeSelectionScreen.java | 93 +++++++++++++++---- .../resources/assets/unicopia/lang/en_us.json | 32 ++++--- 5 files changed, 138 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/TextBlock.java b/src/main/java/com/minelittlepony/unicopia/client/gui/TextBlock.java index 8943da6e..b72f46fc 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/TextBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/TextBlock.java @@ -13,13 +13,13 @@ public class TextBlock extends Label { public TextBlock(int x, int y, int width) { super(x, y); this.maxWidth = width; - this.render(null, x, y, width); } @Override public Bounds getBounds() { Bounds bounds = super.getBounds(); bounds.height = getFont().wrapLines(getStyle().getText(), maxWidth).size() * getFont().fontHeight; + bounds.width = 0; return bounds; } diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeButton.java b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeButton.java index 46e09f3a..2b4549a6 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeButton.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeButton.java @@ -41,10 +41,10 @@ public class TribeButton extends Button { MinecraftClient mc = MinecraftClient.getInstance(); context.drawTexture(TribeSelectionScreen.TEXTURE, getX() - 3, getY() - 13, 0, 0, 76, 69); - if (isHovered()) { + if (isSelected()) { context.drawTexture(TribeSelectionScreen.TEXTURE, getX() - 4, getY() - 14, 76, 0, 78, 71); - if (hovered && screenWidth > 0) { + if (isFocused() && screenWidth > 0) { Identifier id = Race.REGISTRY.getId(race); context.drawCenteredTextWithShadow(getFont(), Text.translatable("gui.unicopia.tribe_selection.describe." + id.getNamespace() + "." + id.getPath()), screenWidth / 2, getY() + height, 0xFFFFFFFF); } @@ -59,7 +59,7 @@ public class TribeButton extends Button { int foreColor = getStyle().getColor(); if (!active) { foreColor = 10526880; - } else if (isHovered()) { + } else if (isSelected()) { foreColor = 16777120; } diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeConfirmationScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeConfirmationScreen.java index bc241ebd..bc8ea6a8 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeConfirmationScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeConfirmationScreen.java @@ -3,6 +3,7 @@ package com.minelittlepony.unicopia.client.gui; import org.lwjgl.glfw.GLFW; import com.minelittlepony.common.client.gui.GameGui; +import com.minelittlepony.common.client.gui.ScrollContainer; import com.minelittlepony.common.client.gui.element.Button; import com.minelittlepony.common.client.gui.element.Label; import com.minelittlepony.unicopia.Race; @@ -17,6 +18,8 @@ public class TribeConfirmationScreen extends GameGui implements HidesHud { private final BooleanConsumer callback; + private final ScrollContainer textBody = new ScrollContainer(); + public TribeConfirmationScreen(BooleanConsumer callback, Race selection) { super(Text.translatable("gui.unicopia.tribe_selection")); this.callback = callback; @@ -25,12 +28,26 @@ public class TribeConfirmationScreen extends GameGui implements HidesHud { @Override protected void init() { - final int columnHeight = 167; final int columnWidth = 310; final int padding = 15; int top = (height - columnHeight) / 2; + int left = (width - columnWidth) / 2 + 8; + int maxWidth = 295; + + textBody.verticalScrollbar.layoutToEnd = true; + textBody.margin.top = top + 43; + textBody.margin.left = left; + textBody.margin.right = width - (left + maxWidth); + textBody.margin.bottom = height - (textBody.margin.top + 130); + textBody.getContentPadding().top = 10; + textBody.getContentPadding().left = 8; + textBody.getContentPadding().bottom = 100; + textBody.getContentPadding().right = 0; + textBody.init(this::buildTextBody); + + getChildElements().add(textBody); addDrawableChild(new Button(width / 2 + 5, top + columnHeight + padding, 100, 20)) .onClick(b -> callback.accept(true)) @@ -42,38 +59,40 @@ public class TribeConfirmationScreen extends GameGui implements HidesHud { addDrawable(new Label(width / 2, top - 30).setCentered()).getStyle().setText(Text.translatable("gui.unicopia.tribe_selection.confirm", selection.getDisplayName().copy().formatted(Formatting.YELLOW))); addDrawable(new TribeButton((width - 70) / 2, top, 0, selection)); + } - top += 43; + private void buildTextBody() { + int top = 0; - int left = (width - columnWidth) / 2 + padding; + int left = 0; Text race = selection.getAltDisplayName().copy().formatted(Formatting.YELLOW); - addDrawable(new Label(left - 3, top += 10)).getStyle().setText(Text.translatable("gui.unicopia.tribe_selection.confirm.goods", race).formatted(Formatting.YELLOW)); + textBody.addButton(new Label(left - 3, top += 10)).getStyle().setText(Text.translatable("gui.unicopia.tribe_selection.confirm.goods", race).formatted(Formatting.YELLOW)); top += 15; - int maxWidth = 280; + int maxWidth = 270; Identifier id = Race.REGISTRY.getId(selection); - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 10; i++) { String key = String.format("gui.unicopia.tribe_selection.confirm.goods.%d.%s.%s", i, id.getNamespace(), id.getPath()); if (Language.getInstance().hasTranslation(key)) { - TextBlock block = addDrawable(new TextBlock(left, top, maxWidth)); + TextBlock block = textBody.addButton(new TextBlock(left, top, maxWidth)); block.getStyle().setText(Text.translatable(key)); top += block.getBounds().height; } } - addDrawable(new Label(left - 3, top += 5)).getStyle().setText(Text.translatable("gui.unicopia.tribe_selection.confirm.bads", race).formatted(Formatting.YELLOW)); + textBody.addButton(new Label(left - 3, top += 5)).getStyle().setText(Text.translatable("gui.unicopia.tribe_selection.confirm.bads", race).formatted(Formatting.YELLOW)); top += 15; - for (int i = 0; i < 5; i++) { + for (int i = 0; i < 10; i++) { String key = String.format("gui.unicopia.tribe_selection.confirm.bads.%d.%s.%s", i, id.getNamespace(), id.getPath()); if (Language.getInstance().hasTranslation(key)) { - TextBlock block = addDrawable(new TextBlock(left, top, maxWidth)); + TextBlock block = textBody.addButton(new TextBlock(left, top, maxWidth)); block.getStyle().setText(Text.translatable(key)); top += block.getBounds().height; } @@ -91,7 +110,7 @@ public class TribeConfirmationScreen extends GameGui implements HidesHud { int top = (height - columnHeight) / 2; int left = (width - columnWidth) / 2; - top += 25; + top += 40; final int zOffset = 0; @@ -106,12 +125,16 @@ public class TribeConfirmationScreen extends GameGui implements HidesHud { left = width / 2; context.drawTexture(TribeSelectionScreen.TEXTURE, left - 55, top, 140, 70, 21, 50); - - context.drawTexture(TribeSelectionScreen.TEXTURE, left - 35, top, 10, 70, 69, 50); - context.drawTexture(TribeSelectionScreen.TEXTURE, left + 35, top, 148, 70, 21, 50); + textBody.render(context, mouseX, mouseY, delta); + + context.getMatrices().push(); + context.getMatrices().translate(0, 0, 2); + context.drawTexture(TribeSelectionScreen.TEXTURE, left - 35, top - 5, 10, 70, 69, 50); + context.drawTexture(TribeSelectionScreen.TEXTURE, left - 35, top - 15, 10, 70, 69, 50); super.render(context, mouseX, mouseY, delta); + context.getMatrices().pop(); } @Override @@ -122,7 +145,11 @@ public class TribeConfirmationScreen extends GameGui implements HidesHud { @Override public boolean keyPressed(int keyCode, int scanCode, int modifiers) { if (keyCode == GLFW.GLFW_KEY_ESCAPE) { - + callback.accept(false); + return true; + } + if (keyCode == GLFW.GLFW_KEY_ENTER) { + callback.accept(true); return true; } return super.keyPressed(keyCode, scanCode, modifiers); diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java index a999236f..73551090 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/TribeSelectionScreen.java @@ -1,8 +1,11 @@ package com.minelittlepony.unicopia.client.gui; +import java.util.ArrayList; import java.util.List; import java.util.Set; +import org.lwjgl.glfw.GLFW; + import com.minelittlepony.common.client.gui.GameGui; import com.minelittlepony.common.client.gui.element.Label; import com.minelittlepony.unicopia.Race; @@ -14,6 +17,7 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.text.Text; import net.minecraft.util.Formatting; import net.minecraft.util.Identifier; +import net.minecraft.util.math.MathHelper; public class TribeSelectionScreen extends GameGui implements HidesHud { static final Identifier TEXTURE = Unicopia.id("textures/gui/tribe_selection.png"); @@ -25,6 +29,13 @@ public class TribeSelectionScreen extends GameGui implements HidesHud { private boolean finished; + private final List options = new ArrayList<>(); + private static int SELECTION = -1; + + private int prevScrollPosition; + private int scrollPosition; + private int targetScroll; + public TribeSelectionScreen(Set allowedRaces, String baseString) { super(Text.translatable(baseString)); this.allowedRaces = allowedRaces; @@ -52,33 +63,26 @@ public class TribeSelectionScreen extends GameGui implements HidesHud { top += block.getBounds().height; top += 30; - final int itemWidth = 70 + 10; - List options = Race.REGISTRY.stream().filter(race -> race.availability().isSelectable()).toList(); - - int columns = Math.min(width / itemWidth, options.size()); - - int x = (width - (columns * itemWidth)) / 2; - int y = top; - - int column = 0; - int row = 0; + this.options.clear(); for (Race race : options) { - addOption(race, x + (column * itemWidth), y + (row * itemWidth)); - column++; - if (column >= columns) { - column = 0; - row++; - } + addOption(race, top); } - top = height - 20; + if (SELECTION == -1) { + SELECTION = options.size() / 2; + } + scroll(SELECTION, false); } - private void addOption(Race race, int x, int y) { - addDrawableChild(new TribeButton(x, y, width, race)).onClick(b -> { + private void addOption(Race race, int y) { + var option = new TribeButton(0, y, width, race); + int index = options.size(); + options.add(addDrawableChild(option)); + option.onClick(b -> { finished = true; + scroll(index, false); client.setScreen(new TribeConfirmationScreen(result -> { finished = false; @@ -92,8 +96,32 @@ public class TribeSelectionScreen extends GameGui implements HidesHud { }).setEnabled(allowedRaces.contains(race)); } + @Override + public void tick() { + prevScrollPosition = scrollPosition; + if (scrollPosition < targetScroll) { + scrollPosition++; + } + if (scrollPosition > targetScroll) { + scrollPosition--; + } + } + + private void updateScolling() { + final int itemWidth = 70 + 10; + int x = (width - itemWidth) / 2; + float diff = MathHelper.lerp(client.getTickDelta(), prevScrollPosition, scrollPosition) / 8F; + + for (int i = 0; i < options.size(); i++) { + var option = options.get(i); + option.setX((int)(x + (i - diff) * itemWidth)); + option.setFocused(i == SELECTION); + } + } + @Override public void render(DrawContext context, int mouseX, int mouseY, float delta) { + updateScolling(); renderBackground(context); super.render(context, mouseX, mouseY, delta); } @@ -104,6 +132,33 @@ public class TribeSelectionScreen extends GameGui implements HidesHud { close(); } + @Override + public boolean keyPressed(int keyCode, int scanCode, int modifiers) { + + if (keyCode == GLFW.GLFW_KEY_LEFT) { + scroll(Math.max(SELECTION - 1, 0), true); + return true; + } + if (keyCode == GLFW.GLFW_KEY_RIGHT) { + scroll(Math.min(SELECTION + 1, options.size() - 1), true); + return true; + } + if (keyCode == GLFW.GLFW_KEY_ENTER) { + options.get(SELECTION).onPress(); + } + + return super.keyPressed(keyCode, scanCode, modifiers); + } + + private void scroll(int target, boolean animate) { + SELECTION = target; + targetScroll = SELECTION * 8; + if (!animate) { + scrollPosition = targetScroll; + prevScrollPosition = scrollPosition; + } + } + @Override public boolean shouldCloseOnEsc() { return false; diff --git a/src/main/resources/assets/unicopia/lang/en_us.json b/src/main/resources/assets/unicopia/lang/en_us.json index febcc802..9d0d14ab 100644 --- a/src/main/resources/assets/unicopia/lang/en_us.json +++ b/src/main/resources/assets/unicopia/lang/en_us.json @@ -535,12 +535,14 @@ "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.human": " - Are omnivores and can eat all types of food except for love", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.human": " - Opposable thumbs", "gui.unicopia.tribe_selection.confirm.goods.3.unicopia.human": " - Walking upright", - "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.human": "", "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.earth": " - Stronger knockback and resistance to magic", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.earth": " - Have extra weight makes them effective against magic and brute force", "gui.unicopia.tribe_selection.confirm.goods.3.unicopia.earth": " - A special connection to the earth that makes farming 10,000% more effective!", - "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.earth": " - Cute floppy pony ears", + "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.earth": " - Has powerful punches, kicks, and stomps that they can use to defend or attack their environment", + "gui.unicopia.tribe_selection.confirm.goods.5.unicopia.earth": " - Is the only one that can hug creepers and survive", + "gui.unicopia.tribe_selection.confirm.goods.6.unicopia.earth": " - Can eat rocks", + "gui.unicopia.tribe_selection.confirm.goods.7.unicopia.earth": " - Cute floppy pony ears", "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.unicorn": " - Teleportation and magic spells", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.unicorn": " - Research and craft magical artefacts that enhance their abilities", @@ -550,7 +552,8 @@ "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.pegasus": " - Flight and the ability to train to build endurace", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.pegasus": " - Use stored mana to dash in short bursts or build it up to perform a powerful rainboom", "gui.unicopia.tribe_selection.confirm.goods.3.unicopia.pegasus": " - Moves faster and takes less fall damage", - "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.pegasus": " - Can eat vegetables and certain types of fish", + "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.pegasus": " - Can interact directly with cloud items and blocks", + "gui.unicopia.tribe_selection.confirm.goods.5.unicopia.pegasus": " - Can eat vegetables and certain types of fish", "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.bat": " - Flight and the ability to train to build endurance", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.bat": " - Sees better in the night", @@ -559,8 +562,8 @@ "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.changeling": " - Able to fly and hover in place", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.changeling": " - Shapeshift and morph into nearly anyone or anyling", - "gui.unicopia.tribe_selection.confirm.goods.3.unicopia.changeling": " - Is carnivorous. Can eat anything that doesn't make them sick", - "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.changeling": "", + "gui.unicopia.tribe_selection.confirm.goods.3.unicopia.changeling": " - Sticks to walls and can climb over almost enything", + "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.changeling": " - Is carnivorous. Can eat anything that doesn't make them sick", "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.kirin": " - Immune to all types of fire damage", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.kirin": " - Can unlock a rage ability by taking damage", @@ -570,18 +573,21 @@ "gui.unicopia.tribe_selection.confirm.goods.1.unicopia.hippogriff": " - Flight and the ability to train to build endurance", "gui.unicopia.tribe_selection.confirm.goods.2.unicopia.hippogriff": " - Can dash whilst flying", "gui.unicopia.tribe_selection.confirm.goods.3.unicopia.hippogriff": " - Has a sharp and noisy beak for screeching and pecking", - "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.hippogriff": " - Loves stairs, fish, and meat, but mostly stairs", + "gui.unicopia.tribe_selection.confirm.goods.4.unicopia.hippogriff": " - Can eat raw and cooked fish, cooked meat", + "gui.unicopia.tribe_selection.confirm.goods.5.unicopia.hippogriff": " - Heals extra hearts when eating pinecones", + "gui.unicopia.tribe_selection.confirm.goods.6.unicopia.hippogriff": " - Loves stairs", "gui.unicopia.tribe_selection.confirm.bads": "but they...", "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.human": " - Have no magical abilities what-so-ever", - "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.human": " - Need an Elytra to fly", + "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.human": " - Needs an Elytra to fly", "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.human": " - Must grow food the old fashioned way, through hard work and manual labour", "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.human": " - No floppy ears", "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.earth": " - Cannot fly", "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.earth": " - Are weak to certain types of magic", "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.earth": " - Can only eat plants and vegetables", + "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.earth": " - Heavier and moves slower than the other ponies", "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.unicorn": " - Cannot fly", "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.unicorn": " - Are weak to brute force attacks", @@ -590,17 +596,15 @@ "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.pegasus": " - Light weight makes them the weakest to brute force", "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.pegasus": " - Must rest between flights to regain their strength", "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.pegasus": " - Cannot use magic without aid from others", - "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.pegasus": "", "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.bat": " - Light weight makes them weak to brute force attacks", "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.bat": " - Must rest between flights to regain their strength", "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.bat": " - Is sometimes scared of even themselves", - "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.bat": " - Is carnivorous. Can eat anything that doesn't make them sick", + "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.bat": " - Is carnivorous. Can eat raw and cooked meat, or sustain themselved purely on stolen love.", "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.changeling": " - Are always starving", "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.changeling": " - Requires love, collected from ponies or other hostile mobs to subsidise their diet", - "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.changeling": " - Becomes sick from eating regular food and must eat love to hasten a cure", - "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.changeling": "", + "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.changeling": " - Becomes sick from eating most regular food and must harvest love to hasten a cure", "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.kirin": " - Are no longer quiet whilst raging", "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.kirin": " - Has a tendency to burn things, especially when close to raging", @@ -608,9 +612,9 @@ "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.kirin": " - Doesn't like water", "gui.unicopia.tribe_selection.confirm.bads.1.unicopia.hippogriff": " - Weak to brute force attacks", - "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.hippogriff": " - Must rest between flights to regain their strength", - "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.hippogriff": " - Has a bird brain", - "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.hippogriff": "", + "gui.unicopia.tribe_selection.confirm.bads.2.unicopia.hippogriff": " - Cannot interact with clouds", + "gui.unicopia.tribe_selection.confirm.bads.3.unicopia.hippogriff": " - Must rest between flights to regain their strength", + "gui.unicopia.tribe_selection.confirm.bads.4.unicopia.hippogriff": " - Has a bird brain", "gui.unicopia.tribe_selection.join": "Join Tribe", "gui.unicopia.tribe_selection.cancel": "Go Back",