1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 09:16:01 +08:00

Minor bugfixes and cleanup

Changed:
 - Private fields with [Serializable] attributes are now also shown
 - Nodes can now only be dragged by the title
 - Output text alignment
Fixed:
 - NodeEditorWindow fields not resetting to null
 - Field editors
This commit is contained in:
Thor Brigsted 2017-09-21 18:15:40 +02:00
parent 846f7f30b1
commit 372099b03d
7 changed files with 50 additions and 39 deletions

View File

@ -7,6 +7,5 @@ public class AddNodeEditor : NodeEditor {
public override void OnNodeGUI() {
GUILayout.Label("YEAH CUSTOM");
base.OnNodeGUI();
}
}

View File

@ -16,7 +16,8 @@ public class NodeEditor {
DrawDefaultNodeGUI();
}
public void DrawDefaultNodeGUI() {
/// <summary> Draws standard field editors for all public fields </summary>
protected void DrawDefaultNodeGUI() {
FieldInfo[] fields = GetInspectorFields(target);
for (int i = 0; i < fields.Length; i++) {
Type fieldType = fields[i].FieldType;
@ -58,16 +59,18 @@ public class NodeEditor {
fieldValue = EditorGUILayout.Vector2Field(fieldName, (Vector2)fieldValue);
}
else if (fieldType == typeof(Vector3)) {
fieldValue = EditorGUILayout.Vector2Field(fieldName, (Vector3)fieldValue);
fieldValue = EditorGUILayout.Vector3Field(new GUIContent(fieldName), (Vector3)fieldValue);
}
else if (fieldType == typeof(Vector4)) {
fieldValue = EditorGUILayout.Vector2Field(fieldName, (Vector4)fieldValue);
fieldValue = EditorGUILayout.Vector4Field(fieldName, (Vector4)fieldValue);
}
else if (fieldType == typeof(Color)) {
fieldValue = EditorGUILayout.ColorField(fieldName, (Color)fieldValue);
}
else if (fieldType == typeof(AnimationCurve)) {
fieldValue = EditorGUILayout.CurveField(fieldName, fieldValue != null ? (AnimationCurve)fieldValue : new AnimationCurve());
AnimationCurve curve = fieldValue != null ? (AnimationCurve)fieldValue : new AnimationCurve();
curve = EditorGUILayout.CurveField(fieldName, curve);
if (fieldValue != curve) fields[i].SetValue(target, curve);
}
else if (fieldType.IsSubclassOf(typeof(UnityEngine.Object)) || fieldType == typeof(UnityEngine.Object)) {
fieldValue = EditorGUILayout.ObjectField(fieldName, (UnityEngine.Object)fieldValue, fieldType, true);
@ -78,8 +81,9 @@ public class NodeEditor {
}
}
}
private static FieldInfo[] GetInspectorFields(Node node) {
return node.GetType().GetFields().Where(f => f.IsPublic).ToArray();
return node.GetType().GetFields().Where(f => f.IsPublic || f.GetCustomAttributes(typeof(SerializeField),false) != null).ToArray();
}
private static bool GetAttrib<T>(object[] attribs, out T attribOut) where T : Attribute {

View File

@ -16,12 +16,13 @@ public partial class NodeEditorWindow {
private bool IsHoveringNode { get { return hoveredNode != null; } }
private bool HasSelectedNode { get { return selectedNode != null; } }
private Node hoveredNode;
private Node selectedNode;
private Node draggedNode;
private NodePort hoveredPort;
private NodePort draggedOutput;
private NodePort draggedOutputTarget;
private Node hoveredNode = null;
[NonSerialized] private Node selectedNode = null;
[NonSerialized] private Node draggedNode = null;
[NonSerialized] private NodePort hoveredPort = null;
[NonSerialized] private NodePort draggedOutput = null;
[NonSerialized] private NodePort draggedOutputTarget = null;
private Rect nodeRects;
@ -81,7 +82,7 @@ public partial class NodeEditorWindow {
}
}
}
else if (IsHoveringNode) {
else if (IsHoveringNode && IsHoveringTitle(hoveredNode)) {
draggedNode = hoveredNode;
dragOffset = hoveredNode.position.position - WindowToGridPosition(e.mousePosition);
}
@ -108,9 +109,6 @@ public partial class NodeEditorWindow {
isPanning = false;
}
UpdateHovered();
break;
case EventType.repaint:
break;
}
}
@ -171,9 +169,16 @@ public partial class NodeEditorWindow {
}
if (newHoverPort != hoveredPort) {
hoveredPort = newHoverPort;
Repaint();
}
}
else hoveredPort = null;
}
bool IsHoveringTitle(Node node) {
Vector2 mousePos = Event.current.mousePosition;
//Get node position
Vector2 nodePos = GridToWindowPosition(node.position.position);
Rect windowRect = new Rect(nodePos, new Vector2(node.position.size.x / zoom, 30 / zoom));
return windowRect.Contains(mousePos);
}
}

View File

@ -12,13 +12,11 @@ public partial class NodeEditorWindow {
Matrix4x4 m = GUI.matrix;
Controls();
if (e.type != EventType.MouseMove && e.type != EventType.MouseDrag) {
DrawGrid(position, zoom, panOffset);
DrawNodes();
DrawConnections();
DrawDraggedConnection();
DrawToolbar();
}
GUI.matrix = m;
}
@ -135,6 +133,7 @@ public partial class NodeEditorWindow {
BeginZoomed(position, zoom);
if (e.type == EventType.Repaint) portRects.Clear();
foreach (Node node in graph.nodes) {
//Get node position
Vector2 nodePos = GridToWindowPositionNoClipped(node.position.position);
@ -173,13 +172,6 @@ public partial class NodeEditorWindow {
nodeEditor.OnNodeGUI();
GUILayout.EndArea();
if (windowRect.position != nodePos) {
nodePos = windowRect.position;
node.position.position = WindowToGridPosition(nodePos);
//Vector2 newPos = windowRect =
}
}
EndZoomed(position, zoom);
EndWindows();

View File

@ -37,12 +37,19 @@ public partial class NodeEditorWindow {
outputColor = new GUIStyle((GUIStyle)"flow shader out 4");
outputTexture = new GUIStyle((GUIStyle)"flow shader out 5");
foreach (GUIStyle style in new GUIStyle[] { inputInt, inputString, inputFloat, inputObject, inputTexture, inputColor, outputInt, outputString, outputFloat, outputObject, outputTexture, outputColor }) {
foreach (GUIStyle style in new GUIStyle[] { inputInt, inputString, inputFloat, inputObject, inputTexture, inputColor }) {
style.normal.textColor = Color.black;
style.fixedHeight = 18;
style.alignment = TextAnchor.MiddleLeft;
style.onHover.textColor = Color.red;
}
foreach (GUIStyle style in new GUIStyle[] { outputInt, outputString, outputFloat, outputObject, outputTexture, outputColor }) {
style.normal.textColor = Color.black;
style.fixedHeight = 18;
style.alignment = TextAnchor.MiddleRight;
style.onHover.textColor = Color.red;
}
}
public GUIStyle GetInputStyle(Type type) {

View File

@ -13,12 +13,16 @@ public partial class NodeEditorWindow {
if (DropdownButton("View", 50)) { }
if (DropdownButton("Settings", 70)) { }
if (DropdownButton("Tools", 50)) ToolsContextMenu();
//Draw hover info
if (Event.current.type == EventType.Layout || Event.current.type == EventType.Repaint) {
if (IsHoveringNode) {
GUILayout.Space(20);
string hoverInfo = hoveredNode.GetType().ToString();
if (IsHoveringPort) hoverInfo += " > " + hoveredPort.name;
GUILayout.Label(hoverInfo);
}
}
// Make the toolbar extend all throughout the window extension.
GUILayout.FlexibleSpace();

View File

@ -4,7 +4,7 @@ using UnityEngine;
using System;
[Serializable]
public class NodePort :ISerializationCallbackReceiver{
public class NodePort : ISerializationCallbackReceiver{
public enum IO { Input, Output}
public int ConnectionCount { get { return connections.Count; } }