From 35ff8912de8f415abb31500f3a0ca14b30d1e5d1 Mon Sep 17 00:00:00 2001 From: janeptrv Date: Thu, 8 Oct 2020 14:20:51 -0400 Subject: [PATCH] Button positions now persist across restart, with some issues. --- .../java/pm/j4/petroleum/PetroleumMod.java | 9 +++-- .../pm/j4/petroleum/gui/PModMenuScreen.java | 17 ++++++-- .../pm/j4/petroleum/gui/PMovableButton.java | 24 +++++++---- .../pm/j4/petroleum/gui/POptionsScreen.java | 2 +- .../pm/j4/petroleum/mixin/ModListMixin.java | 2 +- .../modules/bindings/BindingManager.java | 5 +-- .../pm/j4/petroleum/modules/menu/ModMenu.java | 40 ++++++++++++++----- .../petroleum/util/config/ConfigManager.java | 15 +++++++ .../petroleum/util/config/GlobalConfig.java | 21 +++++++++- .../util/data/ButtonInformation.java | 13 ++++++ .../pm/j4/petroleum/util/data/Category.java | 12 +++--- .../pm/j4/petroleum/util/data/Coordinate.java | 11 ----- .../j4/petroleum/util/module/ModuleBase.java | 4 +- 13 files changed, 127 insertions(+), 48 deletions(-) create mode 100644 src/main/java/pm/j4/petroleum/util/data/ButtonInformation.java delete mode 100644 src/main/java/pm/j4/petroleum/util/data/Coordinate.java diff --git a/src/main/java/pm/j4/petroleum/PetroleumMod.java b/src/main/java/pm/j4/petroleum/PetroleumMod.java index 975e8a6..5f7f785 100644 --- a/src/main/java/pm/j4/petroleum/PetroleumMod.java +++ b/src/main/java/pm/j4/petroleum/PetroleumMod.java @@ -13,13 +13,13 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.options.KeyBinding; import net.minecraft.server.integrated.IntegratedServer; import pm.j4.petroleum.modules.ExampleModule; -import pm.j4.petroleum.util.module.ModuleBase; import pm.j4.petroleum.modules.bindings.BindingManager; import pm.j4.petroleum.modules.list.ModList; import pm.j4.petroleum.modules.menu.ModMenu; import pm.j4.petroleum.modules.splash.SplashText; import pm.j4.petroleum.util.config.ConfigHolder; import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.module.ModuleBase; //TODO: @@ -136,14 +136,17 @@ public class PetroleumMod implements ModInitializer { @Override public void onInitialize() { ConfigManager.initConfig(); - //initialize any keybinds, data, etc. - activeMods.forEach(ModuleBase::init); // always update mod data Optional modContainer = FabricLoader.getInstance().getModContainer("petroleum"); modContainer.ifPresent(container -> modData = container.getMetadata()); Optional conf = ConfigManager.getConfig(); + + + //initialize any keybinds, data, etc. + activeMods.forEach(ModuleBase::init); + //initialize keybind handler conf.ifPresent(configHolder -> ClientTickEvents.END_CLIENT_TICK.register(client -> { if (PetroleumMod.client != client) { diff --git a/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java b/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java index 6ce01af..9f477cd 100644 --- a/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java +++ b/src/main/java/pm/j4/petroleum/gui/PModMenuScreen.java @@ -11,8 +11,8 @@ import net.minecraft.text.LiteralText; import net.minecraft.text.TranslatableText; import pm.j4.petroleum.modules.menu.ModMenu; import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.data.ButtonInformation; import pm.j4.petroleum.util.data.Category; -import pm.j4.petroleum.util.data.Coordinate; /** * The type P mod menu screen. @@ -34,15 +34,24 @@ public class PModMenuScreen extends Screen { @Override protected void init() { - Map coordinateMap = ModMenu.getButtons(); + Map coordinateMap = ModMenu.getButtons(); coordinateMap.forEach((category, coord) -> { - this.addButton(new PMovableButton((int)(coord.x * this.width), (int)(coord.y * this.height), category, Category.getByCategory(category), this)); + this.addButton(new PMovableButton((int) (coord.x * this.width), + (int) (coord.y * this.height), + category, + Category.getByCategory(category), + coord.open, + this)); }); } @Override public void onClose() { - ConfigManager.getConfig().get().disableModule("petroleum.modmenu"); + if (ConfigManager.getConfig().isPresent()) { + ConfigManager.getConfig().get().disableModule("petroleum.modmenu"); + this.buttons.forEach(button -> ((PMovableButton) button).updateCoordinate()); + ConfigManager.save(); + } super.onClose(); } diff --git a/src/main/java/pm/j4/petroleum/gui/PMovableButton.java b/src/main/java/pm/j4/petroleum/gui/PMovableButton.java index 18e3b21..e810493 100644 --- a/src/main/java/pm/j4/petroleum/gui/PMovableButton.java +++ b/src/main/java/pm/j4/petroleum/gui/PMovableButton.java @@ -14,7 +14,7 @@ import net.minecraft.text.TranslatableText; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Matrix4f; import pm.j4.petroleum.modules.menu.ModMenu; -import pm.j4.petroleum.util.data.Coordinate; +import pm.j4.petroleum.util.data.ButtonInformation; import pm.j4.petroleum.util.module.ModuleBase; /** @@ -63,9 +63,9 @@ public class PMovableButton extends AbstractButtonWidget { */ private final int padding = 5; - private String category; + private final String category; - private PModMenuScreen parent; + private final PModMenuScreen parent; /** * Instantiates a new P movable button. @@ -75,7 +75,7 @@ public class PMovableButton extends AbstractButtonWidget { * @param categoryName the category name * @param modules the modules */ - public PMovableButton(int x, int y, String categoryName, List modules, PModMenuScreen parent) { + public PMovableButton(int x, int y, String categoryName, List modules, boolean open, PModMenuScreen parent) { super(x, y, 0, 0, new TranslatableText(categoryName)); this.category = categoryName; int w = MinecraftClient.getInstance().textRenderer.getWidth(categoryName) + 8; @@ -87,6 +87,7 @@ public class PMovableButton extends AbstractButtonWidget { this.collapsedHeight = h; this.expandedHeight = 0; this.moduleHeight = h; + this.expanded = open; this.modules = modules; this.parent = parent; } @@ -117,8 +118,13 @@ public class PMovableButton extends AbstractButtonWidget { protected void onDrag(double mouseX, double mouseY, double deltaX, double deltaY) { this.x += (int) deltaX; this.y += (int) deltaY; + // i really hate to do it but nowhere else will it properly save - ModMenu.updateCoord(this.category, new Coordinate((this.x / (double)parent.width), (this.y / (double)parent.height))); + this.updateCoordinate(); + } + + public void updateCoordinate() { + ModMenu.updateCoord(this.category, new ButtonInformation((this.x / (double) parent.width), (this.y / (double) parent.height), this.expanded)); } // fuck click sounds @@ -130,8 +136,7 @@ public class PMovableButton extends AbstractButtonWidget { if (bl && mouseY > this.y + this.collapsedHeight && mouseY < this.y + this.expandedHeight) { this.onExtraClick(mouseX, mouseY); return true; - } - else if (bl) { + } else if (bl) { this.onClick(mouseX, mouseY); return true; } @@ -188,6 +193,11 @@ public class PMovableButton extends AbstractButtonWidget { this.expandedWidth = w; } }); + // this should only run when opening the screen for the first time + if (this.expanded) { + this.height = expandedHeight; + this.width = expandedWidth; + } } MinecraftClient minecraftClient = MinecraftClient.getInstance(); diff --git a/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java b/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java index a7d1467..e20db0b 100644 --- a/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java +++ b/src/main/java/pm/j4/petroleum/gui/POptionsScreen.java @@ -19,8 +19,8 @@ import net.minecraft.text.StringVisitable; import net.minecraft.text.Text; import net.minecraft.text.TranslatableText; import pm.j4.petroleum.PetroleumMod; -import pm.j4.petroleum.util.module.ModuleBase; import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.module.ModuleBase; /** * The type P options screen. diff --git a/src/main/java/pm/j4/petroleum/mixin/ModListMixin.java b/src/main/java/pm/j4/petroleum/mixin/ModListMixin.java index 1d662af..7ee7248 100644 --- a/src/main/java/pm/j4/petroleum/mixin/ModListMixin.java +++ b/src/main/java/pm/j4/petroleum/mixin/ModListMixin.java @@ -15,10 +15,10 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import pm.j4.petroleum.util.module.ModuleBase; import pm.j4.petroleum.modules.list.ModList; import pm.j4.petroleum.util.config.ConfigHolder; import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.module.ModuleBase; /** * The type Mod list mixin. diff --git a/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java b/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java index 5d726c6..775b1ed 100644 --- a/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java +++ b/src/main/java/pm/j4/petroleum/modules/bindings/BindingManager.java @@ -9,11 +9,11 @@ import net.minecraft.text.TranslatableText; import org.lwjgl.glfw.GLFW; import pm.j4.petroleum.PetroleumMod; import pm.j4.petroleum.gui.PModuleConfigEntry; +import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.config.GlobalConfig; import pm.j4.petroleum.util.module.ConfigurationOption; import pm.j4.petroleum.util.module.ModuleBase; import pm.j4.petroleum.util.module.option.KeybindValue; -import pm.j4.petroleum.util.config.ConfigManager; -import pm.j4.petroleum.util.config.GlobalConfig; /** * The type Binding manager. @@ -44,7 +44,6 @@ public class BindingManager extends ModuleBase { Map, ModuleBase> mapped = new HashMap<>(); if (ConfigManager.getConfig().isPresent()) { Map binds = ConfigManager.getConfig().get().globalConfig.bindings; - System.out.println("Number of bindings: " + binds.size()); binds.forEach((key, func) -> mapped.put(new ConfigurationOption<>(new KeybindValue(key)), func)); } mapped.forEach((bind, module) -> { diff --git a/src/main/java/pm/j4/petroleum/modules/menu/ModMenu.java b/src/main/java/pm/j4/petroleum/modules/menu/ModMenu.java index cc444ce..d973c2a 100644 --- a/src/main/java/pm/j4/petroleum/modules/menu/ModMenu.java +++ b/src/main/java/pm/j4/petroleum/modules/menu/ModMenu.java @@ -5,17 +5,17 @@ import java.util.List; import java.util.Map; import net.minecraft.client.MinecraftClient; import pm.j4.petroleum.gui.PModMenuScreen; +import pm.j4.petroleum.util.config.ConfigManager; +import pm.j4.petroleum.util.data.ButtonInformation; import pm.j4.petroleum.util.data.Category; import pm.j4.petroleum.util.module.ModuleBase; -import pm.j4.petroleum.util.config.ConfigManager; -import pm.j4.petroleum.util.data.Coordinate; /** * The type Mod menu. */ public class ModMenu extends ModuleBase { - private static Map coordinates = new HashMap<>(); + private static final Map coordinates = new HashMap<>(); /** * Instantiates a new Mod menu. @@ -28,25 +28,43 @@ public class ModMenu extends ModuleBase { true); } + // TODO figure out resizing + // the number itself changes, so it should just be probably like some onResize bullshit in PModMenuScreen @Override public void init() { Map> categories = Category.getCategoryMap(); final double[] h = {.1}; categories.forEach((category, moduleList) -> { - System.out.println("category: " + category); - Coordinate coord = new Coordinate(.1, h[0]); + ButtonInformation conf = ConfigManager.getConfig().isPresent() ? + ConfigManager.getConfig().get().globalConfig.getButton(category) : + null; + ButtonInformation coord = conf != null ? conf : new ButtonInformation(.1, h[0], false); h[0] += .01; coordinates.put(category, coord); + if (ConfigManager.getConfig().isPresent()) { + ConfigManager.getConfig().get().globalConfig.setButton(category, coord); + } }); } - public static void updateCoord(String b, Coordinate c) { + public static void updateCoord(String b, ButtonInformation c) { + if (c.x < 0.05) { + c.x = 0.05; + } + if (c.x > .95) { + c.x = .95; + } + if (c.y < 0.05) { + c.y = 0.05; + } + if (c.y > .95) { + c.y = .95; + } if (coordinates.containsKey(b)) { coordinates.replace(b, c); - } - else { - System.out.println("issue! no matching coordinate found"); - System.out.println(b); + if (ConfigManager.getConfig().isPresent()) { + ConfigManager.getConfig().get().globalConfig.setButton(b, c); + } } } @@ -60,7 +78,7 @@ public class ModMenu extends ModuleBase { } } - public static Map getButtons() { + public static Map getButtons() { return coordinates; } } \ No newline at end of file diff --git a/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java b/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java index 99dc427..e83b5e4 100644 --- a/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java +++ b/src/main/java/pm/j4/petroleum/util/config/ConfigManager.java @@ -8,6 +8,8 @@ import java.util.*; import net.fabricmc.loader.api.FabricLoader; import pm.j4.petroleum.PetroleumMod; import pm.j4.petroleum.modules.bindings.BindingInfo; +import pm.j4.petroleum.modules.menu.ModMenu; +import pm.j4.petroleum.util.data.ButtonInformation; /** * The type Config manager. @@ -135,6 +137,12 @@ class SerializationHelper { }); jsonConfig.add("module_configuration", moduleConfigs); + JsonObject tabCoordinates = new JsonObject(); + ModMenu.getButtons().forEach((category, coordinates) -> { + tabCoordinates.add(category, ctx.serialize(coordinates)); + }); + jsonConfig.add("button_coordinates", tabCoordinates); + return jsonConfig; }; @@ -161,6 +169,13 @@ class SerializationHelper { } else { options = new HashMap<>(); } + if (obj.has("button_coordinates")) { + obj.get("button_coordinates").getAsJsonObject().entrySet().forEach( + value -> { + cfg.setButton(value.getKey(), ctx.deserialize(value.getValue(), ButtonInformation.class)); + } + ); + } PetroleumMod.getActiveMods().forEach(module -> { if (options.containsKey(module.getModuleName())) { cfg.deserializeModuleConfiguration(options.get(module.getModuleName()), module); diff --git a/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java b/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java index 5c70af0..ea4896f 100644 --- a/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java +++ b/src/main/java/pm/j4/petroleum/util/config/GlobalConfig.java @@ -5,10 +5,11 @@ import java.util.concurrent.atomic.AtomicBoolean; import net.minecraft.client.options.KeyBinding; import net.minecraft.client.util.InputUtil; import pm.j4.petroleum.PetroleumMod; +import pm.j4.petroleum.modules.bindings.BindingInfo; +import pm.j4.petroleum.util.data.ButtonInformation; import pm.j4.petroleum.util.module.ConfigurationOption; import pm.j4.petroleum.util.module.ModuleBase; import pm.j4.petroleum.util.module.option.OptionTypeMatcher; -import pm.j4.petroleum.modules.bindings.BindingInfo; /** * The type Global config. @@ -139,5 +140,23 @@ public class GlobalConfig extends Config { module.setConfigOption(option.key, new ConfigurationOption<>(OptionTypeMatcher.match(option.type, option.value))); }); } + + private final Map buttonLocations = new HashMap<>(); + + public void setButton(String category, ButtonInformation buttonInformation) { + if (buttonLocations.containsKey(category)) { + buttonLocations.replace(category, buttonInformation); + } else { + buttonLocations.put(category, buttonInformation); + } + } + + public ButtonInformation getButton(String category) { + if (buttonLocations.containsKey(category)) { + return buttonLocations.get(category); + } + System.out.println("Could not find button of category " + category); + return null; + } } diff --git a/src/main/java/pm/j4/petroleum/util/data/ButtonInformation.java b/src/main/java/pm/j4/petroleum/util/data/ButtonInformation.java new file mode 100644 index 0000000..b229bd2 --- /dev/null +++ b/src/main/java/pm/j4/petroleum/util/data/ButtonInformation.java @@ -0,0 +1,13 @@ +package pm.j4.petroleum.util.data; + +public class ButtonInformation { + public double x; + public double y; + public boolean open; + + public ButtonInformation(double x, double y, boolean open) { + this.x = x; + this.y = y; + this.open = open; + } +} diff --git a/src/main/java/pm/j4/petroleum/util/data/Category.java b/src/main/java/pm/j4/petroleum/util/data/Category.java index 6880bc3..1605615 100644 --- a/src/main/java/pm/j4/petroleum/util/data/Category.java +++ b/src/main/java/pm/j4/petroleum/util/data/Category.java @@ -1,6 +1,9 @@ package pm.j4.petroleum.util.data; -import java.util.*; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; import pm.j4.petroleum.PetroleumMod; import pm.j4.petroleum.util.module.ModuleBase; @@ -9,16 +12,15 @@ public class Category { List modules = PetroleumMod.getActiveMods(); Map> categoryMap = new HashMap<>(); modules.forEach(module -> { - if(!categoryMap.containsKey(module.getCategory())) { + if (!categoryMap.containsKey(module.getCategory())) { List m = new ArrayList<>(); m.add(module); categoryMap.put(module.getCategory(), m); - } - else { + } else { List m = categoryMap.get(module.getCategory()); List nm = new ArrayList<>(); nm.addAll(m); - m.add(module); + nm.add(module); categoryMap.replace(module.getCategory(), nm); } }); diff --git a/src/main/java/pm/j4/petroleum/util/data/Coordinate.java b/src/main/java/pm/j4/petroleum/util/data/Coordinate.java deleted file mode 100644 index 8321bda..0000000 --- a/src/main/java/pm/j4/petroleum/util/data/Coordinate.java +++ /dev/null @@ -1,11 +0,0 @@ -package pm.j4.petroleum.util.data; - -public class Coordinate { - public double x; - public double y; - - public Coordinate(double x, double y) { - this.x = x; - this.y = y; - } -} diff --git a/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java b/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java index 1164040..7b95e68 100644 --- a/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java +++ b/src/main/java/pm/j4/petroleum/util/module/ModuleBase.java @@ -70,7 +70,9 @@ public abstract class ModuleBase { private final String category; - public String getCategory() { return this.category; } + public String getCategory() { + return this.category; + } /** * The Readable name.