mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Simplified editor preferences handling.
This update will reset your xnode editor preferences
This commit is contained in:
parent
5ae1b16f14
commit
9dc5653f27
@ -9,7 +9,7 @@ namespace XNodeEditor {
|
||||
public static Texture2D gridTexture {
|
||||
get {
|
||||
VerifyLoaded();
|
||||
if (_gridTexture == null) _gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor);
|
||||
if (_gridTexture == null) _gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
|
||||
return _gridTexture;
|
||||
}
|
||||
}
|
||||
@ -17,25 +17,47 @@ namespace XNodeEditor {
|
||||
public static Texture2D crossTexture {
|
||||
get {
|
||||
VerifyLoaded();
|
||||
if (_crossTexture == null) _crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor);
|
||||
if (_crossTexture == null) _crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
|
||||
return _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>
|
||||
public static bool gridSnap { get { VerifyLoaded(); return settings.gridSnap; } }
|
||||
|
||||
private static Dictionary<string, Color> typeColors = new Dictionary<string, Color>();
|
||||
/// <summary> TypeColors available in EditorPrefs </summary>
|
||||
private static Dictionary<string, Color> prefsTypeColors = new Dictionary<string, Color>();
|
||||
public static bool gridSnap { get { VerifyLoaded(); return _gridSnap; } }
|
||||
private static bool _gridSnap = true;
|
||||
public static Color gridLineColor { get { VerifyLoaded(); return _gridLineColor; } }
|
||||
private static Color _gridLineColor;
|
||||
public static Color gridBgColor { get { VerifyLoaded(); return _gridBgColor; } }
|
||||
private static Color _gridBgColor;
|
||||
private static Settings settings;
|
||||
|
||||
[System.Serializable]
|
||||
private class Settings : ISerializationCallbackReceiver {
|
||||
public Color32 gridLineColor = new Color(0.45f, 0.45f, 0.45f);
|
||||
public Color32 gridBgColor = new Color(0.18f, 0.18f, 0.18f);
|
||||
public bool gridSnap = true;
|
||||
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")]
|
||||
private static void PreferencesGUI() {
|
||||
VerifyLoaded();
|
||||
@ -50,15 +72,14 @@ namespace XNodeEditor {
|
||||
private static void GridSettingsGUI() {
|
||||
//Label
|
||||
EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel);
|
||||
_gridSnap = EditorGUILayout.Toggle("Snap", _gridSnap);
|
||||
settings.gridSnap = EditorGUILayout.Toggle("Snap", settings.gridSnap);
|
||||
|
||||
//EditorGUIUtility.labelWidth = 30;
|
||||
_gridLineColor = EditorGUILayout.ColorField("Color", _gridLineColor);
|
||||
_gridBgColor = EditorGUILayout.ColorField(" ", _gridBgColor);
|
||||
settings.gridLineColor = EditorGUILayout.ColorField("Color", settings.gridLineColor);
|
||||
settings.gridBgColor = EditorGUILayout.ColorField(" ", settings.gridBgColor);
|
||||
if (GUI.changed) {
|
||||
SavePrefs();
|
||||
_gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor);
|
||||
_crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor);
|
||||
_gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
|
||||
_crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
|
||||
NodeEditorWindow.RepaintAll();
|
||||
}
|
||||
EditorGUILayout.Space();
|
||||
@ -78,74 +99,42 @@ namespace XNodeEditor {
|
||||
EditorGUILayout.EndHorizontal();
|
||||
if (EditorGUI.EndChangeCheck()) {
|
||||
typeColors[key] = col;
|
||||
SaveTypeColor(key, col);
|
||||
if (settings.typeColors.ContainsKey(key)) settings.typeColors[key] = col;
|
||||
else settings.typeColors.Add(key, col);
|
||||
SavePrefs();
|
||||
NodeEditorWindow.RepaintAll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void LoadPrefs() {
|
||||
prefsTypeColors = LoadTypeColors();
|
||||
private static Settings LoadPrefs() {
|
||||
// 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_gridcolor0")) EditorPrefs.SetString("xnode_gridcolor0", ColorUtility.ToHtmlStringRGB(new Color(0.45f, 0.45f, 0.45f)));
|
||||
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;
|
||||
if (!EditorPrefs.HasKey("xNode.Settings")) EditorPrefs.SetString("xNode.Settings", JsonUtility.ToJson(new Settings()));
|
||||
return JsonUtility.FromJson<Settings>(EditorPrefs.GetString("xNode.Settings"));
|
||||
}
|
||||
|
||||
/// <summary> Delete all prefs </summary>
|
||||
public static void ResetPrefs() {
|
||||
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");
|
||||
LoadPrefs();
|
||||
if (EditorPrefs.HasKey("xNode.Settings")) EditorPrefs.DeleteKey("xNode.Settings");
|
||||
|
||||
settings = LoadPrefs();
|
||||
typeColors = new Dictionary<string, Color>();
|
||||
_gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
|
||||
_crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
|
||||
NodeEditorWindow.RepaintAll();
|
||||
}
|
||||
|
||||
private static void SavePrefs() {
|
||||
EditorPrefs.SetString("xnode_gridcolor0", ColorUtility.ToHtmlStringRGB(_gridLineColor));
|
||||
EditorPrefs.SetString("xnode_gridcolor1", ColorUtility.ToHtmlStringRGB(_gridBgColor));
|
||||
EditorPrefs.SetBool("xnode_gridsnap", _gridSnap);
|
||||
EditorPrefs.SetString("xNode.Settings", JsonUtility.ToJson(settings));
|
||||
}
|
||||
|
||||
private static void VerifyLoaded() {
|
||||
if (!prefsLoaded) LoadPrefs();
|
||||
if (settings == null) settings = LoadPrefs();
|
||||
}
|
||||
|
||||
/// <summary> Return color based on type </summary>
|
||||
@ -154,7 +143,7 @@ namespace XNodeEditor {
|
||||
if (type == null) return Color.gray;
|
||||
string typeName = type.PrettyName();
|
||||
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 {
|
||||
#if UNITY_5_4_OR_NEWER
|
||||
UnityEngine.Random.InitState(typeName.GetHashCode());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user