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:
parent
d181f432fc
commit
2cc9084955
@ -19,7 +19,7 @@ namespace XNodeEditor
|
||||
if (target is XNode.GroupNode node)
|
||||
{
|
||||
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;
|
||||
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.y) - padding.y
|
||||
);
|
||||
Debug.Log(GetSize(node.children.FirstOrDefault()));
|
||||
GUILayout.Label("");
|
||||
}
|
||||
|
||||
@ -96,50 +97,37 @@ namespace XNodeEditor
|
||||
public override int GetWidth()
|
||||
{
|
||||
int min = base.GetWidth();
|
||||
var node = target as XNode.GroupNode;
|
||||
if (node == null || node.children == null || node.children.Count == 0) return min + (int)padding.x + (int)padding.z;
|
||||
var group = target as XNode.GroupNode;
|
||||
if (group == null || group.children == null || group.children.Count == 0) return min + (int)padding.x + (int)padding.z;
|
||||
return Mathf.Max(min,
|
||||
node.children.Max(x =>
|
||||
GetEditor(x, window).GetWidth() + (int)x.position.x) - (int)target.position.x)
|
||||
group.children.Max(x =>
|
||||
GetSize(x).x + (int)x.position.x) - (int)target.position.x)
|
||||
+ (int)padding.z;
|
||||
}
|
||||
|
||||
public override int GetMinHeight()
|
||||
public override int GetHeight()
|
||||
{
|
||||
int min = base.GetMinHeight();
|
||||
var node = target as XNode.GroupNode;
|
||||
if (node == null || node.children == null || node.children.Count == 0) return min + (int)padding.y + (int)padding.w;
|
||||
int min = base.GetHeight();
|
||||
var group = target as XNode.GroupNode;
|
||||
if (group == null || group.children == null || group.children.Count == 0) return min + (int)padding.y + (int)padding.w;
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
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 => _nodeGroupBody != null ? _nodeGroupBody : _nodeGroupBody = Resources.Load<Texture2D>("xnode_group");
|
||||
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
|
||||
}
|
||||
};
|
||||
return style;
|
||||
}
|
||||
background = nodeGroupBody
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@ -120,10 +120,10 @@ namespace XNodeEditor {
|
||||
else return 208;
|
||||
}
|
||||
|
||||
public virtual int GetMinHeight()
|
||||
public virtual int GetHeight()
|
||||
{
|
||||
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>
|
||||
|
||||
@ -453,7 +453,7 @@ namespace XNodeEditor {
|
||||
{
|
||||
var editor = NodeEditor.GetEditor(group, this);
|
||||
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)))
|
||||
{
|
||||
|
||||
@ -293,8 +293,8 @@ namespace XNodeEditor {
|
||||
Vector2 start = gridPoints[0];
|
||||
Vector2 end = gridPoints[length - 1];
|
||||
//Modify first and last point in array so we can loop trough them nicely.
|
||||
gridPoints[0] = gridPoints[0] + Vector2.right * (20 / zoom);
|
||||
gridPoints[length - 1] = gridPoints[length - 1] + Vector2.left * (20 / zoom);
|
||||
gridPoints[0] = gridPoints[0] + Vector2.right * (16 / zoom);
|
||||
gridPoints[length - 1] = gridPoints[length - 1] + Vector2.left * (16 / zoom);
|
||||
//Draw first vertical lines going out from nodes
|
||||
Handles.color = gradient.Evaluate(0f);
|
||||
DrawAAPolyLineNonAlloc(thickness, start, gridPoints[0]);
|
||||
@ -488,6 +488,8 @@ namespace XNodeEditor {
|
||||
GUILayout.BeginVertical(style);
|
||||
}
|
||||
|
||||
GUILayout.BeginVertical();
|
||||
|
||||
GUI.color = guiColor;
|
||||
EditorGUI.BeginChangeCheck();
|
||||
|
||||
@ -495,14 +497,6 @@ namespace XNodeEditor {
|
||||
nodeEditor.OnHeaderGUI();
|
||||
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 (EditorGUI.EndChangeCheck()) {
|
||||
if (NodeEditor.onUpdateNode != null) NodeEditor.onUpdateNode(node);
|
||||
@ -512,6 +506,20 @@ namespace XNodeEditor {
|
||||
|
||||
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
|
||||
if (e.type == EventType.Repaint) {
|
||||
Vector2 size = GUILayoutUtility.GetLastRect().size;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user