-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathSTNodeEditor.cs
More file actions
2165 lines (2040 loc) · 98.7 KB
/
STNodeEditor.cs
File metadata and controls
2165 lines (2040 loc) · 98.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Threading;
using System.ComponentModel;
using System.Reflection;
using System.IO.Compression;
using SkiaSharp;
using SkiaSharp.Views.Desktop;
/*
MIT License
Copyright (c) 2021 DebugST@crystal_lz
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/*
* create: 2020-12-08
* modify: 2021-04-12
* Author: Crystal_lz
* blog: http://st233.com
* Gitee: https://gitee.com/DebugST
* Github: https://github.com/DebugST
*/
namespace ST.Library.UI.NodeEditor
{
public class STNodeEditor : SKControl
{
private const UInt32 WM_MOUSEHWHEEL = 0x020E;
protected static readonly Type m_type_node = typeof(STNode);
#region protected enum,struct --------------------------------------------------------------------------------------
protected enum CanvasAction //当前鼠标移动操作表示进行下列哪一个行为
{
None, //无
MoveNode, //正在移动 Node
ConnectOption, //正在连接 Option
SelectRectangle, //正在选择矩形区域
DrawMarkDetails //正在绘制标记信息详情
}
protected struct MagnetInfo
{
public bool XMatched; //X轴是否有磁铁匹配上
public bool YMatched;
public int X; //与X轴那个数字匹配上
public int Y;
public int OffsetX; //当前节点X位置与匹配上的X的相对偏移
public int OffsetY;
}
#endregion
#region Properties ------------------------------------------------------------------------------------------------------
private float _CanvasOffsetX;
/// <summary>
/// 获取画布原点相对于控件 X 方向上的偏移位置
/// </summary>
[Browsable(false)]
public float CanvasOffsetX {
get { return _CanvasOffsetX; }
}
private float _CanvasOffsetY;
/// <summary>
/// 获取画布原点相对于控件 Y 方向上的偏移位置
/// </summary>
[Browsable(false)]
public float CanvasOffsetY {
get { return _CanvasOffsetY; }
}
private PointF _CanvasOffset;
/// <summary>
/// 获取画布原点相对于控件偏移位置
/// </summary>
[Browsable(false)]
public PointF CanvasOffset {
get {
_CanvasOffset.X = _CanvasOffsetX;
_CanvasOffset.Y = _CanvasOffsetY;
return _CanvasOffset;
}
}
private Rectangle _CanvasValidBounds;
/// <summary>
/// 获取画布中的有被用到的有效区域
/// </summary>
[Browsable(false)]
public Rectangle CanvasValidBounds {
get { return _CanvasValidBounds; }
}
private float _CanvasScale = 1;
/// <summary>
/// 获取画布的缩放比例
/// </summary>
[Browsable(false)]
public float CanvasScale {
get { return _CanvasScale; }
}
private float _Curvature = 0.3F;
/// <summary>
/// 获取或设置 Option 之间连线的曲度
/// </summary>
[Browsable(false)]
public float Curvature {
get { return _Curvature; }
set {
if (value < 0) value = 0;
if (value > 1) value = 1;
_Curvature = value;
if (m_dic_gp_info.Count != 0) this.BuildLinePath();
}
}
private bool _ShowMagnet = true;
/// <summary>
/// 获取或设置移动画布中 Node 时候 是否启用磁铁效果
/// </summary>
[Description("获取或设置移动画布中 Node 时候 是否启用磁铁效果"), DefaultValue(true)]
public bool ShowMagnet {
get { return _ShowMagnet; }
set { _ShowMagnet = value; }
}
private bool _ShowBorder = true;
/// <summary>
/// 获取或设置 移动画布中是否显示 Node 边框
/// </summary>
[Description("获取或设置 移动画布中是否显示 Node 边框"), DefaultValue(true)]
public bool ShowBorder {
get { return _ShowBorder; }
set {
_ShowBorder = value;
this.Invalidate();
}
}
private bool _ShowGrid = true;
/// <summary>
/// 获取或设置画布中是否绘制背景网格线条
/// </summary>
[Description("获取或设置画布中是否绘制背景网格线条"), DefaultValue(true)]
public bool ShowGrid {
get { return _ShowGrid; }
set {
_ShowGrid = value;
this.Invalidate();
}
}
private bool _ShowLocation = true;
/// <summary>
/// 获取或设置是否在画布边缘显示超出视角的 Node 位置信息
/// </summary>
[Description("获取或设置是否在画布边缘显示超出视角的 Node 位置信息"), DefaultValue(true)]
public bool ShowLocation {
get { return _ShowLocation; }
set {
_ShowLocation = value;
this.Invalidate();
}
}
private STNodeCollection _Nodes;
/// <summary>
/// 获取画布中 Node 集合
/// </summary>
[Browsable(false)]
public STNodeCollection Nodes {
get {
return _Nodes;
}
}
private STNode _ActiveNode;
/// <summary>
/// 获取当前画布中被选中的活动 Node
/// </summary>
[Browsable(false)]
public STNode ActiveNode {
get { return _ActiveNode; }
//set {
// if (value == _ActiveSelectedNode) return;
// if (_ActiveSelectedNode != null) _ActiveSelectedNode.OnLostFocus(EventArgs.Empty);
// _ActiveSelectedNode = value;
// _ActiveSelectedNode.IsActive = true;
// this.Invalidate();
// this.OnSelectedChanged(EventArgs.Empty);
//}
}
private STNode _HoverNode;
/// <summary>
/// 获取当前画布中鼠标悬停的 Node
/// </summary>
[Browsable(false)]
public STNode HoverNode {
get { return _HoverNode; }
}
//========================================color================================
private Color _GridColor = Color.Black;
/// <summary>
/// 获取或设置绘制画布背景时 网格线条颜色
/// </summary>
[Description("获取或设置绘制画布背景时 网格线条颜色"), DefaultValue(typeof(Color), "Black")]
public Color GridColor {
get { return _GridColor; }
set {
_GridColor = value;
this.Invalidate();
}
}
private Color _BorderColor = Color.Black;
/// <summary>
/// 获取或设置画布中 Node 边框颜色
/// </summary>
[Description("获取或设置画布中 Node 边框颜色"), DefaultValue(typeof(Color), "Black")]
public Color BorderColor {
get { return _BorderColor; }
set {
_BorderColor = value;
if (m_img_border != null) m_img_border.Dispose();
m_img_border = this.CreateBorderImage(value);
this.Invalidate();
}
}
private Color _BorderHoverColor = Color.Gray;
/// <summary>
/// 获取或设置画布中悬停 Node 边框颜色
/// </summary>
[Description("获取或设置画布中悬停 Node 边框颜色"), DefaultValue(typeof(Color), "Gray")]
public Color BorderHoverColor {
get { return _BorderHoverColor; }
set {
_BorderHoverColor = value;
if (m_img_border_hover != null) m_img_border_hover.Dispose();
m_img_border_hover = this.CreateBorderImage(value);
this.Invalidate();
}
}
private Color _BorderSelectedColor = Color.Orange;
/// <summary>
/// 获取或设置画布中选中 Node 边框颜色
/// </summary>
[Description("获取或设置画布中选中 Node 边框颜色"), DefaultValue(typeof(Color), "Orange")]
public Color BorderSelectedColor {
get { return _BorderSelectedColor; }
set {
_BorderSelectedColor = value;
if (m_img_border_selected != null) m_img_border_selected.Dispose();
m_img_border_selected = this.CreateBorderImage(value);
this.Invalidate();
}
}
private Color _BorderActiveColor = Color.OrangeRed;
/// <summary>
/// 获取或设置画布中活动 Node 边框颜色
/// </summary>
[Description("获取或设置画布中活动 Node 边框颜色"), DefaultValue(typeof(Color), "OrangeRed")]
public Color BorderActiveColor {
get { return _BorderActiveColor; }
set {
_BorderActiveColor = value;
if (m_img_border_active != null) m_img_border_active.Dispose();
m_img_border_active = this.CreateBorderImage(value);
this.Invalidate();
}
}
private Color _MarkForeColor = Color.White;
/// <summary>
/// 获取或设置画布绘制 Node 标记详情采用的前景色
/// </summary>
[Description("获取或设置画布绘制 Node 标记详情采用的前景色"), DefaultValue(typeof(Color), "White")]
public Color MarkForeColor {
get { return _MarkBackColor; }
set {
_MarkBackColor = value;
this.Invalidate();
}
}
private Color _MarkBackColor = Color.FromArgb(180, Color.Black);
/// <summary>
/// 获取或设置画布绘制 Node 标记详情采用的背景色
/// </summary>
[Description("获取或设置画布绘制 Node 标记详情采用的背景色")]
public Color MarkBackColor {
get { return _MarkBackColor; }
set {
_MarkBackColor = value;
this.Invalidate();
}
}
private Color _MagnetColor = Color.Lime;
/// <summary>
/// 获取或设置画布中移动 Node 时候 磁铁标记颜色
/// </summary>
[Description("获取或设置画布中移动 Node 时候 磁铁标记颜色"), DefaultValue(typeof(Color), "Lime")]
public Color MagnetColor {
get { return _MagnetColor; }
set { _MagnetColor = value; }
}
private Color _SelectedRectangleColor = Color.DodgerBlue;
/// <summary>
/// 获取或设置画布中选择矩形区域的颜色
/// </summary>
[Description("获取或设置画布中选择矩形区域的颜色"), DefaultValue(typeof(Color), "DodgerBlue")]
public Color SelectedRectangleColor {
get { return _SelectedRectangleColor; }
set { _SelectedRectangleColor = value; }
}
private Color _HighLineColor = Color.Cyan;
/// <summary>
/// 获取或设置画布中高亮连线的颜色
/// </summary>
[Description("获取或设置画布中高亮连线的颜色"), DefaultValue(typeof(Color), "Cyan")]
public Color HighLineColor {
get { return _HighLineColor; }
set { _HighLineColor = value; }
}
private Color _LocationForeColor = Color.Red;
/// <summary>
/// 获取或设置画布中边缘位置提示区域前景色
/// </summary>
[Description("获取或设置画布中边缘位置提示区域前景色"), DefaultValue(typeof(Color), "Red")]
public Color LocationForeColor {
get { return _LocationForeColor; }
set {
_LocationForeColor = value;
this.Invalidate();
}
}
private Color _LocationBackColor = Color.FromArgb(120, Color.Black);
/// <summary>
/// 获取或设置画布中边缘位置提示区域背景色
/// </summary>
[Description("获取或设置画布中边缘位置提示区域背景色")]
public Color LocationBackColor {
get { return _LocationBackColor; }
set {
_LocationBackColor = value;
this.Invalidate();
}
}
private Color _UnknownTypeColor = Color.Gray;
/// <summary>
/// 获取或设置画布中当 Node 中 Option 数据类型无法确定时应当使用的颜色
/// </summary>
[Description("获取或设置画布中当 Node 中 Option 数据类型无法确定时应当使用的颜色"), DefaultValue(typeof(Color), "Gray")]
public Color UnknownTypeColor {
get { return _UnknownTypeColor; }
set {
_UnknownTypeColor = value;
this.Invalidate();
}
}
private Dictionary<Type, Color> _TypeColor = new Dictionary<Type, Color>();
/// <summary>
/// 获取或设置画布中 Node 中 Option 数据类型预设颜色
/// </summary>
[Browsable(false)]
public Dictionary<Type, Color> TypeColor {
get { return _TypeColor; }
}
#endregion
#region protected properties ----------------------------------------------------------------------------------------
/// <summary>
/// 当前鼠标在控件中的实时位置
/// </summary>
protected Point m_pt_in_control;
/// <summary>
/// 当前鼠标在画布中的实时位置
/// </summary>
protected PointF m_pt_in_canvas;
/// <summary>
/// 鼠标点击时在控件上的位置
/// </summary>
protected Point m_pt_down_in_control;
/// <summary>
/// 鼠标点击时在画布中的位置
/// </summary>
protected PointF m_pt_down_in_canvas;
/// <summary>
/// 用于鼠标点击移动画布时候 鼠标点下时候的画布坐标位置
/// </summary>
protected PointF m_pt_canvas_old;
/// <summary>
/// 用于保存连线过程中保存点下 Option 的起点坐标
/// </summary>
protected Point m_pt_dot_down;
/// <summary>
/// 用于保存连线过程中鼠标点下的起点Option 当MouseUP时候 确定是否连接此节点
/// </summary>
protected STNodeOption m_option_down;
/// <summary>
/// 当前鼠标点下的 STNode
/// </summary>
protected STNode m_node_down;
/// <summary>
/// 当前鼠标是否位于控件中
/// </summary>
protected bool m_mouse_in_control;
#endregion
public STNodeEditor() {
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this._Nodes = new STNodeCollection(this);
this.BackColor = Color.FromArgb(255, 34, 34, 34);
this.MinimumSize = new Size(100, 100);
this.Size = new Size(200, 200);
this.AllowDrop = true;
m_real_canvas_x = this._CanvasOffsetX = 10;
m_real_canvas_y = this._CanvasOffsetY = 10;
}
#region private fields --------------------------------------------------------------------------------------
private DrawingTools m_drawing_tools;
private SKCanvas m_canvas;
private NodeFindInfo m_find = new NodeFindInfo();
private MagnetInfo m_mi = new MagnetInfo();
private RectangleF m_rect_select = new RectangleF();
//节点边框预设图案
private Image m_img_border;
private Image m_img_border_hover;
private Image m_img_border_selected;
private Image m_img_border_active;
//用于鼠标滚动或者触摸板移动画布时候的动画效果 该值为需要移动到的真实坐标地址 查看->MoveCanvasThread()
private float m_real_canvas_x;
private float m_real_canvas_y;
//用于移动节点时候 保存鼠标点下时候选中的节点初始坐标
private Dictionary<STNode, Point> m_dic_pt_selected = new Dictionary<STNode, Point>();
//用于磁铁效果 移动节点时候 非选择节点的统计出来的需要参与磁铁效果的坐标 查看->BuildMagnetLocation()
private List<int> m_lst_magnet_x = new List<int>();
private List<int> m_lst_magnet_y = new List<int>();
//用于磁铁效果 移动节点时候 活动选择节点统计出来需要参与磁铁效果的坐标 查看->CheckMagnet()
private List<int> m_lst_magnet_mx = new List<int>();
private List<int> m_lst_magnet_my = new List<int>();
//用于鼠标滚动中计算时间触发间隔 根据间隔不同 画布产生的位移不同 查看->OnMouseWheel(),OnMouseHWheel()
private DateTime m_dt_vw = DateTime.Now;
private DateTime m_dt_hw = DateTime.Now;
//移动鼠标过程中的当前行为
private CanvasAction m_ca;
//保存已选中的节点
private HashSet<STNode> m_hs_node_selected = new HashSet<STNode>();
private bool m_is_process_mouse_event = true; //是否向下(Node or NodeControls)传递鼠标相关事件 如断开连接相关操作不应向下传递
private bool m_is_buildpath; //用于重绘过程中 判断该次是否要重新建立缓存连线的路径
private Pen m_p_line = new Pen(Color.Cyan, 2f); //用于绘制已经连接的线条
private Pen m_p_line_hover = new Pen(Color.Cyan, 4f); //用于绘制鼠标悬停时候的线条
private GraphicsPath m_gp_hover; //当前鼠标悬停的连线路径
private StringFormat m_sf = new StringFormat(); //文本格式 用于Mark绘制时候 设置文本格式
//保存每个连接线条与之对应的节点关系
private Dictionary<GraphicsPath, ConnectionInfo> m_dic_gp_info = new Dictionary<GraphicsPath, ConnectionInfo>();
//保存超出视觉区域的 Node 的位置
private List<Point> m_lst_node_out = new List<Point>();
//当前编辑器已加载的 Node 类型 用于从文件或者数据中加载节点使用
private Dictionary<string, Type> m_dic_type = new Dictionary<string, Type>();
private int m_time_alert;
private int m_alpha_alert;
private string m_str_alert;
private Color m_forecolor_alert;
private Color m_backcolor_alert;
private DateTime m_dt_alert;
private Rectangle m_rect_alert;
private AlertLocation m_al;
#endregion
#region event ----------------------------------------------------------------------------------------------------
/// <summary>
/// 活动的节点发生变化时候发生
/// </summary>
[Description("活动的节点发生变化时候发生")]
public event EventHandler ActiveChanged;
/// <summary>
/// 选择的节点发生变化时候发生
/// </summary>
[Description("选择的节点发生变化时候发生")]
public event EventHandler SelectedChanged;
/// <summary>
/// 悬停的节点发生变化时候发生
/// </summary>
[Description("悬停的节点发生变化时候发生")]
public event EventHandler HoverChanged;
/// <summary>
/// 当节点被添加时候发生
/// </summary>
[Description("当节点被添加时候发生")]
public event STNodeEditorEventHandler NodeAdded;
/// <summary>
/// 当节点被移除时候发生
/// </summary>
[Description("当节点被移除时候发生")]
public event STNodeEditorEventHandler NodeRemoved;
/// <summary>
/// 移动画布原点时候发生
/// </summary>
[Description("移动画布原点时候发生")]
public event EventHandler CanvasMoved;
/// <summary>
/// 缩放画布时候发生
/// </summary>
[Description("缩放画布时候发生")]
public event EventHandler CanvasScaled;
/// <summary>
/// 连接节点选项时候发生
/// </summary>
[Description("连接节点选项时候发生")]
public event STNodeEditorOptionEventHandler OptionConnected;
/// <summary>
/// 正在连接节点选项时候发生
/// </summary>
[Description("正在连接节点选项时候发生")]
public event STNodeEditorOptionEventHandler OptionConnecting;
/// <summary>
/// 断开节点选项时候发生
/// </summary>
[Description("断开节点选项时候发生")]
public event STNodeEditorOptionEventHandler OptionDisConnected;
/// <summary>
/// 正在断开节点选项时候发生
/// </summary>
[Description("正在断开节点选项时候发生")]
public event STNodeEditorOptionEventHandler OptionDisConnecting;
protected virtual internal void OnSelectedChanged(EventArgs e) {
if (this.SelectedChanged != null) this.SelectedChanged(this, e);
}
protected virtual void OnActiveChanged(EventArgs e) {
if (this.ActiveChanged != null) this.ActiveChanged(this, e);
}
protected virtual void OnHoverChanged(EventArgs e) {
if (this.HoverChanged != null) this.HoverChanged(this, e);
}
protected internal virtual void OnNodeAdded(STNodeEditorEventArgs e) {
if (this.NodeAdded != null) this.NodeAdded(this, e);
}
protected internal virtual void OnNodeRemoved(STNodeEditorEventArgs e) {
if (this.NodeRemoved != null) this.NodeRemoved(this, e);
}
protected virtual void OnCanvasMoved(EventArgs e) {
if (this.CanvasMoved != null) this.CanvasMoved(this, e);
}
protected virtual void OnCanvasScaled(EventArgs e) {
if (this.CanvasScaled != null) this.CanvasScaled(this, e);
}
protected internal virtual void OnOptionConnected(STNodeEditorOptionEventArgs e) {
if (this.OptionConnected != null) this.OptionConnected(this, e);
}
protected internal virtual void OnOptionDisConnected(STNodeEditorOptionEventArgs e) {
if (this.OptionDisConnected != null) this.OptionDisConnected(this, e);
}
protected internal virtual void OnOptionConnecting(STNodeEditorOptionEventArgs e) {
if (this.OptionConnecting != null) this.OptionConnecting(this, e);
}
protected internal virtual void OnOptionDisConnecting(STNodeEditorOptionEventArgs e) {
if (this.OptionDisConnecting != null) this.OptionDisConnecting(this, e);
}
#endregion event
#region override -----------------------------------------------------------------------------------------------------
protected override void OnCreateControl() {
m_drawing_tools = new DrawingTools() {
Pen = new Pen(Color.Black, 1),
SolidBrush = new SolidBrush(Color.Black)
};
m_img_border = this.CreateBorderImage(this._BorderColor);
m_img_border_active = this.CreateBorderImage(this._BorderActiveColor);
m_img_border_hover = this.CreateBorderImage(this._BorderHoverColor);
m_img_border_selected = this.CreateBorderImage(this._BorderSelectedColor);
base.OnCreateControl();
new Thread(this.MoveCanvasThread) { IsBackground = true }.Start();
new Thread(this.ShowAlertThread) { IsBackground = true }.Start();
m_sf = new StringFormat();
m_sf.Alignment = StringAlignment.Near;
m_sf.FormatFlags = StringFormatFlags.NoWrap;
m_sf.SetTabStops(0, new float[] { 40 });
}
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
try {
Point pt = new Point(((int)m.LParam) >> 16, (ushort)m.LParam);
pt = this.PointToClient(pt);
if (m.Msg == WM_MOUSEHWHEEL) { //获取水平滚动消息
MouseButtons mb = MouseButtons.None;
int n = (ushort)m.WParam;
if ((n & 0x0001) == 0x0001) mb |= MouseButtons.Left;
if ((n & 0x0010) == 0x0010) mb |= MouseButtons.Middle;
if ((n & 0x0002) == 0x0002) mb |= MouseButtons.Right;
if ((n & 0x0020) == 0x0020) mb |= MouseButtons.XButton1;
if ((n & 0x0040) == 0x0040) mb |= MouseButtons.XButton2;
this.OnMouseHWheel(new MouseEventArgs(mb, 0, pt.X, pt.Y, ((int)m.WParam) >> 16));
}
} catch { /*add code*/ }
}
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) {
base.OnPaintSurface(e);
m_canvas = e.Surface.Canvas;
m_drawing_tools.Canvas = m_canvas;
m_canvas.Clear(SkiaDrawingHelper.ToSKColor(this.BackColor));
this.RenderEditorToCanvas(m_canvas);
m_canvas = null;
}
private void RenderEditorToCanvas(SKCanvas canvas) {
if (canvas == null) return;
using (var bitmap = this.RenderEditorToBitmap()) {
canvas.DrawBitmap(bitmap, 0, 0);
}
}
private SKBitmap RenderEditorToBitmap() {
var bitmap = new SKBitmap(Math.Max(this.Width, 1), Math.Max(this.Height, 1), true);
using (var canvas = new SKCanvas(bitmap)) {
canvas.Clear(SkiaDrawingHelper.ToSKColor(this.BackColor));
this.RenderEditorToCanvasLegacy(canvas);
}
return bitmap;
}
private void RenderEditorToCanvasLegacy(SKCanvas canvas) {
if (canvas == null) return;
m_canvas = canvas;
m_drawing_tools.Canvas = m_canvas;
if (this._ShowGrid) this.OnDrawGrid(m_drawing_tools, this.Width, this.Height);
this.RenderLegacyNodeLayer(canvas);
switch (m_ca) {
case CanvasAction.MoveNode:
if (this._ShowMagnet && this._ActiveNode != null) this.OnDrawMagnet(m_drawing_tools, m_mi);
break;
case CanvasAction.SelectRectangle:
this.OnDrawSelectedRectangle(m_drawing_tools, this.CanvasToControl(m_rect_select));
break;
case CanvasAction.DrawMarkDetails:
if (!string.IsNullOrEmpty(m_find.Mark)) this.OnDrawMark(m_drawing_tools);
break;
}
return bitmap;
}
if (this._ShowLocation) this.OnDrawNodeOutLocation(m_drawing_tools, this.Size, m_lst_node_out);
this.OnDrawAlert();
}
private void RenderLegacyNodeLayer(SKCanvas canvas) {
if (canvas == null) return;
m_drawing_tools.Canvas = canvas;
canvas.Save();
canvas.Translate(this._CanvasOffsetX, this._CanvasOffsetY);
canvas.Scale(this._CanvasScale, this._CanvasScale);
this.OnDrawConnectedLine(m_drawing_tools);
this.OnDrawNode(m_drawing_tools, this.ControlToCanvas(this.ClientRectangle));
if (m_ca == CanvasAction.ConnectOption) {
using (var high = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._HighLineColor), Style = SKPaintStyle.Stroke, StrokeWidth = 2f, IsAntialias = true }) {
if (m_option_down.IsInput)
this.DrawBezier(canvas, high, m_pt_in_canvas, m_pt_dot_down, this._Curvature);
else
this.DrawBezier(canvas, high, m_pt_dot_down, m_pt_in_canvas, this._Curvature);
}
}
canvas.Restore();
}
protected override void OnMouseDown(MouseEventArgs e) {
base.OnMouseDown(e);
this.Focus();
m_ca = CanvasAction.None;
m_mi.XMatched = m_mi.YMatched = false;
m_pt_down_in_control = e.Location;
m_pt_down_in_canvas.X = ((e.X - this._CanvasOffsetX) / this._CanvasScale);
m_pt_down_in_canvas.Y = ((e.Y - this._CanvasOffsetY) / this._CanvasScale);
m_pt_canvas_old.X = this._CanvasOffsetX;
m_pt_canvas_old.Y = this._CanvasOffsetY;
if (m_gp_hover != null && e.Button == MouseButtons.Right) { //断开连接
this.DisConnectionHover();
m_is_process_mouse_event = false; //终止MouseClick与MouseUp向下传递
return;
}
NodeFindInfo nfi = this.FindNodeFromPoint(m_pt_down_in_canvas);
if (!string.IsNullOrEmpty(nfi.Mark)) { //如果点下的是标记信息
m_ca = CanvasAction.DrawMarkDetails;
this.Invalidate();
return;
}
if (nfi.NodeOption != null) { //如果点下的Option的连接点
this.StartConnect(nfi.NodeOption);
return;
}
if (nfi.Node != null) {
nfi.Node.OnMouseDown(new MouseEventArgs(e.Button, e.Clicks, (int)m_pt_down_in_canvas.X - nfi.Node.Left, (int)m_pt_down_in_canvas.Y - nfi.Node.Top, e.Delta));
bool bCtrlDown = (Control.ModifierKeys & Keys.Control) == Keys.Control;
if (bCtrlDown) {
if (nfi.Node.IsSelected) {
if (nfi.Node == this._ActiveNode) {
this.SetActiveNode(null);
}
} else {
nfi.Node.SetSelected(true, true);
}
return;
} else if (!nfi.Node.IsSelected) {
foreach (var n in m_hs_node_selected.ToArray()) n.SetSelected(false, false);
}
nfi.Node.SetSelected(true, false); //添加到已选择节点
this.SetActiveNode(nfi.Node);
if (this.PointInRectangle(nfi.Node.TitleRectangle, m_pt_down_in_canvas.X, m_pt_down_in_canvas.Y)) {
if (e.Button == MouseButtons.Right) {
if (nfi.Node.ContextMenuStrip != null) {
nfi.Node.ContextMenuStrip.Show(this.PointToScreen(e.Location));
}
} else {
m_dic_pt_selected.Clear();
lock (m_hs_node_selected) {
foreach (STNode n in m_hs_node_selected) //记录已选择节点位置 如果需要移动已选中节点时候 将会有用
m_dic_pt_selected.Add(n, n.Location);
}
m_ca = CanvasAction.MoveNode; //如果点下的是节点的标题 则可以移动该节点
if (this._ShowMagnet && this._ActiveNode != null) this.BuildMagnetLocation(); //建立磁铁需要的坐标 如果需要移动已选中节点时候 将会有用
}
} else
m_node_down = nfi.Node;
} else {
this.SetActiveNode(null);
foreach (var n in m_hs_node_selected.ToArray()) n.SetSelected(false, false);//没有点下任何东西 清空已经选择节点
m_ca = CanvasAction.SelectRectangle; //进入矩形区域选择模式
m_rect_select.Width = m_rect_select.Height = 0;
m_node_down = null;
}
//this.SetActiveNode(nfi.Node);
}
protected override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
m_pt_in_control = e.Location;
m_pt_in_canvas.X = ((e.X - this._CanvasOffsetX) / this._CanvasScale);
m_pt_in_canvas.Y = ((e.Y - this._CanvasOffsetY) / this._CanvasScale);
if (m_node_down != null) {
m_node_down.OnMouseMove(new MouseEventArgs(e.Button, e.Clicks,
(int)m_pt_in_canvas.X - m_node_down.Left,
(int)m_pt_in_canvas.Y - m_node_down.Top, e.Delta));
return;
}
if (e.Button == MouseButtons.Middle) { //鼠标中键移动画布
this._CanvasOffsetX = m_real_canvas_x = m_pt_canvas_old.X + (e.X - m_pt_down_in_control.X);
this._CanvasOffsetY = m_real_canvas_y = m_pt_canvas_old.Y + (e.Y - m_pt_down_in_control.Y);
this.Invalidate();
return;
}
if (e.Button == MouseButtons.Left) { //如果鼠标左键点下 判断行为
m_gp_hover = null;
switch (m_ca) {
case CanvasAction.MoveNode: this.MoveNode(e.Location); return; //当前移动节点
case CanvasAction.ConnectOption: this.Invalidate(); return; //当前正在连线
case CanvasAction.SelectRectangle: //当前正在选取
m_rect_select.X = m_pt_down_in_canvas.X < m_pt_in_canvas.X ? m_pt_down_in_canvas.X : m_pt_in_canvas.X;
m_rect_select.Y = m_pt_down_in_canvas.Y < m_pt_in_canvas.Y ? m_pt_down_in_canvas.Y : m_pt_in_canvas.Y;
m_rect_select.Width = Math.Abs(m_pt_in_canvas.X - m_pt_down_in_canvas.X);
m_rect_select.Height = Math.Abs(m_pt_in_canvas.Y - m_pt_down_in_canvas.Y);
foreach (STNode n in this._Nodes) {
n.SetSelected(m_rect_select.IntersectsWith(n.Rectangle), false);
}
this.Invalidate();
return;
}
}
//若不存在行为 则判断鼠标下方是否存在其他对象
NodeFindInfo nfi = this.FindNodeFromPoint(m_pt_in_canvas);
bool bRedraw = false;
if (this._HoverNode != nfi.Node) { //鼠标悬停到Node上
if (nfi.Node != null) nfi.Node.OnMouseEnter(EventArgs.Empty);
if (this._HoverNode != null)
this._HoverNode.OnMouseLeave(new MouseEventArgs(e.Button, e.Clicks,
(int)m_pt_in_canvas.X - this._HoverNode.Left,
(int)m_pt_in_canvas.Y - this._HoverNode.Top, e.Delta));
this._HoverNode = nfi.Node;
this.OnHoverChanged(EventArgs.Empty);
bRedraw = true;
}
if (this._HoverNode != null) {
this._HoverNode.OnMouseMove(new MouseEventArgs(e.Button, e.Clicks,
(int)m_pt_in_canvas.X - this._HoverNode.Left,
(int)m_pt_in_canvas.Y - this._HoverNode.Top, e.Delta));
m_gp_hover = null;
} else {
GraphicsPath gp = null;
foreach (var v in m_dic_gp_info) { //判断鼠标是否悬停到连线路径上
if (v.Key.IsOutlineVisible(m_pt_in_canvas, m_p_line_hover)) {
gp = v.Key;
break;
}
}
if (m_gp_hover != gp) {
m_gp_hover = gp;
bRedraw = true;
}
}
if (bRedraw) this.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e) {
base.OnMouseUp(e);
var nfi = this.FindNodeFromPoint(m_pt_in_canvas);
switch (m_ca) { //鼠标抬起时候 判断行为
case CanvasAction.MoveNode: //若正在移动Node 则重新记录当前位置
foreach (STNode n in m_dic_pt_selected.Keys.ToList()) m_dic_pt_selected[n] = n.Location;
break;
case CanvasAction.ConnectOption: //若正在连线 则结束连接
if (e.Location == m_pt_down_in_control) break;
if (nfi.NodeOption != null) {
if (m_option_down.IsInput)
nfi.NodeOption.ConnectOption(m_option_down);
else
m_option_down.ConnectOption(nfi.NodeOption);
}
break;
}
if (m_is_process_mouse_event && this._ActiveNode != null) {
var mea = new MouseEventArgs(e.Button, e.Clicks,
(int)m_pt_in_canvas.X - this._ActiveNode.Left,
(int)m_pt_in_canvas.Y - this._ActiveNode.Top, e.Delta);
this._ActiveNode.OnMouseUp(mea);
m_node_down = null;
}
m_is_process_mouse_event = true; //当前为断开连接操作不进行事件传递 下次将接受事件
m_ca = CanvasAction.None;
this.Invalidate();
}
protected override void OnMouseEnter(EventArgs e) {
base.OnMouseEnter(e);
m_mouse_in_control = true;
}
protected override void OnMouseLeave(EventArgs e) {
base.OnMouseLeave(e);
m_mouse_in_control = false;
if (this._HoverNode != null) this._HoverNode.OnMouseLeave(e);
this._HoverNode = null;
this.Invalidate();
}
protected override void OnMouseWheel(MouseEventArgs e) {
base.OnMouseWheel(e);
if ((Control.ModifierKeys & Keys.Control) == Keys.Control) {
float f = this._CanvasScale + (e.Delta < 0 ? -0.1f : 0.1f);
this.ScaleCanvas(f, this.Width / 2, this.Height / 2);
} else {
if (!m_mouse_in_control) return;
var nfi = this.FindNodeFromPoint(m_pt_in_canvas);
if (this._HoverNode != null) {
this._HoverNode.OnMouseWheel(new MouseEventArgs(e.Button, e.Clicks,
(int)m_pt_in_canvas.X - this._HoverNode.Left,
(int)m_pt_in_canvas.Y - this._HoverNode.Top, e.Delta));
return;
}
int t = (int)DateTime.Now.Subtract(m_dt_vw).TotalMilliseconds;
if (t <= 30) t = 40;
else if (t <= 100) t = 20;
else if (t <= 150) t = 10;
else if (t <= 300) t = 4;
else t = 2;
this.MoveCanvas(this._CanvasOffsetX, m_real_canvas_y + (e.Delta < 0 ? -t : t), true, CanvasMoveArgs.Top);//process mouse mid
m_dt_vw = DateTime.Now;
}
}
protected virtual void OnMouseHWheel(MouseEventArgs e) {
if ((Control.ModifierKeys & Keys.Control) == Keys.Control) return;
if (!m_mouse_in_control) return;
if (this._HoverNode != null) {
this._HoverNode.OnMouseWheel(new MouseEventArgs(e.Button, e.Clicks,
(int)m_pt_in_canvas.X - this._HoverNode.Left,
(int)m_pt_in_canvas.Y - this._HoverNode.Top, e.Delta));
return;
}
int t = (int)DateTime.Now.Subtract(m_dt_hw).TotalMilliseconds;
if (t <= 30) t = 40;
else if (t <= 100) t = 20;
else if (t <= 150) t = 10;
else if (t <= 300) t = 4;
else t = 2;
this.MoveCanvas(m_real_canvas_x + (e.Delta > 0 ? -t : t), this._CanvasOffsetY, true, CanvasMoveArgs.Left);
m_dt_hw = DateTime.Now;
}
//===========================for node other event==================================
protected override void OnMouseClick(MouseEventArgs e) {
base.OnMouseClick(e);
if (this._ActiveNode != null && m_is_process_mouse_event) {
if (!this.PointInRectangle(this._ActiveNode.Rectangle, m_pt_in_canvas.X, m_pt_in_canvas.Y)) return;
this._ActiveNode.OnMouseClick(new MouseEventArgs(e.Button, e.Clicks,
(int)m_pt_down_in_canvas.X - this._ActiveNode.Left,
(int)m_pt_down_in_canvas.Y - this._ActiveNode.Top, e.Delta));
}
}
protected override void OnKeyDown(KeyEventArgs e) {
base.OnKeyDown(e);
if (this._ActiveNode != null) this._ActiveNode.OnKeyDown(e);
}
protected override void OnKeyUp(KeyEventArgs e) {
base.OnKeyUp(e);
if (this._ActiveNode != null) this._ActiveNode.OnKeyUp(e);
m_node_down = null;
}
protected override void OnKeyPress(KeyPressEventArgs e) {
base.OnKeyPress(e);
if (this._ActiveNode != null) this._ActiveNode.OnKeyPress(e);
}
#endregion
protected override void OnDragEnter(DragEventArgs drgevent) {
base.OnDragEnter(drgevent);
if (this.DesignMode) return;
if (drgevent.Data.GetDataPresent("STNodeType"))
drgevent.Effect = DragDropEffects.Copy;
else
drgevent.Effect = DragDropEffects.None;
}
protected override void OnDragDrop(DragEventArgs drgevent) {
base.OnDragDrop(drgevent);
if (this.DesignMode) return;
if (drgevent.Data.GetDataPresent("STNodeType")) {
object data = drgevent.Data.GetData("STNodeType");
if (!(data is Type)) return;
var t = (Type)data;
if (!t.IsSubclassOf(typeof(STNode))) return;
STNode node = (STNode)Activator.CreateInstance((t));
Point pt = new Point(drgevent.X, drgevent.Y);
pt = this.PointToClient(pt);
pt = this.ControlToCanvas(pt);