|
| 1 | +package juuxel.adorn.client.gui.screen; |
| 2 | + |
| 3 | +import com.mojang.blaze3d.systems.RenderSystem; |
| 4 | +import juuxel.adorn.AdornCommon; |
| 5 | +import juuxel.adorn.client.gui.widget.ConfigScreenHeading; |
| 6 | +import juuxel.adorn.config.ConfigManager; |
| 7 | +import juuxel.adorn.util.Colors; |
| 8 | +import juuxel.adorn.util.Displayable; |
| 9 | +import juuxel.adorn.util.PropertyRef; |
| 10 | +import juuxel.adorn.util.animation.AnimationEngine; |
| 11 | +import juuxel.adorn.util.animation.AnimationTask; |
| 12 | +import net.minecraft.client.gui.DrawContext; |
| 13 | +import net.minecraft.client.gui.screen.NoticeScreen; |
| 14 | +import net.minecraft.client.gui.screen.Screen; |
| 15 | +import net.minecraft.client.gui.tooltip.Tooltip; |
| 16 | +import net.minecraft.client.gui.widget.CyclingButtonWidget; |
| 17 | +import net.minecraft.text.Text; |
| 18 | +import net.minecraft.util.Formatting; |
| 19 | +import net.minecraft.util.Identifier; |
| 20 | +import net.minecraft.util.math.MathHelper; |
| 21 | +import net.minecraft.util.math.RotationAxis; |
| 22 | + |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Random; |
| 26 | + |
| 27 | +public abstract class AbstractConfigScreen extends Screen { |
| 28 | + private static final int CONFIG_BUTTON_START_Y = 40; |
| 29 | + public static final int BUTTON_HEIGHT = 20; |
| 30 | + public static final int BUTTON_GAP = 4; |
| 31 | + public static final int BUTTON_SPACING = BUTTON_HEIGHT + BUTTON_GAP; |
| 32 | + private static final int HEART_SIZE = 12; |
| 33 | + private static final int[] HEART_COLORS = new int[] { |
| 34 | + 0xFF_FF0000, // Red |
| 35 | + 0xFF_FC8702, // Orange |
| 36 | + 0xFF_FFFF00, // Yellow |
| 37 | + 0xFF_A7FC58, // Green |
| 38 | + 0xFF_2D61FC, // Blue |
| 39 | + 0xFF_A002FC, // Purple |
| 40 | + 0xFF_58E9FC, // Light blue |
| 41 | + 0xFF_FCA1DF, // Pink |
| 42 | + }; |
| 43 | + private static final Identifier HEART_TEXTURE = AdornCommon.id("textures/gui/heart.png"); |
| 44 | + private static final double MIN_HEART_SPEED = 0.05; |
| 45 | + private static final double MAX_HEART_SPEED = 1.5; |
| 46 | + private static final double MAX_HEART_ANGULAR_SPEED = 0.07; |
| 47 | + private static final int HEART_CHANCE = 65; |
| 48 | + |
| 49 | + private final Screen parent; |
| 50 | + private final Random random = new Random(); |
| 51 | + private final List<Heart> hearts = new ArrayList<>(); |
| 52 | + private boolean restartRequired = false; |
| 53 | + private final AnimationEngine animationEngine = new AnimationEngine(); |
| 54 | + |
| 55 | + /** The Y-coordinate of the next config option or heading to be added. */ |
| 56 | + protected int nextChildY = CONFIG_BUTTON_START_Y; |
| 57 | + |
| 58 | + protected AbstractConfigScreen(Text title, Screen parent) { |
| 59 | + super(title); |
| 60 | + this.parent = parent; |
| 61 | + animationEngine.add(new HeartAnimationTask()); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + protected void init() { |
| 66 | + nextChildY = CONFIG_BUTTON_START_Y; |
| 67 | + animationEngine.start(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void render(DrawContext context, int mouseX, int mouseY, float delta) { |
| 72 | + renderBackground(context, mouseX, mouseY, delta); |
| 73 | + synchronized (hearts) { |
| 74 | + renderHearts(context, delta); |
| 75 | + } |
| 76 | + context.drawCenteredTextWithShadow(textRenderer, title, width / 2, 20, Colors.WHITE); |
| 77 | + super.render(context, mouseX, mouseY, delta); |
| 78 | + } |
| 79 | + |
| 80 | + private void renderHearts(DrawContext context, float delta) { |
| 81 | + for (var heart : hearts) { |
| 82 | + RenderSystem.setShaderColor(Colors.redOf(heart.color), Colors.greenOf(heart.color), Colors.blueOf(heart.color), 1f); |
| 83 | + var matrices = context.getMatrices(); |
| 84 | + matrices.push(); |
| 85 | + matrices.translate(heart.x, MathHelper.lerp(delta, heart.previousY, heart.y), 0.0); |
| 86 | + matrices.translate(0.5 * HEART_SIZE, 0.5 * HEART_SIZE, 0.0); |
| 87 | + var angle = MathHelper.lerp(delta, heart.previousAngle, heart.angle); |
| 88 | + matrices.multiply(RotationAxis.POSITIVE_Z.rotation((float) angle)); |
| 89 | + matrices.translate(-0.5 * HEART_SIZE, -0.5 * HEART_SIZE, 0.0); |
| 90 | + context.drawTexture(HEART_TEXTURE, 0, 0, HEART_SIZE, HEART_SIZE, 0f, 0f, 8, 8, 8, 8); |
| 91 | + matrices.pop(); |
| 92 | + } |
| 93 | + |
| 94 | + RenderSystem.setShaderColor(1f, 1f, 1f, 1f); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public void close() { |
| 99 | + client.setScreen(restartRequired ? new NoticeScreen( |
| 100 | + () -> client.setScreen(parent), |
| 101 | + Text.translatable("gui.adorn.config.restart_required.title"), |
| 102 | + Text.translatable("gui.adorn.config.restart_required.message"), |
| 103 | + Text.translatable("gui.ok"), |
| 104 | + true |
| 105 | + ) : parent); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void removed() { |
| 110 | + animationEngine.stop(); |
| 111 | + } |
| 112 | + |
| 113 | + private void tickHearts() { |
| 114 | + var iter = hearts.iterator(); |
| 115 | + while (iter.hasNext()) { |
| 116 | + var heart = iter.next(); |
| 117 | + |
| 118 | + if (heart.y - HEART_SIZE > height) { |
| 119 | + iter.remove(); |
| 120 | + } else { |
| 121 | + heart.move(); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + if (random.nextInt(HEART_CHANCE) == 0) { |
| 126 | + int x = random.nextInt(width); |
| 127 | + int color = HEART_COLORS[random.nextInt(HEART_COLORS.length)]; |
| 128 | + double speed = random.nextDouble(MIN_HEART_SPEED, MAX_HEART_SPEED); |
| 129 | + double angularSpeed = random.nextDouble(-MAX_HEART_ANGULAR_SPEED, MAX_HEART_ANGULAR_SPEED); |
| 130 | + hearts.add(new Heart(x, -HEART_SIZE, color, speed, angularSpeed)); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + private <T> CyclingButtonWidget<T> createConfigButton(CyclingButtonWidget.Builder<T> builder, int x, int y, int width, PropertyRef<T> property, boolean restartRequired) { |
| 135 | + return builder |
| 136 | + .tooltip(value -> { |
| 137 | + var text = Text.translatable(getTooltipTranslationKey(property.getName())); |
| 138 | + if (restartRequired) { |
| 139 | + text.append(Text.literal("\n")) |
| 140 | + .append(Text.translatable("gui.adorn.config.requires_restart").formatted(Formatting.ITALIC, Formatting.GOLD)); |
| 141 | + } |
| 142 | + return Tooltip.of(text); |
| 143 | + }) |
| 144 | + .build(x, y, width, BUTTON_HEIGHT, Text.translatable(getOptionTranslationKey(property.getName())), (button, value) -> { |
| 145 | + property.set(value); |
| 146 | + ConfigManager.get().save(); |
| 147 | + |
| 148 | + if (restartRequired) { |
| 149 | + this.restartRequired = true; |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + protected void addConfigToggle(int width, PropertyRef<Boolean> property) { |
| 155 | + addConfigToggle(width, property, false); |
| 156 | + } |
| 157 | + |
| 158 | + protected void addConfigToggle(int width, PropertyRef<Boolean> property, boolean restartRequired) { |
| 159 | + var button = createConfigButton( |
| 160 | + CyclingButtonWidget.onOffBuilder(property.get()), |
| 161 | + (this.width - width) / 2, nextChildY, width, property, restartRequired |
| 162 | + ); |
| 163 | + addDrawableChild(button); |
| 164 | + nextChildY += BUTTON_SPACING; |
| 165 | + } |
| 166 | + |
| 167 | + protected <T extends Displayable> void addConfigButton(int width, PropertyRef<T> property, List<T> values) { |
| 168 | + addConfigButton(width, property, values, false); |
| 169 | + } |
| 170 | + |
| 171 | + protected <T extends Displayable> void addConfigButton(int width, PropertyRef<T> property, List<T> values, boolean restartRequired) { |
| 172 | + var button = createConfigButton( |
| 173 | + CyclingButtonWidget.<T>builder(Displayable::getDisplayName).values(values).initially(property.get()), |
| 174 | + (this.width - width) / 2, nextChildY, width, property, restartRequired |
| 175 | + ); |
| 176 | + addDrawableChild(button); |
| 177 | + nextChildY += BUTTON_SPACING; |
| 178 | + } |
| 179 | + |
| 180 | + protected void addHeading(Text text, int width) { |
| 181 | + addDrawable(new ConfigScreenHeading(text, (this.width - width) / 2, nextChildY, width)); |
| 182 | + nextChildY += ConfigScreenHeading.HEIGHT; |
| 183 | + } |
| 184 | + |
| 185 | + protected String getOptionTranslationKey(String name) { |
| 186 | + return "gui.adorn.config.option." + name; |
| 187 | + } |
| 188 | + |
| 189 | + private String getTooltipTranslationKey(String name) { |
| 190 | + return getOptionTranslationKey(name) + ".description"; |
| 191 | + } |
| 192 | + |
| 193 | + private static final class Heart { |
| 194 | + private final int x; |
| 195 | + private double y; |
| 196 | + private final int color; |
| 197 | + private final double speed; |
| 198 | + private final double angularSpeed; |
| 199 | + private double previousY; |
| 200 | + private double previousAngle = 0.0; |
| 201 | + private double angle = 0.0; |
| 202 | + |
| 203 | + private Heart(int x, double y, int color, double speed, double angularSpeed) { |
| 204 | + this.x = x; |
| 205 | + this.y = y; |
| 206 | + this.color = color; |
| 207 | + this.speed = speed; |
| 208 | + this.angularSpeed = angularSpeed; |
| 209 | + previousY = y; |
| 210 | + } |
| 211 | + |
| 212 | + private void move() { |
| 213 | + previousY = y; |
| 214 | + y += speed; |
| 215 | + previousAngle = angle; |
| 216 | + angle = (angle + angularSpeed) % MathHelper.TAU; |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + private final class HeartAnimationTask implements AnimationTask { |
| 221 | + @Override |
| 222 | + public boolean isAlive() { |
| 223 | + return true; |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public void tick() { |
| 228 | + synchronized (hearts) { |
| 229 | + tickHearts(); |
| 230 | + } |
| 231 | + } |
| 232 | + } |
| 233 | +} |
0 commit comments