implement more widgets and screen fixes

This commit is contained in:
Draylar
2021-03-12 16:09:44 -06:00
parent 46d78cc585
commit 683ff10962
6 changed files with 59 additions and 22 deletions
@@ -50,7 +50,7 @@ public class OmegaConfigScreen<T extends Config> extends Screen {
Class<?> unbox = TypeWidgets.unbox(field.getType());
// label
addChild(new LabelWidget(150, 5 + 20 * over, new LiteralText(field.getName())));
addChild(new LabelWidget(150, 15 + 20 * over, new LiteralText(field.getName())));
// get value
field.setAccessible(true);
@@ -60,9 +60,9 @@ public class OmegaConfigScreen<T extends Config> extends Screen {
AbstractButtonWidget button;
WidgetSupplier<Object, AbstractButtonWidget> widgetSupplier = (WidgetSupplier<Object, AbstractButtonWidget>) TypeWidgets.get(unbox);
if(widgetSupplier != null) {
button = widgetSupplier.create(250, 5 + 20 * over, 50, 50, new LiteralText(field.getName()), value);
button = widgetSupplier.create(250, 5 + 25 * over, 50, 20, new LiteralText(field.getName()), value);
} else {
button = new TextFieldWidget(client.textRenderer, 250, 5 + 20 * over, 50, 20, new LiteralText(field.getName()));
button = new TextFieldWidget(client.textRenderer, 250, 5 + 25 * over, 50, 20, new LiteralText(field.getName()));
}
fieldWidgets.put(field, new Pair<>(widgetSupplier, button));
@@ -114,9 +114,8 @@ public class OmegaConfigScreen<T extends Config> extends Screen {
@Override
public void renderBackground(MatrixStack matrices) {
super.renderBackground(matrices);
MinecraftClient.getInstance().getTextureManager().bindTexture(new Identifier("textures/block/dirt.png"));
drawTexture(matrices, 0, 0, 0, 0, 16, 16);
// TODO: option for transparent BG
// this.fillGradient(matrices, 0, 0, this.width, this.height, -1072689136, -804253680);
this.renderBackgroundTexture(0);
}
}
@@ -0,0 +1,27 @@
package draylar.omegaconfiggui.api.screen.widget;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
public class DoubleFieldWidget extends TextFieldWidget {
public DoubleFieldWidget(TextRenderer textRenderer, int x, int y, int width, int height, Text text) {
super(textRenderer, x, y, width, height, text);
}
@Override
public void write(String string) {
try {
if(string.equals(".") && !getText().contains(".")) {
super.write(string);
} else {
Double.parseDouble(string);
super.write(string);
}
} catch(NumberFormatException ignored) {
}
}
}
@@ -1,20 +1,34 @@
package draylar.omegaconfiggui.api.screen.widget.supplier;
import draylar.omegaconfiggui.api.screen.widget.DoubleFieldWidget;
import draylar.omegaconfiggui.api.screen.widget.WidgetSupplier;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.AbstractButtonWidget;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.LiteralText;
public class DoubleWidgetSupplier implements WidgetSupplier<Double, AbstractButtonWidget> {
public class DoubleWidgetSupplier implements WidgetSupplier<Double, DoubleFieldWidget> {
@Override
public AbstractButtonWidget create(int x, int y, int width, int height, LiteralText prompt, Double value) {
return new TextFieldWidget(MinecraftClient.getInstance().textRenderer, x, y, width, height, new LiteralText(String.valueOf(value)));
public DoubleFieldWidget create(int x, int y, int width, int height, LiteralText prompt, Double value) {
LiteralText text = new LiteralText(String.valueOf(value));
DoubleFieldWidget doubleField = new DoubleFieldWidget(MinecraftClient.getInstance().textRenderer, x, y, width, height, text);
doubleField.setText(text.asString());
return doubleField;
}
@Override
public Double get(AbstractButtonWidget widget) {
return Double.parseDouble(widget.getMessage().asString());
public Double get(DoubleFieldWidget widget) {
String message = widget.getText();
// if it is just a . or empty, return 0
if(message.equals(".") || message.isEmpty()) {
return 0.0;
}
// trim potential trailing .
if(message.indexOf(".") == message.length()) {
message = message.substring(0, message.length() - 1);
}
return Double.parseDouble(message);
}
}