From 5f161b80e775fcf74bbbc6af4bbcdc00b849a087 Mon Sep 17 00:00:00 2001 From: Thor Kramer Brigsted Date: Tue, 21 Nov 2017 12:53:56 +0100 Subject: [PATCH] Added grid snap and color settings. --- Scripts/Editor/NodeEditorAction.cs | 4 ++ Scripts/Editor/NodeEditorGUI.cs | 4 +- Scripts/Editor/NodeEditorPreferences.cs | 80 +++++++++++++------------ Scripts/Editor/NodeEditorResources.cs | 8 +-- Scripts/Editor/NodeEditorWindow.cs | 8 +++ 5 files changed, 59 insertions(+), 45 deletions(-) diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index ce021e1..f83a731 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -48,6 +48,10 @@ namespace XNodeEditor { Repaint(); } else if (IsDraggingNode) { draggedNode.position = WindowToGridPosition(e.mousePosition) + dragOffset; + if (NodeEditorPreferences.gridSnap) { + draggedNode.position.x = (Mathf.Round((draggedNode.position.x + 8) / 16) * 16) - 8; + draggedNode.position.y = (Mathf.Round((draggedNode.position.y + 8) / 16) * 16) - 8; + } Repaint(); } } else if (e.button == 1) { diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index d475383..2c0a76e 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -47,8 +47,8 @@ namespace XNodeEditor { rect.position = Vector2.zero; Vector2 center = rect.size / 2f; - Texture2D gridTex = NodeEditorResources.gridTexture; - Texture2D crossTex = NodeEditorResources.crossTexture; + Texture2D gridTex = NodeEditorPreferences.gridTexture; + Texture2D crossTex = NodeEditorPreferences.crossTexture; // Offset from origin in tile units float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width; diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs index f566eca..e5fff03 100644 --- a/Scripts/Editor/NodeEditorPreferences.cs +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -6,22 +6,29 @@ using UnityEngine; namespace XNodeEditor { public static class NodeEditorPreferences { + public static Texture2D gridTexture { get { VerifyLoaded(); return _gridTexture; } } + private static Texture2D _gridTexture; + public static Texture2D crossTexture { get { VerifyLoaded(); return _crossTexture; } } + private static Texture2D _crossTexture; + /// Have we loaded the prefs yet private static bool prefsLoaded = false; private static Dictionary typeColors; private static Dictionary generatedTypeColors; - private static bool gridSnap; + public static bool gridSnap { get { VerifyLoaded(); return _gridSnap; } } + private static bool _gridSnap = true; 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(); + VerifyLoaded(); + GridSettingsGUI(); TypeColorsGUI(); - if (GUILayout.Button("Set Default", GUILayout.Width(120))) { + if (GUILayout.Button(new GUIContent("Set Default", "Reset all values to default"), GUILayout.Width(120))) { ResetPrefs(); } } @@ -29,13 +36,16 @@ namespace XNodeEditor { private static void GridSettingsGUI() { //Label EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel); - gridSnap = EditorGUILayout.Toggle("Snap", gridSnap); + _gridSnap = EditorGUILayout.Toggle("Snap", _gridSnap); //EditorGUIUtility.labelWidth = 30; _gridLineColor = EditorGUILayout.ColorField("Color", _gridLineColor); _gridBgColor = EditorGUILayout.ColorField(" ", _gridBgColor); if (GUI.changed) { SavePrefs(); + _gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor); + _crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor); + NodeEditorWindow.RepaintAll(); } EditorGUILayout.Space(); } @@ -53,6 +63,8 @@ namespace XNodeEditor { if (!EditorGUILayout.Toggle(key, true)) { typeColors.Remove(key); SavePrefs(); + EditorGUILayout.EndHorizontal(); + continue; } Color col = typeColors[key]; col = EditorGUILayout.ColorField(col); @@ -61,6 +73,7 @@ namespace XNodeEditor { } if (GUI.changed) { SavePrefs(); + NodeEditorWindow.RepaintAll(); } //Get generated type keys @@ -73,6 +86,8 @@ namespace XNodeEditor { typeColors.Add(key, generatedTypeColors[key]); generatedTypeColors.Remove(key); SavePrefs(); + EditorGUILayout.EndHorizontal(); + continue; } Color col = generatedTypeColors[key]; EditorGUI.BeginDisabledGroup(true); @@ -86,37 +101,41 @@ namespace XNodeEditor { private static void LoadPrefs() { //Load type colors generatedTypeColors = new Dictionary(); - typeColors = GetTypeColors(); + + if (!EditorPrefs.HasKey("unec_typecolors")) EditorPrefs.SetString("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00"); + string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); + typeColors = new Dictionary(); + for (int i = 0; i < data.Length; i += 2) { + Color col; + if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) { + typeColors.Add(data[i], col); + } + } //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; - } - } + if (!EditorPrefs.HasKey("unec_gridcolor0")) EditorPrefs.SetString("unec_gridcolor0", ColorUtility.ToHtmlStringRGB(new Color(0.45f, 0.45f, 0.45f))); + ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("unec_gridcolor0"), out _gridLineColor); + if (!EditorPrefs.HasKey("unec_gridcolor1")) EditorPrefs.SetString("unec_gridcolor1", ColorUtility.ToHtmlStringRGB(new Color(0.18f, 0.18f, 0.18f))); + ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("unec_gridcolor1"), out _gridBgColor); //Load snap option - if (EditorPrefs.HasKey("unec_gridsnap")) gridSnap = EditorPrefs.GetBool("unec_gridsnap"); + if (EditorPrefs.HasKey("unec_gridsnap")) _gridSnap = EditorPrefs.GetBool("unec_gridsnap"); + _gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor); + _crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor); + NodeEditorWindow.RepaintAll(); 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))); + /// Delete all prefs + public static void ResetPrefs() { + if (EditorPrefs.HasKey("unec_typecolors")) EditorPrefs.DeleteKey("unec_typecolors"); + if (EditorPrefs.HasKey("unec_gridcolor0")) EditorPrefs.DeleteKey("unec_gridcolor0"); + if (EditorPrefs.HasKey("unec_gridcolor1")) EditorPrefs.DeleteKey("unec_gridcolor1"); LoadPrefs(); } private static void SavePrefs() { - if (!prefsLoaded) return; string s = ""; foreach (var item in typeColors) { s += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ","; @@ -124,26 +143,13 @@ namespace XNodeEditor { EditorPrefs.SetString("unec_typecolors", s); EditorPrefs.SetString("unec_gridcolor0", ColorUtility.ToHtmlStringRGB(_gridLineColor)); EditorPrefs.SetString("unec_gridcolor1", ColorUtility.ToHtmlStringRGB(_gridBgColor)); - EditorPrefs.SetBool("unec_gridsnap", gridSnap); + EditorPrefs.SetBool("unec_gridsnap", _gridSnap); } private static void VerifyLoaded() { if (!prefsLoaded) LoadPrefs(); } - public static Dictionary GetTypeColors() { - if (prefsLoaded) return typeColors; - string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); - Dictionary dict = new Dictionary(); - for (int i = 0; i < data.Length; i += 2) { - Color col; - if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) { - dict.Add(data[i], col); - } - } - return dict; - } - /// Return color based on type public static Color GetTypeColor(System.Type type) { if (!prefsLoaded) LoadPrefs(); diff --git a/Scripts/Editor/NodeEditorResources.cs b/Scripts/Editor/NodeEditorResources.cs index a461861..abf7fcb 100644 --- a/Scripts/Editor/NodeEditorResources.cs +++ b/Scripts/Editor/NodeEditorResources.cs @@ -3,10 +3,6 @@ namespace XNodeEditor { public static class NodeEditorResources { //Unec textures - 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(NodeEditorPreferences.gridLineColor); } } - private static Texture2D _crossTexture; public static Texture2D dot { get { return _dot != null ? _dot : _dot = Resources.Load("unec_dot"); } } private static Texture2D _dot; public static Texture2D dotOuter { get { return _dotOuter != null ? _dotOuter : _dotOuter = Resources.Load("unec_dot_outer"); } } @@ -54,8 +50,8 @@ namespace XNodeEditor { for (int y = 0; y < 64; y++) { for (int x = 0; x < 64; x++) { 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); + if (y % 16 == 0 || x % 16 == 0) col = Color.Lerp(line,bg,0.65f); + if (y == 63 || x == 63) col = Color.Lerp(line,bg,0.35f); cols[(y * 64) + x] = col; } } diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs index 5d993c3..1c60e1a 100644 --- a/Scripts/Editor/NodeEditorWindow.cs +++ b/Scripts/Editor/NodeEditorWindow.cs @@ -92,5 +92,13 @@ namespace XNodeEditor { } return false; } + + /// Repaint all open NodeEditorWindows. + public static void RepaintAll() { + NodeEditorWindow[] windows = Resources.FindObjectsOfTypeAll(); + for (int i = 0; i < windows.Length; i++) { + windows[i].Repaint(); + } + } } } \ No newline at end of file