| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Collections;
- using System.Linq;
- namespace HySoft.FlowEditor
- {
- public partial class FlowEditorCanvas : UserControl
- {
- #region 字段
- const int KeyVal_Del = 46;
- const int KeyVal_Enter = 13;
- /// <summary>
- /// 节点连接线与矩形边距的宽度
- /// </summary>
- static int _nodeMargin = 10;
- List<FlowNode> _nodeList;
- List<FlowLine> _lineList;
- FlowNode[] _selectNodes;
- FlowLine[] _selectLines;
- /// <summary>
- /// 选中操作的开始鼠标点
- /// </summary>
- Point _selectBegainPos;
- /// <summary>
- /// 是否开始选中操作
- /// </summary>
- bool _bBegainSelect;
- /// <summary>
- /// 用于显示选中框的容器
- /// </summary>
- Panel _selectControl;
- /// <summary>
- /// 是否是多个节点移动
- /// </summary>
- bool _bMultiNodeMove = false;
- #endregion
- #region 属性
- /// <summary>
- /// 节点列表
- /// </summary>
- public List<FlowNode> NodeList
- {
- get { return _nodeList; }
- }
- /// <summary>
- /// 线列表
- /// </summary>
- public List<FlowLine> LineList
- {
- get { return _lineList; }
- }
- /// <summary>
- /// 被选中节点组
- /// </summary>
- public FlowNode[] SelectedNodes
- {
- get { return _selectNodes; }
- set
- {
- SetSelectedNodes(value);
- }
- }
- /// <summary>
- /// 被选中连线组
- /// </summary>
- public FlowLine[] SelectedLines
- {
- get { return _selectLines; }
- set { _selectLines = value; }
- }
- #endregion
- #region 事件
- /// <summary>
- /// 节点单击事件
- /// </summary>
- public Delegate_ClickNode Evt_ClickNode;
- /// <summary>
- /// 节点双击事件
- /// </summary>
- public Delegate_DoubleClickNode Evt_DoubleClickNode;
- /// <summary>
- /// 线单击事件
- /// </summary>
- public Delegate_ClickLine Evt_ClickLine;
- /// <summary>
- /// 线双击事件
- /// </summary>
- public Delegate_DoubleClickLine Evt_DoubleClickLine;
- /// <summary>
- /// 回车键按下事件
- /// </summary>
- public Delegate_KeyDown_Enter Evt_KeyDown_Enter;
- /// <summary>
- /// 删除键按下事件
- /// </summary>
- public Delegate_KeyDown_Del Evt_KeyDown_Del;
- #endregion
- #region 公开方法
- /// <summary>
- /// 构造方法
- /// </summary>
- public FlowEditorCanvas()
- {
- _nodeList = new List<FlowNode>();
- _lineList = new List<FlowLine>();
- InitializeComponent();
- _bBegainSelect = false;
- _selectControl = new Panel();
- _selectControl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
- _selectControl.BackColor = Color.Transparent;
- _selectControl.Visible = false;
- }
- /// <summary>
- /// 添加一个流程节点
- /// </summary>
- /// <param name="node"></param>
- public void AddANode(FlowNode node)
- {
- _nodeList.Add(node);
- AddNodeControlToCanvasControl(node.Control);
- SetSelectedNodes(new FlowNode[] { node });
- }
- /// <summary>
- /// 添加一个流程节点
- /// </summary>
- /// <param name="node"></param>
- /// <param name="coord"></param>
- public void AddANode(FlowNode node, Point coord)
- {
- _nodeList.Add(node);
- AddNodeControlToCanvasControl(node.Control);
- SetSelectedNodes(new FlowNode[] { node });
- }
- /// <summary>
- /// 添加一条线
- /// </summary>
- /// <param name="line">要添加的线</param>
- /// <param name="headNode">头节点</param>
- /// <param name="tailNode">尾节点</param>
- public void AddALine(FlowLine line, FlowNode headNode, FlowNode tailNode)
- {
- AddALine(line, headNode, null, tailNode, null);
- }
- /// <summary>
- /// 添加一条线
- /// </summary>
- /// <param name="line">要添加的线</param>
- /// <param name="headNode">头节点</param>
- /// <param name="headNodeOutputPort">头结点的出口,如果为null,方法会自动创建一个出口</param>
- /// <param name="tailNode">尾节点</param>
- /// <param name="tailNodeInputPort">尾结点的入口,如果为null,方法会自动创建一个入口</param>
- public void AddALine(FlowLine line, FlowNode headNode, NodePort headNodeOutputPort, FlowNode tailNode, NodePort tailNodeInputPort)
- {
- List<Point> points = GetLinePointListWidthScroll(headNode, tailNode);
- line.PointList = points;
- NodePort outputPort = headNodeOutputPort;
- if (outputPort == null)
- {
- outputPort = new NodePort();
- tailNode.AddOutputPort(outputPort);
- }
- tailNode.AddLineToOutPort(line, outputPort);
- NodePort inputPort = tailNodeInputPort;
- if (inputPort == null)
- {
- inputPort = new NodePort();
- headNode.AddInputPort(inputPort);
- }
- headNode.AddLineToInputPort(line, inputPort);
- _lineList.Add(line);
- AddLineControlToCanvasControl(line.Control);
- }
- /// <summary>
- /// 删除一个流程节点
- /// 同时会删除与该流程节点连接的线及其线对应的两端节点的端口
- /// </summary>
- /// <param name="node"></param>
- public void DelNode(FlowNode node)
- {
- if (_nodeList.IndexOf(node) < 0)
- return;
- SetSelectedNodes(null);
- NodePort[] inputPorts = node.InputPortList.ToArray();
- if (inputPorts != null && inputPorts.Length > 0)
- {
- foreach (NodePort nodePort in inputPorts)
- DelLine(nodePort.Line, true, true);
- }
- NodePort[] outputPorts = node.OutputPortList.ToArray();
- if (outputPorts != null && outputPorts.Length > 0)
- {
- foreach (NodePort nodePort in outputPorts)
- DelLine(nodePort.Line, true, true);
- }
- RemoveNodeControlFromCanvasControl(node.Control);
- _nodeList.Remove(node);
- }
- /// <summary>
- /// 删除一条线
- /// </summary>
- /// <param name="line">指定的线</param>
- /// <param name="bRemoveOutputPort">是否同时移除线对应的出口</param>
- /// <param name="bRemoveInputPort">是否同时移除线对应的入口</param>
- /// <returns>移除成功</returns>
- public void DelLine(FlowLine line, bool bRemoveHeadNodeOutputPort, bool bRemoveTailNodeInputPort)
- {
- if (line == null)
- return;
- if (line.TailNode != null && line.TailNodePort != null)
- {
- NodePort inputPort = line.TailNode.FindInputPort(line);
- if (inputPort != null)
- {
- line.TailNode.RemoveLineFromInputPort(line, inputPort);
- if (bRemoveTailNodeInputPort)
- line.TailNode.RemoveInputPort(inputPort);
- }
- }
- if (line.HeadNode != null && line.HeadNodePort != null)
- {
- NodePort outputPort = line.HeadNode.FindOutputPort(line);
- if (outputPort != null)
- {
- line.HeadNode.RemoveLineFromOutputPort(line, outputPort);
- if (bRemoveTailNodeInputPort)
- line.HeadNode.RemoveOutputPort(outputPort);
- }
- }
- RemoveLineControlFromCanvasControl(line.Control);
- _lineList.Remove(line);
- }
- /// <summary>
- /// 设置被选中的节点
- /// </summary>
- /// <param name="nodes"></param>
- public void SetSelectedNodes(FlowNode[] nodes)
- {
- //保存老的选中节点
- FlowNode[] oldSelectedNodes = _selectNodes;
- //将新的被选中节点设置为未被选中状态
- _selectNodes = nodes;
- if (_selectNodes != null)
- {
- foreach (FlowNode node in nodes)
- {
- if (!node.Control.BeSelected)
- {
- node.Control.BeSelected = true;
- Trace.WriteLine(node.Name + "被设置为选中");
- }
- }
- }
- //将原来的被选中节点设置为未被选中状态
- if (oldSelectedNodes != null)
- {
- if (_selectNodes == null)
- {
- foreach (FlowNode node in oldSelectedNodes)
- {
- node.Control.BeSelected = false;
- Trace.WriteLine(node.Name + "被设置为未选中");
- }
- }
- else
- {
- foreach (FlowNode node in oldSelectedNodes)
- {
- //为了防止闪烁,只将在老的选中节点中而不在新的选中节点中的节点设置成不被选中
- if (!(_selectNodes != null && _selectNodes.Contains(node)))
- {
- node.Control.BeSelected = false;
- Trace.WriteLine(node.Name + "被设置为未选中");
- }
- }
- }
- }
- }
- /// <summary>
- /// 设置被选中的线
- /// </summary>
- /// <param name="lines"></param>
- public void SetSelectedLines(FlowLine[] lines)
- {
- //保存老的选中线
- FlowLine[] oldSelectedLines = _selectLines;
- //将新的被选中线设置为未被选中状态
- _selectLines = lines;
- if (_selectLines != null)
- {
- foreach (FlowLine line in lines)
- {
- if (!line.Control.BeSelected)
- {
- line.Control.BeSelected = true;
- Trace.WriteLine(line.Name + "被设置为选中");
- }
- }
- }
- //将原来的被选中线设置为未被选中状态
- if (oldSelectedLines != null)
- {
- //如果新选中的线为空,则将全部的老选中线设置为未被选中
- if (_selectLines == null)
- {
- foreach (FlowLine line in oldSelectedLines)
- {
- line.Control.BeSelected = false;
- Trace.WriteLine(line.Name + "被设置为未选中");
- }
- }
- else
- {//否则设置原来选中的线设置为未被选中状态
- foreach (FlowLine line in oldSelectedLines)
- {
- //为了防止闪烁,只将在老的选中线中而不在新的选中线中的线设置成不被选中
- if (!(_selectLines !=null && _selectLines.Contains(line)))
- {
- line.Control.BeSelected = false;
- Trace.WriteLine(line.Name + "被设置为未选中");
- }
- }
- }
- }
- }
- #endregion
- #region 控件响应方法
- private void nodeControl_MouseDown(object sender, MouseEventArgs e)
- {
- FlowNodeControl nodeControl = (FlowNodeControl)sender;
- //判断是否是单个节点移动
- if (_selectNodes != null && _selectNodes.Length >= 2 && _selectNodes.Contains(nodeControl.FlowNode))
- _bMultiNodeMove = true;
- if (!_bMultiNodeMove)
- {//单节点移动
- nodeControl.BeMoveing = true;
- nodeControl.MovePos = new Point(e.X, e.Y);
- SetSelectedNodes(new FlowNode[] { nodeControl.FlowNode });
- }
- else
- {//多节点移动
- foreach (FlowNode node in _selectNodes)
- {
- FlowNodeControl control = node.Control;
- control.BeMoveing = true;
- control.MovePos = new Point(e.X, e.Y);
- }
- }
- }
- private void nodeControl_MouseMove(object sender, MouseEventArgs e)
- {
- FlowNodeControl nodeControl = (FlowNodeControl)sender;
- if (nodeControl.BeMoveing)
- {
- //判断是否是单个节点移动
- if (!_bMultiNodeMove)
- {//单节点移动
- nodeControl.Left += Convert.ToInt16(e.X - nodeControl.MovePos.X);//设置x坐标.
- nodeControl.Top += Convert.ToInt16(e.Y - nodeControl.MovePos.Y);//设置y坐标.
- ResetNodeLines(nodeControl.FlowNode);
- }
- else
- {//多节点移动
- if (_selectNodes != null)
- {
- foreach (FlowNode node in _selectNodes)
- {
- FlowNodeControl control = node.Control;
- control.Left += Convert.ToInt16(e.X - control.MovePos.X);//设置x坐标.
- control.Top += Convert.ToInt16(e.Y - control.MovePos.Y);//设置y坐标.
- ResetNodeLines(control.FlowNode);
- }
- }
- }
- }
- }
- private void nodeControl_MouseUp(object sender, MouseEventArgs e)
- {
- FlowNodeControl nodeControl = (FlowNodeControl)sender;
- if (nodeControl.BeMoveing)
- {
- //判断是否是单个节点移动
- if (!_bMultiNodeMove)
- {//单节点移动
- nodeControl.BeMoveing = false;
- nodeControl.Left += Convert.ToInt16(e.X - nodeControl.MovePos.X);//设置x坐标.
- nodeControl.Top += Convert.ToInt16(e.Y - nodeControl.MovePos.Y);//设置y坐标.
- ResetNodeLines(nodeControl.FlowNode);
- }
- else
- {//多节点移动
- if (_selectNodes != null)
- {
- foreach (FlowNode node in _selectNodes)
- {
- FlowNodeControl control = node.Control;
- control.BeMoveing = false;
- control.Left += Convert.ToInt16(e.X - nodeControl.MovePos.X);//设置x坐标.
- control.Top += Convert.ToInt16(e.Y - nodeControl.MovePos.Y);//设置y坐标.
- ResetNodeLines(control.FlowNode);
- }
- }
- _bMultiNodeMove = false;
- }
- }
- }
- private void nodeControl_Click(object sender, EventArgs e)
- {
- if (!_bMultiNodeMove)
- {//如果不是多节点移动,则触发节点单击事件
- FlowNode node = ((FlowNodeControl)sender).FlowNode;
- SetSelectedNodes(new FlowNode[] { node });
- SetSelectedLines(null);
- if (Evt_ClickNode != null)
- Evt_ClickNode(node);
- }
- }
- private void nodeControl_DoubleClick(object sender, EventArgs e)
- {
- FlowNode node = ((FlowNodeControl)sender).FlowNode;
- SetSelectedNodes(new FlowNode[] { node });
- SetSelectedLines(null);
- SetSelectedNodes(new FlowNode[] { node });
- if (Evt_DoubleClickNode != null)
- Evt_DoubleClickNode(node);
- }
- private void nodeControl_KeyDown(object sender, KeyEventArgs e)
- {
- PressKeyDown(e.KeyValue);
- }
- private void lineControl_Click(object sender, EventArgs e)
- {
- FlowLine line = ((FlowBrokenLineControl)sender).FlowLine;
- SetSelectedNodes(null);
- SetSelectedLines(new FlowLine[] { line });
- if (Evt_ClickLine != null)
- Evt_ClickLine(line);
- }
- private void lineControl_DoubleClick(object sender, EventArgs e)
- {
- Trace.WriteLine("-----------lineControl_DoubleClick");
- FlowLine line = ((FlowBrokenLineControl)sender).FlowLine;
- SetSelectedNodes(null);
- SetSelectedLines(new FlowLine[] { line });
- if (Evt_DoubleClickLine != null)
- Evt_DoubleClickLine(line);
- }
- private void lineControl_KeyDown(object sender, KeyEventArgs e)
- {
- PressKeyDown(e.KeyValue);
- }
- private void FlowEditorCanvas_MouseDown(object sender, MouseEventArgs e)
- {
- Trace.WriteLine("---------Canvas_MouseDown");
- //if (e.Button == System.Windows.Forms.MouseButtons.Left)
- //{
- // _bBegainSelect = true;
- // _selectBegainPos = e.Location;
- // _selectControl.Location = e.Location;
- // this.Controls.Add(_selectControl);
- //}
- }
- private void FlowEditorCanvas_MouseMove(object sender, MouseEventArgs e)
- {
- if (e.Button == System.Windows.Forms.MouseButtons.Left && !_bBegainSelect)
- {
- _bBegainSelect = true;
- _selectBegainPos = e.Location;
- _selectControl.Location = e.Location;
- this.Controls.Add(_selectControl);
- }
- if (_bBegainSelect)
- {
- Trace.WriteLine("---------Canvas_MouseMove");
- if (!_selectControl.Visible)
- _selectControl.Visible = true;
- //计算当前点相对于起始点的位置,假设起始点在坐标原点
- //俄制象限
- int offsetX = e.X - _selectBegainPos.X;
- int offsetY = e.Y - _selectBegainPos.Y;
- _selectControl.Height = Math.Abs(offsetY);
- _selectControl.Width = Math.Abs(offsetX);
- if(offsetX >= 0)
- {
- if(offsetY >= 0)
- {//第四象限
- }
- else
- {//第一象限
- _selectControl.Location = new Point(_selectBegainPos.X, e.Y);
- }
- }
- else
- {
- if(offsetY >= 0)
- {//第三象限
- _selectControl.Location = new Point(e.X, _selectBegainPos.Y);
- }
- else
- {//第二象限
- _selectControl.Location = new Point(e.X, e.Y);
- }
- }
- SetSelectedNodes(GetSelectedNode());
- SetSelectedLines(null);
- }
- }
- private void FlowEditorCanvas_MouseUp(object sender, MouseEventArgs e)
- {
- Trace.WriteLine("---------Canvas_MouseUp");
- if (e.Button == System.Windows.Forms.MouseButtons.Left)
- {
- if (_selectControl.Visible)
- _selectControl.Visible = false;
- _bBegainSelect = false;
- this.Controls.Remove(_selectControl);
- }
- }
- private void FlowEditorCanvas_Click(object sender, EventArgs e)
- {
- if (!_bBegainSelect)
- {
- Trace.WriteLine("---------Canvas_Click");
- SetSelectedNodes(null);
- SetSelectedLines(null);
- }
- }
- private void FlowEditorCanvas_DoubleClick(object sender, EventArgs e)
- {
- SetSelectedNodes(null);
- SetSelectedLines(null);
- }
- private void FlowEditorCanvas_KeyDown(object sender, KeyEventArgs e)
- {
- PressKeyDown(e.KeyValue);
- }
- #endregion
- #region 私有方法
- /// <summary>
- /// 获取两个节点之间最短路径的折线的点
- /// </summary>
- /// <param name="node1">第一个节点</param>
- /// <param name="node2">第二个节点</param>
- /// <returns></returns>
- static List<Point> GetTwoNodeShortestPathLinePointList(FlowNode node1, FlowNode node2)
- {
- //获取两个节点的连接点
- List<Point> points = new List<Point>();
-
- int X = node2.CenterPoint.X - node1.CenterPoint.X;
- int Y = node2.CenterPoint.Y - node1.CenterPoint.Y;
- if (X >= 0)
- {
- if (Math.Abs(Y) <= Math.Abs(X))
- {
- Point point_1 = new Point(node1.RightPoint.X, node1.RightPoint.Y);
- points.Add(point_1);
- if (Y != 0)
- {
- Point point_2 = new Point(node1.RightPoint.X + _nodeMargin, point_1.Y);
- points.Add(point_2);
- Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
- points.Add(point_3);
- }
- Point point_4 = new Point(node2.LeftPoint.X, node2.LeftPoint.Y);
- points.Add(point_4);
- return points;
- }
- }
- if (X < 0)
- {
- if (Math.Abs(Y) <= Math.Abs(X))
- {
- Point point_1 = new Point(node1.LeftPoint.X, node1.LeftPoint.Y);
- points.Add(point_1);
- if (Y != 0)
- {
- Point point_2 = new Point(node1.LeftPoint.X - _nodeMargin, node1.LeftPoint.Y);
- points.Add(point_2);
- Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
- points.Add(point_3);
- }
- Point point_4 = new Point(node2.RightPoint.X, node2.RightPoint.Y);
- points.Add(point_4);
- return points;
- }
- }
- if (Y >= 0)
- {
- if (Math.Abs(Y) >= Math.Abs(X))
- {
- Point point_1 = new Point(node1.BottomPoint.X, node1.BottomPoint.Y);
- points.Add(point_1);
- if (X != 0)
- {
- Point point_2 = new Point(node1.BottomPoint.X, point_1.Y + _nodeMargin);
- points.Add(point_2);
- Point point_3 = new Point(node2.TopPoint.X, point_2.Y);
- points.Add(point_3);
- }
- Point point_4 = new Point(node2.TopPoint.X, node2.TopPoint.Y);
- points.Add(point_4);
- return points;
- }
- }
- if (Y < 0)
- {
- if (Math.Abs(Y) >= Math.Abs(X))
- {
- Point point_1 = new Point(node1.TopPoint.X, node1.TopPoint.Y);
- points.Add(point_1);
- if (X != 0)
- {
- Point point_2 = new Point(node1.TopPoint.X, node1.TopPoint.Y - _nodeMargin);
- points.Add(point_2);
- Point point_3 = new Point(node2.BottomPoint.X, point_2.Y);
- points.Add(point_3);
- }
- Point point_4 = new Point(node2.BottomPoint.X, node2.BottomPoint.Y);
- points.Add(point_4);
- return points;
- }
- }
- return points;
- }
- //2017-5-16画直线
- static List<Point> GetTwoNodeShortestPathLinePointListNEW(FlowNode node1, FlowNode node2)
- {
- //获取两个节点的连接点
- List<Point> points = new List<Point>();
- int X = node2.CenterPoint.X - node1.CenterPoint.X;
- int Y = node2.CenterPoint.Y - node1.CenterPoint.Y;
- if (X >= 0)
- {
- if (Math.Abs(Y) <= Math.Abs(X))
- {
- Point point_1 = new Point(node1.RightPoint.X, node1.RightPoint.Y);
- points.Add(point_1);
- Point point_2 = new Point(node2.LeftPoint.X , node2.LeftPoint.Y);
- points.Add(point_2);
- /*if (Y != 0)
- {
- Point point_2 = new Point(node1.RightPoint.X + _nodeMargin, point_1.Y);
- points.Add(point_2);
- Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
- points.Add(point_3);
- }*/
- //Point point_4 = new Point(node2.LeftPoint.X, node2.LeftPoint.Y);
- //points.Add(point_4);
- return points;
- }
- else if (Math.Abs(Y) > Math.Abs(X))
- {
- Point point_1 = new Point(node1.BottomPoint.X, node1.BottomPoint.Y);
- points.Add(point_1);
- Point point_2 = new Point(node2.TopPoint.X + node2.TopPoint .Y);
- points.Add(point_2);
-
- return points;
- }
- }
- if (X < 0)
- {
- if (Math.Abs(Y) <= Math.Abs(X))
- {
- Point point_1 = new Point(node1.LeftPoint.X, node1.LeftPoint.Y);
- points.Add(point_1);
- if (Y != 0)
- {
- Point point_2 = new Point(node1.LeftPoint.X - _nodeMargin, node1.LeftPoint.Y);
- points.Add(point_2);
- Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
- points.Add(point_3);
- }
- Point point_4 = new Point(node2.RightPoint.X, node2.RightPoint.Y);
- points.Add(point_4);
- return points;
- }
- }
- if (Y >= 0)
- {
- if (Math.Abs(Y) >= Math.Abs(X))
- {
- Point point_1 = new Point(node1.BottomPoint.X, node1.BottomPoint.Y);
- points.Add(point_1);
- if (X != 0)
- {
- Point point_2 = new Point(node1.BottomPoint.X, point_1.Y + _nodeMargin);
- points.Add(point_2);
- Point point_3 = new Point(node2.TopPoint.X, point_2.Y);
- points.Add(point_3);
- }
- Point point_4 = new Point(node2.TopPoint.X, node2.TopPoint.Y);
- points.Add(point_4);
- return points;
- }
- }
- if (Y < 0)
- {
- if (Math.Abs(Y) >= Math.Abs(X))
- {
- Point point_1 = new Point(node1.TopPoint.X, node1.TopPoint.Y);
- points.Add(point_1);
- if (X != 0)
- {
- Point point_2 = new Point(node1.TopPoint.X, node1.TopPoint.Y - _nodeMargin);
- points.Add(point_2);
- Point point_3 = new Point(node2.BottomPoint.X, point_2.Y);
- points.Add(point_3);
- }
- Point point_4 = new Point(node2.BottomPoint.X, node2.BottomPoint.Y);
- points.Add(point_4);
- return points;
- }
- }
- return points;
- }
- /// <summary>
- /// 获取基于滚动条的连线的点列表
- /// </summary>
- /// <param name="node1"></param>
- /// <param name="node2"></param>
- /// <returns></returns>
- List<Point> GetLinePointListWidthScroll(FlowNode node1, FlowNode node2)
- {
- List<Point> points = FlowEditorCanvas.GetTwoNodeShortestPathLinePointList(node1, node2);
- if (points != null)
- {
- for (int i = 0; i < points.Count; i++)
- {
- points[i] = new Point(points[i].X - this.AutoScrollPosition.X, points[i].Y - this.AutoScrollPosition.Y);
- }
- }
- return points;
- }
- /// <summary>
- /// 重新布局指定线的连接点
- /// </summary>
- /// <param name="line"></param>
- void ResetLine(FlowLine line)
- {
- Trace.WriteLine("ResetLine Begain----------------------");
- List<Point> points = GetLinePointListWidthScroll(line.HeadNode, line.TailNode);
- line.PointList = points;
- Trace.WriteLine("HeadNode=" + line.HeadNode.CenterPoint.ToString());
- Trace.WriteLine("HeadNodeTopPoint=" + line.HeadNode.TopPoint.ToString());
- Trace.WriteLine("TailNode=" + line.TailNode.CenterPoint.ToString());
- Trace.WriteLine("TailNodeBottomPoint=" + line.HeadNode.BottomPoint.ToString());
- foreach (Point point in points)
- Trace.WriteLine(point);
- Trace.WriteLine("ResetLine End--------------------");
- }
- /// <summary>
- /// 重新布局和指定节点相连的线
- /// </summary>
- /// <param name="node"></param>
- void ResetNodeLines(FlowNode node)
- {
- List<NodePort> inputPortList = node.InputPortList;
- if (inputPortList != null && inputPortList.Count > 0)
- {
- foreach (NodePort port in inputPortList)
- {
- ResetLine(port.Line);
- }
- }
- List<NodePort> outputPortList = node.OutputPortList;
- if (outputPortList != null && outputPortList.Count > 0)
- {
- foreach (NodePort port in outputPortList)
- {
- ResetLine(port.Line);
- }
- }
- }
- /// <summary>
- /// 将节点控件添加到画板控件上
- /// </summary>
- /// <param name="nodeControl"></param>
- void AddNodeControlToCanvasControl(FlowNodeControl nodeControl)
- {
- this.Controls.Add(nodeControl);
- nodeControl.MouseDown += new MouseEventHandler(nodeControl_MouseDown);
- nodeControl.MouseUp += new MouseEventHandler(nodeControl_MouseUp);
- nodeControl.MouseMove += new MouseEventHandler(nodeControl_MouseMove);
- nodeControl.Click += new EventHandler(nodeControl_Click);
- nodeControl.DoubleClick += new EventHandler(nodeControl_DoubleClick);
- nodeControl.KeyDown += new KeyEventHandler(nodeControl_KeyDown);
- }
- /// <summary>
- /// 将节点控件从画板控件上移除
- /// </summary>
- /// <param name="nodeControl"></param>
- void RemoveNodeControlFromCanvasControl(FlowNodeControl nodeControl)
- {
- this.Controls.Remove(nodeControl);
- nodeControl.MouseDown -= new MouseEventHandler(nodeControl_MouseDown);
- nodeControl.MouseUp -= new MouseEventHandler(nodeControl_MouseUp);
- nodeControl.MouseMove -= new MouseEventHandler(nodeControl_MouseMove);
- nodeControl.Click -= new EventHandler(nodeControl_Click);
- nodeControl.DoubleClick -= new EventHandler(nodeControl_DoubleClick);
- nodeControl.KeyDown -= new KeyEventHandler(nodeControl_KeyDown);
- }
- /// <summary>
- /// 将线控件添加到画布控件中
- /// </summary>
- /// <param name="lineControl"></param>
- void AddLineControlToCanvasControl(FlowBrokenLineControl lineControl)
- {
- lineControl.Size = new Size(1000, 1000);
- this.Controls.Add(lineControl);
- lineControl.Click += new EventHandler(lineControl_Click);
- lineControl.DoubleClick += new EventHandler(lineControl_DoubleClick);
- lineControl.KeyDown += new KeyEventHandler(lineControl_KeyDown);
- }
-
- /// <summary>
- /// 将线控件从画布控件中移除
- /// </summary>
- /// <param name="lineControl"></param>
- void RemoveLineControlFromCanvasControl(FlowBrokenLineControl lineControl)
- {
- lineControl.Size = new Size(1000, 1000);
- this.Controls.Remove(lineControl);
- lineControl.Click -= new EventHandler(lineControl_Click);
- lineControl.DoubleClick -= new EventHandler(lineControl_DoubleClick);
- lineControl.KeyDown -= new KeyEventHandler(lineControl_KeyDown);
- }
- /// <summary>
- /// 获取选中的节点
- /// </summary>
- /// <returns></returns>
- FlowNode[] GetSelectedNode()
- {
- if (!_bBegainSelect)
- return null;
- List<FlowNode> selectedNodeList = new List<FlowNode>();
- Rectangle selectRect = new Rectangle(_selectControl.Location, _selectControl.Size);
- foreach (FlowNode node in _nodeList)
- {
- Rectangle nodeRect = new Rectangle(node.Control.Location, node.Control.Size);
- if (selectRect.IntersectsWith(nodeRect))
- selectedNodeList.Add(node);
- }
- return selectedNodeList.ToArray();
- }
- /// <summary>
- /// 处理键盘按下事件
- /// </summary>
- /// <param name="keyVal"></param>
- void PressKeyDown(int keyVal)
- {
- if (keyVal == KeyVal_Del)
- {
- if(Evt_KeyDown_Del != null)
- Evt_KeyDown_Del();
- }
- else if (keyVal == KeyVal_Enter)
- {
- if (Evt_KeyDown_Enter != null)
- Evt_KeyDown_Enter();
- }
- }
-
- #endregion
- }
- #region 画布委托定义
- /// <summary>
- /// 鼠标单击节点
- /// </summary>
- /// <param name="node"></param>
- public delegate void Delegate_ClickNode(FlowNode node);
- /// <summary>
- /// 鼠标双击节点
- /// </summary>
- /// <param name="node"></param>
- public delegate void Delegate_DoubleClickNode(FlowNode node);
- /// <summary>
- /// 鼠标单击线
- /// </summary>
- /// <param name="line"></param>
- public delegate void Delegate_ClickLine(FlowLine line);
- /// <summary>
- /// 鼠标双击线
- /// </summary>
- /// <param name="line"></param>
- public delegate void Delegate_DoubleClickLine(FlowLine line);
- /// <summary>
- /// 删除键被按下
- /// </summary>
- public delegate void Delegate_KeyDown_Del();
- /// <summary>
- /// 回车键被按下
- /// </summary>
- public delegate void Delegate_KeyDown_Enter();
- #endregion
- }
|