diff --git a/Source/Editor/Surface/Archetypes/Tools.cs b/Source/Editor/Surface/Archetypes/Tools.cs index 6d2d31b8c..7a37ad746 100644 --- a/Source/Editor/Surface/Archetypes/Tools.cs +++ b/Source/Editor/Surface/Archetypes/Tools.cs @@ -1510,6 +1510,7 @@ namespace FlaxEditor.Surface.Archetypes "Comment", // Title new Color(1.0f, 1.0f, 1.0f, 0.2f), // Color new Float2(400.0f, 400.0f), // Size + -1, // Order }, }, CurveNode.GetArchetype(12, "Curve", typeof(float), 0.0f, 1.0f), diff --git a/Source/Editor/Surface/SurfaceComment.cs b/Source/Editor/Surface/SurfaceComment.cs index 12b60a302..aad45190e 100644 --- a/Source/Editor/Surface/SurfaceComment.cs +++ b/Source/Editor/Surface/SurfaceComment.cs @@ -53,6 +53,12 @@ namespace FlaxEditor.Surface set => SetValue(2, value, false); } + private int OrderValue + { + get => (int)Values[3]; + set => SetValue(3, value, false); + } + /// public SurfaceComment(uint id, VisjectSurfaceContext context, NodeArchetype nodeArch, GroupArchetype groupArch) : base(id, context, nodeArch, groupArch) @@ -68,6 +74,19 @@ namespace FlaxEditor.Surface Title = TitleValue; Color = ColorValue; Size = SizeValue; + + // Order + // Backwards compatibility - When opening with an older version send the old comments to the back + if (Values.Length < 4) + { + if (IndexInParent > 0) + IndexInParent = 0; + OrderValue = IndexInParent; + } + else if(OrderValue != -1) + { + IndexInParent = OrderValue; + } } /// @@ -77,6 +96,10 @@ namespace FlaxEditor.Surface // Randomize color Color = ColorValue = Color.FromHSV(new Random().NextFloat(0, 360), 0.7f, 0.25f, 0.8f); + + if(OrderValue == -1) + OrderValue = Context.CommentCount - 1; + IndexInParent = OrderValue; } /// @@ -324,10 +347,28 @@ namespace FlaxEditor.Surface menu.AddSeparator(); ContextMenuChildMenu cmOrder = menu.AddChildMenu("Order"); { - cmOrder.ContextMenu.AddButton("Bring Forward", () => { if(IndexInParent < Context.Comments.Count-1) IndexInParent++;}); - cmOrder.ContextMenu.AddButton("Bring to Front", () => { IndexInParent = Context.Comments.Count - 1;}); - cmOrder.ContextMenu.AddButton("Send Backward", () => {if(IndexInParent > 0) IndexInParent--;}); - cmOrder.ContextMenu.AddButton("Send to Back", () => { IndexInParent = 0;}); + cmOrder.ContextMenu.AddButton("Bring Forward", () => + { + if(IndexInParent < Context.CommentCount-1) + IndexInParent++; + OrderValue = IndexInParent; + }); + cmOrder.ContextMenu.AddButton("Bring to Front", () => + { + IndexInParent = Context.CommentCount-1; + OrderValue = IndexInParent; + }); + cmOrder.ContextMenu.AddButton("Send Backward", () => + { + if(IndexInParent > 0) + IndexInParent--; + OrderValue = IndexInParent; + }); + cmOrder.ContextMenu.AddButton("Send to Back", () => + { + IndexInParent = 0; + OrderValue = IndexInParent; + }); } } } diff --git a/Source/Editor/Surface/VisjectSurface.cs b/Source/Editor/Surface/VisjectSurface.cs index 3fe722091..0185e364a 100644 --- a/Source/Editor/Surface/VisjectSurface.cs +++ b/Source/Editor/Surface/VisjectSurface.cs @@ -716,7 +716,18 @@ namespace FlaxEditor.Surface return null; Rectangle surfaceArea = GetNodesBounds(selection).MakeExpanded(80.0f); - return _context.CreateComment(ref surfaceArea, string.IsNullOrEmpty(text) ? "Comment" : text, new Color(1.0f, 1.0f, 1.0f, 0.2f)); + // Order below other selected comments + bool hasCommentsSelected = false; + int lowestCommentOrder = int.MaxValue; + for (int i = 0; i < selection.Count; i++) + { + if (selection[i] is not SurfaceComment || selection[i].IndexInParent >= lowestCommentOrder) + continue; + hasCommentsSelected = true; + lowestCommentOrder = selection[i].IndexInParent; + } + + return _context.CreateComment(ref surfaceArea, string.IsNullOrEmpty(text) ? "Comment" : text, new Color(1.0f, 1.0f, 1.0f, 0.2f), hasCommentsSelected ? lowestCommentOrder : -1); } private static Rectangle GetNodesBounds(List nodes) diff --git a/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs b/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs index 7d75e006b..12f01d4f1 100644 --- a/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs +++ b/Source/Editor/Surface/VisjectSurfaceContext.Serialization.cs @@ -920,12 +920,6 @@ namespace FlaxEditor.Surface // Link control control.OnLoaded(action); control.Parent = RootControl; - - if (control is SurfaceComment) - { - // Move comments to the background - control.IndexInParent = 0; - } } /// diff --git a/Source/Editor/Surface/VisjectSurfaceContext.cs b/Source/Editor/Surface/VisjectSurfaceContext.cs index 36e811c48..54c456d04 100644 --- a/Source/Editor/Surface/VisjectSurfaceContext.cs +++ b/Source/Editor/Surface/VisjectSurfaceContext.cs @@ -85,6 +85,27 @@ namespace FlaxEditor.Surface } } + /// + /// Gets the amount of surface comments + /// + /// + /// This is used as an alternative to , if only the amount of comments is important. + /// Is faster and doesn't allocate as much memory + /// + public int CommentCount + { + get + { + int count = 0; + for (int i = 0; i < RootControl.Children.Count; i++) + { + if (RootControl.Children[i] is SurfaceComment) + count++; + } + return count; + } + } + /// /// Gets a value indicating whether this context is modified (needs saving and flushing with surface data context source). /// @@ -286,13 +307,14 @@ namespace FlaxEditor.Surface /// The comment title. /// The comment color. /// The comment object - public virtual SurfaceComment SpawnComment(ref Rectangle surfaceArea, string title, Color color) + public virtual SurfaceComment SpawnComment(ref Rectangle surfaceArea, string title, Color color, int customOrder = -1) { var values = new object[] { title, // Title color, // Color surfaceArea.Size, // Size + customOrder, // Order }; return (SurfaceComment)SpawnNode(7, 11, surfaceArea.Location, values); } @@ -304,10 +326,10 @@ namespace FlaxEditor.Surface /// The comment title. /// The comment color. /// The comment object - public SurfaceComment CreateComment(ref Rectangle surfaceArea, string title, Color color) + public SurfaceComment CreateComment(ref Rectangle surfaceArea, string title, Color color, int customOrder = -1) { // Create comment - var comment = SpawnComment(ref surfaceArea, title, color); + var comment = SpawnComment(ref surfaceArea, title, color, customOrder); if (comment == null) { Editor.LogWarning("Failed to create comment.");