using Resources;
using SmartCoalApplication.Annotation.Command;
using SmartCoalApplication.Annotation.Enum;
using SmartCoalApplication.Annotation.Label;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace SmartCoalApplication.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 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)
{
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;
resizedObject.isHitTest = 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
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.View)
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)
{
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.isHitTest = false;
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){
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.ViewMoveOnMouseLeftDoubleClickEnable)
//{
if (GetMouseLeftClickPoint != null)
{
PointF pointscroll = drawArea.GetRulerPointInPanel(e.Location);
GetMouseLeftClickPoint(pointscroll, null);
}
//}
}
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()
{
}
}
}