123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563 |
- 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
- {
- /// <summary>
- /// 通用->选择功能->需要满足可选择标注、测量、视场等功能
- /// </summary>
- 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;
- /// <summary>
- /// Left mouse button is pressed
- /// </summary>
- /// <param name="drawArea"></param>
- /// <param name="e"></param>
- 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;
- }
- }
- /// <summary>
- /// Mouse is moved.
- /// None button is pressed, or left button is pressed.
- /// </summary>
- /// <param name="drawArea"></param>
- /// <param name="e"></param>
- 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);
- }
- /// <summary>
- /// Right mouse button is released
- /// </summary>
- /// <param name="drawArea"></param>
- /// <param name="e"></param>
- 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);
- }
- /// <summary>
- /// Remove selected object
- /// </summary>
- /// <param name="drawArea"></param>
- /// <param name="e"></param>
- 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<DrawObject> 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<DrawObject> 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<DrawObject> 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()
- {
- }
- }
- }
|