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

Fixed proper sizing for group nodes

This commit is contained in:
Oli 2023-01-12 20:10:42 +00:00
parent d181f432fc
commit 2cc9084955
4 changed files with 42 additions and 46 deletions

View File

@ -19,7 +19,7 @@ namespace XNodeEditor
if (target is XNode.GroupNode node) if (target is XNode.GroupNode node)
{ {
bool selectChildren = NodeEditorWindow.currentActivity is NodeEditorWindow.NodeActivity.HoldNode or NodeEditorWindow.NodeActivity.DragNode; bool selectChildren = NodeEditorWindow.currentActivity is NodeEditorWindow.NodeActivity.HoldNode or NodeEditorWindow.NodeActivity.DragNode;
if (!_selected && Selection.activeObject == target && selectChildren) if (!_selected && selectChildren && Selection.objects.Contains(target))
{ {
_selected = true; _selected = true;
var selection = Selection.objects.ToList(); var selection = Selection.objects.ToList();
@ -81,6 +81,7 @@ namespace XNodeEditor
node.children.Min(x => x.position.x) - padding.x, node.children.Min(x => x.position.x) - padding.x,
node.children.Min(x => x.position.y) - padding.y node.children.Min(x => x.position.y) - padding.y
); );
Debug.Log(GetSize(node.children.FirstOrDefault()));
GUILayout.Label(""); GUILayout.Label("");
} }
@ -96,50 +97,37 @@ namespace XNodeEditor
public override int GetWidth() public override int GetWidth()
{ {
int min = base.GetWidth(); int min = base.GetWidth();
var node = target as XNode.GroupNode; var group = target as XNode.GroupNode;
if (node == null || node.children == null || node.children.Count == 0) return min + (int)padding.x + (int)padding.z; if (group == null || group.children == null || group.children.Count == 0) return min + (int)padding.x + (int)padding.z;
return Mathf.Max(min, return Mathf.Max(min,
node.children.Max(x => group.children.Max(x =>
GetEditor(x, window).GetWidth() + (int)x.position.x) - (int)target.position.x) GetSize(x).x + (int)x.position.x) - (int)target.position.x)
+ (int)padding.z; + (int)padding.z;
} }
public override int GetMinHeight() public override int GetHeight()
{ {
int min = base.GetMinHeight(); int min = base.GetHeight();
var node = target as XNode.GroupNode; var group = target as XNode.GroupNode;
if (node == null || node.children == null || node.children.Count == 0) return min + (int)padding.y + (int)padding.w; if (group == null || group.children == null || group.children.Count == 0) return min + (int)padding.y + (int)padding.w;
return Mathf.Max(min, return Mathf.Max(min,
node.children.Max(x => GetEditor(x, window).GetMinHeight() + (int) x.position.y - (int) target.position.y)) group.children.Max(x =>
GetSize(x).y + (int) x.position.y) - (int)target.position.y)
+ (int) padding.w; + (int) padding.w;
} }
public Vector2Int GetSize(Node node) => Vector2Int.RoundToInt(window.nodeSizes.TryGetValue(node, out var size) ? size : new Vector2(0,0));
private static int GetNodeWidth(Node node) private static Texture2D nodeGroupBody => _nodeGroupBody != null ? _nodeGroupBody : _nodeGroupBody = Resources.Load<Texture2D>("xnode_group");
{
Type type = node.GetType();
return (type.TryGetAttributeWidth(out int width) ? width : 208);
}
private static int GetNodeHeight(Node node)
{
Type type = node.GetType();
return (type.TryGetAttributeHeight(out int height) ? height : 100);
}
private static Texture2D nodeGroupBody { get { return _nodeGroupBody != null ? _nodeGroupBody : _nodeGroupBody = Resources.Load<Texture2D>("xnode_group"); } }
private static Texture2D _nodeGroupBody; private static Texture2D _nodeGroupBody;
public override GUIStyle GetBodyStyle() private static GUIStyle guiStyle;
public override GUIStyle GetBodyStyle() => guiStyle ??= new GUIStyle(base.GetBodyStyle())
{ {
var style = new GUIStyle(base.GetBodyStyle()) normal =
{ {
normal = background = nodeGroupBody
{ }
background = nodeGroupBody };
}
};
return style;
}
} }
} }

View File

@ -120,10 +120,10 @@ namespace XNodeEditor {
else return 208; else return 208;
} }
public virtual int GetMinHeight() public virtual int GetHeight()
{ {
Type type = target.GetType(); Type type = target.GetType();
return type.TryGetAttributeHeight(out int height) ? height : 80; return type.TryGetAttributeHeight(out int height) ? height : 0;
} }
/// <summary> Returns color for target node </summary> /// <summary> Returns color for target node </summary>

View File

@ -453,7 +453,7 @@ namespace XNodeEditor {
{ {
var editor = NodeEditor.GetEditor(group, this); var editor = NodeEditor.GetEditor(group, this);
editor.target = group; editor.target = group;
var rect = new Rect(group.position.x, group.position.y, editor.GetWidth(), editor.GetMinHeight()); var rect = new Rect(group.position.x, group.position.y, editor.GetWidth(), editor.GetHeight());
if (rect.Contains(WindowToGridPosition(Event.current.mousePosition))) if (rect.Contains(WindowToGridPosition(Event.current.mousePosition)))
{ {

View File

@ -293,8 +293,8 @@ namespace XNodeEditor {
Vector2 start = gridPoints[0]; Vector2 start = gridPoints[0];
Vector2 end = gridPoints[length - 1]; Vector2 end = gridPoints[length - 1];
//Modify first and last point in array so we can loop trough them nicely. //Modify first and last point in array so we can loop trough them nicely.
gridPoints[0] = gridPoints[0] + Vector2.right * (20 / zoom); gridPoints[0] = gridPoints[0] + Vector2.right * (16 / zoom);
gridPoints[length - 1] = gridPoints[length - 1] + Vector2.left * (20 / zoom); gridPoints[length - 1] = gridPoints[length - 1] + Vector2.left * (16 / zoom);
//Draw first vertical lines going out from nodes //Draw first vertical lines going out from nodes
Handles.color = gradient.Evaluate(0f); Handles.color = gradient.Evaluate(0f);
DrawAAPolyLineNonAlloc(thickness, start, gridPoints[0]); DrawAAPolyLineNonAlloc(thickness, start, gridPoints[0]);
@ -488,6 +488,8 @@ namespace XNodeEditor {
GUILayout.BeginVertical(style); GUILayout.BeginVertical(style);
} }
GUILayout.BeginVertical();
GUI.color = guiColor; GUI.color = guiColor;
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
@ -495,14 +497,6 @@ namespace XNodeEditor {
nodeEditor.OnHeaderGUI(); nodeEditor.OnHeaderGUI();
nodeEditor.OnBodyGUI(); nodeEditor.OnBodyGUI();
//Fill remaining vertical space
if (e.type == EventType.Repaint)
{
lastRect = GUILayoutUtility.GetLastRect();
}
float remainingSpace = nodeEditor.GetMinHeight() - lastRect.yMax - 17;
if (remainingSpace > 0) GUILayout.Space(remainingSpace);
//If user changed a value, notify other scripts through onUpdateNode //If user changed a value, notify other scripts through onUpdateNode
if (EditorGUI.EndChangeCheck()) { if (EditorGUI.EndChangeCheck()) {
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node); if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
@ -512,6 +506,20 @@ namespace XNodeEditor {
GUILayout.EndVertical(); GUILayout.EndVertical();
// Fill remaining space
var height = nodeEditor.GetHeight();
if (height > 0)
{
if (e.type == EventType.Repaint)
{
lastRect = GUILayoutUtility.GetLastRect();
}
float remainingSpace = height - lastRect.yMax - 17;
GUILayout.Space(Mathf.Max(0,remainingSpace));
}
GUILayout.EndVertical();
//Cache data about the node for next frame //Cache data about the node for next frame
if (e.type == EventType.Repaint) { if (e.type == EventType.Repaint) {
Vector2 size = GUILayoutUtility.GetLastRect().size; Vector2 size = GUILayoutUtility.GetLastRect().size;