From dcff69979c3cdcfec9ee61efd5859c2a59693e4e Mon Sep 17 00:00:00 2001 From: Thor Kramer Brigsted Date: Tue, 21 Nov 2017 13:21:34 +0100 Subject: [PATCH] Moved NodeEditorGUI.TypeToString to NodeEditorUtilities.PrettyName Improved custom type color handling Removed default color overrides --- Scripts/Editor/NodeEditorGUI.cs | 38 +------------------------ Scripts/Editor/NodeEditorPreferences.cs | 19 +++++++------ Scripts/Editor/NodeEditorUtilities.cs | 38 +++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 46 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 2c0a76e..e3da60a 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -273,7 +273,7 @@ namespace XNodeEditor { if (hoveredPort != null) { Type type = hoveredPort.ValueType; GUIContent content = new GUIContent(); - content.text = TypeToString(type); + content.text = type.PrettyName(); if (hoveredPort.IsStatic && hoveredPort.IsOutput) { object obj = ObjectFromFieldName(hoveredPort.node, hoveredPort.fieldName); if (obj != null) content.text += " = " + obj.ToString(); @@ -284,41 +284,5 @@ namespace XNodeEditor { Repaint(); } } - - private string TypeToString(Type type) { - if (type == null) return "null"; - if (type == typeof(System.Object)) return "object"; - if (type == typeof(float)) return "float"; - else if (type == typeof(int)) return "int"; - else if (type == typeof(long)) return "long"; - else if (type == typeof(double)) return "double"; - else if (type == typeof(string)) return "string"; - else if (type == typeof(bool)) return "bool"; - else if (type.IsGenericType) { - string s = ""; - Type genericType = type.GetGenericTypeDefinition(); - if (genericType == typeof(List<>)) s = "List"; - else s = type.GetGenericTypeDefinition().ToString(); - - Type[] types = type.GetGenericArguments(); - string[] stypes = new string[types.Length]; - for (int i = 0; i < types.Length; i++) { - stypes[i] = TypeToString(types[i]); - } - return s + "<" + string.Join(", ", stypes) + ">"; - } else if (type.IsArray) { - string rank = ""; - for (int i = 1; i < type.GetArrayRank(); i++) { - rank += ","; - } - Type elementType = type.GetElementType(); - if (!elementType.IsArray) return TypeToString(elementType) + "[" + rank + "]"; - else { - string s = TypeToString(elementType); - int i = s.IndexOf('['); - return s.Substring(0, i) + "[" + rank + "]" + s.Substring(i); - } - } else return hoveredPort.ValueType.ToString(); - } } } \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs index e5fff03..3424462 100644 --- a/Scripts/Editor/NodeEditorPreferences.cs +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -60,7 +60,7 @@ namespace XNodeEditor { //Display saved type colors foreach (var key in typeKeys) { EditorGUILayout.BeginHorizontal(); - if (!EditorGUILayout.Toggle(key, true)) { + if (!EditorGUILayout.Toggle(new GUIContent(key, key), true)) { typeColors.Remove(key); SavePrefs(); EditorGUILayout.EndHorizontal(); @@ -82,7 +82,7 @@ namespace XNodeEditor { //Display generated type colors foreach (var key in generatedTypeKeys) { EditorGUILayout.BeginHorizontal(); - if (EditorGUILayout.Toggle(key, false)) { + if (EditorGUILayout.Toggle(new GUIContent(key, key), false)) { typeColors.Add(key, generatedTypeColors[key]); generatedTypeColors.Remove(key); SavePrefs(); @@ -102,7 +102,7 @@ namespace XNodeEditor { //Load type colors generatedTypeColors = new Dictionary(); - if (!EditorPrefs.HasKey("unec_typecolors")) EditorPrefs.SetString("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00"); + if (!EditorPrefs.HasKey("unec_typecolors")) EditorPrefs.SetString("unec_typecolors", ""); string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); typeColors = new Dictionary(); for (int i = 0; i < data.Length; i += 2) { @@ -152,13 +152,14 @@ namespace XNodeEditor { /// Return color based on type public static Color GetTypeColor(System.Type type) { - if (!prefsLoaded) LoadPrefs(); + VerifyLoaded(); if (type == null) return Color.gray; - if (typeColors.ContainsKey(type.Name)) return typeColors[type.Name]; - if (generatedTypeColors.ContainsKey(type.Name)) return generatedTypeColors[type.Name]; - UnityEngine.Random.InitState(type.Name.GetHashCode()); - generatedTypeColors.Add(type.Name, new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value)); - return generatedTypeColors[type.Name]; + string typeName = type.PrettyName(); + if (typeColors.ContainsKey(typeName)) return typeColors[typeName]; + if (generatedTypeColors.ContainsKey(typeName)) return generatedTypeColors[typeName]; + UnityEngine.Random.InitState(typeName.GetHashCode()); + generatedTypeColors.Add(typeName, new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value)); + return generatedTypeColors[typeName]; } } } \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorUtilities.cs b/Scripts/Editor/NodeEditorUtilities.cs index 12e641a..9496ec1 100644 --- a/Scripts/Editor/NodeEditorUtilities.cs +++ b/Scripts/Editor/NodeEditorUtilities.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using System.Reflection; @@ -49,5 +50,42 @@ namespace XNodeEditor { ); return methods.Count() > 0; } + + /// Return a prettiefied type name. + public static string PrettyName(this Type type) { + if (type == null) return "null"; + if (type == typeof(System.Object)) return "object"; + if (type == typeof(float)) return "float"; + else if (type == typeof(int)) return "int"; + else if (type == typeof(long)) return "long"; + else if (type == typeof(double)) return "double"; + else if (type == typeof(string)) return "string"; + else if (type == typeof(bool)) return "bool"; + else if (type.IsGenericType) { + string s = ""; + Type genericType = type.GetGenericTypeDefinition(); + if (genericType == typeof(List<>)) s = "List"; + else s = type.GetGenericTypeDefinition().ToString(); + + Type[] types = type.GetGenericArguments(); + string[] stypes = new string[types.Length]; + for (int i = 0; i < types.Length; i++) { + stypes[i] = types[i].PrettyName(); + } + return s + "<" + string.Join(", ", stypes) + ">"; + } else if (type.IsArray) { + string rank = ""; + for (int i = 1; i < type.GetArrayRank(); i++) { + rank += ","; + } + Type elementType = type.GetElementType(); + if (!elementType.IsArray) return elementType.PrettyName() + "[" + rank + "]"; + else { + string s = elementType.PrettyName(); + int i = s.IndexOf('['); + return s.Substring(0, i) + "[" + rank + "]" + s.Substring(i); + } + } else return type.ToString(); + } } } \ No newline at end of file