aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io
diff options
context:
space:
mode:
authorFalkreon <falkreon@gmail.com>2019-07-22 19:25:10 -0500
committerFalkreon <falkreon@gmail.com>2019-07-22 19:25:10 -0500
commit7a0e6cbc2a2b917cbab65b37f9d379169bf6de8e (patch)
treeb3590022e26350c1b1f0674741dd3f2ee485da18 /src/main/java/io
parenta012d6ead79afa2eda1c8ad9dd1c22bf8d40aa40 (diff)
downloadLibGui-7a0e6cbc2a2b917cbab65b37f9d379169bf6de8e.tar.gz
LibGui-7a0e6cbc2a2b917cbab65b37f9d379169bf6de8e.tar.bz2
LibGui-7a0e6cbc2a2b917cbab65b37f9d379169bf6de8e.zip
Finish WToggleButton, fix config loading, almost done with ConfigGui
Diffstat (limited to 'src/main/java/io')
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java8
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java3
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java46
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java37
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java2
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java18
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java12
-rw-r--r--src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java67
8 files changed, 164 insertions, 29 deletions
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java b/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java
index 902c59a..1b8af53 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/CottonScreenController.java
@@ -5,6 +5,7 @@ import java.util.ArrayList;
import javax.annotation.Nullable;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
+import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.widget.*;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
@@ -28,7 +29,7 @@ import net.minecraft.recipe.RecipeInputProvider;
import net.minecraft.recipe.RecipeType;
import net.minecraft.world.World;
-public abstract class CottonScreenController extends CraftingContainer<Inventory> {
+public abstract class CottonScreenController extends CraftingContainer<Inventory> implements GuiDescription {
protected Inventory blockInventory;
protected PlayerInventory playerInventory;
@@ -37,7 +38,8 @@ public abstract class CottonScreenController extends CraftingContainer<Inventory
protected PropertyDelegate propertyDelegate;
protected WPanel rootPanel = new WGridPanel();
- protected int titleColor = 0xFF404040;
+ protected int titleColor = WLabel.DEFAULT_TEXT_COLOR;
+ protected int darkTitleColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR;
public CottonScreenController(RecipeType<?> recipeType, int syncId, PlayerInventory playerInventory) {
super(null, syncId);
@@ -63,7 +65,7 @@ public abstract class CottonScreenController extends CraftingContainer<Inventory
}
public int getTitleColor() {
- return titleColor;
+ return LibGuiClient.config.darkMode ? darkTitleColor : titleColor;
}
public CottonScreenController setRootPanel(WPanel panel) {
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java
index 642e329..2b2e0d9 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/GuiDescription.java
@@ -19,9 +19,6 @@ public interface GuiDescription {
@Environment(EnvType.CLIENT)
public void addPainters();
-
-
-
@Nullable
public PropertyDelegate getPropertyDelegate();
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java
index b47f8db..bf71991 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/ClientCottonScreen.java
@@ -2,6 +2,7 @@ package io.github.cottonmc.cotton.gui.client;
import io.github.cottonmc.cotton.gui.GuiDescription;
import io.github.cottonmc.cotton.gui.widget.WPanel;
+import io.github.cottonmc.cotton.gui.widget.WWidget;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.text.LiteralText;
@@ -14,6 +15,8 @@ public class ClientCottonScreen extends Screen {
protected int containerWidth = 0;
protected int containerHeight = 0;
+ protected WWidget lastResponder = null;
+
public ClientCottonScreen(GuiDescription description) {
super(new LiteralText(""));
this.description = description;
@@ -73,6 +76,10 @@ public class ClientCottonScreen extends Screen {
root.paintBackground(left, top);
}
}
+
+ if (getTitle() != null) {
+ font.draw(getTitle().asFormattedString(), left, top, description.getTitleColor());
+ }
}
@@ -80,4 +87,43 @@ public class ClientCottonScreen extends Screen {
public void tick() {
super.tick();
}
+
+ @Override
+ public boolean mouseClicked(double mouseX, double mouseY, int mouseButton) {
+ if (description.getRootPanel()==null) return super.mouseClicked(mouseX, mouseY, mouseButton);
+
+ boolean result = super.mouseClicked(mouseX, mouseY, mouseButton);
+ int containerX = (int)mouseX-left;
+ int containerY = (int)mouseY-top;
+ if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result;
+ lastResponder = description.getRootPanel().onMouseDown(containerX, containerY, mouseButton);
+ return result;
+ }
+
+ @Override
+ public boolean mouseReleased(double mouseX, double mouseY, int mouseButton) {
+ if (description.getRootPanel()==null) return super.mouseReleased(mouseX, mouseY, mouseButton);
+
+ boolean result = super.mouseReleased(mouseX, mouseY, mouseButton);
+ int containerX = (int)mouseX-left;
+ int containerY = (int)mouseY-top;
+ if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result;
+
+ WWidget responder = description.getRootPanel().onMouseUp(containerX, containerY, mouseButton);
+ if (responder!=null && responder==lastResponder) description.getRootPanel().onClick(containerX, containerY, mouseButton);
+ lastResponder = null;
+ return result;
+ }
+
+ @Override
+ public boolean mouseDragged(double mouseX, double mouseY, int mouseButton, double unknown_1, double unknown_2) {
+ if (description.getRootPanel()==null) return super.mouseDragged(mouseX, mouseY, mouseButton, unknown_1, unknown_2);
+
+ boolean result = super.mouseDragged(mouseX, mouseY, mouseButton, unknown_1, unknown_2);
+ int containerX = (int)mouseX-left;
+ int containerY = (int)mouseY-top;
+ if (containerX<0 || containerY<0 || containerX>=width || containerY>=height) return result;
+ description.getRootPanel().onMouseDrag(containerX, containerY, mouseButton);
+ return result;
+ }
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java
index 8666832..985cb2d 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LibGuiClient.java
@@ -11,11 +11,12 @@ import org.apache.logging.log4j.Logger;
import java.io.File;
import java.io.FileOutputStream;
+import java.nio.charset.StandardCharsets;
public class LibGuiClient implements ClientModInitializer {
public static final Logger logger = LogManager.getLogger();
public static final String MODID = "libgui";
- public static LibGuiConfig config;
+ public static volatile LibGuiConfig config;
public static final Jankson jankson = JanksonFactory.createJankson();
@@ -24,38 +25,40 @@ public class LibGuiClient implements ClientModInitializer {
config = loadConfig();
}
- public LibGuiConfig loadConfig() {
+ public static LibGuiConfig loadConfig() {
try {
- File file = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("libgui.json5").toFile();
+ File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5");
+
if (!file.exists()) saveConfig(new LibGuiConfig());
+
JsonObject json = jankson.load(file);
- LibGuiConfig result = jankson.fromJson(json, LibGuiConfig.class);
+ config = jankson.fromJson(json, LibGuiConfig.class);
+
+ /*
JsonElement jsonElementNew = jankson.toJson(new LibGuiConfig());
- if(jsonElementNew instanceof JsonObject){
+ if(jsonElementNew instanceof JsonObject) {
JsonObject jsonNew = (JsonObject) jsonElementNew;
- if(json.getDelta(jsonNew).size()>= 0){
- saveConfig(result);
+ if(json.getDelta(jsonNew).size()>= 0) { //TODO: Insert new keys as defaults into `json` IR object instead of writing the config out, so comments are preserved
+ saveConfig(config);
}
- }
+ }*/
} catch (Exception e) {
logger.error("[LibGui] Error loading config: {}", e.getMessage());
}
- return new LibGuiConfig();
+ return config;
}
- public void saveConfig(LibGuiConfig config) {
+ public static void saveConfig(LibGuiConfig config) {
try {
- File file = FabricLoader.getInstance().getConfigDirectory().toPath().resolve("libgui.json5").toFile();
+ File file = new File(FabricLoader.getInstance().getConfigDirectory(),"libgui.json5");
+
JsonElement json = jankson.toJson(config);
String result = json.toJson(true, true);
- if (!file.exists()) file.createNewFile();
- FileOutputStream out = new FileOutputStream(file,false);
- out.write(result.getBytes());
- out.flush();
- out.close();
+ try (FileOutputStream out = new FileOutputStream(file, false)) {
+ out.write(result.getBytes(StandardCharsets.UTF_8));
+ }
} catch (Exception e) {
logger.error("[LibGui] Error saving config: {}", e.getMessage());
}
}
-
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java
index c253cf1..9658fb5 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/LightweightGuiDescription.java
@@ -25,7 +25,7 @@ public class LightweightGuiDescription implements GuiDescription {
@Override
public int getTitleColor() {
- return titleColor;
+ return (LibGuiClient.config.darkMode) ? darkmodeTitleColor : titleColor;
}
@Override
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java
index cb130eb..d2e7ce3 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ConfigGui.java
@@ -1,10 +1,11 @@
package io.github.cottonmc.cotton.gui.client.modmenu;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
+import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.client.LightweightGuiDescription;
import io.github.cottonmc.cotton.gui.widget.WGridPanel;
-import io.github.cottonmc.cotton.gui.widget.WSprite;
-import net.minecraft.util.Identifier;
+import io.github.cottonmc.cotton.gui.widget.WToggleButton;
+import net.minecraft.text.TranslatableText;
public class ConfigGui extends LightweightGuiDescription {
@@ -12,8 +13,17 @@ public class ConfigGui extends LightweightGuiDescription {
WGridPanel root = new WGridPanel();
setRootPanel(root);
- root.add(new WSprite(new Identifier("libgui:icon.png")), 0, 0, 2, 2);
+ WToggleButton darkmodeButton = new WToggleButton(new TranslatableText("option.libgui.darkmode")) {
+ @Override
+ public void onToggle(boolean on) {
+ LibGuiClient.config.darkMode = on;
+ LibGuiClient.saveConfig(LibGuiClient.config);
+ }
+ };
+ darkmodeButton.setToggle(LibGuiClient.config.darkMode);
+ root.add(darkmodeButton, 0, 2, 8, 1); //Why isn't it toggling wider?
+
root.setBackgroundPainter(BackgroundPainter.VANILLA);
- root.setSize(64, 64);
+ root.setSize(9*18, 6*18);
}
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java
index e7d4590..327ee02 100644
--- a/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java
+++ b/src/main/java/io/github/cottonmc/cotton/gui/client/modmenu/ModMenuSupport.java
@@ -6,6 +6,7 @@ import io.github.cottonmc.cotton.gui.client.ClientCottonScreen;
import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.prospector.modmenu.api.ModMenuApi;
import net.minecraft.client.gui.screen.Screen;
+import net.minecraft.text.TranslatableText;
public class ModMenuSupport implements ModMenuApi {
@@ -16,6 +17,15 @@ public class ModMenuSupport implements ModMenuApi {
@Override
public Function<Screen, ? extends Screen> getConfigScreenFactory() {
- return screen -> new ClientCottonScreen(new ConfigGui());
+ return screen -> new ClientCottonScreen(new TranslatableText("options.libgui.libgui_settings"), new ConfigGui()) {
+ public void onClose() {
+ this.minecraft.openScreen(screen);
+ }
+
+ protected void init() {
+ super.init();
+ this.description.getRootPanel().validate(null);
+ };
+ };
}
}
diff --git a/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java
new file mode 100644
index 0000000..3ec1c46
--- /dev/null
+++ b/src/main/java/io/github/cottonmc/cotton/gui/widget/WToggleButton.java
@@ -0,0 +1,67 @@
+package io.github.cottonmc.cotton.gui.widget;
+
+import io.github.cottonmc.cotton.gui.client.LibGuiClient;
+import io.github.cottonmc.cotton.gui.client.ScreenDrawing;
+import net.fabricmc.api.EnvType;
+import net.fabricmc.api.Environment;
+import net.minecraft.client.MinecraftClient;
+import net.minecraft.client.sound.PositionedSoundInstance;
+import net.minecraft.sound.SoundEvents;
+import net.minecraft.text.Text;
+import net.minecraft.util.Identifier;
+
+public class WToggleButton extends WWidget {
+ Text label = null;
+ Identifier off_image = new Identifier("libgui:widget/toggle_off.png");
+ Identifier on_image = new Identifier("libgui:widget/toggle_on.png");
+ boolean on = false;
+ protected int color = WLabel.DEFAULT_TEXT_COLOR;
+ protected int darkmodeColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR;
+
+ public WToggleButton() {
+
+ }
+
+ public WToggleButton(Text text) {
+ this.label = text;
+ }
+
+ public WToggleButton color(int light, int dark) {
+ this.color = light;
+ this.darkmodeColor = dark;
+ return this;
+ }
+
+ @Environment(EnvType.CLIENT)
+ @Override
+ public void paintBackground(int x, int y) {
+ ScreenDrawing.rect(on ? on_image : off_image, x, y, 18, 8, 0xFFFFFFFF);
+
+ if (label!=null) {
+ ScreenDrawing.drawString(label.asFormattedString(), x+20, y, LibGuiClient.config.darkMode ? darkmodeColor : color);
+ }
+ }
+
+ @Override
+ public boolean canResize() {
+ return true;
+ }
+
+ @Override
+ public void onClick(int x, int y, int button) {
+ super.onClick(x, y, button);
+
+ MinecraftClient.getInstance().getSoundManager().play(PositionedSoundInstance.master(SoundEvents.UI_BUTTON_CLICK, 1.0F));
+ this.on = !this.on;
+
+ onToggle(this.on);
+ }
+
+ public void onToggle(boolean on) {
+
+ }
+
+ public void setToggle(boolean on) {
+ this.on = on;
+ }
+}