From e35b611da3c068bfea344be9a90c79fd68fac4ff Mon Sep 17 00:00:00 2001 From: Modmuss50 Date: Sun, 4 Jun 2017 16:36:21 +0100 Subject: Add support for custom themes (#59) --- src/main/java/cuchaz/enigma/Main.java | 4 +- src/main/java/cuchaz/enigma/config/Config.java | 72 ++++++++++++++++++++++ src/main/java/cuchaz/enigma/gui/Gui.java | 9 +-- .../java/cuchaz/enigma/gui/MinecraftSyntaxKit.java | 38 ++++++++++++ .../enigma/gui/highlight/BoxHighlightPainter.java | 5 ++ .../highlight/DeobfuscatedHighlightPainter.java | 4 +- .../gui/highlight/ObfuscatedHighlightPainter.java | 4 +- .../gui/highlight/OtherHighlightPainter.java | 4 +- 8 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 src/main/java/cuchaz/enigma/config/Config.java create mode 100644 src/main/java/cuchaz/enigma/gui/MinecraftSyntaxKit.java (limited to 'src/main/java') diff --git a/src/main/java/cuchaz/enigma/Main.java b/src/main/java/cuchaz/enigma/Main.java index fa8d0622..9f34cd3c 100644 --- a/src/main/java/cuchaz/enigma/Main.java +++ b/src/main/java/cuchaz/enigma/Main.java @@ -11,6 +11,7 @@ package cuchaz.enigma; +import cuchaz.enigma.config.Config; import cuchaz.enigma.gui.Gui; import javax.swing.*; @@ -20,7 +21,8 @@ import java.util.jar.JarFile; public class Main { public static void main(String[] args) throws Exception { - if (System.getProperty("enigma.useSystemLookAndFeel", "true").equals("true")) + Config.loadConfig(); + if (Config.INSTANCE.useSystemLAF) UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); Gui gui = new Gui(); diff --git a/src/main/java/cuchaz/enigma/config/Config.java b/src/main/java/cuchaz/enigma/config/Config.java new file mode 100644 index 00000000..307b221d --- /dev/null +++ b/src/main/java/cuchaz/enigma/config/Config.java @@ -0,0 +1,72 @@ +package cuchaz.enigma.config; + +import com.google.common.io.Files; +import com.google.gson.*; + +import java.io.File; +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); + } + } + +} diff --git a/src/main/java/cuchaz/enigma/gui/Gui.java b/src/main/java/cuchaz/enigma/gui/Gui.java index 77065a90..86c97aa3 100644 --- a/src/main/java/cuchaz/enigma/gui/Gui.java +++ b/src/main/java/cuchaz/enigma/gui/Gui.java @@ -15,6 +15,7 @@ import com.google.common.collect.Lists; import cuchaz.enigma.Constants; import cuchaz.enigma.ExceptionIgnorer; import cuchaz.enigma.analysis.*; +import cuchaz.enigma.config.Config; import cuchaz.enigma.gui.dialog.CrashDialog; import cuchaz.enigma.gui.elements.MenuBar; import cuchaz.enigma.gui.elements.PopupMenuBar; @@ -47,10 +48,8 @@ import java.awt.*; import java.awt.event.*; import java.io.File; import java.io.IOException; -import java.util.Collection; -import java.util.Collections; +import java.util.*; import java.util.List; -import java.util.Vector; import java.util.function.Function; public class Gui { @@ -127,13 +126,15 @@ public class Gui { // init editor DefaultSyntaxKit.initKit(); + DefaultSyntaxKit.registerContentType("text/minecraft", MinecraftSyntaxKit.class.getName()); obfuscatedHighlightPainter = new ObfuscatedHighlightPainter(); deobfuscatedHighlightPainter = new DeobfuscatedHighlightPainter(); otherHighlightPainter = new OtherHighlightPainter(); selectionHighlightPainter = new SelectionHighlightPainter(); this.editor = new PanelEditor(this); JScrollPane sourceScroller = new JScrollPane(this.editor); - this.editor.setContentType("text/java"); + this.editor.setContentType("text/minecraft"); + this.editor.setBackground(new Color(Config.INSTANCE.editorBackground)); DefaultSyntaxKit kit = (DefaultSyntaxKit) this.editor.getEditorKit(); kit.toggleComponent(this.editor, "de.sciss.syntaxpane.components.TokenMarker"); diff --git a/src/main/java/cuchaz/enigma/gui/MinecraftSyntaxKit.java b/src/main/java/cuchaz/enigma/gui/MinecraftSyntaxKit.java new file mode 100644 index 00000000..41de0350 --- /dev/null +++ b/src/main/java/cuchaz/enigma/gui/MinecraftSyntaxKit.java @@ -0,0 +1,38 @@ +package cuchaz.enigma.gui; + +import cuchaz.enigma.config.Config; +import de.sciss.syntaxpane.syntaxkits.JavaSyntaxKit; +import de.sciss.syntaxpane.util.Configuration; + +/** + * Created by Mark on 04/06/2017. + */ +public class MinecraftSyntaxKit extends JavaSyntaxKit { + + public Configuration configuration = null; + + @Override + public Configuration getConfig() { + if(configuration == null){ + initConfig(super.getConfig(JavaSyntaxKit.class)); + } + return configuration; + } + + public void initConfig(Configuration baseConfig){ + configuration = baseConfig; + //See de.sciss.syntaxpane.TokenType + configuration.put("Style.KEYWORD", Config.INSTANCE.highlightColor + ", 0"); + configuration.put("Style.KEYWORD2", Config.INSTANCE.highlightColor + ", 3"); + configuration.put("Style.STRING", Config.INSTANCE.stringColor + ", 0"); + configuration.put("Style.STRING2", Config.INSTANCE.stringColor + ", 1"); + configuration.put("Style.NUMBER", Config.INSTANCE.numberColor + ", 1"); + configuration.put("Style.OPERATOR", Config.INSTANCE.operatorColor + ", 0"); + configuration.put("Style.DELIMITER", Config.INSTANCE.delimiterColor + ", 1"); + configuration.put("Style.TYPE", Config.INSTANCE.typeColor + ", 2"); + configuration.put("Style.TYPE2", Config.INSTANCE.typeColor + ", 1"); + configuration.put("Style.IDENTIFIER", Config.INSTANCE.identifierColor + ", 0"); + configuration.put("Style.DEFAULT", Config.INSTANCE.defaultTextColor + ", 0"); + configuration.put("RightMarginColumn", "999"); //No need to have a right margin, if someone wants it add a config + } +} diff --git a/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java b/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java index 0f649278..976c215e 100644 --- a/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java +++ b/src/main/java/cuchaz/enigma/gui/highlight/BoxHighlightPainter.java @@ -58,4 +58,9 @@ public abstract class BoxHighlightPainter implements Highlighter.HighlightPainte g.setColor(this.borderColor); g.drawRoundRect(bounds.x, bounds.y, bounds.width, bounds.height, 4, 4); } + + protected static Color getColor(int rgb, float alpha){ + Color baseColor = new Color(rgb); + return new Color(baseColor.getRed(), baseColor.getGreen(), baseColor.getBlue(), (int)(255 * alpha)); + } } diff --git a/src/main/java/cuchaz/enigma/gui/highlight/DeobfuscatedHighlightPainter.java b/src/main/java/cuchaz/enigma/gui/highlight/DeobfuscatedHighlightPainter.java index a2d28844..ef651e3e 100644 --- a/src/main/java/cuchaz/enigma/gui/highlight/DeobfuscatedHighlightPainter.java +++ b/src/main/java/cuchaz/enigma/gui/highlight/DeobfuscatedHighlightPainter.java @@ -11,11 +11,11 @@ package cuchaz.enigma.gui.highlight; -import java.awt.*; +import cuchaz.enigma.config.Config; public class DeobfuscatedHighlightPainter extends BoxHighlightPainter { public DeobfuscatedHighlightPainter() { - super(new Color(220, 255, 220), new Color(80, 160, 80)); + super(getColor(Config.INSTANCE.deobfuscatedColor, Config.INSTANCE.deobfuscatedHiglightAlpha), getColor(Config.INSTANCE.deobfuscatedColorOutline, Config.INSTANCE.deobfuscatedOutlineAlpha)); } } diff --git a/src/main/java/cuchaz/enigma/gui/highlight/ObfuscatedHighlightPainter.java b/src/main/java/cuchaz/enigma/gui/highlight/ObfuscatedHighlightPainter.java index 0947d4b7..78879069 100644 --- a/src/main/java/cuchaz/enigma/gui/highlight/ObfuscatedHighlightPainter.java +++ b/src/main/java/cuchaz/enigma/gui/highlight/ObfuscatedHighlightPainter.java @@ -11,11 +11,13 @@ package cuchaz.enigma.gui.highlight; +import cuchaz.enigma.config.Config; + import java.awt.*; public class ObfuscatedHighlightPainter extends BoxHighlightPainter { public ObfuscatedHighlightPainter() { - super(new Color(255, 220, 220), new Color(160, 80, 80)); + super(getColor(Config.INSTANCE.obfuscatedColor, Config.INSTANCE.obfuscatedHiglightAlpha), getColor(Config.INSTANCE.obfuscatedColorOutline, Config.INSTANCE.obfuscatedOutlineAlpha)); } } diff --git a/src/main/java/cuchaz/enigma/gui/highlight/OtherHighlightPainter.java b/src/main/java/cuchaz/enigma/gui/highlight/OtherHighlightPainter.java index 74e7273d..c5154e13 100644 --- a/src/main/java/cuchaz/enigma/gui/highlight/OtherHighlightPainter.java +++ b/src/main/java/cuchaz/enigma/gui/highlight/OtherHighlightPainter.java @@ -11,11 +11,11 @@ package cuchaz.enigma.gui.highlight; -import java.awt.*; +import cuchaz.enigma.config.Config; public class OtherHighlightPainter extends BoxHighlightPainter { public OtherHighlightPainter() { - super(null, new Color(180, 180, 180)); + super(null, getColor(Config.INSTANCE.otherColorOutline, Config.INSTANCE.otherOutlineAlpha)); } } -- cgit v1.2.3