From cfe518380a52f0bd11e6cf03db22c0a459979456 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Tue, 6 May 2025 06:27:42 -0500 Subject: [PATCH] Theming --- .../saturnclient/SaturnClient.java | 5 +- .../saturnclient/config/ConfigManager.java | 38 +++++- .../saturnclient/config/ThemeManager.java | 109 +++++++----------- .../org/saturnclient/ui2/SaturnScreen.java | 2 - .../ui2/components/ElementRenderer.java | 4 - 5 files changed, 82 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/saturnclient/saturnclient/SaturnClient.java b/src/main/java/org/saturnclient/saturnclient/SaturnClient.java index 372dd34..a237e16 100644 --- a/src/main/java/org/saturnclient/saturnclient/SaturnClient.java +++ b/src/main/java/org/saturnclient/saturnclient/SaturnClient.java @@ -9,7 +9,6 @@ import net.minecraft.client.gui.screen.TitleScreen; import org.saturnclient.modules.ModManager; import org.saturnclient.saturnclient.auth.Auth; import org.saturnclient.saturnclient.config.ConfigManager; -import org.saturnclient.saturnclient.config.ThemeManager; import org.saturnclient.saturnclient.cosmetics.Hats; import org.saturnclient.saturnclient.cosmetics.cloaks.Cloaks; import org.saturnclient.saturnclient.event.KeyInputHandler; @@ -32,10 +31,8 @@ public class SaturnClient implements ModInitializer { public void onInitialize() { LOGGER.info("Initializing " + MOD_ID); client = MinecraftClient.getInstance(); - ModManager.init(); SaturnClientConfig.init(); - ThemeManager.load(); - ConfigManager.load(); + ModManager.init(); ClientLifecycleEvents.CLIENT_STOPPING.register(_o -> ConfigManager.save()); ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> { diff --git a/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java b/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java index 853ca64..3bcf10c 100644 --- a/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java +++ b/src/main/java/org/saturnclient/saturnclient/config/ConfigManager.java @@ -15,13 +15,14 @@ import java.util.Map; import org.saturnclient.saturnclient.SaturnClient; public class ConfigManager { - private static File configFile = new File( SaturnClient.client.runDirectory, "saturn.json"); private static Map>> properties = new HashMap<>(); + private static JsonObject cachedThemeJson = null; private Map> currentMap; + private String namespace; public ConfigManager(String namespace) { currentMap = new LinkedHashMap<>(); @@ -43,9 +44,44 @@ public class ConfigManager { public Property property(String name, Property value) { System.out.println("Adding property: " + name); currentMap.put(name, value); + loadProp(name, value); return value; } + @SuppressWarnings("unchecked") + public void loadProp(String name, Property prop) { + if (cachedThemeJson != null) { + JsonElement element = cachedThemeJson.get(namespace); + if (element != null && element.isJsonObject()) { + JsonObject theme = element.getAsJsonObject(); + JsonElement value = theme.get(name); + if (value != null && prop.matchesJson(value)) { + switch (prop.getType()) { + case BOOLEAN: + ((Property) prop).setValue(value.getAsBoolean()); + break; + case INTEGER: + ((Property) prop).setValue(value.getAsInt()); + break; + case FLOAT: + ((Property) prop).setValue(value.getAsFloat()); + break; + case STRING: + ((Property) prop).setValue(value.getAsString()); + break; + case HEX: + ((Property) prop).setValue(value.getAsInt()); + break; + default: + break; + } + } + } + } else { + load(); + } + } + public static void load() { try { SaturnClient.LOGGER.info("Starting to load config..."); diff --git a/src/main/java/org/saturnclient/saturnclient/config/ThemeManager.java b/src/main/java/org/saturnclient/saturnclient/config/ThemeManager.java index ca1bdee..08c9857 100644 --- a/src/main/java/org/saturnclient/saturnclient/config/ThemeManager.java +++ b/src/main/java/org/saturnclient/saturnclient/config/ThemeManager.java @@ -22,6 +22,7 @@ public class ThemeManager { SaturnClient.client.runDirectory, "saturn.theme.json"); private static Map>> properties = new HashMap<>(); + private static JsonObject cachedThemeJson = null; // private Map>> states; private Map> currentStyling; @@ -59,9 +60,12 @@ public class ThemeManager { // Generic method to store any type of property public Property property(String name, Property prop) { for (String state : states) { - properties.get(namespace+"@"+state).put(name, new Property(prop.value)); + Property property = new Property(prop.value); + properties.get(namespace+"@"+state).put(name, property); + loadProp(state, name, property); } + loadProp(null, name, prop); properties.get(namespace).put(name, new Property(prop.value)); currentStyling.put(name, prop); @@ -69,7 +73,43 @@ public class ThemeManager { } public void propertyStateDefault(String state, String name, T value) { - properties.get(namespace+"@"+state).get(name).setValue(value, value); // sets the current and default value + Property prop = properties.get(namespace+"@"+state).get(name); + prop.setValue(value, value); // sets the current and default value + loadProp(state, name, prop); + } + + @SuppressWarnings("unchecked") + public void loadProp(String state, String name, Property prop) { + if (cachedThemeJson == null) { + load(); + } + + JsonElement element = cachedThemeJson.get(state != null ? namespace+"@"+state : namespace); + if (element != null && element.isJsonObject()) { + JsonObject theme = element.getAsJsonObject(); + JsonElement value = theme.get(name); + if (value != null && prop.matchesJson(value)) { + switch (prop.getType()) { + case BOOLEAN: + ((Property) prop).setValue(value.getAsBoolean()); + break; + case INTEGER: + ((Property) prop).setValue(value.getAsInt()); + break; + case FLOAT: + ((Property) prop).setValue(value.getAsFloat()); + break; + case STRING: + ((Property) prop).setValue(value.getAsString()); + break; + case HEX: + ((Property) prop).setValue(value.getAsInt()); + break; + default: + break; + } + } + } } public static void load() { @@ -80,74 +120,13 @@ public class ThemeManager { Files.write(themeFile.toPath(), "{}".getBytes()); } - JsonObject jsonObject = JsonParser.parseString( - new String(Files.readAllBytes(themeFile.toPath()))).getAsJsonObject(); - - for (String namespace : properties.keySet()) { - JsonElement themeElement = jsonObject.get(namespace); - - if (themeElement == null) - continue; - - JsonObject theme = jsonObject.get(namespace).getAsJsonObject(); - - if (theme == null) { - continue; - } - - Map> propertyMap = properties.get(namespace); - loadProperties(theme, propertyMap); - } + cachedThemeJson = JsonParser.parseString( + new String(Files.readAllBytes(themeFile.toPath()))).getAsJsonObject(); } catch (IOException e) { SaturnClient.LOGGER.error("Error reading the theme file", e); } } - @SuppressWarnings("unchecked") - private static void loadProperties(JsonObject theme, Map> propertyMap) { - for (String propertyName : propertyMap.keySet()) { - JsonElement c = theme.get(propertyName); - - if (c == null) { - continue; - } - - Property p = propertyMap.get(propertyName); - if (p.matchesJson(c)) { - if (p.getType() == Property.PropertyType.NAMESPACE) { - // Handle nested namespace - JsonObject nestedTheme = c.getAsJsonObject(); - Map> nestedProperties = p.getNamespaceValue(); - loadProperties(nestedTheme, nestedProperties); - } else { - // Handle regular properties - switch (p.getType()) { - case BOOLEAN: - ((Property) p).value = c.getAsBoolean(); - break; - case INTEGER: - ((Property) p).value = c.getAsInt(); - break; - case FLOAT: - ((Property) p).value = c.getAsFloat(); - break; - case STRING: - ((Property) p).value = c.getAsString(); - break; - case HEX: - ((Property) p).value = c.getAsInt(); - break; - default: - break; - } - } - } else { - SaturnClient.LOGGER.warn( - "Property does not match JSON: " + propertyName); - } - } - } - public static void save() { try { SaturnClient.LOGGER.info("Starting to save theme..."); diff --git a/src/main/java/org/saturnclient/ui2/SaturnScreen.java b/src/main/java/org/saturnclient/ui2/SaturnScreen.java index effe5a4..8ff05ec 100644 --- a/src/main/java/org/saturnclient/ui2/SaturnScreen.java +++ b/src/main/java/org/saturnclient/ui2/SaturnScreen.java @@ -3,7 +3,6 @@ package org.saturnclient.ui2; import java.util.ArrayList; import java.util.List; -import org.saturnclient.saturnclient.config.ThemeManager; import org.saturnclient.saturnclient.mixin.DrawContextAccessor; import org.saturnclient.ui2.anim.Animation; import org.saturnclient.ui2.components.ElementRenderer; @@ -58,7 +57,6 @@ public abstract class SaturnScreen extends Screen { width *= 2; height *= 2; ui(); // abstraction to render the saturn ui and also render extra stuff here - ThemeManager.load(); Animation.execute((Float progress) -> { blurProgress = progress; diff --git a/src/main/java/org/saturnclient/ui2/components/ElementRenderer.java b/src/main/java/org/saturnclient/ui2/components/ElementRenderer.java index 841213f..8f4253a 100644 --- a/src/main/java/org/saturnclient/ui2/components/ElementRenderer.java +++ b/src/main/java/org/saturnclient/ui2/components/ElementRenderer.java @@ -26,8 +26,6 @@ public class ElementRenderer { public static void render(List elements, RenderScope renderScope, int mouseX, int mouseY) { for (Element element : elements) { - renderScope.enableScissor(element.x, element.y, element.x + element.width, element.y + element.height); - renderScope.matrices.push(); renderScope.setOpacity(element.opacity); renderScope.matrices.translate(element.x, element.y, 0); @@ -35,8 +33,6 @@ public class ElementRenderer { element.render(renderScope, new RenderContext(mouseX, mouseY, element)); renderScope.matrices.pop(); renderScope.setRenderLayer(null); - - renderScope.disableScissor(); } }