1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 17:26:02 +08:00
xNode/Scripts/Editor/NodeEditorPreferences.cs
2018-04-13 10:46:51 +02:00

202 lines
9.4 KiB
C#

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace XNodeEditor {
public static class NodeEditorPreferences {
public enum NoodleType { Curve, Line, Angled }
/// <summary> The last editor we checked. This should be the one we modify </summary>
private static XNodeEditor.NodeGraphEditor lastEditor;
/// <summary> The last key we checked. This should be the one we modify </summary>
private static string lastKey = "xNode.Settings";
private static Dictionary<string, Color> typeColors = new Dictionary<string, Color>();
private static Dictionary<string, Settings> settings = new Dictionary<string, Settings>();
[System.Serializable]
public class Settings : ISerializationCallbackReceiver {
[SerializeField] private Color32 _gridLineColor = new Color(0.45f, 0.45f, 0.45f);
public Color32 gridLineColor { get { return _gridLineColor; } set { _gridLineColor = value; _gridTexture = null; _crossTexture = null; } }
[SerializeField] private Color32 _gridBgColor = new Color(0.18f, 0.18f, 0.18f);
public Color32 gridBgColor { get { return _gridBgColor; } set { _gridBgColor = value; _gridTexture = null; } }
public Color32 highlightColor = new Color32(255, 255, 255, 255);
public bool gridSnap = true;
public bool autoSave = true;
[SerializeField] private string typeColorsData = "";
[NonSerialized] public Dictionary<string, Color> typeColors = new Dictionary<string, Color>();
public NoodleType noodleType = NoodleType.Curve;
private Texture2D _gridTexture;
public Texture2D gridTexture {
get {
if (_gridTexture == null) _gridTexture = NodeEditorResources.GenerateGridTexture(gridLineColor, gridBgColor);
return _gridTexture;
}
}
private Texture2D _crossTexture;
public Texture2D crossTexture {
get {
if (_crossTexture == null) _crossTexture = NodeEditorResources.GenerateCrossTexture(gridLineColor);
return _crossTexture;
}
}
public void OnAfterDeserialize() {
// Deserialize typeColorsData
typeColors = new Dictionary<string, Color>();
string[] data = typeColorsData.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < data.Length; i += 2) {
Color col;
if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) {
typeColors.Add(data[i], col);
}
}
}
public void OnBeforeSerialize() {
// Serialize typeColors
typeColorsData = "";
foreach (var item in typeColors) {
typeColorsData += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ",";
}
}
}
/// <summary> Get settings of current active editor </summary>
public static Settings GetSettings() {
if (lastEditor != XNodeEditor.NodeEditorWindow.current.graphEditor) {
object[] attribs = XNodeEditor.NodeEditorWindow.current.graphEditor.GetType().GetCustomAttributes(typeof(XNodeEditor.NodeGraphEditor.CustomNodeGraphEditorAttribute), true);
if (attribs.Length == 1) {
XNodeEditor.NodeGraphEditor.CustomNodeGraphEditorAttribute attrib = attribs[0] as XNodeEditor.NodeGraphEditor.CustomNodeGraphEditorAttribute;
lastEditor = XNodeEditor.NodeEditorWindow.current.graphEditor;
lastKey = attrib.editorPrefsKey;
} else return null;
}
if (!settings.ContainsKey(lastKey)) VerifyLoaded();
return settings[lastKey];
}
[PreferenceItem("Node Editor")]
private static void PreferencesGUI() {
VerifyLoaded();
Settings settings = NodeEditorPreferences.settings[lastKey];
NodeSettingsGUI(lastKey, settings);
GridSettingsGUI(lastKey, settings);
SystemSettingsGUI(lastKey, settings);
TypeColorsGUI(lastKey, settings);
if (GUILayout.Button(new GUIContent("Set Default", "Reset all values to default"), GUILayout.Width(120))) {
ResetPrefs();
}
}
private static void GridSettingsGUI(string key, Settings settings) {
//Label
EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel);
settings.gridSnap = EditorGUILayout.Toggle(new GUIContent("Snap", "Hold CTRL in editor to invert"), settings.gridSnap);
settings.gridLineColor = EditorGUILayout.ColorField("Color", settings.gridLineColor);
settings.gridBgColor = EditorGUILayout.ColorField(" ", settings.gridBgColor);
if (GUI.changed) {
SavePrefs(key, settings);
NodeEditorWindow.RepaintAll();
}
EditorGUILayout.Space();
}
private static void SystemSettingsGUI(string key, Settings settings) {
//Label
EditorGUILayout.LabelField("System", EditorStyles.boldLabel);
settings.autoSave = EditorGUILayout.Toggle(new GUIContent("Autosave", "Disable for better editor performance"), settings.autoSave);
if (GUI.changed) SavePrefs(key, settings);
EditorGUILayout.Space();
}
private static void NodeSettingsGUI(string key, Settings settings) {
//Label
EditorGUILayout.LabelField("Node", EditorStyles.boldLabel);
settings.highlightColor = EditorGUILayout.ColorField("Selection", settings.highlightColor);
settings.noodleType = (NoodleType) EditorGUILayout.EnumPopup("Noodle type", (Enum) settings.noodleType);
if (GUI.changed) {
SavePrefs(key, settings);
NodeEditorWindow.RepaintAll();
}
EditorGUILayout.Space();
}
private static void TypeColorsGUI(string key, Settings settings) {
//Label
EditorGUILayout.LabelField("Types", EditorStyles.boldLabel);
//Display type colors. Save them if they are edited by the user
List<string> typeColorKeys = new List<string>(typeColors.Keys);
foreach (string typeColorKey in typeColorKeys) {
Color col = typeColors[typeColorKey];
EditorGUI.BeginChangeCheck();
EditorGUILayout.BeginHorizontal();
col = EditorGUILayout.ColorField(typeColorKey, col);
EditorGUILayout.EndHorizontal();
if (EditorGUI.EndChangeCheck()) {
typeColors[typeColorKey] = col;
if (settings.typeColors.ContainsKey(typeColorKey)) settings.typeColors[typeColorKey] = col;
else settings.typeColors.Add(typeColorKey, col);
SavePrefs(typeColorKey, settings);
NodeEditorWindow.RepaintAll();
}
}
}
/// <summary> Load prefs if they exist. Create if they don't </summary>
private static Settings LoadPrefs() {
// Create settings if it doesn't exist
if (!EditorPrefs.HasKey(lastKey)) {
if (lastEditor != null) EditorPrefs.SetString(lastKey, JsonUtility.ToJson(lastEditor.GetDefaultPreferences()));
else EditorPrefs.SetString(lastKey, JsonUtility.ToJson(new Settings()));
}
return JsonUtility.FromJson<Settings>(EditorPrefs.GetString(lastKey));
}
/// <summary> Delete all prefs </summary>
public static void ResetPrefs() {
if (EditorPrefs.HasKey(lastKey)) EditorPrefs.DeleteKey(lastKey);
if (settings.ContainsKey(lastKey)) settings.Remove(lastKey);
typeColors = new Dictionary<string, Color>();
VerifyLoaded();
NodeEditorWindow.RepaintAll();
}
/// <summary> Save preferences in EditorPrefs </summary>
private static void SavePrefs(string key, Settings settings) {
EditorPrefs.SetString(key, JsonUtility.ToJson(settings));
}
/// <summary> Check if we have loaded settings for given key. If not, load them </summary>
private static void VerifyLoaded() {
if (!settings.ContainsKey(lastKey)) settings.Add(lastKey, LoadPrefs());
}
/// <summary> Return color based on type </summary>
public static Color GetTypeColor(System.Type type) {
VerifyLoaded();
if (type == null) return Color.gray;
string typeName = type.PrettyName();
if (!typeColors.ContainsKey(typeName)) {
if (settings[lastKey].typeColors.ContainsKey(typeName)) typeColors.Add(typeName, settings[lastKey].typeColors[typeName]);
else {
#if UNITY_5_4_OR_NEWER
UnityEngine.Random.InitState(typeName.GetHashCode());
#else
UnityEngine.Random.seed = typeName.GetHashCode();
#endif
typeColors.Add(typeName, new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value));
}
}
return typeColors[typeName];
}
}
}