Page Index Toggle Pages: 1 Send TopicPrint
Hot Topic (More than 10 Replies) Query regarding Zooming. (Read 7737 times)
Anant_Shukla
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: Mar 16th, 2009
Query regarding Zooming.
Mar 20th, 2009 at 11:58am
Print Post  
Hi,

I have a requirement of Zoom-In and Zoom-Out functionality in my application on MouseScroll event. For the pupose I have used the following code :

private void OnDiagramMouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
Point p = e.GetPosition(Parent as ScrollViewer);
Point oldcurpos = diagram.ClientToDoc(p);

diagram.ZoomFactor += e.Delta / 60;

Point newcurpos = diagram.ClientToDoc(e.GetPosition(Parent as ScrollViewer));// diagram.ClientToDoc(e.GetPosition(diagram));
double diffx = (oldcurpos.X - newcurpos.X);
double diffy = (oldcurpos.Y - newcurpos.Y);

diagram.ScrollTo(new Point(
diagram.ScrollX + diffx < 0 ? 0 : diagram.ScrollX + diffx,
diagram.ScrollY + diffy < 0 ? 0 : diagram.ScrollY + diffy));
}

The issues faced by me are as follows :

1. In my diagram I have a node placed at coordinates (200,-80). Now when I Zoom-In the diagram this node disappears even with the slightest of zoom.

2. I require the diagram should always fit to the container i.e when I Zoom-In and Zoom-Out the diagram should not get resized only the items within that diagram should get zoomed in and zoomed-out. The diagram should always fit to the container.

3. The zoom-in and zoom-out should be respective to the mouse position while moving the mouse wheel.

Any suggestion how can I accomplish these requirements.

Thanks and Regards,
Anant Shukla.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Query regarding Zooming.
Reply #1 - Mar 21st, 2009 at 8:09am
Print Post  
Hi,

1. Replace the ScrollTo call with

diagram.ScrollTo(new Point(
     diagram.ScrollX + diffx < diagram.Bounds.X ? diagram.Bounds.X : diagram.ScrollX + diffx,
     diagram.ScrollY + diffy < diagram.Bounds.Y ? diagram.Bounds.Y : diagram.ScrollY + diffy));
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Query regarding Zooming.
Reply #2 - Mar 21st, 2009 at 8:14am
Print Post  
2. Set the ScrollViewer's Background to have the same value as the diagram's BackBrush and the effect should be pretty much the same.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Query regarding Zooming.
Reply #3 - Mar 21st, 2009 at 9:09am
Print Post  
3. The code above should do that when the diagram.Bounds is larger than the ScrollViewer's visible area. However if it's smaller, it won't work because it would require setting a new scroll position, and the ScrollViewer doesn't allow that when its content is smaller than the visible area. A solution is to always set diagram.Bounds to a value that would make the zoomed content larger than the scrollviewer, e.g. this seems to work

Code
Select All
private void OnDiagramMouseWheel(object sender, MouseWheelEventArgs e)
{
	e.Handled = true;
	Point p = e.GetPosition(Parent as ScrollViewer);
	Point oldcurpos = diagram.ClientToDoc(p);

	double newScrollX = diagram.ScrollX;
	double newScrollY = diagram.ScrollY;
	diagram.ZoomFactor += e.Delta / 60;

	Rect newBounds = diagram.ClientToDoc(
		new Rect(0, 0, scrollViewer.ActualWidth, scrollViewer.ActualHeight));

	Point newcurpos = diagram.ClientToDoc(e.GetPosition(Parent as ScrollViewer));
	double diffx = (oldcurpos.X - newcurpos.X);
	double diffy = (oldcurpos.Y - newcurpos.Y);
	newScrollX += diffx;
	newScrollY += diffy;

	newBounds.X = newScrollX;
	newBounds.Y = newScrollY;
	newBounds.Union(diagram.GetContentBounds(false, false));
	newBounds.Inflate(200, 200);
	diagram.Bounds = newBounds;

	diagram.ScrollTo(new Point(newScrollX, newScrollY));
}
 



I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Anant_Shukla
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: Mar 16th, 2009
Re: Query regarding Zooming.
Reply #4 - Mar 21st, 2009 at 9:51am
Print Post  
Hi Stoyan,

Thanks for the sample code.

The code is working fine.

Whereas I do have another requirement which is I require the diagram to resize itself accordingly to fit all the items present on the diagram at moment of initialization of the diagram. The code used by me to initialize the diagram is as follows :

private void InitDiagram(Diagram diagram)
{
diagram.AllowSelfLoops = false;
diagram.AllowUnconnectedLinks = false;
diagram.AllowLinksRepeat = false;
diagram.Behavior = Behavior.Modify;
diagram.BackBrush = Brushes.LightBlue;
diagram.AutoResize = AutoResize.AllDirections;
double newScrollX = diagram.ScrollX;
double newScrollY = diagram.ScrollY;
Rect newBounds = diagram.ClientToDoc(
new Rect(0,0,scrollViewer.ActualWidth, scrollViewer.ActualHeight));
newBounds.X = newScrollX;
newBounds.Y = newScrollY;
newBounds.Union(diagram.GetContentBounds(false, false));
newBounds.Inflate(50, 50);
diagram.Bounds = newBounds;

diagram.ScrollTo(new Point(newScrollX, newScrollY));

}

Can you please tell me what I am missing here.

Thanks and Regards,
Anant Shukla.
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Query regarding Zooming.
Reply #5 - Mar 22nd, 2009 at 6:33am
Print Post  
Try the ZoomToFit method.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
Anant_Shukla
YaBB Newbies
*
Offline


I love YaBB 1G - SP1!

Posts: 36
Joined: Mar 16th, 2009
Re: Query regarding Zooming.
Reply #6 - Mar 23rd, 2009 at 6:28am
Print Post  
Hi Stoyan,

There are few issues in the same Zoom-In and Zoom-Out functionality.

1. I needed the diagram should occupy the complete container area no matter I Zoom-In or Zoom-Out. You suggested to match the ScrollViewers background property with the diagram's backbrush property. But this do not resolve the issue because the diagram area still resizes when I Zoom-In or Zoom-Out. I do not want the diagram to resize it should always fit to the container.

2. I want my diagram should zoom to fit all the items present on the diagram when the diagram is initialized. I tried the ZoomToFit method but its not working and I also need the diagram to zoom only when required i.e if there is only one node in the diagram then it should get zoomed. The code used by me is as follows :

private void InitDiagram(Diagram diagram)
{
diagram.Behavior = Behavior.Modify;
diagram.BackBrush = Brushes.LightBlue;
diagram.AutoResize = AutoResize.AllDirections;
diagram.AutoScroll = false;
diagram.AllowSelfLoops = false;
diagram.AllowUnconnectedLinks = false;
diagram.AllowLinksRepeat = false;
diagram.LinkCrossings = LinkCrossings.Arcs;
diagram.ZoomToFit();

}

3. In Your suggestion which is as follows :

" The code above should do that when the diagram.Bounds is larger than the ScrollViewer's visible area. However if it's smaller, it won't work because it would require setting a new scroll position, and the ScrollViewer doesn't allow that when its content is smaller than the visible area. A solution is to always set diagram.Bounds to a value that would make the zoomed content larger than the scrollviewer, e.g. this seems to work

Code:

private void OnDiagramMouseWheel(object sender, MouseWheelEventArgs e)
{

e.Handled = true;

Point p = e.GetPosition(Parent as ScrollViewer);

Point oldcurpos = diagram.ClientToDoc(p);


double newScrollX = diagram.ScrollX;

double newScrollY = diagram.ScrollY;

diagram.ZoomFactor += e.Delta / 60;


Rect newBounds = diagram.ClientToDoc(


new Rect(0, 0, scrollViewer.ActualWidth, scrollViewer.ActualHeight));


Point newcurpos = diagram.ClientToDoc(e.GetPosition(Parent as ScrollViewer));

double diffx = (oldcurpos.X - newcurpos.X);

double diffy = (oldcurpos.Y - newcurpos.Y);

newScrollX += diffx;

newScrollY += diffy;


newBounds.X = newScrollX;

newBounds.Y = newScrollY;

newBounds.Union(diagram.GetContentBounds(false, false));

newBounds.Inflate(200, 200);

diagram.Bounds = newBounds;


diagram.ScrollTo(new Point(newScrollX, newScrollY));
}

"
What is the "scrollViewer" variable can you describe in detail. How to declare that. Also after a particular value of zooming out it stops working.

Any suggestion how can I resolve above issues.

Thanks and Regards,
Anant Shukla.


« Last Edit: Mar 23rd, 2009 at 8:16am by Anant_Shukla »  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Query regarding Zooming.
Reply #7 - Apr 9th, 2009 at 8:37am
Print Post  
Hi Stoyan,

I have tried this code for Zoom-In and Zoom-Out.
private void OnDiagramMouseWheel(object sender, MouseWheelEventArgs e)
{

e.Handled = true; 

Point p = e.GetPosition(Parent as ScrollViewer); 

Point oldcurpos = diagram.ClientToDoc(p);


double newScrollX = diagram.ScrollX;

double newScrollY = diagram.ScrollY;

diagram.ZoomFactor += e.Delta / 60;


Rect newBounds = diagram.ClientToDoc(


new Rect(0, 0, scrollViewer.ActualWidth, scrollViewer.ActualHeight));


Point newcurpos = diagram.ClientToDoc(e.GetPosition(Parent as ScrollViewer));

double diffx = (oldcurpos.X - newcurpos.X); 

double diffy = (oldcurpos.Y - newcurpos.Y);

newScrollX += diffx;

newScrollY += diffy;


newBounds.X = newScrollX;

newBounds.Y = newScrollY;

newBounds.Union(diagram.GetContentBounds(false, false));

newBounds.Inflate(200, 200);

diagram.Bounds = newBounds;


diagram.ScrollTo(new Point(newScrollX, newScrollY));
}

Its working fine but only problem is that when I ScrollUp mouse for Zoom-Out,it Zoom -Out the node but also shifts the diagram to some upper position.
I want,Zoom-Out should be performed at the point of cursor only and it does not shifts the diagram when Zoom-In or Zoom-Out.

Thanks in advance,
Anshul
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Query regarding Zooming.
Reply #8 - Apr 9th, 2009 at 9:12am
Print Post  
This happens if the diagram is initially smaller than the scrollviewer, right?
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Query regarding Zooming.
Reply #9 - Apr 9th, 2009 at 10:47am
Print Post  
Hi,
how can i make size of diagram equals to size of scrollviewer ?

I have tried for the same in InitDiagram function by making equal height and width for both, but problem is still the same.when I ScrollUp mouse for Zoom-Out,it Zoom -Out the node but also shifts the diagram to some upper position.

1. Is their any other way to make diagram larger than or equal to scrollviewer ?
2. if the diagram is smaller,is it possible to Zoom-out on the node without shifting the diagram ?

Thanks,
Anshul
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Query regarding Zooming.
Reply #10 - Apr 9th, 2009 at 12:24pm
Print Post  
1. You could add this to the Window_Loaded handler:
Code
Select All
diagram.Bounds = diagram.ClientToDoc(
	new Rect(0, 0, scrollViewer.ActualWidth, scrollViewer.ActualHeight));
 



2. Should be possible if you subtract the offset of the diagram's top-left corner from the scrollviers's top-left corner from ths point in the MouseWheel code:
...
Point p = e.GetPosition(Parent as ScrollViewer); 
p.X -= ???;
p.Y -= ???;
...
I don't know how you can find that offset though.

Stoyan
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Query regarding Zooming.
Reply #11 - Apr 18th, 2009 at 7:22am
Print Post  
Hi Stoyan,

Zoom-In/Zoom-Out of  the nodes has done according to the center of the diagram,but I want to perform same operation at the cursor position.
how I can do this ?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Query regarding Zooming.
Reply #12 - Apr 18th, 2009 at 7:31am
Print Post  
  
Back to top
 
IP Logged
 
Anshul
Senior Member
****
Offline


I love YaBB 1G - SP1!

Posts: 316
Joined: Apr 3rd, 2009
Re: Query regarding Zooming.
Reply #13 - Apr 18th, 2009 at 8:28am
Print Post  
I have already tried this,its not working.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint