1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00

Added Node Editor entry in Preferences. Custom colors for value types can now be assigned

This commit is contained in:
Thor Brigsted 2017-10-14 08:33:46 +02:00
parent e089f8fa7f
commit 9abdf2995e
7 changed files with 115 additions and 10 deletions

Binary file not shown.

View File

@ -83,7 +83,7 @@ public class NodeEditor {
Rect handleRect = new Rect(handlePoint.x-8,handlePoint.y-8,16,16); Rect handleRect = new Rect(handlePoint.x-8,handlePoint.y-8,16,16);
GUI.color = new Color(0.29f, 0.31f, 0.32f); GUI.color = new Color(0.29f, 0.31f, 0.32f);
GUI.DrawTexture(handleRect, NodeEditorResources.dotOuter); GUI.DrawTexture(handleRect, NodeEditorResources.dotOuter);
GUI.color = NodeEditorUtilities.GetTypeColor(port.type); GUI.color = NodeEditorPreferences.GetTypeColor(port.type);
GUI.DrawTexture(handleRect, NodeEditorResources.dot); GUI.DrawTexture(handleRect, NodeEditorResources.dot);
GUI.color = col; GUI.color = col;
return handlePoint; return handlePoint;

View File

@ -133,7 +133,7 @@ public partial class NodeEditorWindow {
if (!_portConnectionPoints.ContainsKey(draggedOutput)) return; if (!_portConnectionPoints.ContainsKey(draggedOutput)) return;
Vector2 from = _portConnectionPoints[draggedOutput].center; Vector2 from = _portConnectionPoints[draggedOutput].center;
Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition); Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition);
Color col = NodeEditorUtilities.GetTypeColor(draggedOutput.type); Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.type);
col.a = 0.6f; col.a = 0.6f;
DrawConnection(from, to, col); DrawConnection(from, to, col);
} }

View File

@ -131,7 +131,7 @@ public partial class NodeEditorWindow {
NodePort input = output.GetConnection(k); NodePort input = output.GetConnection(k);
if (input == null) return; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return. if (input == null) return; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
Vector2 to = _portConnectionPoints[input].center; Vector2 to = _portConnectionPoints[input].center;
DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type)); DrawConnection(from, to, NodeEditorPreferences.GetTypeColor(output.type));
} }
} }
} }

View File

@ -0,0 +1,100 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public static class NodeEditorPreferences {
/// <summary> Have we loaded the prefs yet </summary>
private static bool prefsLoaded = false;
private static Dictionary<string, Color> typeColors;
private static Dictionary<string, Color> generatedTypeColors;
[PreferenceItem ("Node Editor")]
private static void PreferencesGUI () {
if (!prefsLoaded) LoadPrefs();
EditorGUILayout.LabelField("Type colors", EditorStyles.boldLabel);
string[] typeKeys = new string[typeColors.Count];
typeColors.Keys.CopyTo(typeKeys, 0);
foreach (var key in typeKeys) {
EditorGUILayout.BeginHorizontal ();
Color col = typeColors[key];
col = EditorGUILayout.ColorField (key, col);
typeColors[key] = col;
if (!GUILayout.Toggle(true, "")) {
typeColors.Remove(key);
SavePrefs();
}
EditorGUILayout.EndHorizontal ();
}
if(GUI.changed) {
SavePrefs();
}
string[] generatedTypeKeys = new string[generatedTypeColors.Count];
generatedTypeColors.Keys.CopyTo(generatedTypeKeys,0);
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, "")) {
typeColors.Add(key,generatedTypeColors[key]);
generatedTypeColors.Remove(key);
SavePrefs();
}
EditorGUILayout.EndHorizontal ();
}
}
private static void LoadPrefs() {
generatedTypeColors = new Dictionary<string, Color>();
typeColors = GetTypeColors();
prefsLoaded = true;
}
private static void SavePrefs() {
if (!prefsLoaded) return;
string s = "";
foreach (var item in typeColors) {
s+= item.Key+","+ColorUtility.ToHtmlStringRGB(item.Value)+ ",";
}
EditorPrefs.SetString("unec_typecolors",s);
}
public static void SetDefaultTypeColors () {
EditorPrefs.SetString ("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00");
}
public static Dictionary<string, Color> GetTypeColors () {
if (prefsLoaded) return typeColors;
if (!EditorPrefs.HasKey ("unec_typecolors")) SetDefaultTypeColors ();
string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
Dictionary<string, Color> dict = new Dictionary<string, Color> ();
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;
}
/// <summary> Return color based on type </summary>
public static Color GetTypeColor(System.Type type) {
if (!prefsLoaded) LoadPrefs();
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];
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6b1f47e387a6f714c9f2ff82a6888c85
timeCreated: 1507920216
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -39,13 +39,6 @@ public static class NodeEditorUtilities {
return char.ToUpper(s[0]) + s.Substring(1); return char.ToUpper(s[0]) + s.Substring(1);
} }
/// <summary> Return color based on type </summary>
public static Color GetTypeColor(Type type) {
if (type == null) return Color.gray;
UnityEngine.Random.InitState(type.Name.GetHashCode());
return new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
}
/// <summary> Returns true if this can be casted to <see cref="Type"/></summary> /// <summary> Returns true if this can be casted to <see cref="Type"/></summary>
public static bool IsCastableTo(this Type from, Type to) { public static bool IsCastableTo(this Type from, Type to) {
if (to.IsAssignableFrom(from)) return true; if (to.IsAssignableFrom(from)) return true;