From 06ebb1b48376fa21216e27f3cf8fa30aad0d0076 Mon Sep 17 00:00:00 2001 From: Goby56 <60710855+Goby56@users.noreply.github.com> Date: Wed, 1 Jan 2025 18:43:21 +0100 Subject: [PATCH] reset button for color interval slider --- .../com/goby56/wakes/config/WakesConfig.java | 2 + .../wakes/config/gui/ColorIntervalSlider.java | 12 +++- .../wakes/config/gui/ColorPickerScreen.java | 36 ++++++++-- .../wakes/config/gui/TexturedButton.java | 66 ++++++++++++++++++ .../resources/assets/wakes/lang/en_us.yml | 4 +- .../assets/wakes/textures/reset_icon.png | Bin 0 -> 243 bytes 6 files changed, 110 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/goby56/wakes/config/gui/TexturedButton.java create mode 100644 src/main/resources/assets/wakes/textures/reset_icon.png diff --git a/src/main/java/com/goby56/wakes/config/WakesConfig.java b/src/main/java/com/goby56/wakes/config/WakesConfig.java index a204ec0..6744ba9 100644 --- a/src/main/java/com/goby56/wakes/config/WakesConfig.java +++ b/src/main/java/com/goby56/wakes/config/WakesConfig.java @@ -64,6 +64,8 @@ public class WakesConfig extends MidnightConfig { "#b4c4cad1", // LIGHT GRAY "#649ea5b0" // GRAY ); + public static List defaultWakeColorIntervals = Lists.newArrayList(wakeColorIntervals); + public static List defaultWakeColors = Lists.newArrayList(wakeColors); @Entry(category = DEBUG) public static boolean debugColors = false; @Entry(category = DEBUG) public static boolean drawDebugBoxes = false; diff --git a/src/main/java/com/goby56/wakes/config/gui/ColorIntervalSlider.java b/src/main/java/com/goby56/wakes/config/gui/ColorIntervalSlider.java index de2fc92..ba75261 100644 --- a/src/main/java/com/goby56/wakes/config/gui/ColorIntervalSlider.java +++ b/src/main/java/com/goby56/wakes/config/gui/ColorIntervalSlider.java @@ -4,17 +4,15 @@ import com.goby56.wakes.config.WakesConfig; import com.goby56.wakes.render.enums.WakeColor; import com.goby56.wakes.simulation.WakeHandler; -import com.goby56.wakes.utils.WakesUtils; import com.mojang.blaze3d.systems.RenderSystem; -import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; -import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.widget.SliderWidget; import net.minecraft.text.Text; import java.util.ArrayList; import java.util.Collections; +import java.util.Optional; public class ColorIntervalSlider extends SliderWidget { private ArrayList handles; @@ -31,6 +29,14 @@ public ColorIntervalSlider(ColorPickerScreen screenContext, int x, int y, int wi colorPicker.registerListener(this::onColorPicked); } + public void updateColorPicker() { + // Updates the color picker to reflect the config + if (!colorPicker.active || activeSection == null) { + return; + } + this.colorPicker.setColor(WakesConfig.getWakeColor(activeSection), ColorPicker.WidgetUpdateFlag.ALL); + } + private void unfocusHandles() { for (SliderHandle handle : handles) { handle.focused = false; diff --git a/src/main/java/com/goby56/wakes/config/gui/ColorPickerScreen.java b/src/main/java/com/goby56/wakes/config/gui/ColorPickerScreen.java index 70849ac..c9cc8b0 100644 --- a/src/main/java/com/goby56/wakes/config/gui/ColorPickerScreen.java +++ b/src/main/java/com/goby56/wakes/config/gui/ColorPickerScreen.java @@ -1,10 +1,13 @@ package com.goby56.wakes.config.gui; import com.goby56.wakes.WakesClient; +import com.goby56.wakes.config.WakesConfig; import com.goby56.wakes.utils.WakesUtils; +import com.google.common.collect.Lists; +import eu.midnightdust.lib.config.MidnightConfig; import net.minecraft.client.gui.DrawContext; -import net.minecraft.client.gui.screen.GameModeSelectionScreen; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.tooltip.Tooltip; import net.minecraft.client.gui.tooltip.TooltipBackgroundRenderer; import net.minecraft.client.gui.widget.*; import net.minecraft.text.Text; @@ -13,28 +16,49 @@ public class ColorPickerScreen extends Screen { private final Screen parent; private boolean showInfoText = false; + private ColorIntervalSlider colorIntervalSlider; + private static final Identifier INFO_ICON_TEXTURE = Identifier.of("minecraft", "textures/gui/sprites/icon/info.png"); + private static final Identifier RESET_ICON_TEXTURE = Identifier.of(WakesClient.MOD_ID, "textures/reset_icon.png"); public ColorPickerScreen(Screen parent) { super(Text.of("Configure wake colors")); this.parent = parent; + this.colorIntervalSlider = null; } @Override protected void init() { - this.addDrawableChild(new ColorIntervalSlider( + this.colorIntervalSlider = new ColorIntervalSlider( this, (int) (width / 2 - width * 0.8f / 2), 24, - (int) (width * 0.8f), 40)); - TextIconButtonWidget infoButton = TextIconButtonWidget.builder(Text.empty(), this::onInfoClick, true) - .texture(Identifier.ofVanilla("icon/info"), 20, 20) + (int) (width * 0.8f), 40); + this.addDrawableChild(this.colorIntervalSlider); + + TexturedButton infoButton = TexturedButton.builder(this::onInfoClick) + .texture(INFO_ICON_TEXTURE, 20, 20) .dimension(30, 30).build(); infoButton.setPosition((int) (width / 2 - width * 0.8f / 2 - 35), 29); + infoButton.setTooltip(Tooltip.of(WakesUtils.translatable("gui", "colorIntervalSlider", "infoButton", "tooltip"))); this.addDrawableChild(infoButton); + + TexturedButton resetButton = TexturedButton.builder(this::resetConfigurations) + .texture(RESET_ICON_TEXTURE, 20, 20) + .dimension(30, 30).build(); + resetButton.setPosition((int) (width / 2 + width * 0.8f / 2 + 5), 29); + resetButton.setTooltip(Tooltip.of(WakesUtils.translatable("gui", "colorIntervalSlider", "resetButton", "tooltip"))); + this.addDrawableChild(resetButton); } private void onInfoClick(ButtonWidget button) { this.showInfoText = !this.showInfoText; } + private void resetConfigurations(ButtonWidget button) { + WakesConfig.wakeColorIntervals = Lists.newArrayList(WakesConfig.defaultWakeColorIntervals); + WakesConfig.wakeColors = Lists.newArrayList(WakesConfig.defaultWakeColors); + this.colorIntervalSlider.updateColorPicker(); + MidnightConfig.write(WakesClient.MOD_ID); + } + @Override public void close() { client.setScreen(this.parent); @@ -47,7 +71,7 @@ public void render(DrawContext context, int mouseX, int mouseY, float delta) { if (this.showInfoText) { // TODO DYNAMIC TOOLTIP BACKGROUND SIZE DEPENDING ON INFO TEXT LENGTH TooltipBackgroundRenderer.render(context, width - 350, height - 60, 325, 34, 0); - context.drawTextWrapped(textRenderer, WakesUtils.translatable("gui", "colorIntervalSlider", "info"), width - 350, height - 60, 325, 0xa8a8a8); + context.drawTextWrapped(textRenderer, WakesUtils.translatable("gui", "colorIntervalSlider", "infoText"), width - 350, height - 60, 325, 0xa8a8a8); } } diff --git a/src/main/java/com/goby56/wakes/config/gui/TexturedButton.java b/src/main/java/com/goby56/wakes/config/gui/TexturedButton.java new file mode 100644 index 0000000..8aa112d --- /dev/null +++ b/src/main/java/com/goby56/wakes/config/gui/TexturedButton.java @@ -0,0 +1,66 @@ +package com.goby56.wakes.config.gui; + +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.widget.ButtonWidget; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +public class TexturedButton extends ButtonWidget { + private final Identifier texture; + private final int textureWidth; + private final int textureHeight; + protected TexturedButton(int width, int height, PressAction onPress, Identifier texture, int texWidth, int texHeight) { + super(0, 0, width, height, Text.empty(), onPress, DEFAULT_NARRATION_SUPPLIER); + this.texture = texture; + this.textureWidth = texWidth; + this.textureHeight = texHeight; + } + + public static Builder builder(ButtonWidget.PressAction onPress) { + return new Builder(onPress); + } + + @Override + protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) { + super.renderWidget(context, mouseX, mouseY, delta); + int tw = this.textureWidth; + int th = this.textureHeight; + int x = this.getX() + this.getWidth() / 2 - this.textureWidth / 2; + int y = this.getY() + this.getHeight() / 2 - this.textureHeight / 2; + context.drawTexture(this.texture, x, y, 0, 0, tw, th, tw, th); + } + + public static class Builder { + private final ButtonWidget.PressAction onPress; + private int width = 30; + private int height = 30; + private Identifier texture; + private int textureWidth = 20; + private int textureHeight = 20; + + public Builder(ButtonWidget.PressAction onPress) { + this.onPress = onPress; + } + + public Builder dimension(int width, int height) { + this.width = width; + this.height = height; + return this; + } + + public Builder texture(Identifier texture, int width, int height) { + this.texture = texture; + this.textureWidth = width; + this.textureHeight = height; + return this; + } + + public TexturedButton build() { + if (this.texture == null) { + throw new IllegalStateException("Texture not set"); + } else { + return new TexturedButton(this.width, this.height, this.onPress, this.texture, this.textureWidth, this.textureHeight); + } + } + } +} diff --git a/src/main/resources/assets/wakes/lang/en_us.yml b/src/main/resources/assets/wakes/lang/en_us.yml index 23a5214..8464090 100644 --- a/src/main/resources/assets/wakes/lang/en_us.yml +++ b/src/main/resources/assets/wakes/lang/en_us.yml @@ -3,11 +3,13 @@ wakes: title: Wakes config colorIntervalSlider: title: Color Interval Slider - info: | + infoText: | - [Left Click] on a section to edit its color - [Left Drag] a separator to change the extend of that section - [Shift + Left Click] on a section to insert a new separator - [Shift + Left Click] on a separator to remove it + infoButton.tooltip: Show/hide info + resetButton.tooltip: Reset configuration configButton: Mod configurations colorConfigButton: Configure wake colors midnightconfig: diff --git a/src/main/resources/assets/wakes/textures/reset_icon.png b/src/main/resources/assets/wakes/textures/reset_icon.png new file mode 100644 index 0000000000000000000000000000000000000000..61075cda59d8457c3a4a4e85b37f4aee2455a938 GIT binary patch literal 243 zcmeAS@N?(olHy`uVBq!ia0vp^A|TAc1|)ksWqE-VV{wqX6T`Z5GB1IgwVp1HAr`%R zr+V`>DDb$jCqJ5L(f<64%$!b9J(0u9FF#U0C)9Rb%)69fLXyu9hrGx+r#GDa9lpV| zg?UzzkCcw|W}WvoTv^v8D<+-cF%#Nw`hwQRJMqphzOCko_Ds!W^ik0E{%xQY5X`B| zeE!UrkbdJoyb)7l?zM*6eZIuA_1U#=S_`fwUy7<;R;@Yv``zq>N$Xs7DwovGX!o04 r^WfYMrgN1Rv!*V~KEQPUA5&RkYviF>morZS9nRqC>gTe~DWM4fI_zQ7 literal 0 HcmV?d00001