big refactoring and cleanup
This commit is contained in:
@@ -3,27 +3,21 @@ package draylar.omegaconfiggui;
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import draylar.omegaconfiggui.api.screen.ModMenuHelper;
|
||||
import draylar.omegaconfiggui.api.screen.OmegaConfigScreen;
|
||||
import draylar.omegaconfiggui.api.screen.OmegaConfigScreenSupplier;
|
||||
import net.fabricmc.api.ClientModInitializer;
|
||||
import net.fabricmc.api.EnvType;
|
||||
import net.fabricmc.api.Environment;
|
||||
import net.fabricmc.loader.api.FabricLoader;
|
||||
|
||||
@Environment(EnvType.CLIENT)
|
||||
public class OmegaConfigGui implements ClientModInitializer {
|
||||
|
||||
@Override
|
||||
public void onInitializeClient() {
|
||||
|
||||
}
|
||||
public class OmegaConfigGui {
|
||||
|
||||
/**
|
||||
* Registers a ModMenu configuration screen for the given {@link Config} instance.
|
||||
* @param config registered config to create a ModMenu screen for
|
||||
* @param <T> config type
|
||||
*/
|
||||
public static <T extends Config> void registerConfigScreen(T config) {
|
||||
registerConfigScreen(config, parent -> new OmegaConfigScreen<>(config, parent));
|
||||
}
|
||||
|
||||
public static <T extends Config> void registerConfigScreen(T config, OmegaConfigScreenSupplier<T> screenFactory) {
|
||||
if(FabricLoader.getInstance().isModLoaded("modmenu")) {
|
||||
ModMenuHelper.injectScreen(config, screenFactory);
|
||||
ModMenuHelper.injectScreen(config, parent -> new OmegaConfigScreen<>(config, parent));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,15 +14,11 @@ import java.util.Map;
|
||||
public class ModMenuHelper {
|
||||
|
||||
public static <T extends Config> void injectScreen(T config, OmegaConfigScreenSupplier<T> factory) {
|
||||
// collect existing configurations
|
||||
ImmutableMap<String, ConfigScreenFactory<?>> configScreenFactories = ModMenuAccessor.getConfigScreenFactories();
|
||||
Map<String, ConfigScreenFactory<?>> nMap = new HashMap<>();
|
||||
nMap.putAll(configScreenFactories);
|
||||
|
||||
// add our factory
|
||||
nMap.put(config.getModid(), factory::get);
|
||||
|
||||
// they will suspect nothing
|
||||
ModMenuAccessor.setConfigScreenFactories(ImmutableMap.copyOf(nMap));
|
||||
ModMenuAccessor.setConfigScreenFactories(
|
||||
new ImmutableMap.Builder<String, ConfigScreenFactory<?>>()
|
||||
.putAll(ModMenuAccessor.getConfigScreenFactories())
|
||||
.put(config.getModid(), factory::get)
|
||||
.build());
|
||||
}
|
||||
}
|
||||
|
||||
+24
-4
@@ -1,5 +1,6 @@
|
||||
package draylar.omegaconfiggui.api.screen;
|
||||
|
||||
import com.mojang.blaze3d.systems.RenderSystem;
|
||||
import draylar.omegaconfig.api.Config;
|
||||
import draylar.omegaconfiggui.api.screen.widget.LabelWidget;
|
||||
import draylar.omegaconfiggui.api.screen.widget.TypeWidgets;
|
||||
@@ -11,6 +12,9 @@ import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.client.gui.widget.ButtonWidget;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.client.render.BufferBuilder;
|
||||
import net.minecraft.client.render.Tessellator;
|
||||
import net.minecraft.client.render.VertexFormats;
|
||||
import net.minecraft.client.util.math.MatrixStack;
|
||||
import net.minecraft.text.LiteralText;
|
||||
import net.minecraft.util.Identifier;
|
||||
@@ -50,7 +54,7 @@ public class OmegaConfigScreen<T extends Config> extends Screen {
|
||||
Class<?> unbox = TypeWidgets.unbox(field.getType());
|
||||
|
||||
// label
|
||||
addChild(new LabelWidget(150, 15 + 20 * over, new LiteralText(field.getName())));
|
||||
addChild(new LabelWidget(150, 15 + height / 6 + 20 * over, new LiteralText(field.getName())));
|
||||
|
||||
// get value
|
||||
field.setAccessible(true);
|
||||
@@ -60,9 +64,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 + 25 * over, 50, 20, new LiteralText(field.getName()), value);
|
||||
button = widgetSupplier.create(this,250, 5 + height / 6 + 25 * over, 100, 20, new LiteralText(field.getName()), value);
|
||||
} else {
|
||||
button = new TextFieldWidget(client.textRenderer, 250, 5 + 25 * over, 50, 20, new LiteralText(field.getName()));
|
||||
button = new TextFieldWidget(client.textRenderer, 250, 5 + height / 6 + 25 * over, 100, 20, new LiteralText(field.getName()));
|
||||
}
|
||||
|
||||
fieldWidgets.put(field, new Pair<>(widgetSupplier, button));
|
||||
@@ -116,6 +120,22 @@ public class OmegaConfigScreen<T extends Config> extends Screen {
|
||||
public void renderBackground(MatrixStack matrices) {
|
||||
// TODO: option for transparent BG
|
||||
// this.fillGradient(matrices, 0, 0, this.width, this.height, -1072689136, -804253680);
|
||||
this.renderBackgroundTexture(0);
|
||||
renderCustomBackgroundTexture(0, 64, 64, 64, 0, 0, this.height, this.width);
|
||||
renderCustomBackgroundTexture(0, 32, 32, 32, 0, this.height / 8, this.height / 8 * 6, this.width);
|
||||
textRenderer.draw(matrices, config.getName(), width / 2f - textRenderer.getWidth(config.getName()) / 2f, 10, 0xffffff);
|
||||
}
|
||||
|
||||
public void renderCustomBackgroundTexture(int vOffset, int r, int g, int b, int x, int y, int height, int width) {
|
||||
Tessellator tessellator = Tessellator.getInstance();
|
||||
BufferBuilder bufferBuilder = tessellator.getBuffer();
|
||||
this.client.getTextureManager().bindTexture(OPTIONS_BACKGROUND_TEXTURE);
|
||||
RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
|
||||
|
||||
bufferBuilder.begin(7, VertexFormats.POSITION_TEXTURE_COLOR);
|
||||
bufferBuilder.vertex(x, height + y, 0.0D).texture(0.0F, (float)height / 32.0F + (float)vOffset).color(r, g, b, 255).next();
|
||||
bufferBuilder.vertex(width + x, height + y, 0.0D).texture((float)width / 32.0F, (float)height / 32.0F + (float)vOffset).color(r, g, b, 255).next();
|
||||
bufferBuilder.vertex(width + x, y, 0.0D).texture((float)width / 32.0F, (float)vOffset).color(r, g, b, 255).next();
|
||||
bufferBuilder.vertex(x, y, 0.0D).texture(0.0F, (float)vOffset).color(r, g, b, 255).next();
|
||||
tessellator.draw();
|
||||
}
|
||||
}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget;
|
||||
|
||||
import draylar.omegaconfiggui.mixin.AbstractButtonWidgetAccessor;
|
||||
import net.minecraft.client.font.TextRenderer;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
public class BaseTextFieldWidget extends TextFieldWidget {
|
||||
|
||||
protected final Screen parent;
|
||||
|
||||
public BaseTextFieldWidget(Screen parent, TextRenderer textRenderer, int x, int y, int width, int height, Text text) {
|
||||
super(textRenderer, x, y, width, height, text);
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTextFieldFocused(boolean focused) {
|
||||
// unfocus all others
|
||||
parent.children().forEach(element -> {
|
||||
if(element instanceof AbstractButtonWidget) {
|
||||
((AbstractButtonWidgetAccessor) element).callSetFocused(false);
|
||||
}
|
||||
});
|
||||
|
||||
super.setFocused(focused);
|
||||
}
|
||||
}
|
||||
+4
-4
@@ -1,13 +1,13 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget;
|
||||
|
||||
import net.minecraft.client.font.TextRenderer;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.text.Text;
|
||||
|
||||
public class DoubleFieldWidget extends TextFieldWidget {
|
||||
public class DoubleFieldWidget extends BaseTextFieldWidget {
|
||||
|
||||
public DoubleFieldWidget(TextRenderer textRenderer, int x, int y, int width, int height, Text text) {
|
||||
super(textRenderer, x, y, width, height, text);
|
||||
public DoubleFieldWidget(Screen parent, TextRenderer textRenderer, int x, int y, int width, int height, Text text) {
|
||||
super(parent, textRenderer, x, y, width, height, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+2
-1
@@ -1,9 +1,10 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget;
|
||||
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public interface WidgetSupplier<T, A extends AbstractButtonWidget> {
|
||||
AbstractButtonWidget create(int x, int y, int width, int height, LiteralText prompt, T value);
|
||||
AbstractButtonWidget create(Screen parent, int x, int y, int width, int height, LiteralText prompt, T value);
|
||||
T get(A widget);
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,13 +1,14 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget.supplier;
|
||||
|
||||
import draylar.omegaconfiggui.api.screen.widget.WidgetSupplier;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.CheckboxWidget;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class BooleanWidgetSupplier implements WidgetSupplier<Boolean, CheckboxWidget> {
|
||||
|
||||
@Override
|
||||
public CheckboxWidget create(int x, int y, int width, int height, LiteralText prompt, Boolean value) {
|
||||
public CheckboxWidget create(Screen parent, int x, int y, int width, int height, LiteralText prompt, Boolean value) {
|
||||
return new CheckboxWidget(x, y, 20, 20, new LiteralText(""), value);
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -3,16 +3,17 @@ 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.screen.Screen;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class DoubleWidgetSupplier implements WidgetSupplier<Double, DoubleFieldWidget> {
|
||||
|
||||
@Override
|
||||
public DoubleFieldWidget create(int x, int y, int width, int height, LiteralText prompt, Double value) {
|
||||
public DoubleFieldWidget create(Screen parent, 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;
|
||||
DoubleFieldWidget widget = new DoubleFieldWidget(parent, MinecraftClient.getInstance().textRenderer, x, y, width, height, text);
|
||||
widget.setText(text.asString());
|
||||
return widget;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+10
-5
@@ -1,20 +1,25 @@
|
||||
package draylar.omegaconfiggui.api.screen.widget.supplier;
|
||||
|
||||
import draylar.omegaconfiggui.api.screen.widget.BaseTextFieldWidget;
|
||||
import draylar.omegaconfiggui.api.screen.widget.WidgetSupplier;
|
||||
import net.minecraft.client.MinecraftClient;
|
||||
import net.minecraft.client.gui.screen.Screen;
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import net.minecraft.client.gui.widget.TextFieldWidget;
|
||||
import net.minecraft.text.LiteralText;
|
||||
|
||||
public class StringWidgetSupplier implements WidgetSupplier<String, AbstractButtonWidget> {
|
||||
public class StringWidgetSupplier implements WidgetSupplier<String, TextFieldWidget> {
|
||||
|
||||
@Override
|
||||
public AbstractButtonWidget create(int x, int y, int width, int height, LiteralText prompt, String value) {
|
||||
return new TextFieldWidget(MinecraftClient.getInstance().textRenderer, x, y, width, height, new LiteralText(value));
|
||||
public TextFieldWidget create(Screen parent, int x, int y, int width, int height, LiteralText prompt, String value) {
|
||||
LiteralText text = new LiteralText(value);
|
||||
TextFieldWidget textField = new BaseTextFieldWidget(parent, MinecraftClient.getInstance().textRenderer, x, y, width, height, text);
|
||||
textField.setText(value);
|
||||
return textField;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String get(AbstractButtonWidget widget) {
|
||||
return widget.getMessage().asString();
|
||||
public String get(TextFieldWidget widget) {
|
||||
return widget.getText();
|
||||
}
|
||||
}
|
||||
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package draylar.omegaconfiggui.mixin;
|
||||
|
||||
import net.minecraft.client.gui.widget.AbstractButtonWidget;
|
||||
import org.spongepowered.asm.mixin.Mixin;
|
||||
import org.spongepowered.asm.mixin.gen.Invoker;
|
||||
|
||||
@Mixin(AbstractButtonWidget.class)
|
||||
public interface AbstractButtonWidgetAccessor {
|
||||
@Invoker
|
||||
void callSetFocused(boolean focused);
|
||||
}
|
||||
@@ -10,11 +10,6 @@
|
||||
"contact": {},
|
||||
"license": "MIT",
|
||||
"environment": "*",
|
||||
"entrypoints": {
|
||||
"client": [
|
||||
"draylar.omegaconfiggui.OmegaConfigGui"
|
||||
]
|
||||
},
|
||||
"mixins": [
|
||||
"omega-config-gui.mixins.json"
|
||||
],
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
"package": "draylar.omegaconfiggui.mixin",
|
||||
"compatibilityLevel": "JAVA_8",
|
||||
"mixins": [
|
||||
"AbstractButtonWidgetAccessor",
|
||||
"ModMenuAccessor"
|
||||
],
|
||||
"client": [
|
||||
|
||||
],
|
||||
"injectors": {
|
||||
"defaultRequire": 1
|
||||
|
||||
@@ -14,6 +14,8 @@ public class TestConfig implements Config {
|
||||
@Comment(value = "I'm a double.")
|
||||
double doubleTest = 0.0;
|
||||
|
||||
String stringTest = "Hello, world!";
|
||||
|
||||
@Comment(value = "This is an inner static class.")
|
||||
public Test test = new Test();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user