Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Operation of custom DrawShapesBehavior subclass (Read 4566 times)
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Operation of custom DrawShapesBehavior subclass
Sep 10th, 2020 at 8:54am
Print Post  
Hi, Slavcho Smiley

Before implementing the custom class RectanglesBehavior, you can drag the view at will in the diagram by holding down the left mouse button, but it won’t work after the implementation. What kind of modification is needed for RectanglesBehavior?

namespace AOI
{
    class RectanglesBehavior:DrawShapesBehavior
    {
        public RectanglesBehavior(Diagram diagram):base(diagram)
        {
        }

        public override InteractionState StartDraw(Point point)
        {
            Diagram.ModifierKeyActions.Control = ModifierKeyAction.None;
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                //return new InteractionState(new ShapeNode(Diagram), null, MindFusion.Diagramming.Wpf.Action.Create);
                var node = new ShapeNode(Diagram);
                node.Brush = Brushes.Transparent;
                return new InteractionState(node, null, MindFusion.Diagramming.Wpf.Action.Create);
            }

            var selected = Diagram.ActiveItem as ShapeNode;
            if (selected!=null)
            {
                var handle = selected.HitTestHandle(point);
                if (handle != null)
                    return new InteractionState(selected, handle, MindFusion.Diagramming.Wpf.Action.Modify);
            }

            /*
           var rect = NearestRect(point);
           if (rect != null)
           {
                 var handlesStyle = rect.HandlesStyle;
                 rect.HandlesStyle = HandlesStyle.SquareHandles2;
                 var handle = rect.HitTestHandle(point);
                 rect.HandlesStyle = handlesStyle;
                 if (handle != null)
                       return new InteractionState(rect, handle, Action.Modify);
           }*/

            // return base.StartDraw(point);
            return null;
        }

           /*public override CursorHint SetMouseCursor(Point point, out bool startInteraction)
     {
           return base.SetMouseCursor(point, out startInteraction);
     }*/

           /*protected override void OnMouseUp(Point mousePosition, MindFusion.Diagramming.Wpf.MouseButton mouseButton)
           {
                 base.OnMouseUp(mousePosition, mouseButton);
           }*/


           DiagramNode NearestRect(Point point)
           {
                 DiagramNode nearest = null;
                 double minDist = double.MaxValue;
                 foreach (var node in Diagram.Nodes)
                 {
                       if (node.Locked)
                             continue;
                       var dist = DistToRect(point, node.Bounds);
                       if (dist < minDist)
                       {
                             minDist = dist;
                             nearest = node;
                       }
                 }
                 return nearest;
           }

        double DistToRect(Point point, Rect rect)
        {
            var v = new[]
            {
            new Point(rect.Left, rect.Top),
            new Point(rect.Right, rect.Top),
            new Point(rect.Right, rect.Bottom),
            new Point(rect.Left, rect.Bottom),
        };
            double minDist = double.MaxValue;
            for (var i = 0; i < 4; i++)
            {
                var v1 = v[i];
                var v2 = v[(i + 1) % 4];
                var dist = MindFusion.Utilities.DistToLineSegment(point, v1, v2);
                if (dist < minDist)
                    minDist = dist;
            }
            return minDist;
        }
    }

   
}

Best regards.
« Last Edit: Sep 10th, 2020 at 11:46am by JackPan »  

9_10_4.png ( 22 KB | 118 Downloads )
9_10_4.png
9_10_5.png ( 21 KB | 104 Downloads )
9_10_5.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Operation of custom DrawShapesBehavior subclass
Reply #1 - Sep 11th, 2020 at 5:09am
Print Post  
Switch to diagram.Behavior = Pan when you need to pan by dragging the view.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Re: Operation of custom DrawShapesBehavior subclass
Reply #2 - Sep 11th, 2020 at 8:23am
Print Post  
Hi, Slavcho Smiley

I put diagram.Behavior = Behavior.Pan; in the constructor of RectanglesBehavior or in the Window_Loaded of the MainWindow class, but nothing happens. At first, I set the Diagram property Behavior to "PanAndModify" in MainWindow.xaml. If RectanglesBehavior is not instantiated: diagram.CustomBehavior = new RectanglesBehavior(diagram); you can press and drag the left mouse button to move the view of the diagram normally, but Not after instantiating RectanglesBehavior. May I ask what caused it? How to solve it?

Best regards.
  

9_11_0.png ( 29 KB | 115 Downloads )
9_11_0.png
9_11_1.png ( 93 KB | 119 Downloads )
9_11_1.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Operation of custom DrawShapesBehavior subclass
Reply #3 - Sep 14th, 2020 at 6:58am
Print Post  
I meant switch dynamically between the two properties (Behavior and CustomBehavior) depending on where the mouse is. However here's something better, this makes the built-in PanBehavior class public and you can inherit your RectanglesBehavior class from it -

https://mindfusion.eu/_temp/wpfdiag_pan.zip

Code
Select All
class RectanglesBehavior : PanBehavior
{
	public RectanglesBehavior(Diagram diagram) : base(diagram, false, true)
	{
............ 



Now whenever you return null from StartDraw, the base class will pan the view.

Code
Select All
public override InteractionState StartDraw(Point point)
{
	Diagram.ModifierKeyActions.Control = ModifierKeyAction.None;
	if (Keyboard.Modifiers == ModifierKeys.Control)
	{
		var node = new ShapeNode(Diagram);
		node.Brush = Brushes.Transparent;
		return new InteractionState(node, null, Action.Create);
	}

	var selected = Diagram.ActiveItem as ShapeNode;
	if (selected != null )
	{
		var handle = selected.HitTestHandle(point);
		if (handle != null)
			return new InteractionState(selected, handle, Action.Modify);
	}

	/*var rect = NearestRect(point);
	if (rect != null)
	{
		var handlesStyle = rect.HandlesStyle;
		rect.HandlesStyle = HandlesStyle.SquareHandles2;
		var handle = rect.HitTestHandle(point);
		rect.HandlesStyle = handlesStyle;
		if (handle != null)
			return new InteractionState(rect, handle, Action.Modify);
	}*/

	return null; // pan
} 



Regards,
Slavcho
  
Back to top
 
IP Logged
 
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Re: Operation of custom DrawShapesBehavior subclass
Reply #4 - Sep 14th, 2020 at 8:22am
Print Post  
Thanks, Slavcho Smiley

I understand that when the border of ShapeNode is selected, it is switched to CustomBehavior, otherwise it is switched to Behavior, but I don't understand that there has been no assignment operation to the properties of CustomBehavior. How do I switch?

Is it necessary to import the dll of the wpfdiag_pan.zip compressed package to recognize the PanBehavior class?

Best regards.
  

9_14_0.png ( 12 KB | 112 Downloads )
9_14_0.png
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Operation of custom DrawShapesBehavior subclass
Reply #5 - Sep 14th, 2020 at 9:53am
Print Post  
Quote:
Is it necessary to import the dll of the wpfdiag_pan.zip compressed package to recognize the PanBehavior class?


Yes, it's an internal class in previous version, this makes it public.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Re: Operation of custom DrawShapesBehavior subclass
Reply #6 - Sep 14th, 2020 at 11:09am
Print Post  
Hi, Slavcho Smiley

I just tried to add all the dlls in the folder to the reference of the project, but the version conflict occurred during compilation. Which dlls should be added?

Best regards.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Operation of custom DrawShapesBehavior subclass
Reply #7 - Sep 14th, 2020 at 12:32pm
Print Post  
Change your project's reference path to where you've unzipped the assemblies, or overwrite the files in folder where the project was originally referencing them from (e.g. program files/mindfusion/diagramming.wpf/.net 4.5). Also check project/references/mindfusion.*  in VS property window - make sure none of them has the "use specific version" property enabled.
  
Back to top
 
IP Logged
 
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Re: Operation of custom DrawShapesBehavior subclass
Reply #8 - Sep 14th, 2020 at 2:24pm
Print Post  
Thanks, Slavcho Smiley

I changed the reference path in the project property page to the location of the unzipped assembly wpfdiag_pan, but the result is still the same. I don’t know if it’s because I didn’t find this location: project / references / mindfusion? The result is still using the previous reference.

Best regards.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Operation of custom DrawShapesBehavior subclass
Reply #9 - Sep 15th, 2020 at 5:27am
Print Post  
By project -> references I mean expand the project's references section in Solution Explorer window. You should find mindfusion.*.dll assemblies you use listed there, check their properties in Property Window to verify any of them not having 'use specific version' enabled. Otherwise read your error message carefully, it should be telling you what's wrong. I've also added a test project with your rectangles code here, it compiles correctly for me -
https://mindfusion.eu/_temp/wpfdiag_pan.zip
  
Back to top
 
IP Logged
 
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Re: Operation of custom DrawShapesBehavior subclass
Reply #10 - Sep 15th, 2020 at 6:15am
Print Post  
Thanks very mush, Slavcho Smiley

Yes, I have successfully implemented it according to the settings of TestDraw, but I don’t understand why all references except MindFusion itself and other references must be set to the property of "Use a specific version" not enabled, please ask this "Use specific version" What is the role of the "version" attribute?

Best regards.
  
Back to top
 
IP Logged
 
Slavcho
YaBB Moderator
*****
Offline


tech.support

Posts: 3152
Joined: Oct 19th, 2005
Re: Operation of custom DrawShapesBehavior subclass
Reply #11 - Sep 15th, 2020 at 1:27pm
Print Post  
The version it refers to is the assembly "Version" attribute you can see in same property window. If "use specific version" is enabled, the compiler will refuse to use the newer assemblies if we've incremented the version number, and fall back to older assemblies it finds in locations such as bin/debug or paths from project's reference paths list. Then if you have "use specific version" enabled only for some assemblies, you'd probably get newer assemblies loading along with older ones and leading to version conflicts.

Regards,
Slavcho
  
Back to top
 
IP Logged
 
JackPan
Full Member
***
Offline


I Love MindFusion!

Posts: 134
Joined: Apr 9th, 2020
Re: Operation of custom DrawShapesBehavior subclass
Reply #12 - Sep 15th, 2020 at 1:33pm
Print Post  
Thanks very much, Slavcho Smiley

I finally got it.

Best regards. Smiley Smiley Smiley
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint