Theming
This commit is contained in:
@@ -9,7 +9,6 @@ import net.minecraft.client.gui.screen.TitleScreen;
|
|||||||
import org.saturnclient.modules.ModManager;
|
import org.saturnclient.modules.ModManager;
|
||||||
import org.saturnclient.saturnclient.auth.Auth;
|
import org.saturnclient.saturnclient.auth.Auth;
|
||||||
import org.saturnclient.saturnclient.config.ConfigManager;
|
import org.saturnclient.saturnclient.config.ConfigManager;
|
||||||
import org.saturnclient.saturnclient.config.ThemeManager;
|
|
||||||
import org.saturnclient.saturnclient.cosmetics.Hats;
|
import org.saturnclient.saturnclient.cosmetics.Hats;
|
||||||
import org.saturnclient.saturnclient.cosmetics.cloaks.Cloaks;
|
import org.saturnclient.saturnclient.cosmetics.cloaks.Cloaks;
|
||||||
import org.saturnclient.saturnclient.event.KeyInputHandler;
|
import org.saturnclient.saturnclient.event.KeyInputHandler;
|
||||||
@@ -32,10 +31,8 @@ public class SaturnClient implements ModInitializer {
|
|||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
LOGGER.info("Initializing " + MOD_ID);
|
LOGGER.info("Initializing " + MOD_ID);
|
||||||
client = MinecraftClient.getInstance();
|
client = MinecraftClient.getInstance();
|
||||||
ModManager.init();
|
|
||||||
SaturnClientConfig.init();
|
SaturnClientConfig.init();
|
||||||
ThemeManager.load();
|
ModManager.init();
|
||||||
ConfigManager.load();
|
|
||||||
|
|
||||||
ClientLifecycleEvents.CLIENT_STOPPING.register(_o -> ConfigManager.save());
|
ClientLifecycleEvents.CLIENT_STOPPING.register(_o -> ConfigManager.save());
|
||||||
ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
|
ScreenEvents.BEFORE_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
|
||||||
|
|||||||
@@ -15,13 +15,14 @@ import java.util.Map;
|
|||||||
import org.saturnclient.saturnclient.SaturnClient;
|
import org.saturnclient.saturnclient.SaturnClient;
|
||||||
|
|
||||||
public class ConfigManager {
|
public class ConfigManager {
|
||||||
|
|
||||||
private static File configFile = new File(
|
private static File configFile = new File(
|
||||||
SaturnClient.client.runDirectory,
|
SaturnClient.client.runDirectory,
|
||||||
"saturn.json");
|
"saturn.json");
|
||||||
private static Map<String, Map<String, Property<?>>> properties = new HashMap<>();
|
private static Map<String, Map<String, Property<?>>> properties = new HashMap<>();
|
||||||
|
private static JsonObject cachedThemeJson = null;
|
||||||
|
|
||||||
private Map<String, Property<?>> currentMap;
|
private Map<String, Property<?>> currentMap;
|
||||||
|
private String namespace;
|
||||||
|
|
||||||
public ConfigManager(String namespace) {
|
public ConfigManager(String namespace) {
|
||||||
currentMap = new LinkedHashMap<>();
|
currentMap = new LinkedHashMap<>();
|
||||||
@@ -43,9 +44,44 @@ public class ConfigManager {
|
|||||||
public <T> Property<T> property(String name, Property<T> value) {
|
public <T> Property<T> property(String name, Property<T> value) {
|
||||||
System.out.println("Adding property: " + name);
|
System.out.println("Adding property: " + name);
|
||||||
currentMap.put(name, value);
|
currentMap.put(name, value);
|
||||||
|
loadProp(name, value);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public <T> void loadProp(String name, Property<T> 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<Boolean>) prop).setValue(value.getAsBoolean());
|
||||||
|
break;
|
||||||
|
case INTEGER:
|
||||||
|
((Property<Integer>) prop).setValue(value.getAsInt());
|
||||||
|
break;
|
||||||
|
case FLOAT:
|
||||||
|
((Property<Float>) prop).setValue(value.getAsFloat());
|
||||||
|
break;
|
||||||
|
case STRING:
|
||||||
|
((Property<String>) prop).setValue(value.getAsString());
|
||||||
|
break;
|
||||||
|
case HEX:
|
||||||
|
((Property<Integer>) prop).setValue(value.getAsInt());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
load();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static void load() {
|
public static void load() {
|
||||||
try {
|
try {
|
||||||
SaturnClient.LOGGER.info("Starting to load config...");
|
SaturnClient.LOGGER.info("Starting to load config...");
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ public class ThemeManager {
|
|||||||
SaturnClient.client.runDirectory,
|
SaturnClient.client.runDirectory,
|
||||||
"saturn.theme.json");
|
"saturn.theme.json");
|
||||||
private static Map<String, Map<String, Property<?>>> properties = new HashMap<>();
|
private static Map<String, Map<String, Property<?>>> properties = new HashMap<>();
|
||||||
|
private static JsonObject cachedThemeJson = null;
|
||||||
|
|
||||||
// private Map<String, Map<String, Property<?>>> states;
|
// private Map<String, Map<String, Property<?>>> states;
|
||||||
private Map<String, Property<?>> currentStyling;
|
private Map<String, Property<?>> currentStyling;
|
||||||
@@ -59,9 +60,12 @@ public class ThemeManager {
|
|||||||
// Generic method to store any type of property
|
// Generic method to store any type of property
|
||||||
public <T> Property<T> property(String name, Property<T> prop) {
|
public <T> Property<T> property(String name, Property<T> prop) {
|
||||||
for (String state : states) {
|
for (String state : states) {
|
||||||
properties.get(namespace+"@"+state).put(name, new Property<T>(prop.value));
|
Property<?> property = new Property<T>(prop.value);
|
||||||
|
properties.get(namespace+"@"+state).put(name, property);
|
||||||
|
loadProp(state, name, property);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loadProp(null, name, prop);
|
||||||
properties.get(namespace).put(name, new Property<T>(prop.value));
|
properties.get(namespace).put(name, new Property<T>(prop.value));
|
||||||
currentStyling.put(name, prop);
|
currentStyling.put(name, prop);
|
||||||
|
|
||||||
@@ -69,7 +73,43 @@ public class ThemeManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> void propertyStateDefault(String state, String name, T value) {
|
public <T> 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 <T> void loadProp(String state, String name, Property<T> 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<Boolean>) prop).setValue(value.getAsBoolean());
|
||||||
|
break;
|
||||||
|
case INTEGER:
|
||||||
|
((Property<Integer>) prop).setValue(value.getAsInt());
|
||||||
|
break;
|
||||||
|
case FLOAT:
|
||||||
|
((Property<Float>) prop).setValue(value.getAsFloat());
|
||||||
|
break;
|
||||||
|
case STRING:
|
||||||
|
((Property<String>) prop).setValue(value.getAsString());
|
||||||
|
break;
|
||||||
|
case HEX:
|
||||||
|
((Property<Integer>) prop).setValue(value.getAsInt());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void load() {
|
public static void load() {
|
||||||
@@ -80,74 +120,13 @@ public class ThemeManager {
|
|||||||
Files.write(themeFile.toPath(), "{}".getBytes());
|
Files.write(themeFile.toPath(), "{}".getBytes());
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonObject jsonObject = JsonParser.parseString(
|
cachedThemeJson = JsonParser.parseString(
|
||||||
new String(Files.readAllBytes(themeFile.toPath()))).getAsJsonObject();
|
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<String, Property<?>> propertyMap = properties.get(namespace);
|
|
||||||
loadProperties(theme, propertyMap);
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
SaturnClient.LOGGER.error("Error reading the theme file", e);
|
SaturnClient.LOGGER.error("Error reading the theme file", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
private static void loadProperties(JsonObject theme, Map<String, Property<?>> 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<String, Property<?>> nestedProperties = p.getNamespaceValue();
|
|
||||||
loadProperties(nestedTheme, nestedProperties);
|
|
||||||
} else {
|
|
||||||
// Handle regular properties
|
|
||||||
switch (p.getType()) {
|
|
||||||
case BOOLEAN:
|
|
||||||
((Property<Boolean>) p).value = c.getAsBoolean();
|
|
||||||
break;
|
|
||||||
case INTEGER:
|
|
||||||
((Property<Integer>) p).value = c.getAsInt();
|
|
||||||
break;
|
|
||||||
case FLOAT:
|
|
||||||
((Property<Float>) p).value = c.getAsFloat();
|
|
||||||
break;
|
|
||||||
case STRING:
|
|
||||||
((Property<String>) p).value = c.getAsString();
|
|
||||||
break;
|
|
||||||
case HEX:
|
|
||||||
((Property<Integer>) p).value = c.getAsInt();
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
SaturnClient.LOGGER.warn(
|
|
||||||
"Property does not match JSON: " + propertyName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void save() {
|
public static void save() {
|
||||||
try {
|
try {
|
||||||
SaturnClient.LOGGER.info("Starting to save theme...");
|
SaturnClient.LOGGER.info("Starting to save theme...");
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package org.saturnclient.ui2;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.saturnclient.saturnclient.config.ThemeManager;
|
|
||||||
import org.saturnclient.saturnclient.mixin.DrawContextAccessor;
|
import org.saturnclient.saturnclient.mixin.DrawContextAccessor;
|
||||||
import org.saturnclient.ui2.anim.Animation;
|
import org.saturnclient.ui2.anim.Animation;
|
||||||
import org.saturnclient.ui2.components.ElementRenderer;
|
import org.saturnclient.ui2.components.ElementRenderer;
|
||||||
@@ -58,7 +57,6 @@ public abstract class SaturnScreen extends Screen {
|
|||||||
width *= 2;
|
width *= 2;
|
||||||
height *= 2;
|
height *= 2;
|
||||||
ui(); // abstraction to render the saturn ui and also render extra stuff here
|
ui(); // abstraction to render the saturn ui and also render extra stuff here
|
||||||
ThemeManager.load();
|
|
||||||
|
|
||||||
Animation.execute((Float progress) -> {
|
Animation.execute((Float progress) -> {
|
||||||
blurProgress = progress;
|
blurProgress = progress;
|
||||||
|
|||||||
@@ -26,8 +26,6 @@ public class ElementRenderer {
|
|||||||
|
|
||||||
public static void render(List<Element> elements, RenderScope renderScope, int mouseX, int mouseY) {
|
public static void render(List<Element> elements, RenderScope renderScope, int mouseX, int mouseY) {
|
||||||
for (Element element : elements) {
|
for (Element element : elements) {
|
||||||
renderScope.enableScissor(element.x, element.y, element.x + element.width, element.y + element.height);
|
|
||||||
|
|
||||||
renderScope.matrices.push();
|
renderScope.matrices.push();
|
||||||
renderScope.setOpacity(element.opacity);
|
renderScope.setOpacity(element.opacity);
|
||||||
renderScope.matrices.translate(element.x, element.y, 0);
|
renderScope.matrices.translate(element.x, element.y, 0);
|
||||||
@@ -35,8 +33,6 @@ public class ElementRenderer {
|
|||||||
element.render(renderScope, new RenderContext(mouseX, mouseY, element));
|
element.render(renderScope, new RenderContext(mouseX, mouseY, element));
|
||||||
renderScope.matrices.pop();
|
renderScope.matrices.pop();
|
||||||
renderScope.setRenderLayer(null);
|
renderScope.setRenderLayer(null);
|
||||||
|
|
||||||
renderScope.disableScissor();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user