[Feature] Text input and Color Picker

This commit is contained in:
Frank 2022-07-19 17:36:58 +02:00
parent bbce498129
commit 0a940d3e20
11 changed files with 261 additions and 13 deletions

View file

@ -165,16 +165,16 @@ public class ColorUtil {
int color, shift;
if (len == 3) {
hexColor = ""
+ hexColor.charAt(2) + hexColor.charAt(2)
+ hexColor.charAt(0) + hexColor.charAt(0)
+ hexColor.charAt(1) + hexColor.charAt(1)
+ hexColor.charAt(0) + hexColor.charAt(0);
+ hexColor.charAt(2) + hexColor.charAt(2);
len = 6;
} else if (len == 4) {
hexColor = ""
+ hexColor.charAt(3) + hexColor.charAt(3)
+ hexColor.charAt(2) + hexColor.charAt(2)
+ hexColor.charAt(0) + hexColor.charAt(0)
+ 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;
}
@ -210,16 +210,16 @@ public class ColorUtil {
int color, shift;
if (len == 3) {
hexColor = ""
+ hexColor.charAt(2) + hexColor.charAt(2)
+ hexColor.charAt(0) + hexColor.charAt(0)
+ hexColor.charAt(1) + hexColor.charAt(1)
+ hexColor.charAt(0) + hexColor.charAt(0);
+ hexColor.charAt(2) + hexColor.charAt(2);
len = 6;
} else if (len == 4) {
hexColor = ""
+ hexColor.charAt(3) + hexColor.charAt(3)
+ hexColor.charAt(2) + hexColor.charAt(2)
+ hexColor.charAt(0) + hexColor.charAt(0)
+ 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;
}

View file

@ -208,5 +208,19 @@ public abstract class AbstractStack<R extends ComponentRenderer, T extends Abstr
add(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;
}
}

View file

@ -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());
}
}

View file

@ -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;
}
}

View file

@ -78,4 +78,9 @@ public class Input extends AbstractVanillaComponent<EditBox, Input> {
return this;
}
@Override
protected Component contentComponent() {
return Component.literal(initialValue + "..");
}
}

View file

@ -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> {
}

View file

@ -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();
}
}