From 81f5879e921d7442e2d7f6eafcb5b3e83684e76b Mon Sep 17 00:00:00 2001 From: Thor Kramer Brigsted Date: Tue, 21 Nov 2017 10:51:27 +0100 Subject: [PATCH] Started work on snap and grid color --- Scripts/Editor/NodeEditorPreferences.cs | 87 +++++++++++++++++++++---- Scripts/Editor/NodeEditorReflection.cs | 2 + Scripts/Editor/NodeEditorResources.cs | 22 +++---- 3 files changed, 83 insertions(+), 28 deletions(-) diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs index 80412fc..f566eca 100644 --- a/Scripts/Editor/NodeEditorPreferences.cs +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -11,53 +11,110 @@ namespace XNodeEditor { private static Dictionary typeColors; private static Dictionary generatedTypeColors; - + private static bool gridSnap; + public static Color gridLineColor { get { VerifyLoaded(); return _gridLineColor; } } + private static Color _gridLineColor; + public static Color gridBgColor { get { VerifyLoaded(); return _gridBgColor; } } + private static Color _gridBgColor; [PreferenceItem("Node Editor")] private static void PreferencesGUI() { if (!prefsLoaded) LoadPrefs(); + GridSettingsGUI(); + TypeColorsGUI(); + if (GUILayout.Button("Set Default", GUILayout.Width(120))) { + ResetPrefs(); + } + } + + private static void GridSettingsGUI() { + //Label + EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel); + gridSnap = EditorGUILayout.Toggle("Snap", gridSnap); + + //EditorGUIUtility.labelWidth = 30; + _gridLineColor = EditorGUILayout.ColorField("Color", _gridLineColor); + _gridBgColor = EditorGUILayout.ColorField(" ", _gridBgColor); + if (GUI.changed) { + SavePrefs(); + } + EditorGUILayout.Space(); + } + + private static void TypeColorsGUI() { + //Label EditorGUILayout.LabelField("Type colors", EditorStyles.boldLabel); + //Get saved type keys string[] typeKeys = new string[typeColors.Count]; typeColors.Keys.CopyTo(typeKeys, 0); - + //Display saved type colors foreach (var key in typeKeys) { EditorGUILayout.BeginHorizontal(); - Color col = typeColors[key]; - col = EditorGUILayout.ColorField(key, col); - typeColors[key] = col; - if (!GUILayout.Toggle(true, "")) { + if (!EditorGUILayout.Toggle(key, true)) { typeColors.Remove(key); SavePrefs(); } + Color col = typeColors[key]; + col = EditorGUILayout.ColorField(col); + typeColors[key] = col; EditorGUILayout.EndHorizontal(); } if (GUI.changed) { SavePrefs(); } + //Get generated type keys string[] generatedTypeKeys = new string[generatedTypeColors.Count]; generatedTypeColors.Keys.CopyTo(generatedTypeKeys, 0); + //Display generated type colors foreach (var key in generatedTypeKeys) { EditorGUILayout.BeginHorizontal(); - Color col = generatedTypeColors[key]; - EditorGUI.BeginDisabledGroup(true); - col = EditorGUILayout.ColorField(key, col); - EditorGUI.EndDisabledGroup(); - if (GUILayout.Toggle(false, "")) { + if (EditorGUILayout.Toggle(key, false)) { typeColors.Add(key, generatedTypeColors[key]); generatedTypeColors.Remove(key); SavePrefs(); } + Color col = generatedTypeColors[key]; + EditorGUI.BeginDisabledGroup(true); + col = EditorGUILayout.ColorField(col); + EditorGUI.EndDisabledGroup(); + EditorGUILayout.EndHorizontal(); } } private static void LoadPrefs() { + //Load type colors generatedTypeColors = new Dictionary(); typeColors = GetTypeColors(); + + //Load grid colors + if (EditorPrefs.HasKey("unec_gridcolor0")) { + Color color; + if (ColorUtility.TryParseHtmlString(EditorPrefs.GetString("unec_gridcolor0"), out color)) { + _gridLineColor = color; + } + } + if (EditorPrefs.HasKey("unec_gridcolor1")) { + Color color; + if (ColorUtility.TryParseHtmlString(EditorPrefs.GetString("unec_gridcolor1"), out color)) { + _gridBgColor = color; + } + } + + //Load snap option + if (EditorPrefs.HasKey("unec_gridsnap")) gridSnap = EditorPrefs.GetBool("unec_gridsnap"); + prefsLoaded = true; } + private static void ResetPrefs() { + EditorPrefs.SetString("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00"); + EditorPrefs.SetString("unec_gridcolor0", ColorUtility.ToHtmlStringRGB(new Color(0.45f, 0.45f, 0.45f))); + EditorPrefs.SetString("unec_gridcolor1", ColorUtility.ToHtmlStringRGB(new Color(0.18f, 0.18f, 0.18f))); + LoadPrefs(); + } + private static void SavePrefs() { if (!prefsLoaded) return; string s = ""; @@ -65,15 +122,17 @@ namespace XNodeEditor { s += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ","; } EditorPrefs.SetString("unec_typecolors", s); + EditorPrefs.SetString("unec_gridcolor0", ColorUtility.ToHtmlStringRGB(_gridLineColor)); + EditorPrefs.SetString("unec_gridcolor1", ColorUtility.ToHtmlStringRGB(_gridBgColor)); + EditorPrefs.SetBool("unec_gridsnap", gridSnap); } - public static void SetDefaultTypeColors() { - EditorPrefs.SetString("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00"); + private static void VerifyLoaded() { + if (!prefsLoaded) LoadPrefs(); } public static Dictionary GetTypeColors() { if (prefsLoaded) return typeColors; - if (!EditorPrefs.HasKey("unec_typecolors")) SetDefaultTypeColors(); string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); Dictionary dict = new Dictionary(); for (int i = 0; i < data.Length; i += 2) { diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 540d61e..d1eba68 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -99,6 +99,7 @@ namespace XNodeEditor { return kvp.ToArray(); } + /// Very crude. Uses a lot of reflection. public static void OpenPreferences() { try { //Open preferences window @@ -120,6 +121,7 @@ namespace XNodeEditor { FieldInfo sectionsField = type.GetField("m_Sections", BindingFlags.Instance | BindingFlags.NonPublic); IList sections = sectionsField.GetValue(window) as IList; + //Iterate through sections and check contents Type sectionType = sectionsField.FieldType.GetGenericArguments() [0]; FieldInfo sectionContentField = sectionType.GetField("content", BindingFlags.Instance | BindingFlags.Public); diff --git a/Scripts/Editor/NodeEditorResources.cs b/Scripts/Editor/NodeEditorResources.cs index 1e1b004..a461861 100644 --- a/Scripts/Editor/NodeEditorResources.cs +++ b/Scripts/Editor/NodeEditorResources.cs @@ -3,9 +3,9 @@ namespace XNodeEditor { public static class NodeEditorResources { //Unec textures - public static Texture2D gridTexture { get { return _gridTexture != null ? _gridTexture : _gridTexture = GenerateGridTexture(); } } + public static Texture2D gridTexture { get { return _gridTexture != null ? _gridTexture : _gridTexture = GenerateGridTexture(NodeEditorPreferences.gridLineColor,NodeEditorPreferences.gridBgColor); } } private static Texture2D _gridTexture; - public static Texture2D crossTexture { get { return _crossTexture != null ? _crossTexture : _crossTexture = GenerateCrossTexture(); } } + public static Texture2D crossTexture { get { return _crossTexture != null ? _crossTexture : _crossTexture = GenerateCrossTexture(NodeEditorPreferences.gridLineColor); } } private static Texture2D _crossTexture; public static Texture2D dot { get { return _dot != null ? _dot : _dot = Resources.Load("unec_dot"); } } private static Texture2D _dot; @@ -14,12 +14,6 @@ namespace XNodeEditor { public static Texture2D nodeBody { get { return _nodeBody != null ? _nodeBody : _nodeBody = Resources.Load("unec_node"); } } private static Texture2D _nodeBody; - //Grid colors - private static Color backgroundColor = new Color(0.18f, 0.18f, 0.18f); - private static Color veinColor = new Color(0.25f, 0.25f, 0.25f); - private static Color arteryColor = new Color(0.34f, 0.34f, 0.34f); - private static Color crossColor = new Color(0.45f, 0.45f, 0.45f); - //Unec styles public static Styles styles { get { return _styles != null ? _styles : _styles = new Styles(); } } public static Styles _styles = null; @@ -54,14 +48,14 @@ namespace XNodeEditor { } } - public static Texture2D GenerateGridTexture() { + public static Texture2D GenerateGridTexture(Color line, Color bg) { Texture2D tex = new Texture2D(64, 64); Color[] cols = new Color[64 * 64]; for (int y = 0; y < 64; y++) { for (int x = 0; x < 64; x++) { - Color col = backgroundColor; - if (y % 16 == 0 || x % 16 == 0) col = veinColor; - if (y == 63 || x == 63) col = arteryColor; + Color col = bg; + if (y % 16 == 0 || x % 16 == 0) col = Color.Lerp(line,bg,0.6f); + if (y == 63 || x == 63) col = Color.Lerp(line,bg,0.3f); cols[(y * 64) + x] = col; } } @@ -73,12 +67,12 @@ namespace XNodeEditor { return tex; } - public static Texture2D GenerateCrossTexture() { + public static Texture2D GenerateCrossTexture(Color line) { Texture2D tex = new Texture2D(64, 64); Color[] cols = new Color[64 * 64]; for (int y = 0; y < 64; y++) { for (int x = 0; x < 64; x++) { - Color col = crossColor; + Color col = line; if (y != 31 && x != 31) col.a = 0; cols[(y * 64) + x] = col; }