Page Index Toggle Pages: 1 [2]  Send TopicPrint
Hot Topic (More than 10 Replies) move selected node using arrow keys (Read 9379 times)
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3148
Joined: Oct 19th, 2005
Re: move selected node using arrow keys
Reply #15 - Apr 20th, 2017 at 10:11am
Print Post  
Modify the FindOverlaps method to take bounds argument -

Code
Select All
List<DiagramNode> FindOverlaps(
	DiagramNode modifiedNode, Rect bounds, double minDist)
{
	bounds.Inflate(minDist - 1, minDist - 1);

	var overlaps = new List<DiagramNode>();
	.... 



Older places where you call it such as OnNodeModifying, also pass node.Bounds like FindOverlaps(e.Node, e.Node.Bounds, 5). In arrow-key movements methods add an if statement that checks whether new rect leads to overlaps before assigning it -

Code
Select All
var rect = node.Bounds;
rect.X = rect.Left - 10;
if (FindOverlaps(node, rect, 5).Count == 0)
    node.Move(node.Bounds.Left - 10, node.Bounds.Top); 

  
Back to top
 
IP Logged
 
kelum
Full Member
***
Offline


I Love MindFusion!

Posts: 100
Joined: Mar 25th, 2017
Re: move selected node using arrow keys
Reply #16 - Apr 20th, 2017 at 5:47pm
Print Post  
so as you mentioned I changed code

Code
Select All
        List<DiagramNode> FindOverlaps(DiagramNode modifiedNode, Rect bounds, double minDist)
        {

            bounds.Inflate(minDist - 1, minDist - 1);

            var overlaps = new List<DiagramNode>();
            foreach (var node in flowchart.Nodes)
            {
                if (modifiedNode == node)
                    continue;
                if (bounds.IntersectsWith(node.Bounds))
                    overlaps.Add(node);
            }
            return overlaps;
        } 



and all the arrow key operations changed as follows

Code
Select All
            // left arrow handler
            flowchart.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateLeft, (sender, args) =>
                {
                    foreach (var node in flowchart.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Left - 10;

                        if (node != null && node.Bounds.Left - 10 >= flowchart.Bounds.Left && (FindOverlaps(node, rect, 5).Count == 0))
                        {
                            node.Move(node.Bounds.Left - 10, node.Bounds.Top);
                        }

                    }
                }));

            // right arrow handler
            flowchart.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateRight, (sender, args) =>
                {
                    foreach (var node in flowchart.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Left - 10;

                        if (node != null && node.Bounds.Right + 10 <= flowchart.Bounds.Right && (FindOverlaps(node, rect, 5).Count == 0))
                        {
                            node.Move(node.Bounds.Left + 10, node.Bounds.Top);
                        }
                    }
                }));

            // up arrow handler
            flowchart.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateUp, (sender, args) =>
                {
                    foreach (var node in flowchart.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Left - 10;

                        if (node != null && node.Bounds.Top - 10 >= flowchart.Bounds.Top && (FindOverlaps(node, rect, 5).Count == 0))
                        {
                            node.Move(node.Bounds.Left, node.Bounds.Top - 10);
                        }
                    }


                }));

            // down arrow handler
            flowchart.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateDown, (sender, args) =>
                {
                    foreach (var node in flowchart.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Left - 10;

                        if (node != null && node.Bounds.Bottom + 10 <= flowchart.Bounds.Bottom && (FindOverlaps(node, rect, 5).Count == 0))
                        {
                            node.Move(node.Bounds.Left, node.Bounds.Top + 10);
                        }
                    }
                })); 



this is compiling without errors, but in here once move one node over other node (overlap) then its locking each other, by selecting one of that overlapped node I can't move node away using arrow keys
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3148
Joined: Oct 19th, 2005
Re: move selected node using arrow keys
Reply #17 - Apr 21st, 2017 at 5:40am
Print Post  
rect.X = rect.Left - 10 is only valid when you move to the left, you'll need to alter this expression for other directions.

If you allow nodes to overlap at any point, than offsetting by just 10 pixels might not find a non-overlapping position so it won't accept the movement. You might want to change the logic so that it always accepts movement if the moved node currently overlaps with another one.
  
Back to top
 
IP Logged
 
kelum
Full Member
***
Offline


I Love MindFusion!

Posts: 100
Joined: Mar 25th, 2017
Re: move selected node using arrow keys
Reply #18 - Apr 21st, 2017 at 9:04am
Print Post  
so as you mentioned I'updated code,

then Left Arrow Key and Up Arrow Key started to working properly (which is i can close up to 5 pixels, then those are not overlap)

Code
Select All
            // left arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateLeft, (sender, args) =>
                {
                    foreach (var node in diagram.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Left - 10;

                        if (node != null && node.Bounds.Left - 10 >= diagram.Bounds.Left && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left - 10, node.Bounds.Top);
                        }

                    }
                }));

		   // up arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateUp, (sender, args) =>
                {

                    foreach (var node in diagram.Selection.Nodes)
                    {

                        var rect = node.Bounds;
                        rect.Y = rect.Top - 10;

                        if (node != null && node.Bounds.Top - 10 >= diagram.Bounds.Top && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left, node.Bounds.Top - 10);
                        }
                    }


                })); 



Left Arrow Key Operation with proper distance


Up Arrow Key Operation with proper distance


but Right arrow key and Bottom Arrow key not working like Left Arrow Key and Up Arrow Key , which is can't close up to 5 pixels they are locating with different spaces
Code
Select All
            // right arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateRight, (sender, args) =>
                {

                    foreach (var node in diagram.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Right + 10;

                        if (node != null && node.Bounds.Right + 10 <= diagram.Bounds.Right && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left + 10, node.Bounds.Top);
                        }
                    }
                }));



            // down arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateDown, (sender, args) =>
                {

                    foreach (var node in diagram.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.Y = rect.Bottom + 10;

                        if (node != null && node.Bounds.Bottom + 10 <= diagram.Bounds.Bottom && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left, node.Bounds.Top + 10);
                        }
                    }
                }));  



Bottom Arrow Key Operation with wide distance



Right Arrow Key Operation with wide distance



How can I reduce more spaces in right and down arrow handlers like up and left handlers
  
Back to top
 
IP Logged
 
kelum
Full Member
***
Offline


I Love MindFusion!

Posts: 100
Joined: Mar 25th, 2017
Re: move selected node using arrow keys
Reply #19 - Apr 21st, 2017 at 9:17am
Print Post  
Anyway I found the solution like this,


Code
Select All
            // left arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateLeft, (sender, args) =>
                {
                    foreach (var node in diagram.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Left - 10;

                        if (node != null && node.Bounds.Left - 10 >= diagram.Bounds.Left && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left - 10, node.Bounds.Top);
                        }

                    }
                }));



            // up arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateUp, (sender, args) =>
                {

                    foreach (var node in diagram.Selection.Nodes)
                    {

                        var rect = node.Bounds;
                        rect.Y = rect.Top - 10;

                        if (node != null && node.Bounds.Top - 10 >= diagram.Bounds.Top && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left, node.Bounds.Top - 10);
                        }
                    }


                }));

		    // right arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateRight, (sender, args) =>
                {

                    foreach (var node in diagram.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.X = rect.Left + 10;

                        if (node != null && node.Bounds.Right + 10 <= diagram.Bounds.Right && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left + 10, node.Bounds.Top);
                        }
                    }
                }));

            // down arrow handler
            diagram.CommandBindings.Add(
                new CommandBinding(Diagram.NavigateDown, (sender, args) =>
                {

                    foreach (var node in diagram.Selection.Nodes)
                    {
                        var rect = node.Bounds;
                        rect.Y = rect.Top + 10;

                        if (node != null && node.Bounds.Bottom + 10 <= diagram.Bounds.Bottom && (FindOverlaps(node, rect, 2).Count == 0))
                        {
                            node.Move(node.Bounds.Left, node.Bounds.Top + 10);
                        }
                    }
                })); 



let me know if you have suggestion
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Online


tech.support

Posts: 3148
Joined: Oct 19th, 2005
Re: move selected node using arrow keys
Reply #20 - Apr 21st, 2017 at 10:05am
Print Post  
It should look like this as you've found, otherwise it didn't match the positions where you are moving the node on next line -

// right
rect.X = rect.Left + 10;

// down
rect.Y = rect.Top + 10;
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1 [2] 
Send TopicPrint