1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-21 01:36:03 +08:00

Simplified editor preferences handling.

This update will reset your xnode editor preferences
This commit is contained in:
Thor Brigsted 2018-01-25 10:27:55 +01:00
parent 5ae1b16f14
commit 9dc5653f27

View File

@ -9,7 +9,7 @@ namespace XNodeEditor {
public static Texture2D gridTexture { public static Texture2D gridTexture {
get { get {
VerifyLoaded(); VerifyLoaded();
if (_gridTexture == null) _gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor); if (_gridTexture == null) _gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
return _gridTexture; return _gridTexture;
} }
} }
@ -17,25 +17,47 @@ namespace XNodeEditor {
public static Texture2D crossTexture { public static Texture2D crossTexture {
get { get {
VerifyLoaded(); VerifyLoaded();
if (_crossTexture == null) _crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor); if (_crossTexture == null) _crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
return _crossTexture; return _crossTexture;
} }
} }
private static Texture2D _crossTexture; private static Texture2D _crossTexture;
/// <summary> Have we loaded the prefs yet </summary>
private static bool prefsLoaded = false;
/// <summary> TypeColors requested by the editor </summary> /// <summary> TypeColors requested by the editor </summary>
public static bool gridSnap { get { VerifyLoaded(); return settings.gridSnap; } }
private static Dictionary<string, Color> typeColors = new Dictionary<string, Color>(); private static Dictionary<string, Color> typeColors = new Dictionary<string, Color>();
/// <summary> TypeColors available in EditorPrefs </summary> private static Settings settings;
private static Dictionary<string, Color> prefsTypeColors = new Dictionary<string, Color>();
public static bool gridSnap { get { VerifyLoaded(); return _gridSnap; } } [System.Serializable]
private static bool _gridSnap = true; private class Settings : ISerializationCallbackReceiver {
public static Color gridLineColor { get { VerifyLoaded(); return _gridLineColor; } } public Color32 gridLineColor = new Color(0.45f, 0.45f, 0.45f);
private static Color _gridLineColor; public Color32 gridBgColor = new Color(0.18f, 0.18f, 0.18f);
public static Color gridBgColor { get { VerifyLoaded(); return _gridBgColor; } } public bool gridSnap = true;
private static Color _gridBgColor; public string typeColorsData = "";
public Dictionary<string, Color> typeColors = new Dictionary<string, Color>();
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) + ",";
}
}
}
[PreferenceItem("Node Editor")] [PreferenceItem("Node Editor")]
private static void PreferencesGUI() { private static void PreferencesGUI() {
VerifyLoaded(); VerifyLoaded();
@ -50,15 +72,14 @@ namespace XNodeEditor {
private static void GridSettingsGUI() { private static void GridSettingsGUI() {
//Label //Label
EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel); EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel);
_gridSnap = EditorGUILayout.Toggle("Snap", _gridSnap); settings.gridSnap = EditorGUILayout.Toggle("Snap", settings.gridSnap);
//EditorGUIUtility.labelWidth = 30; settings.gridLineColor = EditorGUILayout.ColorField("Color", settings.gridLineColor);
_gridLineColor = EditorGUILayout.ColorField("Color", _gridLineColor); settings.gridBgColor = EditorGUILayout.ColorField(" ", settings.gridBgColor);
_gridBgColor = EditorGUILayout.ColorField(" ", _gridBgColor);
if (GUI.changed) { if (GUI.changed) {
SavePrefs(); SavePrefs();
_gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor); _gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
_crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor); _crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
NodeEditorWindow.RepaintAll(); NodeEditorWindow.RepaintAll();
} }
EditorGUILayout.Space(); EditorGUILayout.Space();
@ -78,74 +99,42 @@ namespace XNodeEditor {
EditorGUILayout.EndHorizontal(); EditorGUILayout.EndHorizontal();
if (EditorGUI.EndChangeCheck()) { if (EditorGUI.EndChangeCheck()) {
typeColors[key] = col; typeColors[key] = col;
SaveTypeColor(key, col); if (settings.typeColors.ContainsKey(key)) settings.typeColors[key] = col;
else settings.typeColors.Add(key, col);
SavePrefs();
NodeEditorWindow.RepaintAll(); NodeEditorWindow.RepaintAll();
} }
} }
} }
private static void LoadPrefs() { private static Settings LoadPrefs() {
prefsTypeColors = LoadTypeColors(); // Remove obsolete editorprefs
if (EditorPrefs.HasKey("xnode_typecolors")) EditorPrefs.DeleteKey("xnode_typecolors");
if (EditorPrefs.HasKey("xnode_gridcolor0")) EditorPrefs.DeleteKey("xnode_gridcolor0");
if (EditorPrefs.HasKey("xnode_gridcolor1")) EditorPrefs.DeleteKey("xnode_gridcolor1");
if (EditorPrefs.HasKey("xnode_gridsnap")) EditorPrefs.DeleteKey("xnode_gridcolor1");
//Load grid colors if (!EditorPrefs.HasKey("xNode.Settings")) EditorPrefs.SetString("xNode.Settings", JsonUtility.ToJson(new Settings()));
if (!EditorPrefs.HasKey("xnode_gridcolor0")) EditorPrefs.SetString("xnode_gridcolor0", ColorUtility.ToHtmlStringRGB(new Color(0.45f, 0.45f, 0.45f))); return JsonUtility.FromJson<Settings>(EditorPrefs.GetString("xNode.Settings"));
ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("xnode_gridcolor0"), out _gridLineColor);
if (!EditorPrefs.HasKey("xnode_gridcolor1")) EditorPrefs.SetString("xnode_gridcolor1", ColorUtility.ToHtmlStringRGB(new Color(0.18f, 0.18f, 0.18f)));
ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("xnode_gridcolor1"), out _gridBgColor);
//Load snap option
if (EditorPrefs.HasKey("xnode_gridsnap")) _gridSnap = EditorPrefs.GetBool("xnode_gridsnap");
NodeEditorWindow.RepaintAll();
prefsLoaded = true;
}
/// <summary> Get Type Colors from EditorPrefs. Colors are saved as CSV in pairs of two hexcolor/name </summary>
public static Dictionary<string, Color> LoadTypeColors() {
//Load type colors
Dictionary<string, Color> result = new Dictionary<string, Color>();
if (!EditorPrefs.HasKey("xnode_typecolors")) EditorPrefs.SetString("xnode_typecolors", "");
string[] data = EditorPrefs.GetString("xnode_typecolors").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < data.Length; i += 2) {
Color col;
if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) {
result.Add(data[i], col);
}
}
return result;
}
/// <summary> Get Type Colors from EditorPrefs. Colors are saved as CSV in pairs of two hexcolor/name ""
public static Dictionary<string, Color> SaveTypeColor(string typeName, Color col) {
//Load type colors
Dictionary<string, Color> result = LoadTypeColors();
if (result.ContainsKey(typeName)) result[typeName] = col;
else result.Add(typeName, col);
string s = "";
foreach (var item in result) {
s += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ",";
}
EditorPrefs.SetString("xnode_typecolors", s);
return result;
} }
/// <summary> Delete all prefs </summary> /// <summary> Delete all prefs </summary>
public static void ResetPrefs() { public static void ResetPrefs() {
if (EditorPrefs.HasKey("xnode_typecolors")) EditorPrefs.DeleteKey("xnode_typecolors"); if (EditorPrefs.HasKey("xNode.Settings")) EditorPrefs.DeleteKey("xNode.Settings");
if (EditorPrefs.HasKey("xnode_gridcolor0")) EditorPrefs.DeleteKey("xnode_gridcolor0");
if (EditorPrefs.HasKey("xnode_gridcolor1")) EditorPrefs.DeleteKey("xnode_gridcolor1"); settings = LoadPrefs();
LoadPrefs(); typeColors = new Dictionary<string, Color>();
_gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
_crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
NodeEditorWindow.RepaintAll();
} }
private static void SavePrefs() { private static void SavePrefs() {
EditorPrefs.SetString("xnode_gridcolor0", ColorUtility.ToHtmlStringRGB(_gridLineColor)); EditorPrefs.SetString("xNode.Settings", JsonUtility.ToJson(settings));
EditorPrefs.SetString("xnode_gridcolor1", ColorUtility.ToHtmlStringRGB(_gridBgColor));
EditorPrefs.SetBool("xnode_gridsnap", _gridSnap);
} }
private static void VerifyLoaded() { private static void VerifyLoaded() {
if (!prefsLoaded) LoadPrefs(); if (settings == null) settings = LoadPrefs();
} }
/// <summary> Return color based on type </summary> /// <summary> Return color based on type </summary>
@ -154,7 +143,7 @@ namespace XNodeEditor {
if (type == null) return Color.gray; if (type == null) return Color.gray;
string typeName = type.PrettyName(); string typeName = type.PrettyName();
if (!typeColors.ContainsKey(typeName)) { if (!typeColors.ContainsKey(typeName)) {
if (prefsTypeColors.ContainsKey(typeName)) typeColors.Add(typeName, prefsTypeColors[typeName]); if (settings.typeColors.ContainsKey(typeName)) typeColors.Add(typeName, settings.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());