implement more widgets and screen fixes
This commit is contained in:
@@ -8,8 +8,6 @@ import draylar.omegaconfig.api.SyncableExclusionStrategy;
|
||||
import draylar.omegaconfig.exception.NoValidConstructorException;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import net.fabricmc.api.ModInitializer;
|
||||
import net.fabricmc.fabric.api.network.ServerSidePacketRegistry;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerLoginNetworking;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
@@ -101,7 +99,7 @@ public class OmegaConfig implements ModInitializer {
|
||||
}
|
||||
}
|
||||
|
||||
private static <T extends Config> void writeConfig(Class<T> configClass, T instance) {
|
||||
public static <T extends Config> void writeConfig(Class<T> configClass, T instance) {
|
||||
// Write the config to disk with the default values.
|
||||
String json = GSON.toJson(instance);
|
||||
|
||||
@@ -199,7 +197,7 @@ public class OmegaConfig implements ModInitializer {
|
||||
}
|
||||
|
||||
public static Path getConfigPath(Config config) {
|
||||
return Paths.get(FabricLoader.getInstance().getConfigDir().toString(), String.format("%s.json", config.getFileName()));
|
||||
return Paths.get(FabricLoader.getInstance().getConfigDir().toString(), String.format("%s.json", config.getName()));
|
||||
}
|
||||
|
||||
public static boolean configExists(Config config) {
|
||||
|
||||
@@ -10,7 +10,6 @@ import net.minecraft.client.RunArgs;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.nbt.ListTag;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.Unique;
|
||||
import org.spongepowered.asm.mixin.injection.At;
|
||||
@@ -47,7 +46,7 @@ public class ClientMixin {
|
||||
|
||||
// find configuration class by name
|
||||
for (Config config : OmegaConfig.getRegisteredConfigurations()) {
|
||||
if (config.getFileName().equals(name)) {
|
||||
if (config.getName().equals(name)) {
|
||||
// bring values from server to object
|
||||
Config server = OmegaConfig.GSON.fromJson(json, config.getClass());
|
||||
|
||||
@@ -83,7 +82,7 @@ public class ClientMixin {
|
||||
private void restoreConfigurations(Screen screen, CallbackInfo ci) {
|
||||
for(Config config : savedClientConfig) {
|
||||
for(Config potentiallySynced : OmegaConfig.getRegisteredConfigurations()) {
|
||||
if(config.getFileName().equals(potentiallySynced.getFileName())) {
|
||||
if(config.getName().equals(potentiallySynced.getName())) {
|
||||
boolean allConfigSyncs = Arrays.stream(config.getClass().getAnnotations()).anyMatch(annotation -> annotation instanceof Syncing);
|
||||
|
||||
// mutate object in registered configurations
|
||||
|
||||
+6
-7
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
+27
@@ -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) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+21
-7
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,7 +24,7 @@ public class TestConfig implements Config {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
public String getName() {
|
||||
return "test-config";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user