Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Scroll to view center (Read 2422 times)
due
YaBB Newbies
*
Offline



Posts: 27
Joined: Jun 20th, 2007
Scroll to view center
Nov 28th, 2007 at 8:19am
Print Post  
Hi,

I have to center a diagram element in the middle of the view. My following (scroll) approach does not do the right thing.
Code
Select All
	//extract view state
	RectangleF viewBounds = diagramView.Bounds;
	float viewWidth = viewBounds.Width;
	float viewHeight = viewBounds.Height;
	float viewX = viewBounds.X;
	float viewY = viewBounds.Y;

	//extract document state
	RectangleF docBounds = diagram.Bounds;
	float docWidth = docBounds.Width;
	float docHeight = docBounds.Height;

	//scrolling necessary
	if (docWidth >= viewWidth &&
	    docHeight >= viewHeight) return false;

	//prepare scrolling
	float scrollX = -1;
	float scrollY = -1;

	//scrolling X necessary
	if (viewWidth < docWidth) {
	  float maxScrollX = docWidth - viewWidth;
	  float centerX = rectangle.X + (rectangle.Width / 2);
	  scrollX = centerX - (viewWidth / 2);
	  if (scrollX < 0) scrollX = 0;
	  if (scrollX > maxScrollX) scrollX = maxScrollX;
	} else {
	  //take current value
	  scrollX = viewX;
	}

	//scrolling Y necessary
	if (viewHeight < docHeight) {
	  float maxScrollY = docHeight - viewHeight;
	  float centerY = rectangle.Y + (rectangle.Height / 2);
	  scrollY = centerY - (viewHeight / 2);
	  if (scrollY < 0) scrollY = 0;
	  if (scrollY > maxScrollY) scrollY = maxScrollY;
	} else {
	  //take current value
	  scrollY = viewY;
	}

	//do scrolling
	diagramView.ScrollTo(scrollX, scrollY);
 



What is here the problem or how can I achieve the centering of diagram elements?
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Scroll to view center
Reply #1 - Nov 28th, 2007 at 1:34pm
Print Post  
Hi,

The BringIntoView method of DiagramView centers the item if it is not in the visible area. The code looks like this:

Code
Select All
public void BringIntoView(DiagramItem item)
{
	if (item != null)
	{
		// Get the currently visible document area
		RectangleF rcClient = ClientToDoc(ClientRectangle);

		// Check if the objects is entirely in the visible area
		RectangleF rcObj = item.GetRepaintRect(false);
		if (rcObj.Left < rcClient.Left || rcObj.Right > rcClient.Right ||
			rcObj.Top < rcClient.Top || rcObj.Bottom > rcClient.Bottom)
		{
			// Scroll to center the object in the visible area
			float cx = (rcObj.Left + rcObj.Right - rcClient.Width) / 2;
			float cy = (rcObj.Top + rcObj.Bottom - rcClient.Height) / 2;

			if (cx < diagram.Bounds.Left)
				cx = diagram.Bounds.Left;
			else if (cx > diagram.Bounds.Right - rcClient.Width)
				cx = diagram.Bounds.Right - rcClient.Width;

			if (cy < diagram.Bounds.Top)
				cy = diagram.Bounds.Top;
			else if (cy > diagram.Bounds.Bottom - rcClient.Height)
				cy = diagram.Bounds.Bottom - rcClient.Height;

			ScrollTo(cx, cy);
		}
	}
}
 



You could just remove the "if" statement that checks if the item is not already in the view. Centering will not work for items that are near the diagram boundary, because you cannot scroll to a position outside diagram.Bounds.

Stoyan
  
Back to top
 
IP Logged
 
due
YaBB Newbies
*
Offline



Posts: 27
Joined: Jun 20th, 2007
Re: Scroll to view center
Reply #2 - Nov 30th, 2007 at 8:44am
Print Post  
Fine, it works. Thx!

Problem:
You have to use
diagramView.ClientToDoc(diagramView.ClientRectangle) instead of
diagramView.Bounds
to extract view bounds.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint