From ba5ffc258f6d58bf9d01226baea016db10cfd811 Mon Sep 17 00:00:00 2001 From: Modmuss50 Date: Wed, 7 Jun 2017 08:29:37 +0100 Subject: Added theme switcher + dark theme (#60) * Initial work on the color config * Save and read config from file.+ * Allow changing the editor colors * Remove the right margin * Move config to the user's home dir * Use Guava instead of Apache commons * Add runtime theme switching, includes example "dark" theme. * Rename dark theme as requested * Small clean up * Include suggestions from @Thog --- src/main/java/cuchaz/enigma/config/Config.java | 164 ++++++++++++++++--------- src/main/java/cuchaz/enigma/config/Themes.java | 66 ++++++++++ 2 files changed, 170 insertions(+), 60 deletions(-) create mode 100644 src/main/java/cuchaz/enigma/config/Themes.java (limited to 'src/main/java/cuchaz/enigma/config') diff --git a/src/main/java/cuchaz/enigma/config/Config.java b/src/main/java/cuchaz/enigma/config/Config.java index 307b221..87ef353 100644 --- a/src/main/java/cuchaz/enigma/config/Config.java +++ b/src/main/java/cuchaz/enigma/config/Config.java @@ -8,65 +8,109 @@ import java.io.IOException; import java.lang.reflect.Type; import java.nio.charset.Charset; -/** - * Created by Mark on 04/06/2017. - */ public class Config { - public static Config INSTANCE = new Config(); - - public Integer obfuscatedColor = 0xFFDCDC; - public float obfuscatedHiglightAlpha = 1.0F; - public Integer obfuscatedColorOutline = 0xA05050; - public float obfuscatedOutlineAlpha = 1.0F; - - public Integer deobfuscatedColor = 0xDCFFDC; - public float deobfuscatedHiglightAlpha = 1.0F; - public Integer deobfuscatedColorOutline = 0x50A050; - public float deobfuscatedOutlineAlpha = 1.0F; - - public Integer otherColorOutline = 0xB4B4B4; - public float otherOutlineAlpha = 1.0F; - - //Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139 - public Integer editorBackground = 0xFFFFFF; - public Integer highlightColor = 0x3333EE; - public Integer stringColor = 0xCC6600; - public Integer numberColor = 0x999933; - public Integer operatorColor = 0x000000; - public Integer delimiterColor = 0x000000; - public Integer typeColor = 0x000000; - public Integer identifierColor = 0x000000; - public Integer defaultTextColor = 0x000000; - - public boolean useSystemLAF = true; - - public static void loadConfig() throws IOException { - Gson gson = new GsonBuilder().registerTypeAdapter(Integer.class, new IntSerializer()).registerTypeAdapter(Integer.class, new IntDeserializer()).setPrettyPrinting().create(); - File dirHome = new File(System.getProperty("user.home")); - File engimaDir = new File(dirHome, ".enigma"); - if(!engimaDir.exists()){ - engimaDir.mkdirs(); - } - File configFile = new File(engimaDir, "config.json"); - if (configFile.exists()) { - INSTANCE = gson.fromJson(Files.toString(configFile, Charset.defaultCharset()), Config.class); - } else { - Files.touch(configFile); - } - Files.write(gson.toJson(INSTANCE), configFile, Charset.defaultCharset()); - } - - private static class IntSerializer implements JsonSerializer { - public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { - return new JsonPrimitive("#" + Integer.toHexString(src).toUpperCase()); - } - } - - private static class IntDeserializer implements JsonDeserializer { - public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { - return (int) Long.parseLong(json.getAsString().replace("#", ""), 16); - } - } - -} + private static final File DIR_HOME = new File(System.getProperty("user.home")); + private static final File ENIGMA_DIR = new File(DIR_HOME, ".enigma"); + private static final File CONFIG_FILE = new File(ENIGMA_DIR, "config.json"); + private static final Config INSTANCE = new Config(); + + private final transient Gson gson; // transient to exclude it from being exposed + + public Integer obfuscatedColor; + public float obfuscatedHiglightAlpha; + public Integer obfuscatedColorOutline; + public float obfuscatedOutlineAlpha; + public Integer deobfuscatedColor; + public float deobfuscatedHiglightAlpha; + public Integer deobfuscatedColorOutline; + public float deobfuscatedOutlineAlpha; + public Integer otherColorOutline; + public float otherOutlineAlpha; + + //Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139 + public Integer editorBackground; + public Integer highlightColor; + + public Integer stringColor; + public Integer numberColor; + public Integer operatorColor; + public Integer delimiterColor; + public Integer typeColor; + public Integer identifierColor; + public Integer defaultTextColor; + + public boolean useSystemLAF = true; + + private Config() { + gson = new GsonBuilder() + .registerTypeAdapter(Integer.class, new IntSerializer()) + .registerTypeAdapter(Integer.class, new IntDeserializer()) + .registerTypeAdapter(Config.class, (InstanceCreator) type -> this) + .setPrettyPrinting() + .create(); + try { + this.loadConfig(); + } catch (IOException ignored) { + try { + this.reset(); + } catch (IOException ignored1) { + } + } + } + + public void loadConfig() throws IOException { + if (!ENIGMA_DIR.exists()) ENIGMA_DIR.mkdirs(); + File configFile = new File(ENIGMA_DIR, "config.json"); + if (configFile.exists()) gson.fromJson(Files.toString(configFile, Charset.defaultCharset()), Config.class); + else { + this.reset(); + Files.touch(configFile); + } + saveConfig(); + } + + public void saveConfig() throws IOException { + Files.write(gson.toJson(this), CONFIG_FILE, Charset.defaultCharset()); + } + + public void reset() throws IOException { + this.obfuscatedColor = 0xFFDCDC; + this.obfuscatedHiglightAlpha = 1.0F; + this.obfuscatedColorOutline = 0xA05050; + this.obfuscatedOutlineAlpha = 1.0F; + this.deobfuscatedColor = 0xDCFFDC; + this.deobfuscatedHiglightAlpha = 1.0F; + this.deobfuscatedColorOutline = 0x50A050; + this.deobfuscatedOutlineAlpha = 1.0F; + this.otherColorOutline = 0xB4B4B4; + this.otherOutlineAlpha = 1.0F; + this.editorBackground = 0xFFFFFF; + this.highlightColor = 0x3333EE; + this.stringColor = 0xCC6600; + this.numberColor = 0x999933; + this.operatorColor = 0x000000; + this.delimiterColor = 0x000000; + this.typeColor = 0x000000; + this.identifierColor = 0x000000; + this.defaultTextColor = 0x000000; + this.useSystemLAF = true; + this.saveConfig(); + } + + private static class IntSerializer implements JsonSerializer { + public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { + return new JsonPrimitive("#" + Integer.toHexString(src).toUpperCase()); + } + } + + private static class IntDeserializer implements JsonDeserializer { + public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { + return (int) Long.parseLong(json.getAsString().replace("#", ""), 16); + } + } + + public static Config getInstance() { + return INSTANCE; + } +} \ No newline at end of file diff --git a/src/main/java/cuchaz/enigma/config/Themes.java b/src/main/java/cuchaz/enigma/config/Themes.java new file mode 100644 index 0000000..79c245b --- /dev/null +++ b/src/main/java/cuchaz/enigma/config/Themes.java @@ -0,0 +1,66 @@ +package cuchaz.enigma.config; + +import cuchaz.enigma.gui.Gui; +import cuchaz.enigma.gui.MinecraftSyntaxKit; +import cuchaz.enigma.gui.highlight.DeobfuscatedHighlightPainter; +import cuchaz.enigma.gui.highlight.ObfuscatedHighlightPainter; +import cuchaz.enigma.gui.highlight.OtherHighlightPainter; +import de.sciss.syntaxpane.DefaultSyntaxKit; + +import java.awt.*; +import java.io.IOException; + +public class Themes { + + public static void setDefault(Gui gui) { + //TODO set to default + try { + Config.getInstance().reset(); + } catch (IOException e) { + e.printStackTrace(); + } + updateTheme(gui); + } + + public static void setDark(Gui gui) { + //Based off colors found here: https://github.com/dracula/dracula-theme/ + Config.getInstance().obfuscatedColor = 0xFF5555; + Config.getInstance().obfuscatedHiglightAlpha = 0.3F; + Config.getInstance().obfuscatedColorOutline = 0xFF5555; + Config.getInstance().obfuscatedOutlineAlpha = 0.5F; + Config.getInstance().deobfuscatedColor = 0x50FA7B; + Config.getInstance().deobfuscatedHiglightAlpha = 0.3F; + Config.getInstance().deobfuscatedColorOutline = 0x50FA7B; + Config.getInstance().deobfuscatedOutlineAlpha = 0.5F; + Config.getInstance().otherColorOutline = 0xB4B4B4; + Config.getInstance().otherOutlineAlpha = 0.0F; + Config.getInstance().editorBackground = 0x282A36; + Config.getInstance().highlightColor = 0xFF79C6; + Config.getInstance().stringColor = 0xF1FA8C; + Config.getInstance().numberColor = 0xBD93F9; + Config.getInstance().operatorColor = 0xF8F8F2; + Config.getInstance().delimiterColor = 0xF8F8F2; + Config.getInstance().typeColor = 0xF8F8F2; + Config.getInstance().identifierColor = 0xF8F8F2; + Config.getInstance().defaultTextColor = 0xF8F8F2; + updateTheme(gui); + } + + public static void updateTheme(Gui gui) { + try { + Config.getInstance().saveConfig(); + } catch (IOException e) { + e.printStackTrace(); + } + MinecraftSyntaxKit.invalidate(); + DefaultSyntaxKit.initKit(); + DefaultSyntaxKit.registerContentType("text/minecraft", MinecraftSyntaxKit.class.getName()); + gui.obfuscatedHighlightPainter = new ObfuscatedHighlightPainter(); + gui.deobfuscatedHighlightPainter = new DeobfuscatedHighlightPainter(); + gui.otherHighlightPainter = new OtherHighlightPainter(); + gui.editor.updateUI(); + gui.editor.setBackground(new Color(Config.getInstance().editorBackground)); + gui.getController().refreshCurrentClass(); + } + +} -- cgit v1.2.3