mirror of
https://github.com/Siccity/xNode.git
synced 2026-02-04 22:34:54 +08:00
Moved NodeEditorGUI.TypeToString to NodeEditorUtilities.PrettyName
Improved custom type color handling Removed default color overrides
This commit is contained in:
parent
5f161b80e7
commit
dcff69979c
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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<string, Color>();
|
||||
|
||||
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<string, Color>();
|
||||
for (int i = 0; i < data.Length; i += 2) {
|
||||
@ -152,13 +152,14 @@ namespace XNodeEditor {
|
||||
|
||||
/// <summary> Return color based on type </summary>
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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;
|
||||
}
|
||||
|
||||
/// <summary> Return a prettiefied type name. </summary>
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user