ivr流程编辑器

FlowEditorCanvas.cs 35KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System.Collections;
  7. using System.Linq;
  8. namespace HySoft.FlowEditor
  9. {
  10. public partial class FlowEditorCanvas : UserControl
  11. {
  12. #region 字段
  13. const int KeyVal_Del = 46;
  14. const int KeyVal_Enter = 13;
  15. /// <summary>
  16. /// 节点连接线与矩形边距的宽度
  17. /// </summary>
  18. static int _nodeMargin = 10;
  19. List<FlowNode> _nodeList;
  20. List<FlowLine> _lineList;
  21. FlowNode[] _selectNodes;
  22. FlowLine[] _selectLines;
  23. /// <summary>
  24. /// 选中操作的开始鼠标点
  25. /// </summary>
  26. Point _selectBegainPos;
  27. /// <summary>
  28. /// 是否开始选中操作
  29. /// </summary>
  30. bool _bBegainSelect;
  31. /// <summary>
  32. /// 用于显示选中框的容器
  33. /// </summary>
  34. Panel _selectControl;
  35. /// <summary>
  36. /// 是否是多个节点移动
  37. /// </summary>
  38. bool _bMultiNodeMove = false;
  39. #endregion
  40. #region 属性
  41. /// <summary>
  42. /// 节点列表
  43. /// </summary>
  44. public List<FlowNode> NodeList
  45. {
  46. get { return _nodeList; }
  47. }
  48. /// <summary>
  49. /// 线列表
  50. /// </summary>
  51. public List<FlowLine> LineList
  52. {
  53. get { return _lineList; }
  54. }
  55. /// <summary>
  56. /// 被选中节点组
  57. /// </summary>
  58. public FlowNode[] SelectedNodes
  59. {
  60. get { return _selectNodes; }
  61. set
  62. {
  63. SetSelectedNodes(value);
  64. }
  65. }
  66. /// <summary>
  67. /// 被选中连线组
  68. /// </summary>
  69. public FlowLine[] SelectedLines
  70. {
  71. get { return _selectLines; }
  72. set { _selectLines = value; }
  73. }
  74. #endregion
  75. #region 事件
  76. /// <summary>
  77. /// 节点单击事件
  78. /// </summary>
  79. public Delegate_ClickNode Evt_ClickNode;
  80. /// <summary>
  81. /// 节点双击事件
  82. /// </summary>
  83. public Delegate_DoubleClickNode Evt_DoubleClickNode;
  84. /// <summary>
  85. /// 线单击事件
  86. /// </summary>
  87. public Delegate_ClickLine Evt_ClickLine;
  88. /// <summary>
  89. /// 线双击事件
  90. /// </summary>
  91. public Delegate_DoubleClickLine Evt_DoubleClickLine;
  92. /// <summary>
  93. /// 回车键按下事件
  94. /// </summary>
  95. public Delegate_KeyDown_Enter Evt_KeyDown_Enter;
  96. /// <summary>
  97. /// 删除键按下事件
  98. /// </summary>
  99. public Delegate_KeyDown_Del Evt_KeyDown_Del;
  100. #endregion
  101. #region 公开方法
  102. /// <summary>
  103. /// 构造方法
  104. /// </summary>
  105. public FlowEditorCanvas()
  106. {
  107. _nodeList = new List<FlowNode>();
  108. _lineList = new List<FlowLine>();
  109. InitializeComponent();
  110. _bBegainSelect = false;
  111. _selectControl = new Panel();
  112. _selectControl.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  113. _selectControl.BackColor = Color.Transparent;
  114. _selectControl.Visible = false;
  115. }
  116. /// <summary>
  117. /// 添加一个流程节点
  118. /// </summary>
  119. /// <param name="node"></param>
  120. public void AddANode(FlowNode node)
  121. {
  122. _nodeList.Add(node);
  123. AddNodeControlToCanvasControl(node.Control);
  124. SetSelectedNodes(new FlowNode[] { node });
  125. }
  126. /// <summary>
  127. /// 添加一个流程节点
  128. /// </summary>
  129. /// <param name="node"></param>
  130. /// <param name="coord"></param>
  131. public void AddANode(FlowNode node, Point coord)
  132. {
  133. _nodeList.Add(node);
  134. AddNodeControlToCanvasControl(node.Control);
  135. SetSelectedNodes(new FlowNode[] { node });
  136. }
  137. /// <summary>
  138. /// 添加一条线
  139. /// </summary>
  140. /// <param name="line">要添加的线</param>
  141. /// <param name="headNode">头节点</param>
  142. /// <param name="tailNode">尾节点</param>
  143. public void AddALine(FlowLine line, FlowNode headNode, FlowNode tailNode)
  144. {
  145. AddALine(line, headNode, null, tailNode, null);
  146. }
  147. /// <summary>
  148. /// 添加一条线
  149. /// </summary>
  150. /// <param name="line">要添加的线</param>
  151. /// <param name="headNode">头节点</param>
  152. /// <param name="headNodeOutputPort">头结点的出口,如果为null,方法会自动创建一个出口</param>
  153. /// <param name="tailNode">尾节点</param>
  154. /// <param name="tailNodeInputPort">尾结点的入口,如果为null,方法会自动创建一个入口</param>
  155. public void AddALine(FlowLine line, FlowNode headNode, NodePort headNodeOutputPort, FlowNode tailNode, NodePort tailNodeInputPort)
  156. {
  157. List<Point> points = GetLinePointListWidthScroll(headNode, tailNode);
  158. line.PointList = points;
  159. NodePort outputPort = headNodeOutputPort;
  160. if (outputPort == null)
  161. {
  162. outputPort = new NodePort();
  163. tailNode.AddOutputPort(outputPort);
  164. }
  165. tailNode.AddLineToOutPort(line, outputPort);
  166. NodePort inputPort = tailNodeInputPort;
  167. if (inputPort == null)
  168. {
  169. inputPort = new NodePort();
  170. headNode.AddInputPort(inputPort);
  171. }
  172. headNode.AddLineToInputPort(line, inputPort);
  173. _lineList.Add(line);
  174. AddLineControlToCanvasControl(line.Control);
  175. }
  176. /// <summary>
  177. /// 删除一个流程节点
  178. /// 同时会删除与该流程节点连接的线及其线对应的两端节点的端口
  179. /// </summary>
  180. /// <param name="node"></param>
  181. public void DelNode(FlowNode node)
  182. {
  183. if (_nodeList.IndexOf(node) < 0)
  184. return;
  185. SetSelectedNodes(null);
  186. NodePort[] inputPorts = node.InputPortList.ToArray();
  187. if (inputPorts != null && inputPorts.Length > 0)
  188. {
  189. foreach (NodePort nodePort in inputPorts)
  190. DelLine(nodePort.Line, true, true);
  191. }
  192. NodePort[] outputPorts = node.OutputPortList.ToArray();
  193. if (outputPorts != null && outputPorts.Length > 0)
  194. {
  195. foreach (NodePort nodePort in outputPorts)
  196. DelLine(nodePort.Line, true, true);
  197. }
  198. RemoveNodeControlFromCanvasControl(node.Control);
  199. _nodeList.Remove(node);
  200. }
  201. /// <summary>
  202. /// 删除一条线
  203. /// </summary>
  204. /// <param name="line">指定的线</param>
  205. /// <param name="bRemoveOutputPort">是否同时移除线对应的出口</param>
  206. /// <param name="bRemoveInputPort">是否同时移除线对应的入口</param>
  207. /// <returns>移除成功</returns>
  208. public void DelLine(FlowLine line, bool bRemoveHeadNodeOutputPort, bool bRemoveTailNodeInputPort)
  209. {
  210. if (line == null)
  211. return;
  212. if (line.TailNode != null && line.TailNodePort != null)
  213. {
  214. NodePort inputPort = line.TailNode.FindInputPort(line);
  215. if (inputPort != null)
  216. {
  217. line.TailNode.RemoveLineFromInputPort(line, inputPort);
  218. if (bRemoveTailNodeInputPort)
  219. line.TailNode.RemoveInputPort(inputPort);
  220. }
  221. }
  222. if (line.HeadNode != null && line.HeadNodePort != null)
  223. {
  224. NodePort outputPort = line.HeadNode.FindOutputPort(line);
  225. if (outputPort != null)
  226. {
  227. line.HeadNode.RemoveLineFromOutputPort(line, outputPort);
  228. if (bRemoveTailNodeInputPort)
  229. line.HeadNode.RemoveOutputPort(outputPort);
  230. }
  231. }
  232. RemoveLineControlFromCanvasControl(line.Control);
  233. _lineList.Remove(line);
  234. }
  235. /// <summary>
  236. /// 设置被选中的节点
  237. /// </summary>
  238. /// <param name="nodes"></param>
  239. public void SetSelectedNodes(FlowNode[] nodes)
  240. {
  241. //保存老的选中节点
  242. FlowNode[] oldSelectedNodes = _selectNodes;
  243. //将新的被选中节点设置为未被选中状态
  244. _selectNodes = nodes;
  245. if (_selectNodes != null)
  246. {
  247. foreach (FlowNode node in nodes)
  248. {
  249. if (!node.Control.BeSelected)
  250. {
  251. node.Control.BeSelected = true;
  252. Trace.WriteLine(node.Name + "被设置为选中");
  253. }
  254. }
  255. }
  256. //将原来的被选中节点设置为未被选中状态
  257. if (oldSelectedNodes != null)
  258. {
  259. if (_selectNodes == null)
  260. {
  261. foreach (FlowNode node in oldSelectedNodes)
  262. {
  263. node.Control.BeSelected = false;
  264. Trace.WriteLine(node.Name + "被设置为未选中");
  265. }
  266. }
  267. else
  268. {
  269. foreach (FlowNode node in oldSelectedNodes)
  270. {
  271. //为了防止闪烁,只将在老的选中节点中而不在新的选中节点中的节点设置成不被选中
  272. if (!(_selectNodes != null && _selectNodes.Contains(node)))
  273. {
  274. node.Control.BeSelected = false;
  275. Trace.WriteLine(node.Name + "被设置为未选中");
  276. }
  277. }
  278. }
  279. }
  280. }
  281. /// <summary>
  282. /// 设置被选中的线
  283. /// </summary>
  284. /// <param name="lines"></param>
  285. public void SetSelectedLines(FlowLine[] lines)
  286. {
  287. //保存老的选中线
  288. FlowLine[] oldSelectedLines = _selectLines;
  289. //将新的被选中线设置为未被选中状态
  290. _selectLines = lines;
  291. if (_selectLines != null)
  292. {
  293. foreach (FlowLine line in lines)
  294. {
  295. if (!line.Control.BeSelected)
  296. {
  297. line.Control.BeSelected = true;
  298. Trace.WriteLine(line.Name + "被设置为选中");
  299. }
  300. }
  301. }
  302. //将原来的被选中线设置为未被选中状态
  303. if (oldSelectedLines != null)
  304. {
  305. //如果新选中的线为空,则将全部的老选中线设置为未被选中
  306. if (_selectLines == null)
  307. {
  308. foreach (FlowLine line in oldSelectedLines)
  309. {
  310. line.Control.BeSelected = false;
  311. Trace.WriteLine(line.Name + "被设置为未选中");
  312. }
  313. }
  314. else
  315. {//否则设置原来选中的线设置为未被选中状态
  316. foreach (FlowLine line in oldSelectedLines)
  317. {
  318. //为了防止闪烁,只将在老的选中线中而不在新的选中线中的线设置成不被选中
  319. if (!(_selectLines !=null && _selectLines.Contains(line)))
  320. {
  321. line.Control.BeSelected = false;
  322. Trace.WriteLine(line.Name + "被设置为未选中");
  323. }
  324. }
  325. }
  326. }
  327. }
  328. #endregion
  329. #region 控件响应方法
  330. private void nodeControl_MouseDown(object sender, MouseEventArgs e)
  331. {
  332. FlowNodeControl nodeControl = (FlowNodeControl)sender;
  333. //判断是否是单个节点移动
  334. if (_selectNodes != null && _selectNodes.Length >= 2 && _selectNodes.Contains(nodeControl.FlowNode))
  335. _bMultiNodeMove = true;
  336. if (!_bMultiNodeMove)
  337. {//单节点移动
  338. nodeControl.BeMoveing = true;
  339. nodeControl.MovePos = new Point(e.X, e.Y);
  340. SetSelectedNodes(new FlowNode[] { nodeControl.FlowNode });
  341. }
  342. else
  343. {//多节点移动
  344. foreach (FlowNode node in _selectNodes)
  345. {
  346. FlowNodeControl control = node.Control;
  347. control.BeMoveing = true;
  348. control.MovePos = new Point(e.X, e.Y);
  349. }
  350. }
  351. }
  352. private void nodeControl_MouseMove(object sender, MouseEventArgs e)
  353. {
  354. FlowNodeControl nodeControl = (FlowNodeControl)sender;
  355. if (nodeControl.BeMoveing)
  356. {
  357. //判断是否是单个节点移动
  358. if (!_bMultiNodeMove)
  359. {//单节点移动
  360. nodeControl.Left += Convert.ToInt16(e.X - nodeControl.MovePos.X);//设置x坐标.
  361. nodeControl.Top += Convert.ToInt16(e.Y - nodeControl.MovePos.Y);//设置y坐标.
  362. ResetNodeLines(nodeControl.FlowNode);
  363. }
  364. else
  365. {//多节点移动
  366. if (_selectNodes != null)
  367. {
  368. foreach (FlowNode node in _selectNodes)
  369. {
  370. FlowNodeControl control = node.Control;
  371. control.Left += Convert.ToInt16(e.X - control.MovePos.X);//设置x坐标.
  372. control.Top += Convert.ToInt16(e.Y - control.MovePos.Y);//设置y坐标.
  373. ResetNodeLines(control.FlowNode);
  374. }
  375. }
  376. }
  377. }
  378. }
  379. private void nodeControl_MouseUp(object sender, MouseEventArgs e)
  380. {
  381. FlowNodeControl nodeControl = (FlowNodeControl)sender;
  382. if (nodeControl.BeMoveing)
  383. {
  384. //判断是否是单个节点移动
  385. if (!_bMultiNodeMove)
  386. {//单节点移动
  387. nodeControl.BeMoveing = false;
  388. nodeControl.Left += Convert.ToInt16(e.X - nodeControl.MovePos.X);//设置x坐标.
  389. nodeControl.Top += Convert.ToInt16(e.Y - nodeControl.MovePos.Y);//设置y坐标.
  390. ResetNodeLines(nodeControl.FlowNode);
  391. }
  392. else
  393. {//多节点移动
  394. if (_selectNodes != null)
  395. {
  396. foreach (FlowNode node in _selectNodes)
  397. {
  398. FlowNodeControl control = node.Control;
  399. control.BeMoveing = false;
  400. control.Left += Convert.ToInt16(e.X - nodeControl.MovePos.X);//设置x坐标.
  401. control.Top += Convert.ToInt16(e.Y - nodeControl.MovePos.Y);//设置y坐标.
  402. ResetNodeLines(control.FlowNode);
  403. }
  404. }
  405. _bMultiNodeMove = false;
  406. }
  407. }
  408. }
  409. private void nodeControl_Click(object sender, EventArgs e)
  410. {
  411. if (!_bMultiNodeMove)
  412. {//如果不是多节点移动,则触发节点单击事件
  413. FlowNode node = ((FlowNodeControl)sender).FlowNode;
  414. SetSelectedNodes(new FlowNode[] { node });
  415. SetSelectedLines(null);
  416. if (Evt_ClickNode != null)
  417. Evt_ClickNode(node);
  418. }
  419. }
  420. private void nodeControl_DoubleClick(object sender, EventArgs e)
  421. {
  422. FlowNode node = ((FlowNodeControl)sender).FlowNode;
  423. SetSelectedNodes(new FlowNode[] { node });
  424. SetSelectedLines(null);
  425. SetSelectedNodes(new FlowNode[] { node });
  426. if (Evt_DoubleClickNode != null)
  427. Evt_DoubleClickNode(node);
  428. }
  429. private void nodeControl_KeyDown(object sender, KeyEventArgs e)
  430. {
  431. PressKeyDown(e.KeyValue);
  432. }
  433. private void lineControl_Click(object sender, EventArgs e)
  434. {
  435. FlowLine line = ((FlowBrokenLineControl)sender).FlowLine;
  436. SetSelectedNodes(null);
  437. SetSelectedLines(new FlowLine[] { line });
  438. if (Evt_ClickLine != null)
  439. Evt_ClickLine(line);
  440. }
  441. private void lineControl_DoubleClick(object sender, EventArgs e)
  442. {
  443. Trace.WriteLine("-----------lineControl_DoubleClick");
  444. FlowLine line = ((FlowBrokenLineControl)sender).FlowLine;
  445. SetSelectedNodes(null);
  446. SetSelectedLines(new FlowLine[] { line });
  447. if (Evt_DoubleClickLine != null)
  448. Evt_DoubleClickLine(line);
  449. }
  450. private void lineControl_KeyDown(object sender, KeyEventArgs e)
  451. {
  452. PressKeyDown(e.KeyValue);
  453. }
  454. private void FlowEditorCanvas_MouseDown(object sender, MouseEventArgs e)
  455. {
  456. Trace.WriteLine("---------Canvas_MouseDown");
  457. //if (e.Button == System.Windows.Forms.MouseButtons.Left)
  458. //{
  459. // _bBegainSelect = true;
  460. // _selectBegainPos = e.Location;
  461. // _selectControl.Location = e.Location;
  462. // this.Controls.Add(_selectControl);
  463. //}
  464. }
  465. private void FlowEditorCanvas_MouseMove(object sender, MouseEventArgs e)
  466. {
  467. if (e.Button == System.Windows.Forms.MouseButtons.Left && !_bBegainSelect)
  468. {
  469. _bBegainSelect = true;
  470. _selectBegainPos = e.Location;
  471. _selectControl.Location = e.Location;
  472. this.Controls.Add(_selectControl);
  473. }
  474. if (_bBegainSelect)
  475. {
  476. Trace.WriteLine("---------Canvas_MouseMove");
  477. if (!_selectControl.Visible)
  478. _selectControl.Visible = true;
  479. //计算当前点相对于起始点的位置,假设起始点在坐标原点
  480. //俄制象限
  481. int offsetX = e.X - _selectBegainPos.X;
  482. int offsetY = e.Y - _selectBegainPos.Y;
  483. _selectControl.Height = Math.Abs(offsetY);
  484. _selectControl.Width = Math.Abs(offsetX);
  485. if(offsetX >= 0)
  486. {
  487. if(offsetY >= 0)
  488. {//第四象限
  489. }
  490. else
  491. {//第一象限
  492. _selectControl.Location = new Point(_selectBegainPos.X, e.Y);
  493. }
  494. }
  495. else
  496. {
  497. if(offsetY >= 0)
  498. {//第三象限
  499. _selectControl.Location = new Point(e.X, _selectBegainPos.Y);
  500. }
  501. else
  502. {//第二象限
  503. _selectControl.Location = new Point(e.X, e.Y);
  504. }
  505. }
  506. SetSelectedNodes(GetSelectedNode());
  507. SetSelectedLines(null);
  508. }
  509. }
  510. private void FlowEditorCanvas_MouseUp(object sender, MouseEventArgs e)
  511. {
  512. Trace.WriteLine("---------Canvas_MouseUp");
  513. if (e.Button == System.Windows.Forms.MouseButtons.Left)
  514. {
  515. if (_selectControl.Visible)
  516. _selectControl.Visible = false;
  517. _bBegainSelect = false;
  518. this.Controls.Remove(_selectControl);
  519. }
  520. }
  521. private void FlowEditorCanvas_Click(object sender, EventArgs e)
  522. {
  523. if (!_bBegainSelect)
  524. {
  525. Trace.WriteLine("---------Canvas_Click");
  526. SetSelectedNodes(null);
  527. SetSelectedLines(null);
  528. }
  529. }
  530. private void FlowEditorCanvas_DoubleClick(object sender, EventArgs e)
  531. {
  532. SetSelectedNodes(null);
  533. SetSelectedLines(null);
  534. }
  535. private void FlowEditorCanvas_KeyDown(object sender, KeyEventArgs e)
  536. {
  537. PressKeyDown(e.KeyValue);
  538. }
  539. #endregion
  540. #region 私有方法
  541. /// <summary>
  542. /// 获取两个节点之间最短路径的折线的点
  543. /// </summary>
  544. /// <param name="node1">第一个节点</param>
  545. /// <param name="node2">第二个节点</param>
  546. /// <returns></returns>
  547. static List<Point> GetTwoNodeShortestPathLinePointList(FlowNode node1, FlowNode node2)
  548. {
  549. //获取两个节点的连接点
  550. List<Point> points = new List<Point>();
  551. int X = node2.CenterPoint.X - node1.CenterPoint.X;
  552. int Y = node2.CenterPoint.Y - node1.CenterPoint.Y;
  553. if (X >= 0)
  554. {
  555. if (Math.Abs(Y) <= Math.Abs(X))
  556. {
  557. Point point_1 = new Point(node1.RightPoint.X, node1.RightPoint.Y);
  558. points.Add(point_1);
  559. if (Y != 0)
  560. {
  561. Point point_2 = new Point(node1.RightPoint.X + _nodeMargin, point_1.Y);
  562. points.Add(point_2);
  563. Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
  564. points.Add(point_3);
  565. }
  566. Point point_4 = new Point(node2.LeftPoint.X, node2.LeftPoint.Y);
  567. points.Add(point_4);
  568. return points;
  569. }
  570. }
  571. if (X < 0)
  572. {
  573. if (Math.Abs(Y) <= Math.Abs(X))
  574. {
  575. Point point_1 = new Point(node1.LeftPoint.X, node1.LeftPoint.Y);
  576. points.Add(point_1);
  577. if (Y != 0)
  578. {
  579. Point point_2 = new Point(node1.LeftPoint.X - _nodeMargin, node1.LeftPoint.Y);
  580. points.Add(point_2);
  581. Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
  582. points.Add(point_3);
  583. }
  584. Point point_4 = new Point(node2.RightPoint.X, node2.RightPoint.Y);
  585. points.Add(point_4);
  586. return points;
  587. }
  588. }
  589. if (Y >= 0)
  590. {
  591. if (Math.Abs(Y) >= Math.Abs(X))
  592. {
  593. Point point_1 = new Point(node1.BottomPoint.X, node1.BottomPoint.Y);
  594. points.Add(point_1);
  595. if (X != 0)
  596. {
  597. Point point_2 = new Point(node1.BottomPoint.X, point_1.Y + _nodeMargin);
  598. points.Add(point_2);
  599. Point point_3 = new Point(node2.TopPoint.X, point_2.Y);
  600. points.Add(point_3);
  601. }
  602. Point point_4 = new Point(node2.TopPoint.X, node2.TopPoint.Y);
  603. points.Add(point_4);
  604. return points;
  605. }
  606. }
  607. if (Y < 0)
  608. {
  609. if (Math.Abs(Y) >= Math.Abs(X))
  610. {
  611. Point point_1 = new Point(node1.TopPoint.X, node1.TopPoint.Y);
  612. points.Add(point_1);
  613. if (X != 0)
  614. {
  615. Point point_2 = new Point(node1.TopPoint.X, node1.TopPoint.Y - _nodeMargin);
  616. points.Add(point_2);
  617. Point point_3 = new Point(node2.BottomPoint.X, point_2.Y);
  618. points.Add(point_3);
  619. }
  620. Point point_4 = new Point(node2.BottomPoint.X, node2.BottomPoint.Y);
  621. points.Add(point_4);
  622. return points;
  623. }
  624. }
  625. return points;
  626. }
  627. //2017-5-16画直线
  628. static List<Point> GetTwoNodeShortestPathLinePointListNEW(FlowNode node1, FlowNode node2)
  629. {
  630. //获取两个节点的连接点
  631. List<Point> points = new List<Point>();
  632. int X = node2.CenterPoint.X - node1.CenterPoint.X;
  633. int Y = node2.CenterPoint.Y - node1.CenterPoint.Y;
  634. if (X >= 0)
  635. {
  636. if (Math.Abs(Y) <= Math.Abs(X))
  637. {
  638. Point point_1 = new Point(node1.RightPoint.X, node1.RightPoint.Y);
  639. points.Add(point_1);
  640. Point point_2 = new Point(node2.LeftPoint.X , node2.LeftPoint.Y);
  641. points.Add(point_2);
  642. /*if (Y != 0)
  643. {
  644. Point point_2 = new Point(node1.RightPoint.X + _nodeMargin, point_1.Y);
  645. points.Add(point_2);
  646. Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
  647. points.Add(point_3);
  648. }*/
  649. //Point point_4 = new Point(node2.LeftPoint.X, node2.LeftPoint.Y);
  650. //points.Add(point_4);
  651. return points;
  652. }
  653. else if (Math.Abs(Y) > Math.Abs(X))
  654. {
  655. Point point_1 = new Point(node1.BottomPoint.X, node1.BottomPoint.Y);
  656. points.Add(point_1);
  657. Point point_2 = new Point(node2.TopPoint.X + node2.TopPoint .Y);
  658. points.Add(point_2);
  659. return points;
  660. }
  661. }
  662. if (X < 0)
  663. {
  664. if (Math.Abs(Y) <= Math.Abs(X))
  665. {
  666. Point point_1 = new Point(node1.LeftPoint.X, node1.LeftPoint.Y);
  667. points.Add(point_1);
  668. if (Y != 0)
  669. {
  670. Point point_2 = new Point(node1.LeftPoint.X - _nodeMargin, node1.LeftPoint.Y);
  671. points.Add(point_2);
  672. Point point_3 = new Point(point_2.X, node2.LeftPoint.Y);
  673. points.Add(point_3);
  674. }
  675. Point point_4 = new Point(node2.RightPoint.X, node2.RightPoint.Y);
  676. points.Add(point_4);
  677. return points;
  678. }
  679. }
  680. if (Y >= 0)
  681. {
  682. if (Math.Abs(Y) >= Math.Abs(X))
  683. {
  684. Point point_1 = new Point(node1.BottomPoint.X, node1.BottomPoint.Y);
  685. points.Add(point_1);
  686. if (X != 0)
  687. {
  688. Point point_2 = new Point(node1.BottomPoint.X, point_1.Y + _nodeMargin);
  689. points.Add(point_2);
  690. Point point_3 = new Point(node2.TopPoint.X, point_2.Y);
  691. points.Add(point_3);
  692. }
  693. Point point_4 = new Point(node2.TopPoint.X, node2.TopPoint.Y);
  694. points.Add(point_4);
  695. return points;
  696. }
  697. }
  698. if (Y < 0)
  699. {
  700. if (Math.Abs(Y) >= Math.Abs(X))
  701. {
  702. Point point_1 = new Point(node1.TopPoint.X, node1.TopPoint.Y);
  703. points.Add(point_1);
  704. if (X != 0)
  705. {
  706. Point point_2 = new Point(node1.TopPoint.X, node1.TopPoint.Y - _nodeMargin);
  707. points.Add(point_2);
  708. Point point_3 = new Point(node2.BottomPoint.X, point_2.Y);
  709. points.Add(point_3);
  710. }
  711. Point point_4 = new Point(node2.BottomPoint.X, node2.BottomPoint.Y);
  712. points.Add(point_4);
  713. return points;
  714. }
  715. }
  716. return points;
  717. }
  718. /// <summary>
  719. /// 获取基于滚动条的连线的点列表
  720. /// </summary>
  721. /// <param name="node1"></param>
  722. /// <param name="node2"></param>
  723. /// <returns></returns>
  724. List<Point> GetLinePointListWidthScroll(FlowNode node1, FlowNode node2)
  725. {
  726. List<Point> points = FlowEditorCanvas.GetTwoNodeShortestPathLinePointList(node1, node2);
  727. if (points != null)
  728. {
  729. for (int i = 0; i < points.Count; i++)
  730. {
  731. points[i] = new Point(points[i].X - this.AutoScrollPosition.X, points[i].Y - this.AutoScrollPosition.Y);
  732. }
  733. }
  734. return points;
  735. }
  736. /// <summary>
  737. /// 重新布局指定线的连接点
  738. /// </summary>
  739. /// <param name="line"></param>
  740. void ResetLine(FlowLine line)
  741. {
  742. Trace.WriteLine("ResetLine Begain----------------------");
  743. List<Point> points = GetLinePointListWidthScroll(line.HeadNode, line.TailNode);
  744. line.PointList = points;
  745. Trace.WriteLine("HeadNode=" + line.HeadNode.CenterPoint.ToString());
  746. Trace.WriteLine("HeadNodeTopPoint=" + line.HeadNode.TopPoint.ToString());
  747. Trace.WriteLine("TailNode=" + line.TailNode.CenterPoint.ToString());
  748. Trace.WriteLine("TailNodeBottomPoint=" + line.HeadNode.BottomPoint.ToString());
  749. foreach (Point point in points)
  750. Trace.WriteLine(point);
  751. Trace.WriteLine("ResetLine End--------------------");
  752. }
  753. /// <summary>
  754. /// 重新布局和指定节点相连的线
  755. /// </summary>
  756. /// <param name="node"></param>
  757. void ResetNodeLines(FlowNode node)
  758. {
  759. List<NodePort> inputPortList = node.InputPortList;
  760. if (inputPortList != null && inputPortList.Count > 0)
  761. {
  762. foreach (NodePort port in inputPortList)
  763. {
  764. ResetLine(port.Line);
  765. }
  766. }
  767. List<NodePort> outputPortList = node.OutputPortList;
  768. if (outputPortList != null && outputPortList.Count > 0)
  769. {
  770. foreach (NodePort port in outputPortList)
  771. {
  772. ResetLine(port.Line);
  773. }
  774. }
  775. }
  776. /// <summary>
  777. /// 将节点控件添加到画板控件上
  778. /// </summary>
  779. /// <param name="nodeControl"></param>
  780. void AddNodeControlToCanvasControl(FlowNodeControl nodeControl)
  781. {
  782. this.Controls.Add(nodeControl);
  783. nodeControl.MouseDown += new MouseEventHandler(nodeControl_MouseDown);
  784. nodeControl.MouseUp += new MouseEventHandler(nodeControl_MouseUp);
  785. nodeControl.MouseMove += new MouseEventHandler(nodeControl_MouseMove);
  786. nodeControl.Click += new EventHandler(nodeControl_Click);
  787. nodeControl.DoubleClick += new EventHandler(nodeControl_DoubleClick);
  788. nodeControl.KeyDown += new KeyEventHandler(nodeControl_KeyDown);
  789. }
  790. /// <summary>
  791. /// 将节点控件从画板控件上移除
  792. /// </summary>
  793. /// <param name="nodeControl"></param>
  794. void RemoveNodeControlFromCanvasControl(FlowNodeControl nodeControl)
  795. {
  796. this.Controls.Remove(nodeControl);
  797. nodeControl.MouseDown -= new MouseEventHandler(nodeControl_MouseDown);
  798. nodeControl.MouseUp -= new MouseEventHandler(nodeControl_MouseUp);
  799. nodeControl.MouseMove -= new MouseEventHandler(nodeControl_MouseMove);
  800. nodeControl.Click -= new EventHandler(nodeControl_Click);
  801. nodeControl.DoubleClick -= new EventHandler(nodeControl_DoubleClick);
  802. nodeControl.KeyDown -= new KeyEventHandler(nodeControl_KeyDown);
  803. }
  804. /// <summary>
  805. /// 将线控件添加到画布控件中
  806. /// </summary>
  807. /// <param name="lineControl"></param>
  808. void AddLineControlToCanvasControl(FlowBrokenLineControl lineControl)
  809. {
  810. lineControl.Size = new Size(1000, 1000);
  811. this.Controls.Add(lineControl);
  812. lineControl.Click += new EventHandler(lineControl_Click);
  813. lineControl.DoubleClick += new EventHandler(lineControl_DoubleClick);
  814. lineControl.KeyDown += new KeyEventHandler(lineControl_KeyDown);
  815. }
  816. /// <summary>
  817. /// 将线控件从画布控件中移除
  818. /// </summary>
  819. /// <param name="lineControl"></param>
  820. void RemoveLineControlFromCanvasControl(FlowBrokenLineControl lineControl)
  821. {
  822. lineControl.Size = new Size(1000, 1000);
  823. this.Controls.Remove(lineControl);
  824. lineControl.Click -= new EventHandler(lineControl_Click);
  825. lineControl.DoubleClick -= new EventHandler(lineControl_DoubleClick);
  826. lineControl.KeyDown -= new KeyEventHandler(lineControl_KeyDown);
  827. }
  828. /// <summary>
  829. /// 获取选中的节点
  830. /// </summary>
  831. /// <returns></returns>
  832. FlowNode[] GetSelectedNode()
  833. {
  834. if (!_bBegainSelect)
  835. return null;
  836. List<FlowNode> selectedNodeList = new List<FlowNode>();
  837. Rectangle selectRect = new Rectangle(_selectControl.Location, _selectControl.Size);
  838. foreach (FlowNode node in _nodeList)
  839. {
  840. Rectangle nodeRect = new Rectangle(node.Control.Location, node.Control.Size);
  841. if (selectRect.IntersectsWith(nodeRect))
  842. selectedNodeList.Add(node);
  843. }
  844. return selectedNodeList.ToArray();
  845. }
  846. /// <summary>
  847. /// 处理键盘按下事件
  848. /// </summary>
  849. /// <param name="keyVal"></param>
  850. void PressKeyDown(int keyVal)
  851. {
  852. if (keyVal == KeyVal_Del)
  853. {
  854. if(Evt_KeyDown_Del != null)
  855. Evt_KeyDown_Del();
  856. }
  857. else if (keyVal == KeyVal_Enter)
  858. {
  859. if (Evt_KeyDown_Enter != null)
  860. Evt_KeyDown_Enter();
  861. }
  862. }
  863. #endregion
  864. }
  865. #region 画布委托定义
  866. /// <summary>
  867. /// 鼠标单击节点
  868. /// </summary>
  869. /// <param name="node"></param>
  870. public delegate void Delegate_ClickNode(FlowNode node);
  871. /// <summary>
  872. /// 鼠标双击节点
  873. /// </summary>
  874. /// <param name="node"></param>
  875. public delegate void Delegate_DoubleClickNode(FlowNode node);
  876. /// <summary>
  877. /// 鼠标单击线
  878. /// </summary>
  879. /// <param name="line"></param>
  880. public delegate void Delegate_ClickLine(FlowLine line);
  881. /// <summary>
  882. /// 鼠标双击线
  883. /// </summary>
  884. /// <param name="line"></param>
  885. public delegate void Delegate_DoubleClickLine(FlowLine line);
  886. /// <summary>
  887. /// 删除键被按下
  888. /// </summary>
  889. public delegate void Delegate_KeyDown_Del();
  890. /// <summary>
  891. /// 回车键被按下
  892. /// </summary>
  893. public delegate void Delegate_KeyDown_Enter();
  894. #endregion
  895. }