using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
namespace Main.Ranging
{
public class Ruler
{
public delegate void PolygonAddedDelegate(Polygon Points);
public event PolygonAddedDelegate PolygonAddedEvent;
UInt16 AreaTempCount = 0;
double Sum = 0;//距离
Point mousePosition; //鼠标坐标
bool TrunOn_OffSwitch = false;//测距开关量
bool RulerKey = false;//标尺显示开关量
double Ruler_X = 0;//顶点X值
double Ruler_Y = 0;//顶点Y值
double movePlank_X = 0;
double movePlank_Y = 0;
UC_Ruler UC_ruler = new UC_Ruler();
Canvas RulerBoard;
Canvas MovePlank;
double zoomValue = 1;
PointCollection PointListTemp = new PointCollection();
Polygon MyPolygon = new Polygon();
bool _Eraser = false;
/// <summary>
/// 用于判断操作类型的开关
/// </summary>
public bool Eraser
{
get { return _Eraser; }
set { _Eraser = value; }
}
public double ZoomValue
{
get { return zoomValue; }
set { zoomValue = value; }
}
double x_cut, y_cut;
/// <summary>
///
/// </summary>
/// <param name="movePlank"></param>
/// <param name="rulerBoard">要计算的底层面板</param>
/// <param name="x_cut">绝对坐标偏差</param>
/// <param name="y_cut">相对坐标偏差</param>
public Ruler(Canvas movePlank, Canvas rulerBoard, double x_cut, double y_cut)
{
this.x_cut = x_cut;
this.y_cut = y_cut;
this.MovePlank = movePlank;
this.RulerBoard = rulerBoard;
movePlank.MouseMove += new MouseEventHandler(RulerBoard_MouseMove);
movePlank.MouseLeftButtonDown += new MouseButtonEventHandler(RulerBoard_MouseLeftButtonDown);
movePlank.MouseRightButtonDown += new MouseButtonEventHandler(RulerBoard_MouseRightButtonDown);
MovePlank.MouseWheel += new MouseWheelEventHandler(RulerBoard_MouseWheel);
//PointListTemp.Add(new PointCollection());
init();
}
private void init()
{
TrunOn_OffSwitch = false;//测距开关量
RulerKey = false;//标尺显示开关量
Ruler_X = 0;//顶点X值
Ruler_Y = 0;//顶点Y值
Sum = 0;//距离
//RulerBoard.Width = 3300;
//RulerBoard.Height = 2200;
}
public void AddElement(UserControl element, double x, double y)
{
Canvas.SetTop(element, y);
Canvas.SetLeft(element, x);
RulerBoard.Children.Add(element);
}
/// <summary>
/// 开始测量或关闭测量
/// </summary>
/// <param name="TrunOn_Off">true为开false为关</param>
public void RulerSwitch(bool TrunOn_Off)
{
TrunOn_OffSwitch = TrunOn_Off;
}
private void RulerBoard_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (!_Eraser)
{
if (TrunOn_OffSwitch)
{
if (RulerKey)
{
RulerBoard.Children.Remove(UC_ruler);
RulerKey = !RulerKey;
}
mousePosition = e.GetPosition(null);
UC_Coordinates coordinates = new UC_Coordinates();
//addElement(coordinates, (mousePosition.X - movePlank_X) / zoomValue, (mousePosition.Y - movePlank_Y) / zoomValue);
AddElement(coordinates, (mousePosition.X - movePlank_X - x_cut) / zoomValue, (mousePosition.Y - movePlank_Y - y_cut) / zoomValue);
PointListTemp.Add(new Point((mousePosition.X - movePlank_X - x_cut) / zoomValue, (mousePosition.Y - movePlank_Y - y_cut) / zoomValue));
string text = "起 点";
if (Ruler_X == 0 && Ruler_Y == 0)
{
//Ruler_X = (mousePosition.X - movePlank_X) / zoomValue;
//Ruler_Y = (mousePosition.Y - movePlank_Y) / zoomValue;
Ruler_X = (mousePosition.X - x_cut - movePlank_X) / zoomValue;
Ruler_Y = (mousePosition.Y - movePlank_Y - y_cut) / zoomValue;
coordinates.setValue(text);
return;
}
//Point startPt = new Point(Ruler_X, Ruler_Y);
//Sum += setDifference((mousePosition.X - movePlank_X) / zoomValue, (mousePosition.Y - movePlank_Y) / zoomValue);
Sum += setDifference((mousePosition.X - movePlank_X - x_cut) / zoomValue, (mousePosition.Y - movePlank_Y - y_cut) / zoomValue);
text = Sum.ToString() + "米";
coordinates.setValue(text);
//Point endPt = new Point((mousePosition.X - movePlank_X) / zoomValue, (mousePosition.Y - movePlank_Y) / zoomValue);
//Ruler_X = (mousePosition.X - movePlank_X) / zoomValue;
//Ruler_Y = (mousePosition.Y - movePlank_Y) / zoomValue;
//Point endPt = new Point((mousePosition.X - movePlank_X - x_cut) / zoomValue, (mousePosition.Y - movePlank_Y - y_cut) / zoomValue);
Ruler_X = (mousePosition.X - movePlank_X - x_cut) / zoomValue;
Ruler_Y = (mousePosition.Y - movePlank_Y - y_cut) / zoomValue;
//DrawingLine(startPt, endPt);
for (int i = 0; i < RulerBoard.Children.Count; i++)
{
FrameworkElement element = RulerBoard.Children[i] as FrameworkElement;
if (element.Tag != null)
if ("TempPolygon" == element.Tag.ToString())
{
RulerBoard.Children.RemoveAt(i);
break;
}
}
MyPolygon.Stroke = System.Windows.Media.Brushes.Black;
MyPolygon.Fill = System.Windows.Media.Brushes.LightSeaGreen;
MyPolygon.StrokeThickness = 2;
MyPolygon.HorizontalAlignment = HorizontalAlignment.Left;
MyPolygon.VerticalAlignment = VerticalAlignment.Center;
MyPolygon.Points = PointListTemp;
MyPolygon.Tag = "TempPolygon";
RulerBoard.Children.Add(MyPolygon);
}
}
}
private void RulerBoard_MouseMove(object sender, MouseEventArgs e)
{
if (!_Eraser)
{
if (TrunOn_OffSwitch)
{
string text = "单击确定起点";
FrameworkElement element = MovePlank as FrameworkElement;
movePlank_X = (double)element.GetValue(Canvas.LeftProperty);
movePlank_Y = (double)element.GetValue(Canvas.TopProperty);
mousePosition = e.GetPosition(null);
if (!RulerKey)
{
//addElement(UC_ruler, (mousePosition.X - movePlank_X) / zoomValue, (mousePosition.Y - movePlank_Y) / zoomValue);
AddElement(UC_ruler, (mousePosition.X - movePlank_X - x_cut) / zoomValue, (mousePosition.Y - movePlank_Y) / zoomValue);
RulerKey = !RulerKey;
}
//UC_ruler.SetValue(Canvas.LeftProperty, (mousePosition.X - movePlank_X) / zoomValue);
//UC_ruler.SetValue(Canvas.TopProperty, (mousePosition.Y - movePlank_Y) / zoomValue);
UC_ruler.SetValue(Canvas.LeftProperty, (mousePosition.X - movePlank_X - x_cut) / zoomValue);
UC_ruler.SetValue(Canvas.TopProperty, (mousePosition.Y - movePlank_Y) / zoomValue);
if (Ruler_X == 0 && Ruler_Y == 0)
{
UC_ruler.setValue(text);
return;
}
//double str = Sum + setDifference(mousePosition.X / zoomValue, mousePosition.Y / zoomValue);
double str = Sum + setDifference((mousePosition.X - movePlank_X - x_cut) / zoomValue, (mousePosition.Y - movePlank_X) / zoomValue);
text = "总长:" + str.ToString() + "米";
UC_ruler.setValue(text);
e.Handled = true;
}
}
}
private void RulerBoard_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
if (!_Eraser)
{
if (PointListTemp.Count > 0)
PolygonAddedEvent(MyPolygon);
//RulerBoard.Children.Remove(UC_ruler);
//init();
//PointListTemp.Clear();
}
}
private void RulerBoard_MouseWheel(object sender, MouseWheelEventArgs e)
{
FrameworkElement element = MovePlank as FrameworkElement;
movePlank_X = (double)element.GetValue(Canvas.LeftProperty);
movePlank_Y = (double)element.GetValue(Canvas.TopProperty);
//ruler.SetValue(Canvas.LeftProperty, (mousePosition.X - movePlank_X) / zoomValue);
//ruler.SetValue(Canvas.TopProperty, (mousePosition.Y - movePlank_Y) / zoomValue);
UC_ruler.SetValue(Canvas.LeftProperty, (mousePosition.X - x_cut - movePlank_X) / zoomValue);
UC_ruler.SetValue(Canvas.TopProperty, (mousePosition.Y - y_cut - movePlank_Y) / zoomValue);
e.Handled = true;
}
private double setDifference(double x, double y)
{
return Math.Round(Math.Sqrt(Math.Pow(x - Ruler_X, 2) + Math.Pow(y - Ruler_Y, 2)), 2);
}
//划线
protected void DrawingLine(Point startPt, Point endPt)
{
LineGeometry myLineGeometry = new LineGeometry();
myLineGeometry.StartPoint = startPt;
myLineGeometry.EndPoint = endPt;
Path myPath = new Path();
myPath.Stroke = Brushes.Red;
myPath.StrokeThickness = 2;
myPath.Data = myLineGeometry;
RulerBoard.Children.Add(myPath);
}
public void ClearAll()
{
init();
RulerBoard.Children.Clear();
}
public void AddNewArea()
{
PointListTemp.Clear();
//AreaTempCount++;
}
}
}