Skip to content

Commit

Permalink
reset button for color interval slider
Browse files Browse the repository at this point in the history
  • Loading branch information
Goby56 committed Jan 1, 2025
1 parent b9c808f commit 06ebb1b
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/goby56/wakes/config/WakesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public class WakesConfig extends MidnightConfig {
"#b4c4cad1", // LIGHT GRAY
"#649ea5b0" // GRAY
);
public static List<Float> defaultWakeColorIntervals = Lists.newArrayList(wakeColorIntervals);
public static List<String> defaultWakeColors = Lists.newArrayList(wakeColors);

@Entry(category = DEBUG) public static boolean debugColors = false;
@Entry(category = DEBUG) public static boolean drawDebugBoxes = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SliderHandle> handles;
Expand All @@ -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;
Expand Down
36 changes: 30 additions & 6 deletions src/main/java/com/goby56/wakes/config/gui/ColorPickerScreen.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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);
}
}

Expand Down
66 changes: 66 additions & 0 deletions src/main/java/com/goby56/wakes/config/gui/TexturedButton.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
4 changes: 3 additions & 1 deletion src/main/resources/assets/wakes/lang/en_us.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 06ebb1b

Please sign in to comment.