[Feature] Text input and Color Picker
This commit is contained in:
parent
bbce498129
commit
0a940d3e20
11 changed files with 261 additions and 13 deletions
|
@ -39,6 +39,20 @@ public class TestScreen extends LayoutScreen {
|
||||||
).centerHorizontal()
|
).centerHorizontal()
|
||||||
);
|
);
|
||||||
rows.addSpacer(16);
|
rows.addSpacer(16);
|
||||||
|
rows.add(new Input(Value.fitOrFill(), Value.fit(), Component.literal("Input"), "0xff00ff"));
|
||||||
|
rows.add(new ColorSwatch(Value.fit(), Value.fit(), ColorUtil.LIGHT_PURPLE).centerHorizontal());
|
||||||
|
rows.add(new ColorPicker(
|
||||||
|
Value.fill(),
|
||||||
|
Value.fit(),
|
||||||
|
Component.literal("Color"),
|
||||||
|
ColorUtil.GREEN
|
||||||
|
).centerHorizontal());
|
||||||
|
rows.add(new Text(
|
||||||
|
Value.fitOrFill(), Value.fixed(20),
|
||||||
|
Component.literal("Some blue text")
|
||||||
|
).centerHorizontal().setColor(ColorUtil.BLUE)
|
||||||
|
);
|
||||||
|
rows.addSpacer(16);
|
||||||
rows.add(new Image(
|
rows.add(new Image(
|
||||||
Value.fixed(24), Value.fixed(24),
|
Value.fixed(24), Value.fixed(24),
|
||||||
BCLib.makeID("icon.png"),
|
BCLib.makeID("icon.png"),
|
||||||
|
@ -51,6 +65,7 @@ public class TestScreen extends LayoutScreen {
|
||||||
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
|
"Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.")
|
||||||
).setColor(ColorUtil.LIGHT_PURPLE).centerHorizontal()
|
).setColor(ColorUtil.LIGHT_PURPLE).centerHorizontal()
|
||||||
);
|
);
|
||||||
|
|
||||||
rows.addSpacer(16);
|
rows.addSpacer(16);
|
||||||
rows.add(new Range<>(
|
rows.add(new Range<>(
|
||||||
Value.fill(), Value.fit(),
|
Value.fill(), Value.fit(),
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package org.betterx.bclib.integration.modmenu;
|
package org.betterx.bclib.integration.modmenu;
|
||||||
|
|
||||||
import org.betterx.bclib.client.gui.modmenu.MainScreen;
|
import org.betterx.bclib.client.gui.modmenu.TestScreen;
|
||||||
|
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
|
||||||
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
|
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
|
||||||
import com.terraformersmc.modmenu.api.ModMenuApi;
|
import com.terraformersmc.modmenu.api.ModMenuApi;
|
||||||
|
@ -26,8 +28,8 @@ public class ModMenuEntryPoint implements ModMenuApi {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ConfigScreenFactory<?> getModConfigScreenFactory() {
|
public ConfigScreenFactory<?> getModConfigScreenFactory() {
|
||||||
//return (parent) -> new TestScreen(parent, Component.literal("Hello Test"));
|
return (parent) -> new TestScreen(parent, Component.literal("Hello Test"));
|
||||||
return (parent) -> new MainScreen(parent);
|
//return (parent) -> new MainScreen(parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -165,16 +165,16 @@ public class ColorUtil {
|
||||||
int color, shift;
|
int color, shift;
|
||||||
if (len == 3) {
|
if (len == 3) {
|
||||||
hexColor = ""
|
hexColor = ""
|
||||||
+ hexColor.charAt(2) + hexColor.charAt(2)
|
+ hexColor.charAt(0) + hexColor.charAt(0)
|
||||||
+ hexColor.charAt(1) + hexColor.charAt(1)
|
+ hexColor.charAt(1) + hexColor.charAt(1)
|
||||||
+ hexColor.charAt(0) + hexColor.charAt(0);
|
+ hexColor.charAt(2) + hexColor.charAt(2);
|
||||||
len = 6;
|
len = 6;
|
||||||
} else if (len == 4) {
|
} else if (len == 4) {
|
||||||
hexColor = ""
|
hexColor = ""
|
||||||
+ hexColor.charAt(3) + hexColor.charAt(3)
|
+ hexColor.charAt(0) + hexColor.charAt(0)
|
||||||
+ hexColor.charAt(2) + hexColor.charAt(2)
|
|
||||||
+ hexColor.charAt(1) + hexColor.charAt(1)
|
+ hexColor.charAt(1) + hexColor.charAt(1)
|
||||||
+ hexColor.charAt(0) + hexColor.charAt(0);
|
+ hexColor.charAt(2) + hexColor.charAt(2)
|
||||||
|
+ hexColor.charAt(3) + hexColor.charAt(3);
|
||||||
len = 8;
|
len = 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -210,16 +210,16 @@ public class ColorUtil {
|
||||||
int color, shift;
|
int color, shift;
|
||||||
if (len == 3) {
|
if (len == 3) {
|
||||||
hexColor = ""
|
hexColor = ""
|
||||||
+ hexColor.charAt(2) + hexColor.charAt(2)
|
+ hexColor.charAt(0) + hexColor.charAt(0)
|
||||||
+ hexColor.charAt(1) + hexColor.charAt(1)
|
+ hexColor.charAt(1) + hexColor.charAt(1)
|
||||||
+ hexColor.charAt(0) + hexColor.charAt(0);
|
+ hexColor.charAt(2) + hexColor.charAt(2);
|
||||||
len = 6;
|
len = 6;
|
||||||
} else if (len == 4) {
|
} else if (len == 4) {
|
||||||
hexColor = ""
|
hexColor = ""
|
||||||
+ hexColor.charAt(3) + hexColor.charAt(3)
|
+ hexColor.charAt(0) + hexColor.charAt(0)
|
||||||
+ hexColor.charAt(2) + hexColor.charAt(2)
|
|
||||||
+ hexColor.charAt(1) + hexColor.charAt(1)
|
+ hexColor.charAt(1) + hexColor.charAt(1)
|
||||||
+ hexColor.charAt(0) + hexColor.charAt(0);
|
+ hexColor.charAt(2) + hexColor.charAt(2)
|
||||||
|
+ hexColor.charAt(3) + hexColor.charAt(3);
|
||||||
len = 8;
|
len = 8;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -208,5 +208,19 @@ public abstract class AbstractStack<R extends ComponentRenderer, T extends Abstr
|
||||||
add(r);
|
add(r);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Input addInput(
|
||||||
|
Value width, Value height, net.minecraft.network.chat.Component titleOrNull, String initialValue
|
||||||
|
) {
|
||||||
|
Input i = new Input(width, height, titleOrNull, initialValue);
|
||||||
|
add(i);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ColorSwatch addColorSwatch(Value width, Value height, int color) {
|
||||||
|
ColorSwatch c = new ColorSwatch(width, height, color);
|
||||||
|
add(c);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,42 @@
|
||||||
|
package org.betterx.ui.layout.components;
|
||||||
|
|
||||||
|
import org.betterx.ui.ColorUtil;
|
||||||
|
import org.betterx.ui.layout.values.Value;
|
||||||
|
|
||||||
|
import net.minecraft.network.chat.Component;
|
||||||
|
|
||||||
|
public class ColorPicker extends HorizontalStack {
|
||||||
|
ColorSwatch swatch;
|
||||||
|
Input input;
|
||||||
|
|
||||||
|
public ColorPicker(Value width, Value height, Component title, int color) {
|
||||||
|
super(width, height);
|
||||||
|
swatch = addColorSwatch(Value.fixed(20), Value.fixed(20), color);
|
||||||
|
input = addInput(Value.fill(), Value.fit(), title, ColorUtil.toRGBHex(color));
|
||||||
|
|
||||||
|
//input.setFilter(ColorUtil::validHexColor);
|
||||||
|
input.setResponder(this::inputResponder);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void inputResponder(String value) {
|
||||||
|
if (ColorUtil.validHexColor(value)) {
|
||||||
|
int color = ColorUtil.parseHex(value);
|
||||||
|
swatch.setColor(color);
|
||||||
|
swatch.setOffsetInner(false);
|
||||||
|
swatch.setBorderColor(ColorUtil.BLACK);
|
||||||
|
} else {
|
||||||
|
swatch.setOffsetInner(true);
|
||||||
|
swatch.setBorderColor(ColorUtil.RED);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getContentWidth() {
|
||||||
|
return swatch.getContentWidth() + input.getContentWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getContentHeight() {
|
||||||
|
return Math.max(swatch.getContentHeight(), input.getContentHeight());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package org.betterx.ui.layout.components;
|
||||||
|
|
||||||
|
import org.betterx.ui.ColorUtil;
|
||||||
|
import org.betterx.ui.layout.components.render.RenderHelper;
|
||||||
|
import org.betterx.ui.layout.values.Rectangle;
|
||||||
|
import org.betterx.ui.layout.values.Value;
|
||||||
|
|
||||||
|
import com.mojang.blaze3d.vertex.PoseStack;
|
||||||
|
import net.minecraft.client.gui.GuiComponent;
|
||||||
|
|
||||||
|
public class ColorSwatch extends CustomRenderComponent<ColorSwatch> {
|
||||||
|
private int color;
|
||||||
|
private int borderColor = ColorUtil.BLACK;
|
||||||
|
private boolean offsetInner = false;
|
||||||
|
|
||||||
|
public ColorSwatch(Value width, Value height, int color) {
|
||||||
|
super(width, height);
|
||||||
|
this.color = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void customRender(PoseStack stack, int x, int y, float deltaTicks, Rectangle bounds, Rectangle clipRect) {
|
||||||
|
int o = offsetInner ? 2 : 1;
|
||||||
|
RenderHelper.outline(stack, 0, 0, bounds.width, bounds.height, borderColor);
|
||||||
|
GuiComponent.fill(stack, o, o, bounds.width - o, bounds.height - o, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getColor() {
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ColorSwatch setColor(int color) {
|
||||||
|
this.color = color;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getBorderColor() {
|
||||||
|
return borderColor;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ColorSwatch setBorderColor(int color) {
|
||||||
|
this.borderColor = color;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean getOffsetInner() {
|
||||||
|
return offsetInner;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ColorSwatch setOffsetInner(boolean val) {
|
||||||
|
this.offsetInner = val;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getContentWidth() {
|
||||||
|
return 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getContentHeight() {
|
||||||
|
return 20;
|
||||||
|
}
|
||||||
|
}
|
|
@ -78,4 +78,9 @@ public class Input extends AbstractVanillaComponent<EditBox, Input> {
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Component contentComponent() {
|
||||||
|
return Component.literal(initialValue + "..");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
package org.betterx.ui.layout.components.render;
|
||||||
|
|
||||||
|
import org.betterx.ui.layout.components.AbstractVanillaComponentRenderer;
|
||||||
|
import org.betterx.ui.layout.components.Input;
|
||||||
|
|
||||||
|
import net.fabricmc.api.EnvType;
|
||||||
|
import net.fabricmc.api.Environment;
|
||||||
|
|
||||||
|
@Environment(EnvType.CLIENT)
|
||||||
|
public class EditBoxRenderer extends AbstractVanillaComponentRenderer<net.minecraft.client.gui.components.EditBox, Input> {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
package org.betterx.ui.layout.components.render;
|
||||||
|
|
||||||
|
import org.betterx.ui.ColorUtil;
|
||||||
|
|
||||||
|
import com.mojang.blaze3d.systems.RenderSystem;
|
||||||
|
import com.mojang.blaze3d.vertex.*;
|
||||||
|
import com.mojang.math.Matrix4f;
|
||||||
|
import net.minecraft.client.renderer.GameRenderer;
|
||||||
|
|
||||||
|
public class RenderHelper {
|
||||||
|
public static void outline(PoseStack poseStack, int x0, int y0, int x1, int y1, int color) {
|
||||||
|
outline(poseStack, x0, y0, x1, y1, color, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void outline(PoseStack poseStack, int x0, int y0, int x1, int y1, int color1, int color2) {
|
||||||
|
int n;
|
||||||
|
if (x1 < x0) {
|
||||||
|
n = x0;
|
||||||
|
x0 = x1;
|
||||||
|
x1 = n;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (y1 < y0) {
|
||||||
|
n = y0;
|
||||||
|
y0 = y1;
|
||||||
|
y1 = n;
|
||||||
|
}
|
||||||
|
y1--;
|
||||||
|
x1--;
|
||||||
|
|
||||||
|
Matrix4f transform = poseStack.last().pose();
|
||||||
|
innerHLine(transform, x0, x1, y0, color1);
|
||||||
|
innerVLine(transform, x0, y0 + 1, y1, color1);
|
||||||
|
innerHLine(transform, x0 + 1, x1, y1, color1);
|
||||||
|
innerVLine(transform, x1, y0 + 1, y1 - 1, color2);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void hLine(PoseStack poseStack, int x0, int x1, int y, int color) {
|
||||||
|
if (x1 < x0) {
|
||||||
|
int m = x0;
|
||||||
|
x0 = x1;
|
||||||
|
x1 = m;
|
||||||
|
}
|
||||||
|
|
||||||
|
innerHLine(poseStack.last().pose(), x0, x1, y, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void innerHLine(Matrix4f transform, int x0, int x1, int y, int color) {
|
||||||
|
innerFill(transform, x0, y, x1 + 1, y + 1, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void vLine(PoseStack poseStack, int x, int y0, int y1, int color) {
|
||||||
|
if (y1 < y0) {
|
||||||
|
int m = y0;
|
||||||
|
y0 = y1;
|
||||||
|
y1 = m;
|
||||||
|
}
|
||||||
|
innerVLine(poseStack.last().pose(), x, y0, y1, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static void innerVLine(Matrix4f transform, int x, int y0, int y1, int color) {
|
||||||
|
innerFill(transform, x, y0, x + 1, y1 + 1, color);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void innerFill(Matrix4f transform, int x0, int y0, int x1, int y1, int color) {
|
||||||
|
float[] cl = ColorUtil.toFloatArray(color);
|
||||||
|
|
||||||
|
BufferBuilder bufferBuilder = Tesselator.getInstance().getBuilder();
|
||||||
|
RenderSystem.enableBlend();
|
||||||
|
RenderSystem.disableTexture();
|
||||||
|
RenderSystem.defaultBlendFunc();
|
||||||
|
RenderSystem.setShader(GameRenderer::getPositionColorShader);
|
||||||
|
bufferBuilder.begin(VertexFormat.Mode.QUADS, DefaultVertexFormat.POSITION_COLOR);
|
||||||
|
bufferBuilder.vertex(transform, (float) x0, (float) y1, 0.0F).color(cl[0], cl[1], cl[2], cl[3]).endVertex();
|
||||||
|
bufferBuilder.vertex(transform, (float) x1, (float) y1, 0.0F).color(cl[0], cl[1], cl[2], cl[3]).endVertex();
|
||||||
|
bufferBuilder.vertex(transform, (float) x1, (float) y0, 0.0F).color(cl[0], cl[1], cl[2], cl[3]).endVertex();
|
||||||
|
bufferBuilder.vertex(transform, (float) x0, (float) y0, 0.0F).color(cl[0], cl[1], cl[2], cl[3]).endVertex();
|
||||||
|
BufferUploader.drawWithShader(bufferBuilder.end());
|
||||||
|
RenderSystem.enableTexture();
|
||||||
|
RenderSystem.disableBlend();
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,6 +38,7 @@
|
||||||
"mixins": [
|
"mixins": [
|
||||||
"together.mixins.common.json",
|
"together.mixins.common.json",
|
||||||
"together.mixins.client.json",
|
"together.mixins.client.json",
|
||||||
|
"ui.mixins.client.json",
|
||||||
"bclib.mixins.common.json",
|
"bclib.mixins.common.json",
|
||||||
"bclib.mixins.client.json"
|
"bclib.mixins.client.json"
|
||||||
],
|
],
|
||||||
|
|
10
src/main/resources/ui.mixins.client.json
Normal file
10
src/main/resources/ui.mixins.client.json
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"required": true,
|
||||||
|
"minVersion": "0.8",
|
||||||
|
"package": "org.betterx.ui.mixin.client",
|
||||||
|
"compatibilityLevel": "JAVA_17",
|
||||||
|
"client": [],
|
||||||
|
"injectors": {
|
||||||
|
"defaultRequire": 1
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue