boolean toggle

This commit is contained in:
zelophed 2021-04-11 03:12:43 +02:00
parent 3de3de89db
commit 5b04e82163
8 changed files with 182 additions and 23 deletions

View file

@ -8,6 +8,7 @@ import com.simibubi.create.foundation.gui.widgets.AbstractSimiWidget;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.widget.list.ExtendedList;
import net.minecraft.util.text.IFormattableTextComponent;
public class ConfigScreenList extends ExtendedList<ConfigScreenList.Entry> {
@ -28,12 +29,12 @@ public class ConfigScreenList extends ExtendedList<ConfigScreenList.Entry> {
@Override
public int getRowWidth() {
return width-10;
return width-18;
}
@Override
protected int getScrollbarPositionX() {
return left + this.width;
return left + this.width-5;
}
public void tick() {
@ -41,13 +42,11 @@ public class ConfigScreenList extends ExtendedList<ConfigScreenList.Entry> {
}
public static abstract class Entry extends ExtendedList.AbstractListEntry<Entry> {
public void tick() {
}
public void tick() {}
}
public static class LabeledEntry extends Entry {
protected StencilElement label;
protected TextStencilElement label;
public LabeledEntry(String label) {
this.label = new TextStencilElement(Minecraft.getInstance().fontRenderer, label);
@ -56,6 +55,10 @@ public class ConfigScreenList extends ExtendedList<ConfigScreenList.Entry> {
@Override
public void render(MatrixStack ms, int index, int y, int x, int width, int height, int mouseX, int mouseY, boolean p_230432_9_, float partialTicks) {
UIRenderHelper.streak(ms, 0, x, y+height/2, height, width/2, 0x0);
IFormattableTextComponent component = label.getComponent();
if (Minecraft.getInstance().fontRenderer.getWidth(component) > width/2 - 10) {
label.withText(Minecraft.getInstance().fontRenderer.trimToWidth(component, width / 2 - 15).getString() + "...");
}
label.at(x + 5, y + height/2 - 4, 0).render(ms);
}
}

View file

@ -10,6 +10,7 @@ import org.apache.commons.lang3.mutable.MutableInt;
import com.electronwill.nightconfig.core.AbstractConfig;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.foundation.config.ui.entries.BooleanEntry;
import com.simibubi.create.foundation.config.ui.entries.SubMenuEntry;
import com.simibubi.create.foundation.gui.ScreenOpener;
import com.simibubi.create.foundation.gui.TextStencilElement;
@ -54,8 +55,10 @@ public class SubMenuConfigScreen extends ConfigScreen {
MutableInt y = new MutableInt(15);
configGroup.valueMap().forEach((s, o) -> {
String humanKey = toHumanReadable(s);
if (o instanceof AbstractConfig) {
SubMenuEntry entry = new SubMenuEntry(this, toHumanReadable(s), spec, (UnmodifiableConfig) o);
SubMenuEntry entry = new SubMenuEntry(this, humanKey, spec, (UnmodifiableConfig) o);
list.children().add(entry);
} else if (o instanceof ForgeConfigSpec.ConfigValue<?>) {
@ -63,11 +66,16 @@ public class SubMenuConfigScreen extends ConfigScreen {
ForgeConfigSpec.ValueSpec valueSpec = spec.getRaw(configValue.getPath());
Object value = configValue.get();
AbstractSimiWidget widget = createWidgetForValue(configValue, valueSpec, value, s, this);
widget.y = y.getValue();
//list.children().add(new ConfigScreenList.WrappedEntry(widget));
list.children().add(new ConfigScreenList.LabeledEntry(toHumanReadable(s) + " : " + value));
//widgets.add(widget);
if (value instanceof Boolean) {
BooleanEntry entry = new BooleanEntry(humanKey, (ForgeConfigSpec.ConfigValue<Boolean>) configValue, valueSpec);
list.children().add(entry);
} else {
AbstractSimiWidget widget = createWidgetForValue(configValue, valueSpec, value, s, this);
widget.y = y.getValue();
//list.children().add(new ConfigScreenList.WrappedEntry(widget));
list.children().add(new ConfigScreenList.LabeledEntry(humanKey + " : " + value));
//widgets.add(widget);
}
}
y.add(50);

View file

@ -0,0 +1,65 @@
package com.simibubi.create.foundation.config.ui.entries;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.foundation.config.ui.ConfigButton;
import com.simibubi.create.foundation.gui.CombinedStencilElement;
import com.simibubi.create.foundation.gui.TextStencilElement;
import com.simibubi.create.foundation.gui.UIRenderHelper;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.ForgeConfigSpec;
public class BooleanEntry extends ValueEntry<Boolean> {
TextStencilElement enabled;
TextStencilElement disabled;
CombinedStencilElement buttonStencil;
ConfigButton button;
public BooleanEntry(String label, ForgeConfigSpec.ConfigValue<Boolean> value, ForgeConfigSpec.ValueSpec spec) {
super(label, value, spec);
enabled = new TextStencilElement(Minecraft.getInstance().fontRenderer, "Enabled")
.centered(true, true)
.withElementRenderer((ms, width, height) -> UIRenderHelper.angledGradient(ms, 0, 0, height/2, height, width, 0xff_88f788, 0xff_20cc20));
disabled = new TextStencilElement(Minecraft.getInstance().fontRenderer, "Disabled")
.centered(true, true)
.withElementRenderer((ms, width, height) -> UIRenderHelper.angledGradient(ms, 0, 0, height/2, height, width, 0xff_f78888, 0xff_cc2020));
button = ConfigButton.createFromStencilElement(0, 0, enabled)
.withCallback(() -> {
value.set(!value.get());
buttonStencil.withSecond(value.get() ? enabled : disabled);
});
buttonStencil = ((CombinedStencilElement) button.getStencilElement())
.withMode(CombinedStencilElement.ElementMode.BOTH)
.withSecond(value.get() ? enabled : disabled);
}
@Override
public void tick() {
super.tick();
button.tick();
}
@Override
public void render(MatrixStack ms, int index, int y, int x, int width, int height, int mouseX, int mouseY, boolean p_230432_9_, float partialTicks) {
super.render(ms, index, y, x, width, height, mouseX, mouseY, p_230432_9_, partialTicks);
button.x = x + width/2;
button.y = y;
button.withBounds(width/2 - 34, height).render(ms, mouseX, mouseY, partialTicks);
}
@Override
protected void onReset() {
buttonStencil.withSecond(value.get() ? enabled : disabled);
}
@Override
public boolean mouseClicked(double mX, double mY, int button) {
return this.button.mouseClicked(mX, mY, button) || super.mouseClicked(mX, mY, button);
}
}

View file

@ -0,0 +1,51 @@
package com.simibubi.create.foundation.config.ui.entries;
import com.mojang.blaze3d.matrix.MatrixStack;
import com.simibubi.create.foundation.config.ui.ConfigButton;
import com.simibubi.create.foundation.config.ui.ConfigScreenList;
import com.simibubi.create.foundation.gui.TextStencilElement;
import net.minecraft.client.Minecraft;
import net.minecraftforge.common.ForgeConfigSpec;
public class ValueEntry<T> extends ConfigScreenList.LabeledEntry {
protected ForgeConfigSpec.ConfigValue<T> value;
protected ForgeConfigSpec.ValueSpec spec;
protected ConfigButton reset;
public ValueEntry(String label, ForgeConfigSpec.ConfigValue<T> value, ForgeConfigSpec.ValueSpec spec) {
super(label);
this.value = value;
this.spec = spec;
TextStencilElement text = new TextStencilElement(Minecraft.getInstance().fontRenderer, "R").centered(true, true);
reset = ConfigButton.createFromStencilElement(0, 0, text)
.withBounds(30, 30)
.withCallback(() -> {
value.set((T) spec.getDefault());
this.onReset();
});
}
@Override
public void tick() {
reset.tick();
}
@Override
public void render(MatrixStack ms, int index, int y, int x, int width, int height, int mouseX, int mouseY, boolean p_230432_9_, float partialTicks) {
super.render(ms, index, y, x, width, height, mouseX, mouseY, p_230432_9_, partialTicks);
reset.x = x + width - 32;
reset.y = y + 10;
reset.render(ms, mouseX, mouseY, partialTicks);
}
@Override
public boolean mouseClicked(double mX, double mY, int button) {
return reset.mouseClicked(mX, mY, button);
}
protected void onReset() {}
}

View file

@ -23,6 +23,24 @@ public class CombinedStencilElement extends StencilElement {
return e;
}
public <T extends CombinedStencilElement> T withFirst(StencilElement element) {
this.element1 = element;
//noinspection unchecked
return (T) this;
}
public <T extends CombinedStencilElement> T withSecond(StencilElement element) {
this.element2 = element;
//noinspection unchecked
return (T) this;
}
public <T extends CombinedStencilElement> T withMode(ElementMode mode) {
this.mode = mode;
//noinspection unchecked
return (T) this;
}
@Override
protected void renderStencil(MatrixStack ms) {
ms.push();

View file

@ -21,14 +21,16 @@ public class DelegatedStencilElement extends StencilElement {
this.element = element;
}
public DelegatedStencilElement withStencilRenderer(ElementRenderer renderer) {
public <T extends DelegatedStencilElement> T withStencilRenderer(ElementRenderer renderer) {
stencil = renderer;
return this;
//noinspection unchecked
return (T) this;
}
public DelegatedStencilElement withElementRenderer(ElementRenderer renderer) {
public <T extends DelegatedStencilElement> T withElementRenderer(ElementRenderer renderer) {
element = renderer;
return this;
//noinspection unchecked
return (T) this;
}
@Override

View file

@ -61,10 +61,22 @@ public class TextStencilElement extends DelegatedStencilElement {
@Override
protected void renderElement(MatrixStack ms) {
element.render(ms, font.getWidth(component), height);
float x = 0, y = 0;
if (centerHorizontally)
x = width / 2f - font.getWidth(component) / 2f;
if (centerVertically)
y = height / 2f - font.FONT_HEIGHT / 2f;
ms.push();
ms.translate(x, y, 0);
element.render(ms, font.getWidth(component), font.FONT_HEIGHT + 2);
ms.pop();
}
public IFormattableTextComponent getComponent() {
return component;
}
public static class Centered extends TextStencilElement {

View file

@ -113,8 +113,8 @@ public class UIRenderHelper {
/**
* @see #angledGradient(MatrixStack, float, int, int, int, int, int, int, int)
*/
public static void angledGradient(@Nonnull MatrixStack ms, float angle, int x, int y, int width, int length, int color1, int color2) {
angledGradient(ms, angle, x, y, 0, width, length, color1, color2);
public static void angledGradient(@Nonnull MatrixStack ms, float angle, int x, int y, int breadth, int length, int color1, int color2) {
angledGradient(ms, angle, x, y, 0, breadth, length, color1, color2);
}
/**
* x and y specify the middle point of the starting edge
@ -122,16 +122,16 @@ public class UIRenderHelper {
* @param angle the angle of the gradient in degrees; 0° means from left to right
* @param color1 the color at the starting edge
* @param color2 the color at the ending edge
* @param width the total width of the gradient
* @param breadth the total width of the gradient
*
*/
public static void angledGradient(@Nonnull MatrixStack ms, float angle, int x, int y, int z, int width, int length, int color1, int color2) {
public static void angledGradient(@Nonnull MatrixStack ms, float angle, int x, int y, int z, int breadth, int length, int color1, int color2) {
ms.push();
ms.translate(x, y, z);
ms.multiply(Vector3f.POSITIVE_Z.getDegreesQuaternion(angle - 90));
Matrix4f model = ms.peek().getModel();
int w = width / 2;
int w = breadth / 2;
GuiUtils.drawGradientRect(model, 0, -w, 0, w, length, color1, color2);
ms.pop();