From 5b04e821635fea8211b8fa40236ae070da319425 Mon Sep 17 00:00:00 2001 From: zelophed Date: Sun, 11 Apr 2021 03:12:43 +0200 Subject: [PATCH] boolean toggle --- .../config/ui/ConfigScreenList.java | 15 +++-- .../config/ui/SubMenuConfigScreen.java | 20 ++++-- .../config/ui/entries/BooleanEntry.java | 65 +++++++++++++++++++ .../config/ui/entries/ValueEntry.java | 51 +++++++++++++++ .../gui/CombinedStencilElement.java | 18 +++++ .../gui/DelegatedStencilElement.java | 10 +-- .../foundation/gui/TextStencilElement.java | 16 ++++- .../create/foundation/gui/UIRenderHelper.java | 10 +-- 8 files changed, 182 insertions(+), 23 deletions(-) create mode 100644 src/main/java/com/simibubi/create/foundation/config/ui/entries/BooleanEntry.java create mode 100644 src/main/java/com/simibubi/create/foundation/config/ui/entries/ValueEntry.java diff --git a/src/main/java/com/simibubi/create/foundation/config/ui/ConfigScreenList.java b/src/main/java/com/simibubi/create/foundation/config/ui/ConfigScreenList.java index 9b4d62b98..b06b70396 100644 --- a/src/main/java/com/simibubi/create/foundation/config/ui/ConfigScreenList.java +++ b/src/main/java/com/simibubi/create/foundation/config/ui/ConfigScreenList.java @@ -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 { @@ -28,12 +29,12 @@ public class ConfigScreenList extends ExtendedList { @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 { } public static abstract class Entry extends ExtendedList.AbstractListEntry { - 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 { @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); } } diff --git a/src/main/java/com/simibubi/create/foundation/config/ui/SubMenuConfigScreen.java b/src/main/java/com/simibubi/create/foundation/config/ui/SubMenuConfigScreen.java index d469b1bcb..eb232bf5c 100644 --- a/src/main/java/com/simibubi/create/foundation/config/ui/SubMenuConfigScreen.java +++ b/src/main/java/com/simibubi/create/foundation/config/ui/SubMenuConfigScreen.java @@ -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) 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); diff --git a/src/main/java/com/simibubi/create/foundation/config/ui/entries/BooleanEntry.java b/src/main/java/com/simibubi/create/foundation/config/ui/entries/BooleanEntry.java new file mode 100644 index 000000000..94f702df2 --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/config/ui/entries/BooleanEntry.java @@ -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 { + + TextStencilElement enabled; + TextStencilElement disabled; + CombinedStencilElement buttonStencil; + ConfigButton button; + + public BooleanEntry(String label, ForgeConfigSpec.ConfigValue 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); + } +} diff --git a/src/main/java/com/simibubi/create/foundation/config/ui/entries/ValueEntry.java b/src/main/java/com/simibubi/create/foundation/config/ui/entries/ValueEntry.java new file mode 100644 index 000000000..7a05ad41c --- /dev/null +++ b/src/main/java/com/simibubi/create/foundation/config/ui/entries/ValueEntry.java @@ -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 extends ConfigScreenList.LabeledEntry { + + protected ForgeConfigSpec.ConfigValue value; + protected ForgeConfigSpec.ValueSpec spec; + protected ConfigButton reset; + + public ValueEntry(String label, ForgeConfigSpec.ConfigValue 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() {} +} diff --git a/src/main/java/com/simibubi/create/foundation/gui/CombinedStencilElement.java b/src/main/java/com/simibubi/create/foundation/gui/CombinedStencilElement.java index caa577f58..07789469f 100644 --- a/src/main/java/com/simibubi/create/foundation/gui/CombinedStencilElement.java +++ b/src/main/java/com/simibubi/create/foundation/gui/CombinedStencilElement.java @@ -23,6 +23,24 @@ public class CombinedStencilElement extends StencilElement { return e; } + public T withFirst(StencilElement element) { + this.element1 = element; + //noinspection unchecked + return (T) this; + } + + public T withSecond(StencilElement element) { + this.element2 = element; + //noinspection unchecked + return (T) this; + } + + public T withMode(ElementMode mode) { + this.mode = mode; + //noinspection unchecked + return (T) this; + } + @Override protected void renderStencil(MatrixStack ms) { ms.push(); diff --git a/src/main/java/com/simibubi/create/foundation/gui/DelegatedStencilElement.java b/src/main/java/com/simibubi/create/foundation/gui/DelegatedStencilElement.java index 08973b0b2..9feb61f84 100644 --- a/src/main/java/com/simibubi/create/foundation/gui/DelegatedStencilElement.java +++ b/src/main/java/com/simibubi/create/foundation/gui/DelegatedStencilElement.java @@ -21,14 +21,16 @@ public class DelegatedStencilElement extends StencilElement { this.element = element; } - public DelegatedStencilElement withStencilRenderer(ElementRenderer renderer) { + public T withStencilRenderer(ElementRenderer renderer) { stencil = renderer; - return this; + //noinspection unchecked + return (T) this; } - public DelegatedStencilElement withElementRenderer(ElementRenderer renderer) { + public T withElementRenderer(ElementRenderer renderer) { element = renderer; - return this; + //noinspection unchecked + return (T) this; } @Override diff --git a/src/main/java/com/simibubi/create/foundation/gui/TextStencilElement.java b/src/main/java/com/simibubi/create/foundation/gui/TextStencilElement.java index b278c7151..f7f5a556e 100644 --- a/src/main/java/com/simibubi/create/foundation/gui/TextStencilElement.java +++ b/src/main/java/com/simibubi/create/foundation/gui/TextStencilElement.java @@ -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 { diff --git a/src/main/java/com/simibubi/create/foundation/gui/UIRenderHelper.java b/src/main/java/com/simibubi/create/foundation/gui/UIRenderHelper.java index 5fca88e7c..28b5e77ac 100644 --- a/src/main/java/com/simibubi/create/foundation/gui/UIRenderHelper.java +++ b/src/main/java/com/simibubi/create/foundation/gui/UIRenderHelper.java @@ -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();