using PaintDotNet.Annotation.Command;
using PaintDotNet.Annotation.Enum;
using PaintDotNet.Annotation.FieldView;
using PaintDotNet.Annotation.Label;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
namespace PaintDotNet.Annotation
{
///
/// 通用->选择功能->需要满足可选择标注、测量、视场等功能
///
public class ToolPointer : Tool
{
private enum SelectionMode
{
None,
NetSelection, // group selection is active
Move, // object(s) are moves
Size // object is resized
}
private static SelectionMode selectMode = SelectionMode.None;
// Object which is currently resized:
private static DrawObject resizedObject;
private static int resizedObjectHandle;
// Keep state about last and current pointscroll (used to move and resize objects)
private static Point lastPoint = new Point(0, 0);
private static Point startPoint = new Point(0, 0);
private static CommandChangeState commandChangeState;
private static bool wasMove;
private static RichTextBox lab;
//private static NumericUpDown numLab;
private static LabelUsedDialog labelUsedDialog;
private static ISurfaceBox areaNow;
private static bool isModifyTextString = false;
//private static bool isModifyWorkType = false;
private static DrawObject dObject;
public static EventHandler GetMouseLeftClickPoint;
///
/// Left mouse button is pressed
///
///
///
public static void OnMouseDown(ISurfaceBox drawArea, MouseEventArgs e)
{
areaNow = drawArea;
if (isModifyTextString && dObject != null)
{
dObject.textboxMessage = lab.Text;
drawArea.Controls.Remove(lab);
drawArea.Refresh();
isModifyTextString = false;
}
//if (isModifyWorkType && dObject != null)
//{
// if(numLab.Value > 0)
// dObject.textboxMessage = numLab.Value.ToString();
// drawArea.Controls.Remove(numLab);
// drawArea.Refresh();
// isModifyWorkType = false;
//}
Point pointscroll = GetEventPointInArea(drawArea, e.Location);
if (e.Button == MouseButtons.Left)
{
for (int i = 0; i < drawArea.GraphicsList.Count; i++)
{
drawArea.GraphicsList[i].MouseDownPoint(pointscroll);
}
}
commandChangeState = null;
wasMove = false;
selectMode = SelectionMode.None;
// Test for resizing (only if control is selected, cursor is on the handle)
foreach (DrawObject o in drawArea.GraphicsList.Selection)
{
if (!o.isLocked)
{
int handleNumber = o.HitTest(pointscroll);
if (handleNumber > 0)
{
selectMode = SelectionMode.Size;
// keep resized object in class member
resizedObject = o;
resizedObjectHandle = handleNumber;
// Since we want to resize only one object, unselect all other objects
drawArea.GraphicsList.UnselectAll();
o.Selected = true;
commandChangeState = new CommandChangeState(drawArea.GraphicsList);
drawArea.GraphicsList.Dirty = true;
}
}
}
// Test for move (cursor is on the object)
if (selectMode == SelectionMode.None)
{
int n1 = drawArea.GraphicsList.Count;
DrawObject o = null;
for (int i = 0; i < n1; i++)
{
if (drawArea.GraphicsList[i].HitTest(pointscroll) == 0)
{
o = drawArea.GraphicsList[i];
break;
}
}
if (o != null)
{
selectMode = SelectionMode.Move;
// Unselect all if Ctrl is not pressed and clicked object is not selected yet
if ((Control.ModifierKeys & Keys.Control) == 0 && !o.Selected)
drawArea.GraphicsList.UnselectAll();
// Select clicked object
if (!o.isLocked)
{
o.Selected = true;
}
commandChangeState = new CommandChangeState(drawArea.GraphicsList);
drawArea.Cursor = Cursors.SizeAll;
drawArea.GraphicsList.Dirty = true;
}
}
// Net selection
if (selectMode == SelectionMode.None)
{
// click on background
if ((Control.ModifierKeys & Keys.Control) == 0)
drawArea.GraphicsList.UnselectAll();
selectMode = SelectionMode.NetSelection;
}
lastPoint.X = e.X;
lastPoint.Y = e.Y;
startPoint.X = e.X;
startPoint.Y = e.Y;
//drawArea.Capture = true;
drawArea.Refresh();
if (selectMode == SelectionMode.NetSelection)
{
drawArea.DrawRectangleFlag = true;
drawArea.DrawRectangle = new Rectangle(0, 0, 0, 0);
// Draw selection rectangle in initial position
/*ControlPaint.DrawReversibleFrame(
drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, lastPoint)),
Color.Black,
FrameStyle.Dashed);*/
}
OnMouseDownOne(drawArea, e);
if (e.Button == MouseButtons.Right)
{
int selectCount = 0;
for (int i = 0; i < drawArea.GraphicsList.Count; i++)
{
if (drawArea.GraphicsList[i].Selected)
{
selectCount++;
if (drawArea.GraphicsList[i].objectType == DrawClass.Measure)
areaNow.ToolNumber = 1;
else if (drawArea.GraphicsList[i].objectType == DrawClass.Label)
areaNow.ToolNumber = 2;
}
}
if (selectCount == 0)
areaNow.ToolNumber = 0;
}
}
///
/// Mouse is moved.
/// None button is pressed, or left button is pressed.
///
///
///
public static void OnMouseMove(ISurfaceBox drawArea, MouseEventArgs e)
{
Point pointscroll = GetEventPointInArea(drawArea, e.Location);
Point oldPoint = lastPoint;
wasMove = true;
// set cursor when mouse button is not pressed
if (e.Button == MouseButtons.None)
{
Cursor cursor = null;
for (int i = 0; i < drawArea.GraphicsList.Count; i++)
{
int n = drawArea.GraphicsList[i].HitTest(pointscroll);
if (n > 0)
{
cursor = drawArea.GraphicsList[i].GetHandleCursor(n);
break;
}
}
if (cursor == null)
cursor = Cursors.Default;
drawArea.Cursor = cursor;
return;
}
if (e.Button != MouseButtons.Left)
return;
/// Left button is pressed
// Find difference between previous and current position
int dx = e.X - lastPoint.X;
int dy = e.Y - lastPoint.Y;
lastPoint.X = e.X;
lastPoint.Y = e.Y;
// resize
if (selectMode == SelectionMode.Size)
{
if (resizedObject != null)
{
resizedObject.MoveHandleTo(pointscroll, resizedObjectHandle);
drawArea.SetDirty();
drawArea.Refresh();
drawArea.GraphicsList.Dirty = true;
}
}
// move
if (selectMode == SelectionMode.Move)
{
foreach (DrawObject o in drawArea.GraphicsList.Selection)
{
if (o is ViewBase)
{
var x = drawArea.UnscaleScalar(dx);
var y = drawArea.UnscaleScalar(dy);
o.Move(x, y);
}
else
o.Move(dx, dy);
}
drawArea.Cursor = Cursors.SizeAll;
drawArea.SetDirty();
drawArea.Refresh();
drawArea.GraphicsList.Dirty = true;
}
if (selectMode == SelectionMode.NetSelection)
{
drawArea.DrawRectangle = DrawObject.GetNormalizedRectangle(startPoint, new Point(e.X, e.Y));
drawArea.Refresh();
/*// Remove old selection rectangle
ControlPaint.DrawReversibleFrame(
drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, oldPoint)),
Color.Black,
FrameStyle.Dashed);
// Draw new selection rectangle
ControlPaint.DrawReversibleFrame(
drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, new Point(e.X, e.Y))),
Color.Black,
FrameStyle.Dashed);*/
return;
}
OnMouseMoveOne(drawArea, e);
}
///
/// Right mouse button is released
///
///
///
public static void OnMouseUp(ISurfaceBox drawArea, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
for (int i = 0; i < drawArea.GraphicsList.Count; i++)
{
drawArea.GraphicsList[i].MouseUp(false);
}
}
if (selectMode == SelectionMode.NetSelection)
{
drawArea.DrawRectangleFlag = false;
/*// Remove old selection rectangle
ControlPaint.DrawReversibleFrame(
drawArea.RectangleToScreen(DrawObject.GetNormalizedRectangle(startPoint, lastPoint)),
Color.Black,
FrameStyle.Dashed);*/
//Point pointscroll = GetEventPointInArea(drawArea, e);
// Make group selection
drawArea.GraphicsList.SelectInRectangle(
DrawRectangle.GetNormalizedRectangle(GetEventPointInArea(drawArea, new Point(startPoint.X, startPoint.Y)),
GetEventPointInArea(drawArea, new Point(lastPoint.X, lastPoint.Y))));
selectMode = SelectionMode.None;
}
if (resizedObject != null)
{
// after resizing
resizedObject.Normalize();
resizedObject = null;
}
drawArea.Capture = false;
drawArea.Refresh();
drawArea.GraphicsList.Dirty = true;
if (commandChangeState != null && wasMove)
{
// Keep state after moving/resizing and add command to history
commandChangeState.NewState(drawArea.GraphicsList);
drawArea.AddCommandToHistory(commandChangeState);
commandChangeState = null;
}
OnMouseUpOne(drawArea, e);
}
///
/// Remove selected object
///
///
///
public static void OnDelKeyDown(ISurfaceBox drawArea, MouseEventArgs e)
{
if (drawArea != null && drawArea.GraphicsList != null && drawArea.GraphicsList.Count > 0)
{
for (int i = drawArea.GraphicsList.Count - 1; i >= 0; i--)
{
if (drawArea.GraphicsList[i].Selected == true)
{
//对数字标记做单独处理
if (drawArea.GraphicsList[i].drawToolType == DrawToolType.DrawNumberMark)
{
DrawNumberMark drawNumberMark = (DrawNumberMark)drawArea.GraphicsList[i];
List objList = drawArea.GraphicsList.GetDrawToolTypeList(DrawToolType.DrawNumberMark);
if (objList != null && objList.Count > 0)
{
for (int j = 0; j < objList.Count; j++)
{
DrawNumberMark modifyNum = (DrawNumberMark)objList[j];
if (modifyNum.nowNum > drawNumberMark.nowNum)
{
modifyNum.nowNum -= 1;
}
}
}
}
drawArea.GraphicsList.RemoveObj(drawArea.GraphicsList[i]);
}
}
drawArea.Refresh();
}
}
public static void OnMouseLeftDoubleClick(ISurfaceBox drawArea, MouseEventArgs e)
{
if (drawArea != null && drawArea.GraphicsList != null && drawArea.GraphicsList.Count > 0)
{
areaNow = drawArea;
if (drawArea.GraphicsList.IsExsitSpecificObject(DrawToolType.DrawTextString))
{
Point pointscroll = GetEventPointInArea(drawArea, e.Location);
List objList = drawArea.GraphicsList.GetDrawToolTypeList(DrawToolType.DrawTextString);
Point temp = drawArea.GetCalcOriginPoint();
for (int i = 0; i < objList.Count; i++)
{
if (objList[i].Rectangle.Contains(pointscroll))
{
dObject = objList[i];
lab = new RichTextBox();
lab.Width = (int)(objList[i].Rectangle.Width * drawArea.ScaleRatio) + 1;
lab.Height = (int)(objList[i].Rectangle.Height * drawArea.ScaleRatio) + 1;
lab.BackColor = Color.White;
lab.Cursor = Cursors.Default;
lab.LostFocus += new EventHandler(OnTextBoxLostFocus);
lab.Location = new Point((int)(objList[i].Rectangle.X * drawArea.ScaleRatio) + temp.X + 16, (int)(objList[i].Rectangle.Y * drawArea.ScaleRatio) + temp.Y + 16);
lab.Text = objList[i].textboxMessage;
lab.ScrollBars = 0;
drawArea.Controls.Add(lab);
lab.Focus();
lab.BringToFront();
isModifyTextString = true;
break;
}
}
}
if (drawArea.GraphicsList.IsExsitSpecificObject(DrawToolType.DrawWorkType))
{
Point pointscroll = GetEventPointInArea(drawArea, e.Location);
List objList = drawArea.GraphicsList.GetDrawToolTypeList(DrawToolType.DrawWorkType);
Point temp = drawArea.GetCalcOriginPoint();
for (int i = 0; i < objList.Count; i++)
{
if (objList[i].Rectangle.Contains(pointscroll))
{
dObject = objList[i];
labelUsedDialog = new LabelUsedDialog();
labelUsedDialog.Text = "工字线距离";
labelUsedDialog.textBox1.Text = objList[i].textboxMessage;
labelUsedDialog.button1.Click += new EventHandler(LabelUsedDialogButton1_Click);
labelUsedDialog.textBox1.KeyPress += new KeyPressEventHandler(TextBox1_KeyPress);
labelUsedDialog.StartPosition = FormStartPosition.CenterParent;
labelUsedDialog.ShowDialog();
//numLab = new NumericUpDown();
//numLab.Cursor = Cursors.Default;
//numLab.LostFocus += new EventHandler(OnNumbericLostFocus);
////numLab.Location = new Point(pointscroll.X + temp.X, pointscroll.Y + temp.Y);
////numLab.Location = new Point(e.X, e.Y);
//numLab.Location = new Point(temp.X + (int)(pointscroll.X * drawArea.ScaleRatio) + 16, temp.Y + (int)(pointscroll.Y * drawArea.ScaleRatio) + 16);
//numLab.Maximum = 9999;
//numLab.Minimum = 0;
//numLab.DecimalPlaces = 2;
//numLab.Increment = 0.5M;
//numLab.Value = decimal.Parse(objList[i].textboxMessage);
//drawArea.Controls.Add(numLab);
//numLab.Focus();
//numLab.BringToFront();
//isModifyWorkType = true;
break;
}
}
}
}
//if (drawArea.ViewMoveOnMouseLeftDoubleClickEnable)
//{
if (GetMouseLeftClickPoint != null)
{
PointF pointscroll = drawArea.GetRulerPointInPanel(e.Location);
GetMouseLeftClickPoint(pointscroll, null);
}
//}
}
private static void LabelUsedDialogButton1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(labelUsedDialog.textBox1.Text))
{
MessageBox.Show(PdnResources.GetString("Menu.Thedistancevalue.text"));
return;
}
else
{
dObject.textboxMessage = labelUsedDialog.textBox1.Text;
areaNow.Refresh();
labelUsedDialog.Close();
}
}
private static void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox textBox1 = (TextBox)sender;
//判断按键是不是要输入的类型
if (((int)e.KeyChar < 48 || (int)e.KeyChar > 57) && (int)e.KeyChar != 8 && (int)e.KeyChar != 46)
e.Handled = true;
//小数点的处理
if ((int)e.KeyChar == 46)//小数点
{
if (textBox1.Text.Length <= 0)
e.Handled = true; //小数点不能在第一位
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(textBox1.Text, out oldf);
b2 = float.TryParse(textBox1.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
private static void OnTextBoxLostFocus(object sender, EventArgs e)
{
if (areaNow != null && lab != null)
{
dObject.textboxMessage = lab.Text;
areaNow.Controls.Remove(lab);
areaNow.Refresh();
isModifyTextString = false;
}
}
//private static void OnNumbericLostFocus(object sender, EventArgs e)
//{
// if (areaNow != null && numLab != null)
// {
// if (numLab.Value > 0)
// dObject.textboxMessage = numLab.Value.ToString();
// areaNow.Controls.Remove(numLab);
// areaNow.Refresh();
// isModifyWorkType = false;
// }
//}
public static void OnMouseClick(ISurfaceBox surfacebox, MouseEventArgs e)
{
}
public static void beginWithNewObject()
{
}
}
}