Skip to content

Commit

Permalink
Settings<T> now implement Supplier<T> and form elements now have cons…
Browse files Browse the repository at this point in the history
…tructors that accept a Supplier<T> so you can pass Setting<T> values to them directly
  • Loading branch information
Sollace committed Nov 14, 2024
1 parent 6aa2073 commit 072f3cd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -11,6 +14,10 @@ public class EnumSlider<T extends Enum<T>> extends AbstractSlider<T> {

private final T[] values;

public EnumSlider(int x, int y, Supplier<T> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package com.minelittlepony.common.client.gui.element;

import java.util.Objects;
import java.util.function.Supplier;

/**
* A slider for sliding.
*
* @author Sollace
*/
public class Slider extends AbstractSlider<Float> {

public Slider(int x, int y, float min, float max, Supplier<Float> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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).
Expand All @@ -22,6 +26,10 @@ public class Toggle extends Button implements IField<Boolean, Toggle> {
@NotNull
private IChangeCallback<Boolean> action = IChangeCallback::none;

public Toggle(int x, int y, Supplier<Boolean> value) {
this(x, y, Objects.requireNonNull(value.get(), "value was null"));
}

public Toggle(int x, int y, boolean value) {
super(x, y, 30, 15);

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Any settings.
*/
public interface Setting<T> extends IChangeCallback<T> {
public interface Setting<T> extends IChangeCallback<T>, Supplier<T> {
String name();

@NotNull
Expand All @@ -32,6 +32,7 @@ default T getDefault() {
/**
* Gets the config value associated with this entry.
*/
@Override
@NotNull
T get();

Expand Down

0 comments on commit 072f3cd

Please sign in to comment.