From 072f3cd2ba9aa34fc89a6318f7ea92b13b2984e5 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 14 Nov 2024 16:13:29 +0000 Subject: [PATCH] Settings now implement Supplier and form elements now have constructors that accept a Supplier so you can pass Setting values to them directly --- .../common/client/gui/element/AbstractSlider.java | 5 +++-- .../common/client/gui/element/Button.java | 9 ++++++++- .../common/client/gui/element/EnumSlider.java | 7 +++++++ .../common/client/gui/element/Slider.java | 7 +++++++ .../common/client/gui/element/Toggle.java | 12 ++++++++++-- .../minelittlepony/common/util/settings/Setting.java | 3 ++- 6 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/AbstractSlider.java b/src/main/java/com/minelittlepony/common/client/gui/element/AbstractSlider.java index 540297dc..3bb6f23f 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/AbstractSlider.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/AbstractSlider.java @@ -9,6 +9,7 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.render.RenderLayer; import net.minecraft.text.Text; +import net.minecraft.util.math.ColorHelper; import net.minecraft.util.math.MathHelper; import com.minelittlepony.common.client.gui.IField; @@ -149,9 +150,9 @@ protected void onDrag(double mouseX, double mouseY, double mouseDX, double mouse @Override protected void renderBackground(DrawContext context, MinecraftClient mc, int mouseX, int mouseY) { - context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(false, isSelected()), getX(), getY(), getWidth(), getHeight()); + context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(false, isSelected()), getX(), getY(), getWidth(), getHeight(), ColorHelper.getWhite(alpha)); int sliderX = getX() + (int)(value * (getWidth() - 8)); - context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(active, isSelected()), sliderX, getY(), 8, getHeight()); + context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(active, isSelected()), sliderX, getY(), 8, getHeight(), ColorHelper.getWhite(alpha)); } static float convertFromRange(float value, float min, float max) { diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Button.java b/src/main/java/com/minelittlepony/common/client/gui/element/Button.java index 988461a3..31a88a4f 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/Button.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Button.java @@ -28,6 +28,7 @@ import net.minecraft.client.render.RenderLayer; import net.minecraft.screen.ScreenTexts; import net.minecraft.util.Identifier; +import net.minecraft.util.math.ColorHelper; import net.minecraft.util.math.MathHelper; /** @@ -232,7 +233,13 @@ public void renderWidget(DrawContext context, int mouseX, int mouseY, float tick } protected void renderBackground(DrawContext context, MinecraftClient mc, int mouseX, int mouseY) { - context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(active, this.isSelected()), getX(), getY(), getWidth(), getHeight()); + context.drawGuiTexture( + RenderLayer::getGuiTextured, + TEXTURES.get(active, isSelected()), + getX(), getY(), + getWidth(), getHeight(), + ColorHelper.getWhite(alpha) + ); } protected void drawIcon(DrawContext context, int mouseX, int mouseY, float partialTicks) { diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java b/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java index 177904cb..8f56b54a 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/EnumSlider.java @@ -1,5 +1,8 @@ package com.minelittlepony.common.client.gui.element; +import java.util.Objects; +import java.util.function.Supplier; + import net.minecraft.text.Text; /** @@ -11,6 +14,10 @@ public class EnumSlider> extends AbstractSlider { private final T[] values; + public EnumSlider(int x, int y, Supplier value) { + this(x, y, Objects.requireNonNull(value.get(), "value was null")); + } + @SuppressWarnings("unchecked") public EnumSlider(int x, int y, T value) { super(x, y, 0, value.getClass().getEnumConstants().length - 1, value); diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java b/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java index c7c55f83..02041c0d 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Slider.java @@ -1,5 +1,8 @@ package com.minelittlepony.common.client.gui.element; +import java.util.Objects; +import java.util.function.Supplier; + /** * A slider for sliding. * @@ -7,6 +10,10 @@ */ public class Slider extends AbstractSlider { + public Slider(int x, int y, float min, float max, Supplier value) { + this(x, y, min, max, Objects.requireNonNull(value.get(), "value was null")); + } + public Slider(int x, int y, float min, float max, float value) { super(x, y, min, max, value); } diff --git a/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java b/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java index 1a0286de..e7e2d072 100644 --- a/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java +++ b/src/main/java/com/minelittlepony/common/client/gui/element/Toggle.java @@ -1,5 +1,8 @@ package com.minelittlepony.common.client.gui.element; +import java.util.Objects; +import java.util.function.Supplier; + import org.jetbrains.annotations.NotNull; import com.minelittlepony.common.client.gui.IField; @@ -9,6 +12,7 @@ import net.minecraft.client.gui.DrawContext; import net.minecraft.client.render.RenderLayer; import net.minecraft.text.Text; +import net.minecraft.util.math.ColorHelper; /** * Implements a toggle (switch) element with two states (ON/OFF). @@ -22,6 +26,10 @@ public class Toggle extends Button implements IField { @NotNull private IChangeCallback action = IChangeCallback::none; + public Toggle(int x, int y, Supplier value) { + this(x, y, Objects.requireNonNull(value.get(), "value was null")); + } + public Toggle(int x, int y, boolean value) { super(x, y, 30, 15); @@ -70,9 +78,9 @@ public void onPress() { @Override protected void renderBackground(DrawContext context, MinecraftClient mc, int mouseX, int mouseY) { - context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(false, isSelected()), getX(), getY(), getWidth(), getHeight()); + context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(false, isSelected()), getX(), getY(), getWidth(), getHeight(), ColorHelper.getWhite(alpha)); int sliderX = getX() + (on ? getWidth() - 8 : 0); - context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(active, isSelected()), sliderX, getY(), 8, getHeight()); + context.drawGuiTexture(RenderLayer::getGuiTextured, TEXTURES.get(active, isSelected()), sliderX, getY(), 8, getHeight(), ColorHelper.getWhite(alpha)); } @Override diff --git a/src/main/java/com/minelittlepony/common/util/settings/Setting.java b/src/main/java/com/minelittlepony/common/util/settings/Setting.java index 31c335de..6458ccc8 100644 --- a/src/main/java/com/minelittlepony/common/util/settings/Setting.java +++ b/src/main/java/com/minelittlepony/common/util/settings/Setting.java @@ -14,7 +14,7 @@ /** * Any settings. */ -public interface Setting extends IChangeCallback { +public interface Setting extends IChangeCallback, Supplier { String name(); @NotNull @@ -32,6 +32,7 @@ default T getDefault() { /** * Gets the config value associated with this entry. */ + @Override @NotNull T get();