diff options
| author | 2017-06-07 08:29:37 +0100 | |
|---|---|---|
| committer | 2017-06-07 08:29:37 +0100 | |
| commit | ba5ffc258f6d58bf9d01226baea016db10cfd811 (patch) | |
| tree | 4fd3b77e619198a47d95ee55a742c8057dea591a /src/main/java/cuchaz/enigma/config | |
| parent | Add support for custom themes (#59) (diff) | |
| download | enigma-fork-ba5ffc258f6d58bf9d01226baea016db10cfd811.tar.gz enigma-fork-ba5ffc258f6d58bf9d01226baea016db10cfd811.tar.xz enigma-fork-ba5ffc258f6d58bf9d01226baea016db10cfd811.zip | |
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
Diffstat (limited to 'src/main/java/cuchaz/enigma/config')
| -rw-r--r-- | src/main/java/cuchaz/enigma/config/Config.java | 164 | ||||
| -rw-r--r-- | src/main/java/cuchaz/enigma/config/Themes.java | 66 |
2 files changed, 170 insertions, 60 deletions
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; | |||
| 8 | import java.lang.reflect.Type; | 8 | import java.lang.reflect.Type; |
| 9 | import java.nio.charset.Charset; | 9 | import java.nio.charset.Charset; |
| 10 | 10 | ||
| 11 | /** | ||
| 12 | * Created by Mark on 04/06/2017. | ||
| 13 | */ | ||
| 14 | public class Config { | 11 | public class Config { |
| 15 | 12 | ||
| 16 | public static Config INSTANCE = new Config(); | 13 | private static final File DIR_HOME = new File(System.getProperty("user.home")); |
| 17 | 14 | private static final File ENIGMA_DIR = new File(DIR_HOME, ".enigma"); | |
| 18 | public Integer obfuscatedColor = 0xFFDCDC; | 15 | private static final File CONFIG_FILE = new File(ENIGMA_DIR, "config.json"); |
| 19 | public float obfuscatedHiglightAlpha = 1.0F; | 16 | private static final Config INSTANCE = new Config(); |
| 20 | public Integer obfuscatedColorOutline = 0xA05050; | 17 | |
| 21 | public float obfuscatedOutlineAlpha = 1.0F; | 18 | private final transient Gson gson; // transient to exclude it from being exposed |
| 22 | 19 | ||
| 23 | public Integer deobfuscatedColor = 0xDCFFDC; | 20 | public Integer obfuscatedColor; |
| 24 | public float deobfuscatedHiglightAlpha = 1.0F; | 21 | public float obfuscatedHiglightAlpha; |
| 25 | public Integer deobfuscatedColorOutline = 0x50A050; | 22 | public Integer obfuscatedColorOutline; |
| 26 | public float deobfuscatedOutlineAlpha = 1.0F; | 23 | public float obfuscatedOutlineAlpha; |
| 27 | 24 | public Integer deobfuscatedColor; | |
| 28 | public Integer otherColorOutline = 0xB4B4B4; | 25 | public float deobfuscatedHiglightAlpha; |
| 29 | public float otherOutlineAlpha = 1.0F; | 26 | public Integer deobfuscatedColorOutline; |
| 30 | 27 | public float deobfuscatedOutlineAlpha; | |
| 31 | //Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139 | 28 | public Integer otherColorOutline; |
| 32 | public Integer editorBackground = 0xFFFFFF; | 29 | public float otherOutlineAlpha; |
| 33 | public Integer highlightColor = 0x3333EE; | 30 | |
| 34 | public Integer stringColor = 0xCC6600; | 31 | //Defaults found here: https://github.com/Sciss/SyntaxPane/blob/122da367ff7a5d31627a70c62a48a9f0f4f85a0a/src/main/resources/de/sciss/syntaxpane/defaultsyntaxkit/config.properties#L139 |
| 35 | public Integer numberColor = 0x999933; | 32 | public Integer editorBackground; |
| 36 | public Integer operatorColor = 0x000000; | 33 | public Integer highlightColor; |
| 37 | public Integer delimiterColor = 0x000000; | 34 | |
| 38 | public Integer typeColor = 0x000000; | 35 | public Integer stringColor; |
| 39 | public Integer identifierColor = 0x000000; | 36 | public Integer numberColor; |
| 40 | public Integer defaultTextColor = 0x000000; | 37 | public Integer operatorColor; |
| 41 | 38 | public Integer delimiterColor; | |
| 42 | public boolean useSystemLAF = true; | 39 | public Integer typeColor; |
| 43 | 40 | public Integer identifierColor; | |
| 44 | public static void loadConfig() throws IOException { | 41 | public Integer defaultTextColor; |
| 45 | Gson gson = new GsonBuilder().registerTypeAdapter(Integer.class, new IntSerializer()).registerTypeAdapter(Integer.class, new IntDeserializer()).setPrettyPrinting().create(); | 42 | |
| 46 | File dirHome = new File(System.getProperty("user.home")); | 43 | public boolean useSystemLAF = true; |
| 47 | File engimaDir = new File(dirHome, ".enigma"); | 44 | |
| 48 | if(!engimaDir.exists()){ | 45 | private Config() { |
| 49 | engimaDir.mkdirs(); | 46 | gson = new GsonBuilder() |
| 50 | } | 47 | .registerTypeAdapter(Integer.class, new IntSerializer()) |
| 51 | File configFile = new File(engimaDir, "config.json"); | 48 | .registerTypeAdapter(Integer.class, new IntDeserializer()) |
| 52 | if (configFile.exists()) { | 49 | .registerTypeAdapter(Config.class, (InstanceCreator<Config>) type -> this) |
| 53 | INSTANCE = gson.fromJson(Files.toString(configFile, Charset.defaultCharset()), Config.class); | 50 | .setPrettyPrinting() |
| 54 | } else { | 51 | .create(); |
| 55 | Files.touch(configFile); | 52 | try { |
| 56 | } | 53 | this.loadConfig(); |
| 57 | Files.write(gson.toJson(INSTANCE), configFile, Charset.defaultCharset()); | 54 | } catch (IOException ignored) { |
| 58 | } | 55 | try { |
| 59 | 56 | this.reset(); | |
| 60 | private static class IntSerializer implements JsonSerializer<Integer> { | 57 | } catch (IOException ignored1) { |
| 61 | public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { | 58 | } |
| 62 | return new JsonPrimitive("#" + Integer.toHexString(src).toUpperCase()); | 59 | } |
| 63 | } | 60 | } |
| 64 | } | 61 | |
| 65 | 62 | public void loadConfig() throws IOException { | |
| 66 | private static class IntDeserializer implements JsonDeserializer<Integer> { | 63 | if (!ENIGMA_DIR.exists()) ENIGMA_DIR.mkdirs(); |
| 67 | public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { | 64 | File configFile = new File(ENIGMA_DIR, "config.json"); |
| 68 | return (int) Long.parseLong(json.getAsString().replace("#", ""), 16); | 65 | if (configFile.exists()) gson.fromJson(Files.toString(configFile, Charset.defaultCharset()), Config.class); |
| 69 | } | 66 | else { |
| 70 | } | 67 | this.reset(); |
| 71 | 68 | Files.touch(configFile); | |
| 72 | } | 69 | } |
| 70 | saveConfig(); | ||
| 71 | } | ||
| 72 | |||
| 73 | public void saveConfig() throws IOException { | ||
| 74 | Files.write(gson.toJson(this), CONFIG_FILE, Charset.defaultCharset()); | ||
| 75 | } | ||
| 76 | |||
| 77 | public void reset() throws IOException { | ||
| 78 | this.obfuscatedColor = 0xFFDCDC; | ||
| 79 | this.obfuscatedHiglightAlpha = 1.0F; | ||
| 80 | this.obfuscatedColorOutline = 0xA05050; | ||
| 81 | this.obfuscatedOutlineAlpha = 1.0F; | ||
| 82 | this.deobfuscatedColor = 0xDCFFDC; | ||
| 83 | this.deobfuscatedHiglightAlpha = 1.0F; | ||
| 84 | this.deobfuscatedColorOutline = 0x50A050; | ||
| 85 | this.deobfuscatedOutlineAlpha = 1.0F; | ||
| 86 | this.otherColorOutline = 0xB4B4B4; | ||
| 87 | this.otherOutlineAlpha = 1.0F; | ||
| 88 | this.editorBackground = 0xFFFFFF; | ||
| 89 | this.highlightColor = 0x3333EE; | ||
| 90 | this.stringColor = 0xCC6600; | ||
| 91 | this.numberColor = 0x999933; | ||
| 92 | this.operatorColor = 0x000000; | ||
| 93 | this.delimiterColor = 0x000000; | ||
| 94 | this.typeColor = 0x000000; | ||
| 95 | this.identifierColor = 0x000000; | ||
| 96 | this.defaultTextColor = 0x000000; | ||
| 97 | this.useSystemLAF = true; | ||
| 98 | this.saveConfig(); | ||
| 99 | } | ||
| 100 | |||
| 101 | private static class IntSerializer implements JsonSerializer<Integer> { | ||
| 102 | public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { | ||
| 103 | return new JsonPrimitive("#" + Integer.toHexString(src).toUpperCase()); | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | private static class IntDeserializer implements JsonDeserializer<Integer> { | ||
| 108 | public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) { | ||
| 109 | return (int) Long.parseLong(json.getAsString().replace("#", ""), 16); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | public static Config getInstance() { | ||
| 114 | return INSTANCE; | ||
| 115 | } | ||
| 116 | } \ 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 @@ | |||
| 1 | package cuchaz.enigma.config; | ||
| 2 | |||
| 3 | import cuchaz.enigma.gui.Gui; | ||
| 4 | import cuchaz.enigma.gui.MinecraftSyntaxKit; | ||
| 5 | import cuchaz.enigma.gui.highlight.DeobfuscatedHighlightPainter; | ||
| 6 | import cuchaz.enigma.gui.highlight.ObfuscatedHighlightPainter; | ||
| 7 | import cuchaz.enigma.gui.highlight.OtherHighlightPainter; | ||
| 8 | import de.sciss.syntaxpane.DefaultSyntaxKit; | ||
| 9 | |||
| 10 | import java.awt.*; | ||
| 11 | import java.io.IOException; | ||
| 12 | |||
| 13 | public class Themes { | ||
| 14 | |||
| 15 | public static void setDefault(Gui gui) { | ||
| 16 | //TODO set to default | ||
| 17 | try { | ||
| 18 | Config.getInstance().reset(); | ||
| 19 | } catch (IOException e) { | ||
| 20 | e.printStackTrace(); | ||
| 21 | } | ||
| 22 | updateTheme(gui); | ||
| 23 | } | ||
| 24 | |||
| 25 | public static void setDark(Gui gui) { | ||
| 26 | //Based off colors found here: https://github.com/dracula/dracula-theme/ | ||
| 27 | Config.getInstance().obfuscatedColor = 0xFF5555; | ||
| 28 | Config.getInstance().obfuscatedHiglightAlpha = 0.3F; | ||
| 29 | Config.getInstance().obfuscatedColorOutline = 0xFF5555; | ||
| 30 | Config.getInstance().obfuscatedOutlineAlpha = 0.5F; | ||
| 31 | Config.getInstance().deobfuscatedColor = 0x50FA7B; | ||
| 32 | Config.getInstance().deobfuscatedHiglightAlpha = 0.3F; | ||
| 33 | Config.getInstance().deobfuscatedColorOutline = 0x50FA7B; | ||
| 34 | Config.getInstance().deobfuscatedOutlineAlpha = 0.5F; | ||
| 35 | Config.getInstance().otherColorOutline = 0xB4B4B4; | ||
| 36 | Config.getInstance().otherOutlineAlpha = 0.0F; | ||
| 37 | Config.getInstance().editorBackground = 0x282A36; | ||
| 38 | Config.getInstance().highlightColor = 0xFF79C6; | ||
| 39 | Config.getInstance().stringColor = 0xF1FA8C; | ||
| 40 | Config.getInstance().numberColor = 0xBD93F9; | ||
| 41 | Config.getInstance().operatorColor = 0xF8F8F2; | ||
| 42 | Config.getInstance().delimiterColor = 0xF8F8F2; | ||
| 43 | Config.getInstance().typeColor = 0xF8F8F2; | ||
| 44 | Config.getInstance().identifierColor = 0xF8F8F2; | ||
| 45 | Config.getInstance().defaultTextColor = 0xF8F8F2; | ||
| 46 | updateTheme(gui); | ||
| 47 | } | ||
| 48 | |||
| 49 | public static void updateTheme(Gui gui) { | ||
| 50 | try { | ||
| 51 | Config.getInstance().saveConfig(); | ||
| 52 | } catch (IOException e) { | ||
| 53 | e.printStackTrace(); | ||
| 54 | } | ||
| 55 | MinecraftSyntaxKit.invalidate(); | ||
| 56 | DefaultSyntaxKit.initKit(); | ||
| 57 | DefaultSyntaxKit.registerContentType("text/minecraft", MinecraftSyntaxKit.class.getName()); | ||
| 58 | gui.obfuscatedHighlightPainter = new ObfuscatedHighlightPainter(); | ||
| 59 | gui.deobfuscatedHighlightPainter = new DeobfuscatedHighlightPainter(); | ||
| 60 | gui.otherHighlightPainter = new OtherHighlightPainter(); | ||
| 61 | gui.editor.updateUI(); | ||
| 62 | gui.editor.setBackground(new Color(Config.getInstance().editorBackground)); | ||
| 63 | gui.getController().refreshCurrentClass(); | ||
| 64 | } | ||
| 65 | |||
| 66 | } | ||