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

Fixed Begin- & EndZoomed to allow usage multiple times in one frame

Updated comment selecting code for better ux
This commit is contained in:
Jeroen van de Haterd 2019-01-11 09:39:27 +01:00
parent 4730f6a0df
commit 76dcbb94c6
2 changed files with 49 additions and 29 deletions

View File

@ -23,6 +23,7 @@ namespace XNodeEditor {
[NonSerialized] private XNode.NodePort draggedOutputTarget = null; [NonSerialized] private XNode.NodePort draggedOutputTarget = null;
[NonSerialized] private List<Vector2> draggedOutputReroutes = new List<Vector2>(); [NonSerialized] private List<Vector2> draggedOutputReroutes = new List<Vector2>();
[NonSerialized] private XNode.NodeGraphComment hoveredComment = null; [NonSerialized] private XNode.NodeGraphComment hoveredComment = null;
[NonSerialized] private bool deselectingComment = false;
[NonSerialized] private XNode.NodeGraphComment resizingComment = null; [NonSerialized] private XNode.NodeGraphComment resizingComment = null;
public enum NodeGraphCommentSide { Top, TopRight, Right, BottomRight, Bottom, BottomLeft, Left, TopLeft } public enum NodeGraphCommentSide { Top, TopRight, Right, BottomRight, Bottom, BottomLeft, Left, TopLeft }
public static NodeGraphCommentSide resizingCommentSide; public static NodeGraphCommentSide resizingCommentSide;
@ -75,6 +76,7 @@ namespace XNodeEditor {
} }
Repaint(); Repaint();
} else if (currentActivity == NodeActivity.HoldNode || currentActivity == NodeActivity.HoldComment) { } else if (currentActivity == NodeActivity.HoldNode || currentActivity == NodeActivity.HoldComment) {
deselectingComment = false;
RecalculateDragOffsets(e); RecalculateDragOffsets(e);
currentActivity = NodeActivity.DragNode; currentActivity = NodeActivity.DragNode;
Repaint(); Repaint();
@ -251,14 +253,7 @@ namespace XNodeEditor {
SelectNodesInComment(hoveredComment); SelectNodesInComment(hoveredComment);
} }
else SelectComment(hoveredComment, e.control || e.shift); else SelectComment(hoveredComment, e.control || e.shift);
} } else deselectingComment = true;
else if (e.control || e.shift) {
DeselectComment(hoveredComment);
if (e.shift)
{
DeselectNodesInComment(hoveredComment);
}
} else SelectComment(hoveredComment, false);
e.Use(); e.Use();
currentActivity = NodeActivity.HoldComment; currentActivity = NodeActivity.HoldComment;
@ -319,6 +314,15 @@ namespace XNodeEditor {
SelectNode(hoveredNode, false); SelectNode(hoveredNode, false);
} }
if (currentActivity == NodeActivity.HoldComment && deselectingComment) {
DeselectComment(hoveredComment);
if (e.shift) {
DeselectNodesInComment(hoveredComment);
}
deselectingComment = false;
}
// If click reroute, select it. // If click reroute, select it.
if (IsHoveringReroute && !(e.control || e.shift)) { if (IsHoveringReroute && !(e.control || e.shift)) {
selectedReroutes = new List<RerouteReference>() { hoveredReroute }; selectedReroutes = new List<RerouteReference>() { hoveredReroute };

View File

@ -15,6 +15,7 @@ namespace XNodeEditor {
public event Action onLateGUI; public event Action onLateGUI;
public XNode.NodeGraphComment renamingComment; public XNode.NodeGraphComment renamingComment;
public bool renamingStarted = false; public bool renamingStarted = false;
private Matrix4x4 _prevGuiMatrix;
private void OnGUI() { private void OnGUI() {
Event e = Event.current; Event e = Event.current;
@ -48,24 +49,40 @@ namespace XNodeEditor {
GUI.matrix = m; GUI.matrix = m;
} }
public static void BeginZoomed(Rect rect, float zoom, float topPadding) { public static Rect ScaleSizeBy(Rect rect, float scale, Vector2 pivotPoint) {
GUI.EndClip(); Rect result = rect;
result.x -= pivotPoint.x;
GUIUtility.ScaleAroundPivot(Vector2.one / zoom, rect.size * 0.5f); result.y -= pivotPoint.y;
Vector4 padding = new Vector4(0, topPadding, 0, 0); result.xMin *= scale;
padding *= zoom; result.xMax *= scale;
GUI.BeginClip(new Rect(-((rect.width * zoom) - rect.width) * 0.5f, -(((rect.height * zoom) - rect.height) * 0.5f) + (topPadding * zoom), result.yMin *= scale;
rect.width * zoom, result.yMax *= scale;
rect.height * zoom)); result.x += pivotPoint.x;
result.y += pivotPoint.y;
return result;
} }
public static void EndZoomed(Rect rect, float zoom, float topPadding) { public static Vector2 TopLeft(Rect rect) {
GUIUtility.ScaleAroundPivot(Vector2.one * zoom, rect.size * 0.5f); return new Vector2(rect.xMin, rect.yMin);
Vector3 offset = new Vector3( }
(((rect.width * zoom) - rect.width) * 0.5f),
(((rect.height * zoom) - rect.height) * 0.5f) + (-topPadding * zoom) + topPadding, public void BeginZoomed() {
0); GUI.EndGroup();
GUI.matrix = Matrix4x4.TRS(offset, Quaternion.identity, Vector3.one);
Rect clippedArea = ScaleSizeBy(position, zoom, TopLeft(position));
GUI.BeginGroup(clippedArea);
_prevGuiMatrix = GUI.matrix;
Matrix4x4 translation = Matrix4x4.TRS(TopLeft(clippedArea), Quaternion.identity, Vector3.one);
Matrix4x4 scale = Matrix4x4.Scale(new Vector3(1.0f / zoom, 1.0f / zoom, 1.0f));
GUI.matrix = translation * scale * translation.inverse * GUI.matrix;
}
public void EndZoomed() {
GUI.matrix = _prevGuiMatrix;
GUI.EndGroup();
GUI.BeginGroup(new Rect(0.0f, topPadding, Screen.width, Screen.height));
} }
public void DrawGrid(Rect rect, float zoom, Vector2 panOffset) { public void DrawGrid(Rect rect, float zoom, Vector2 panOffset) {
@ -254,7 +271,7 @@ namespace XNodeEditor {
if (onValidate != null) nodeHash = Selection.activeObject.GetHashCode(); if (onValidate != null) nodeHash = Selection.activeObject.GetHashCode();
} }
BeginZoomed(position, zoom, topPadding); BeginZoomed();
Vector2 mousePos = Event.current.mousePosition; Vector2 mousePos = Event.current.mousePosition;
@ -386,7 +403,7 @@ namespace XNodeEditor {
} }
if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray(); if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray();
EndZoomed(position, zoom, topPadding); EndZoomed();
//If a change in hash is detected in the selected node, call OnValidate method. //If a change in hash is detected in the selected node, call OnValidate method.
//This is done through reflection because OnValidate is only relevant in editor, //This is done through reflection because OnValidate is only relevant in editor,
@ -429,7 +446,7 @@ namespace XNodeEditor {
{ {
Event e = Event.current; Event e = Event.current;
BeginZoomed(position, zoom, topPadding); BeginZoomed();
Vector2 mousePos = Event.current.mousePosition; Vector2 mousePos = Event.current.mousePosition;
if (e.type != EventType.Layout) { if (e.type != EventType.Layout) {
@ -502,7 +519,6 @@ namespace XNodeEditor {
GUI.Label(lastRect, comment.comment, NodeEditorResources.styles.commentHeader); GUI.Label(lastRect, comment.comment, NodeEditorResources.styles.commentHeader);
} }
GUILayout.FlexibleSpace(); GUILayout.FlexibleSpace();
GUILayout.EndVertical(); GUILayout.EndVertical();
@ -582,7 +598,7 @@ namespace XNodeEditor {
} }
if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray(); if (e.type != EventType.Layout && currentActivity == NodeActivity.DragGrid) Selection.objects = preSelection.ToArray();
EndZoomed(position, zoom, topPadding); EndZoomed();
} }
} }
} }