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

Added grid snap and color settings.

This commit is contained in:
Thor Kramer Brigsted 2017-11-21 12:53:56 +01:00
parent 81f5879e92
commit 5f161b80e7
5 changed files with 59 additions and 45 deletions

View File

@ -48,6 +48,10 @@ namespace XNodeEditor {
Repaint();
} else if (IsDraggingNode) {
draggedNode.position = WindowToGridPosition(e.mousePosition) + dragOffset;
if (NodeEditorPreferences.gridSnap) {
draggedNode.position.x = (Mathf.Round((draggedNode.position.x + 8) / 16) * 16) - 8;
draggedNode.position.y = (Mathf.Round((draggedNode.position.y + 8) / 16) * 16) - 8;
}
Repaint();
}
} else if (e.button == 1) {

View File

@ -47,8 +47,8 @@ namespace XNodeEditor {
rect.position = Vector2.zero;
Vector2 center = rect.size / 2f;
Texture2D gridTex = NodeEditorResources.gridTexture;
Texture2D crossTex = NodeEditorResources.crossTexture;
Texture2D gridTex = NodeEditorPreferences.gridTexture;
Texture2D crossTex = NodeEditorPreferences.crossTexture;
// Offset from origin in tile units
float xOffset = -(center.x * zoom + panOffset.x) / gridTex.width;

View File

@ -6,22 +6,29 @@ using UnityEngine;
namespace XNodeEditor {
public static class NodeEditorPreferences {
public static Texture2D gridTexture { get { VerifyLoaded(); return _gridTexture; } }
private static Texture2D _gridTexture;
public static Texture2D crossTexture { get { VerifyLoaded(); return _crossTexture; } }
private static Texture2D _crossTexture;
/// <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;
private static bool gridSnap;
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;
[PreferenceItem("Node Editor")]
private static void PreferencesGUI() {
if (!prefsLoaded) LoadPrefs();
VerifyLoaded();
GridSettingsGUI();
TypeColorsGUI();
if (GUILayout.Button("Set Default", GUILayout.Width(120))) {
if (GUILayout.Button(new GUIContent("Set Default", "Reset all values to default"), GUILayout.Width(120))) {
ResetPrefs();
}
}
@ -29,13 +36,16 @@ namespace XNodeEditor {
private static void GridSettingsGUI() {
//Label
EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel);
gridSnap = EditorGUILayout.Toggle("Snap", gridSnap);
_gridSnap = EditorGUILayout.Toggle("Snap", _gridSnap);
//EditorGUIUtility.labelWidth = 30;
_gridLineColor = EditorGUILayout.ColorField("Color", _gridLineColor);
_gridBgColor = EditorGUILayout.ColorField(" ", _gridBgColor);
if (GUI.changed) {
SavePrefs();
_gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor);
_crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor);
NodeEditorWindow.RepaintAll();
}
EditorGUILayout.Space();
}
@ -53,6 +63,8 @@ namespace XNodeEditor {
if (!EditorGUILayout.Toggle(key, true)) {
typeColors.Remove(key);
SavePrefs();
EditorGUILayout.EndHorizontal();
continue;
}
Color col = typeColors[key];
col = EditorGUILayout.ColorField(col);
@ -61,6 +73,7 @@ namespace XNodeEditor {
}
if (GUI.changed) {
SavePrefs();
NodeEditorWindow.RepaintAll();
}
//Get generated type keys
@ -73,6 +86,8 @@ namespace XNodeEditor {
typeColors.Add(key, generatedTypeColors[key]);
generatedTypeColors.Remove(key);
SavePrefs();
EditorGUILayout.EndHorizontal();
continue;
}
Color col = generatedTypeColors[key];
EditorGUI.BeginDisabledGroup(true);
@ -86,37 +101,41 @@ namespace XNodeEditor {
private static void LoadPrefs() {
//Load type colors
generatedTypeColors = new Dictionary<string, Color>();
typeColors = GetTypeColors();
if (!EditorPrefs.HasKey("unec_typecolors")) EditorPrefs.SetString("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00");
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) {
Color col;
if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) {
typeColors.Add(data[i], col);
}
}
//Load grid colors
if (EditorPrefs.HasKey("unec_gridcolor0")) {
Color color;
if (ColorUtility.TryParseHtmlString(EditorPrefs.GetString("unec_gridcolor0"), out color)) {
_gridLineColor = color;
}
}
if (EditorPrefs.HasKey("unec_gridcolor1")) {
Color color;
if (ColorUtility.TryParseHtmlString(EditorPrefs.GetString("unec_gridcolor1"), out color)) {
_gridBgColor = color;
}
}
if (!EditorPrefs.HasKey("unec_gridcolor0")) EditorPrefs.SetString("unec_gridcolor0", ColorUtility.ToHtmlStringRGB(new Color(0.45f, 0.45f, 0.45f)));
ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("unec_gridcolor0"), out _gridLineColor);
if (!EditorPrefs.HasKey("unec_gridcolor1")) EditorPrefs.SetString("unec_gridcolor1", ColorUtility.ToHtmlStringRGB(new Color(0.18f, 0.18f, 0.18f)));
ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("unec_gridcolor1"), out _gridBgColor);
//Load snap option
if (EditorPrefs.HasKey("unec_gridsnap")) gridSnap = EditorPrefs.GetBool("unec_gridsnap");
if (EditorPrefs.HasKey("unec_gridsnap")) _gridSnap = EditorPrefs.GetBool("unec_gridsnap");
_gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor);
_crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor);
NodeEditorWindow.RepaintAll();
prefsLoaded = true;
}
private static void ResetPrefs() {
EditorPrefs.SetString("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00");
EditorPrefs.SetString("unec_gridcolor0", ColorUtility.ToHtmlStringRGB(new Color(0.45f, 0.45f, 0.45f)));
EditorPrefs.SetString("unec_gridcolor1", ColorUtility.ToHtmlStringRGB(new Color(0.18f, 0.18f, 0.18f)));
/// <summary> Delete all prefs </summary>
public static void ResetPrefs() {
if (EditorPrefs.HasKey("unec_typecolors")) EditorPrefs.DeleteKey("unec_typecolors");
if (EditorPrefs.HasKey("unec_gridcolor0")) EditorPrefs.DeleteKey("unec_gridcolor0");
if (EditorPrefs.HasKey("unec_gridcolor1")) EditorPrefs.DeleteKey("unec_gridcolor1");
LoadPrefs();
}
private static void SavePrefs() {
if (!prefsLoaded) return;
string s = "";
foreach (var item in typeColors) {
s += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ",";
@ -124,26 +143,13 @@ namespace XNodeEditor {
EditorPrefs.SetString("unec_typecolors", s);
EditorPrefs.SetString("unec_gridcolor0", ColorUtility.ToHtmlStringRGB(_gridLineColor));
EditorPrefs.SetString("unec_gridcolor1", ColorUtility.ToHtmlStringRGB(_gridBgColor));
EditorPrefs.SetBool("unec_gridsnap", gridSnap);
EditorPrefs.SetBool("unec_gridsnap", _gridSnap);
}
private static void VerifyLoaded() {
if (!prefsLoaded) LoadPrefs();
}
public static Dictionary<string, Color> GetTypeColors() {
if (prefsLoaded) return typeColors;
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();

View File

@ -3,10 +3,6 @@
namespace XNodeEditor {
public static class NodeEditorResources {
//Unec textures
public static Texture2D gridTexture { get { return _gridTexture != null ? _gridTexture : _gridTexture = GenerateGridTexture(NodeEditorPreferences.gridLineColor,NodeEditorPreferences.gridBgColor); } }
private static Texture2D _gridTexture;
public static Texture2D crossTexture { get { return _crossTexture != null ? _crossTexture : _crossTexture = GenerateCrossTexture(NodeEditorPreferences.gridLineColor); } }
private static Texture2D _crossTexture;
public static Texture2D dot { get { return _dot != null ? _dot : _dot = Resources.Load<Texture2D>("unec_dot"); } }
private static Texture2D _dot;
public static Texture2D dotOuter { get { return _dotOuter != null ? _dotOuter : _dotOuter = Resources.Load<Texture2D>("unec_dot_outer"); } }
@ -54,8 +50,8 @@ namespace XNodeEditor {
for (int y = 0; y < 64; y++) {
for (int x = 0; x < 64; x++) {
Color col = bg;
if (y % 16 == 0 || x % 16 == 0) col = Color.Lerp(line,bg,0.6f);
if (y == 63 || x == 63) col = Color.Lerp(line,bg,0.3f);
if (y % 16 == 0 || x % 16 == 0) col = Color.Lerp(line,bg,0.65f);
if (y == 63 || x == 63) col = Color.Lerp(line,bg,0.35f);
cols[(y * 64) + x] = col;
}
}

View File

@ -92,5 +92,13 @@ namespace XNodeEditor {
}
return false;
}
/// <summary> Repaint all open NodeEditorWindows. </summary>
public static void RepaintAll() {
NodeEditorWindow[] windows = Resources.FindObjectsOfTypeAll<NodeEditorWindow>();
for (int i = 0; i < windows.Length; i++) {
windows[i].Repaint();
}
}
}
}