using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace Vectorform
{
public class CanvasViewport
{
#region Private Data
///
/// Provides easy access to the Canvas' Translation
///
private TranslateTransform _Translation;
///
/// Provides easy access to the Canvas' Scale
///
private ScaleTransform _Scale;
///
/// Provides easy access to the canvas.
///
private Canvas _TheCanvas;
#endregion
#region Properties
///
/// Get or set the size of the viewport (in logical units).
///
public Size Size
{
get
{
return new Size(_TheCanvas.Width / _Scale.ScaleX, _TheCanvas.Height / _Scale.ScaleY);
}
set
{
_Scale.ScaleX = _TheCanvas.Width / value.Width;
_Scale.ScaleY = _TheCanvas.Height / value.Height;
}
}
///
/// Get or set the center of the view (in logical units).
///
public Point Center
{
get
{
return new Point(_Translation.X + Size.Width * 0.5, _Translation.Y + Size.Height * 0.5);
}
set
{
_Translation.X = -(value.X - Size.Width * 0.5) * _Scale.ScaleX;
_Translation.Y = -(value.Y - Size.Height * 0.5) * _Scale.ScaleY;
}
}
#endregion
#region Methods
///
/// Construct the viewport on a canvas.
///
///
public CanvasViewport(Canvas thePage)
{
_TheCanvas = thePage;
// Find the transformations.
TransformGroup xforms = thePage.RenderTransform as TransformGroup;
if (xforms != null)
{
foreach (Transform x in xforms.Children)
{
if (x is TranslateTransform)
{
_Translation = x as TranslateTransform;
}
else if (x is ScaleTransform)
{
_Scale = x as ScaleTransform;
}
}
}
}
///
/// Convert from a screen point to a logical point.
///
///
///
public Point ToLogical(Point devicePoint)
{
return new Point((devicePoint.X - _Translation.X) / _Scale.ScaleX, (devicePoint.Y - _Translation.Y) / _Scale.ScaleY);
}
#endregion
}
}