forked from codeurzebs/CodeWalker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTNodeEditor.cs
More file actions
2109 lines (1990 loc) · 97.2 KB
/
STNodeEditor.cs
File metadata and controls
2109 lines (1990 loc) · 97.2 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;
/*
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 : Control
{
private const UInt32 WM_MOUSEHWHEEL = 0x020E;
protected static readonly Type m_type_node = typeof(STNode);
#region protected enum,struct --------------------------------------------------------------------------------------
protected enum CanvasAction //Which of the following actions is performed by the current mouse movement operation
{
None, //None
MoveNode, // is moving Node
ConnectOption, //Connecting Option
SelectRectangle, // is selecting a rectangular area
DrawMarkDetails //Drawing mark information details
}
protected struct MagnetInfo
{
public bool XMatched; //Whether there is a magnet matching on the X axis
public bool YMatched;
public int X; //match the number on the X axis
public int Y;
public int OffsetX; //The relative offset between the current node X position and the matching X
public int OffsetY;
}
#endregion
#region Properties ------------------------------------------------------------------------------------------------------
private float _CanvasOffsetX;
/// <summary>
/// Get the offset position of the canvas origin relative to the X direction of the control
/// </summary>
[Browsable(false)]
public float CanvasOffsetX {
get { return _CanvasOffsetX; }
}
private float _CanvasOffsetY;
/// <summary>
/// Get the offset position of the canvas origin relative to the Y direction of the control
/// </summary>
[Browsable(false)]
public float CanvasOffsetY {
get { return _CanvasOffsetY; }
}
private PointF _CanvasOffset;
/// <summary>
/// Get the offset position of the canvas origin relative to the control
/// </summary>
[Browsable(false)]
public PointF CanvasOffset {
get {
_CanvasOffset.X = _CanvasOffsetX;
_CanvasOffset.Y = _CanvasOffsetY;
return _CanvasOffset;
}
}
private Rectangle _CanvasValidBounds;
/// <summary>
/// Get the valid area in the canvas that is used
/// </summary>
[Browsable(false)]
public Rectangle CanvasValidBounds {
get { return _CanvasValidBounds; }
}
private float _CanvasScale = 1;
/// <summary>
/// Get the zoom ratio of the canvas
/// </summary>
[Browsable(false)]
public float CanvasScale {
get { return _CanvasScale; }
}
private float _Curvature = 0.3F;
/// <summary>
/// Gets or sets the curvature of the connection between 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>
/// Gets or sets whether to enable the magnet effect when moving the Node in the canvas
/// </summary>
[Description("Get or set whether to enable the magnet effect when moving Node in the canvas"), DefaultValue(true)]
public bool ShowMagnet {
get { return _ShowMagnet; }
set { _ShowMagnet = value; }
}
private bool _ShowBorder = true;
/// <summary>
/// Gets or sets whether to display the Node border in the mobile canvas
/// </summary>
[Description("Get or set whether to display the Node border in the mobile canvas"), DefaultValue(true)]
public bool ShowBorder {
get { return _ShowBorder; }
set {
_ShowBorder = value;
this.Invalidate();
}
}
private bool _ShowGrid = true;
/// <summary>
/// Gets or sets whether to draw background grid lines in the canvas
/// </summary>
[Description("Get or set whether to draw background grid lines in the canvas"), DefaultValue(true)]
public bool ShowGrid {
get { return _ShowGrid; }
set {
_ShowGrid = value;
this.Invalidate();
}
}
private bool _ShowLocation = true;
/// <summary>
/// Gets or sets whether to display Node position information beyond the viewing angle at the edge of the canvas
/// </summary>
[Description("Get or set whether to display Node position information beyond the viewing angle at the edge of the canvas"), DefaultValue(true)]
public bool ShowLocation {
get { return _ShowLocation; }
set {
_ShowLocation = value;
this.Invalidate();
}
}
private STNodeCollection _Nodes;
/// <summary>
/// Get the Node collection in the canvas
/// </summary>
[Browsable(false)]
public STNodeCollection Nodes {
get {
return _Nodes;
}
}
private STNode _ActiveNode;
/// <summary>
/// Get the active Node selected in the current canvas
/// </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>
/// Get the Node that the mouse hovers over in the current canvas
/// </summary>
[Browsable(false)]
public STNode HoverNode {
get { return _HoverNode; }
}
//========================================color================================
private Color _GridColor = Color.Black;
/// <summary>
/// Gets or sets the grid line color when drawing the canvas background
/// </summary>
[Description("Get or set the grid line color when drawing the canvas background"), DefaultValue(typeof(Color), "Black")]
public Color GridColor {
get { return _GridColor; }
set {
_GridColor = value;
this.Invalidate();
}
}
private Color _BorderColor = Color.Black;
/// <summary>
/// Gets or sets the border color of the Node in the canvas
/// </summary>
[Description("Get or set the border color of Node in the canvas"), 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>
/// Gets or sets the border color of the hovering Node in the canvas
/// </summary>
[Description("Get or set the border color of the hovering Node in the canvas"), 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>
/// Gets or sets the border color of the selected Node in the canvas
/// </summary>
[Description("Get or set the border color of the selected Node in the canvas"), 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>
/// Gets or sets the border color of the active Node in the canvas
/// </summary>
[Description("Get or set the border color of the active Node in the canvas"), 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>
/// Gets or sets the foreground color used by the canvas to draw the Node tag details
/// </summary>
[Description("Get or set the foreground color of the canvas drawing Node tag details"), DefaultValue(typeof(Color), "White")]
public Color MarkForeColor {
get { return _MarkBackColor; }
set {
_MarkBackColor = value;
this.Invalidate();
}
}
private Color _MarkBackColor = Color.FromArgb(180, Color.Black);
/// <summary>
/// Gets or sets the background color used by the canvas to draw the Node tag details
/// </summary>
[Description("Get or set the background color used for drawing Node tag details on canvas")]
public Color MarkBackColor {
get { return _MarkBackColor; }
set {
_MarkBackColor = value;
this.Invalidate();
}
}
private Color _MagnetColor = Color.Lime;
/// <summary>
/// Gets or sets the color of the magnet marker when moving the Node in the canvas
/// </summary>
[Description("Get or set the magnet mark color when moving Node in the canvas"), DefaultValue(typeof(Color), "Lime")]
public Color MagnetColor {
get { return _MagnetColor; }
set { _MagnetColor = value; }
}
private Color _SelectedRectangleColor = Color.DodgerBlue;
/// <summary>
/// Gets or sets the color of the selection rectangle in the canvas
/// </summary>
[Description("Get or set the color of the selection rectangle in the canvas"), DefaultValue(typeof(Color), "DodgerBlue")]
public Color SelectedRectangleColor {
get { return _SelectedRectangleColor; }
set { _SelectedRectangleColor = value; }
}
private Color _HighLineColor = Color.Cyan;
/// <summary>
/// Gets or sets the color of the highlighted line in the canvas
/// </summary>
[Description("Get or set the color of the highlighted line in the canvas"), DefaultValue(typeof(Color), "Cyan")]
public Color HighLineColor {
get { return _HighLineColor; }
set { _HighLineColor = value; }
}
private Color _LocationForeColor = Color.Red;
/// <summary>
/// Get or set the foreground color of the edge position hint area in the canvas
/// </summary>
[Description("Get or set the foreground color of the edge position hint area in the canvas"), DefaultValue(typeof(Color), "Red")]
public Color LocationForeColor {
get { return _LocationForeColor; }
set {
_LocationForeColor = value;
this.Invalidate();
}
}
private Color _LocationBackColor = Color.FromArgb(120, Color.Black);
/// <summary>
/// Get or set the background color of the edge position hint area in the canvas
/// </summary>
[Description("Get or set the background color of the edge position hint area in the canvas")]
public Color LocationBackColor {
get { return _LocationBackColor; }
set {
_LocationBackColor = value;
this.Invalidate();
}
}
private Color _UnknownTypeColor = Color.Gray;
/// <summary>
/// Gets or sets the color that should be used in the canvas when the Option data type in Node cannot be determined
/// </summary>
[Description("Get or set the color that should be used when the Option data type in Node cannot be determined"), 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>
/// Get or set the preset color of the Option data type in the Node in the canvas
/// </summary>
[Browsable(false)]
public Dictionary<Type, Color> TypeColor {
get { return _TypeColor; }
}
#endregion
#region protected properties ----------------------------------------------------------------------------------------
/// <summary>
/// The real-time position of the current mouse in the control
/// </summary>
protected Point m_pt_in_control;
/// <summary>
/// The real-time position of the current mouse in the canvas
/// </summary>
protected PointF m_pt_in_canvas;
/// <summary>
/// The position on the control when the mouse is clicked
/// </summary>
protected Point m_pt_down_in_control;
/// <summary>
/// The position in the canvas when the mouse is clicked
/// </summary>
protected PointF m_pt_down_in_canvas;
/// <summary>
/// Used for the coordinate position of the canvas when the mouse is clicked when the mouse is clicked to move the canvas
/// </summary>
protected PointF m_pt_canvas_old;
/// <summary>
/// Used to save the starting point coordinates of the Option under the save point during the connection process
/// </summary>
protected Point m_pt_dot_down;
/// <summary>
/// Used to save the starting point of the mouse click during the connection process. Option When MouseUP determines whether to connect this node
/// </summary>
protected STNodeOption m_option_down;
/// <summary>
/// STNode under the current mouse click
/// </summary>
protected STNode m_node_down;
/// <summary>
/// Whether the current mouse is in the control
/// </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 NodeFindInfo m_find = new NodeFindInfo();
private MagnetInfo m_mi = new MagnetInfo ();
private RectangleF m_rect_select = new RectangleF();
//Node border preset pattern
private Image m_img_border;
private Image m_img_border_hover;
private Image m_img_border_selected;
private Image m_img_border_active;
//Used for the animation effect when the mouse scrolls or the touchpad moves the canvas. This value is the real coordinate address that needs to be moved to view ->MoveCanvasThread()
private float m_real_canvas_x;
private float m_real_canvas_y;
//Used to save the initial coordinates of the node selected when the mouse is clicked when moving the node
private Dictionary<STNode, Point> m_dic_pt_selected = new Dictionary<STNode, Point>();
//For the magnet effect to move the node, the statistics of the non-selected nodes need to participate in the coordinate view of the magnet effect ->BuildMagnetLocation()
private List<int> m_lst_magnet_x = new List<int>();
private List<int> m_lst_magnet_y = new List<int>();
//Used for the magnet effect to move the node when the active selection node is counted to check the coordinates that need to participate in the magnet effect ->CheckMagnet()
private List<int> m_lst_magnet_mx = new List<int>();
private List<int> m_lst_magnet_my = new List<int>();
//Used to calculate the time trigger interval in mouse scrolling. View the displacement generated by different canvases according to the interval -> OnMouseWheel(), OnMouseHWheel()
private DateTime m_dt_vw = DateTime.Now;
private DateTime m_dt_hw = DateTime.Now;
// current behavior during mouse movement
private CanvasAction m_ca;
// save the selected node
private HashSet<STNode> m_hs_node_selected = new HashSet<STNode>();
private bool m_is_process_mouse_event = true; //Whether to pass down (Node or NodeControls) mouse related events such as disconnection related operations should not pass down
private bool m_is_buildpath; //The path used to determine whether to re-establish the cache connection this time during the redraw process
private Pen m_p_line = new Pen(Color.Cyan, 2f); // used to draw connected lines
private Pen m_p_line_hover = new Pen(Color.Cyan, 4f); //Used to draw the line when the mouse is hovered
private GraphicsPath m_gp_hover; //The path of the current mouse hover
private StringFormat m_sf = new StringFormat(); //The text format is used to set the text format when Mark draws
//Save the node relationship corresponding to each connection line
private Dictionary<GraphicsPath, ConnectionInfo> m_dic_gp_info = new Dictionary<GraphicsPath, ConnectionInfo>();
//Save the position of Node beyond the visual area
private List<Point> m_lst_node_out = new List<Point>();
//The Node type loaded by the current editor is used to load nodes from files or data
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>
/// Occurs when the active node changes
/// </summary>
[Description("Occurs when the active node changes")]
public event EventHandler ActiveChanged;
/// <summary>
/// Occurs when the selected node changes
/// </summary>
[Description("Occurs when the selected node changes")]
public event EventHandler SelectedChanged;
/// <summary>
/// Occurs when the hovered node changes
/// </summary>
[Description("Occurs when the hovered node changes")]
public event EventHandler HoverChanged;
/// <summary>
/// Occurs when a node is added
/// </summary>
[Description("Occurs when a node is added")]
public event STNodeEditorEventHandler NodeAdded;
/// <summary>
/// Occurs when the node is removed
/// </summary>
[Description("Occurs when a node is removed")]
public event STNodeEditorEventHandler NodeRemoved;
/// <summary>
/// Occurs when the origin of the canvas is moved
/// </summary>
[Description("Occurs when moving the origin of the canvas")]
public event EventHandler CanvasMoved;
/// <summary>
/// Occurs when the canvas is zoomed
/// </summary>
[Description("Occurs when zooming the canvas")]
public event EventHandler CanvasScaled;
/// <summary>
/// Occurs when connecting node options
/// </summary>
[Description("Occurs when connecting node options")]
public event STNodeEditorOptionEventHandler OptionConnected;
/// <summary>
/// Occurs when connecting node options
/// </summary>
[Description("Occurs when connecting node options")]
public event STNodeEditorOptionEventHandler OptionConnecting;
/// <summary>
/// Occurs when the node option is disconnected
/// </summary>
[Description("Occurs when the node option is disconnected")]
public event STNodeEditorOptionEventHandler OptionDisConnected;
/// <summary>
/// Occurs when disconnecting node options
/// </summary>
[Description("Occurs when disconnecting node options")]
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) { //Get the horizontal scrolling message
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 OnPaint(PaintEventArgs e) {
base.OnPaint(e);
Graphics g = e.Graphics;
g.Clear(this.BackColor);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
m_drawing_tools.Graphics = g;
SolidBrush brush = m_drawing_tools.SolidBrush;
if (this._ShowGrid) this.OnDrawGrid(m_drawing_tools, this.Width, this.Height);
g.TranslateTransform(this._CanvasOffsetX, this._CanvasOffsetY); //Move the coordinate system
g.ScaleTransform(this._CanvasScale, this._CanvasScale); //Scale the drawing surface
this.OnDrawConnectedLine(m_drawing_tools);
this.OnDrawNode(m_drawing_tools, this.ControlToCanvas(this.ClientRectangle));
if (m_ca == CanvasAction.ConnectOption) { //If connecting
m_drawing_tools.Pen.Color = this._HighLineColor;
g.SmoothingMode = SmoothingMode.HighQuality;
if (m_option_down.IsInput)
this.DrawBezier(g, m_drawing_tools.Pen, m_pt_in_canvas, m_pt_dot_down, this._Curvature);
else
this.DrawBezier(g, m_drawing_tools.Pen, m_pt_dot_down, m_pt_in_canvas, this._Curvature);
}
//Reset the drawing coordinates I think other decoration-related drawing other than nodes should not be drawn in the Canvas coordinate system but should be drawn using the coordinates of the control, otherwise it will be affected by the zoom ratio
g.ResetTransform();
switch (m_ca) {
case CanvasAction.MoveNode: //Draw alignment guides during movement
if (this._ShowMagnet && this._ActiveNode != null) this.OnDrawMagnet(m_drawing_tools, m_mi);
break;
case CanvasAction.SelectRectangle: //Draw rectangle selection
this.OnDrawSelectedRectangle(m_drawing_tools, this.CanvasToControl(m_rect_select));
break;
case CanvasAction.DrawMarkDetails: //Draw mark information details
if (!string.IsNullOrEmpty(m_find.Mark)) this.OnDrawMark(m_drawing_tools);
break;
}
if (this._ShowLocation) this.OnDrawNodeOutLocation(m_drawing_tools, this.Size, m_lst_node_out);
this.OnDrawAlert(g);
}
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) { //Disconnect
this.DisConnectionHover();
m_is_process_mouse_event = false; // Terminate the downward transmission of MouseClick and MouseUp
return;
}
NodeFindInfo nfi = this.FindNodeFromPoint(m_pt_down_in_canvas);
if (!string.IsNullOrEmpty(nfi.Mark)) { //If the point is marked information
m_ca = CanvasAction.DrawMarkDetails;
this.Invalidate();
return;
}
if (nfi.NodeOption != null) { //If the connection point of the Option under the point
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); //Add to the selected node
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) //Record the position of the selected node, which will be useful if you need to move the selected node
m_dic_pt_selected.Add(n, n.Location);
}
m_ca = CanvasAction.MoveNode; //If the title of the node is under the point, the node can be moved
if (this._ShowMagnet && this._ActiveNode != null) this.BuildMagnetLocation(); //The coordinates needed to build the magnet will be useful if you need to move the selected node
}
} else
m_node_down = nfi.Node;
} else {
this.SetActiveNode(null);
foreach (var n in m_hs_node_selected.ToArray()) n.SetSelected(false, false);//Empty the selected node without clicking anything
m_ca = CanvasAction.SelectRectangle; //Enter rectangular area selection mode
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) { //The middle mouse button moves the canvas
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) { //If the left mouse button is clicked to judge the behavior
m_gp_hover = null;
switch (m_ca) {
case CanvasAction.MoveNode: this.MoveNode(e.Location); return; //The current moving node
case CanvasAction.ConnectOption: this.Invalidate(); return; //Currently connecting
case CanvasAction.SelectRectangle: //Currently being selected
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;
}
}
//If there is no behavior, determine whether there are other objects under the mouse
NodeFindInfo nfi = this.FindNodeFromPoint(m_pt_in_canvas);
bool bRedraw = false;
if (this._HoverNode != nfi.Node) { //Mouse over 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) { //Determine whether the mouse hovers over the connection path
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) { //Judgment behavior when the mouse is raised
case CanvasAction.MoveNode: //If the Node is being moved, re-record the current position
foreach (STNode n in m_dic_pt_selected.Keys.ToList()) m_dic_pt_selected[n] = n.Location;
break;
case CanvasAction.ConnectOption: //If it is connecting, end the connection
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; //Currently, no event delivery is performed for the disconnection operation, and the event will be accepted next time
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);
node.Left = pt.X; node.Top = pt.Y;
this.Nodes.Add(node);
}
}
#region protected ----------------------------------------------------------------------------------------------------
/// <summary>
/// Occurs when the background grid lines are drawn
/// </summary>
/// <param name="dt">Drawing tool</param>
/// <param name="nWidth">Need to draw width</param>
/// <param name="nHeight">Need to draw height</param>
protected virtual void OnDrawGrid(DrawingTools dt, int nWidth, int nHeight) {
Graphics g = dt.Graphics;
using (Pen p_2 = new Pen(Color.FromArgb(65, this._GridColor))) {
using (Pen p_1 = new Pen(Color.FromArgb(30, this._GridColor))) {
float nIncrement = (20 * this._CanvasScale); //The interval between grids is drawn according to the scale
int n = 5 - (int)(this._CanvasOffsetX / nIncrement);
for (float f = this._CanvasOffsetX % nIncrement; f < nWidth; f += nIncrement)
g.DrawLine((n++ % 5 == 0 ? p_2 : p_1), f, 0, f, nHeight);
n = 5 - (int)(this._CanvasOffsetY / nIncrement);
for (float f = this._CanvasOffsetY % nIncrement; f < nHeight; f += nIncrement)
g.DrawLine((n++ % 5 == 0 ? p_2 : p_1), 0, f, nWidth, f);
// two antennas at the origin
p_1.Color = Color.FromArgb(this._Nodes.Count == 0 ? 255 : 120, this._GridColor);
g.DrawLine(p_1, this._CanvasOffsetX, 0, this._CanvasOffsetX, nHeight);
g.DrawLine(p_1, 0, this._CanvasOffsetY, nWidth, this._CanvasOffsetY);
}
}
}
/// <summary>
/// Occurs when the Node is drawn