1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-10 09:08:43 +08:00

Changed so Dictionary now uses Type as key.

Uses PrettyName less.
Also changed to TryGetValue instead of ContainsKey in GetTypeColor()
This commit is contained in:
Simon Rodriguez 2018-12-21 13:39:52 +01:00
parent fe2b7a9684
commit 842101720e

View File

@ -12,7 +12,7 @@ namespace XNodeEditor {
/// <summary> The last key we checked. This should be the one we modify </summary> /// <summary> The last key we checked. This should be the one we modify </summary>
private static string lastKey = "xNode.Settings"; private static string lastKey = "xNode.Settings";
private static Dictionary<string, Color> typeColors = new Dictionary<string, Color>(); private static Dictionary<Type, Color> typeColors = new Dictionary<Type, Color>();
private static Dictionary<string, Settings> settings = new Dictionary<string, Settings>(); private static Dictionary<string, Settings> settings = new Dictionary<string, Settings>();
[System.Serializable] [System.Serializable]
@ -134,15 +134,16 @@ namespace XNodeEditor {
EditorGUILayout.LabelField("Types", EditorStyles.boldLabel); EditorGUILayout.LabelField("Types", EditorStyles.boldLabel);
//Display type colors. Save them if they are edited by the user //Display type colors. Save them if they are edited by the user
List<string> typeColorKeys = new List<string>(typeColors.Keys); foreach (var typeColor in typeColors) {
foreach (string typeColorKey in typeColorKeys) { Type type = typeColor.Key;
Color col = typeColors[typeColorKey]; string typeColorKey = NodeEditorUtilities.PrettyName(type);
Color col = typeColor.Value;
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
EditorGUILayout.BeginHorizontal(); EditorGUILayout.BeginHorizontal();
col = EditorGUILayout.ColorField(typeColorKey, col); col = EditorGUILayout.ColorField(typeColorKey, col);
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
if (EditorGUI.EndChangeCheck()) { if (EditorGUI.EndChangeCheck()) {
typeColors[typeColorKey] = col; typeColors[type] = col;
if (settings.typeColors.ContainsKey(typeColorKey)) settings.typeColors[typeColorKey] = col; if (settings.typeColors.ContainsKey(typeColorKey)) settings.typeColors[typeColorKey] = col;
else settings.typeColors.Add(typeColorKey, col); else settings.typeColors.Add(typeColorKey, col);
SavePrefs(typeColorKey, settings); SavePrefs(typeColorKey, settings);
@ -165,7 +166,7 @@ namespace XNodeEditor {
public static void ResetPrefs() { public static void ResetPrefs() {
if (EditorPrefs.HasKey(lastKey)) EditorPrefs.DeleteKey(lastKey); if (EditorPrefs.HasKey(lastKey)) EditorPrefs.DeleteKey(lastKey);
if (settings.ContainsKey(lastKey)) settings.Remove(lastKey); if (settings.ContainsKey(lastKey)) settings.Remove(lastKey);
typeColors = new Dictionary<string, Color>(); typeColors = new Dictionary<Type, Color>();
VerifyLoaded(); VerifyLoaded();
NodeEditorWindow.RepaintAll(); NodeEditorWindow.RepaintAll();
} }
@ -184,19 +185,21 @@ namespace XNodeEditor {
public static Color GetTypeColor(System.Type type) { public static Color GetTypeColor(System.Type type) {
VerifyLoaded(); VerifyLoaded();
if (type == null) return Color.gray; if (type == null) return Color.gray;
string typeName = type.PrettyName(); Color col;
if (!typeColors.ContainsKey(typeName)) { if (!typeColors.TryGetValue(type, out col)) {
if (settings[lastKey].typeColors.ContainsKey(typeName)) typeColors.Add(typeName, settings[lastKey].typeColors[typeName]); string typeName = type.PrettyName();
if (settings[lastKey].typeColors.ContainsKey(typeName)) typeColors.Add(type, settings[lastKey].typeColors[typeName]);
else { else {
#if UNITY_5_4_OR_NEWER #if UNITY_5_4_OR_NEWER
UnityEngine.Random.InitState(typeName.GetHashCode()); UnityEngine.Random.InitState(typeName.GetHashCode());
#else #else
UnityEngine.Random.seed = typeName.GetHashCode(); UnityEngine.Random.seed = typeName.GetHashCode();
#endif #endif
typeColors.Add(typeName, new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value)); col = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
typeColors.Add(type, col);
} }
} }
return typeColors[typeName]; return col;
} }
} }
} }