forked from DebugST/STNodeEditor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTNodeEditor.cs
More file actions
2318 lines (2162 loc) · 106 KB
/
Copy pathSTNodeEditor.cs
File metadata and controls
2318 lines (2162 loc) · 106 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 //The current mouse movement operation indicates which of the following actions is performed
{
None, //None
MoveNode, //Moving Node
ConnectOption, //Connecting Option
SelectRectangle, //Selecting rectangular area
DrawMarkDetails //Drawing marker information details
}
protected struct MagnetInfo
{
public bool XMatched; //Is there 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 of the current node's X position and the matched 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 effective 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>
/// Get or set the curvature of the lines between Options.
/// </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("Gets or sets whether to enable the magnet effect when moving the 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 moving canvas.
/// </summary>
[Description("Gets or sets whether to display the Node border in the moving 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("Gets or sets 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>
/// Get or set whether to display Node position information beyond the angle of view at the edge of the canvas.
/// </summary>
[Description("Get or set whether to display Node position information beyond the angle of view 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 selected active Node 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 where the mouse is hovering 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("Gets or sets 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>
/// Get or set the border color of 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("Gets or sets 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("Gets or sets 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("Gets or sets 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 for canvas drawing Node mark details.
/// </summary>
[Description("Gets or sets the foreground color used for canvas drawing Node mark 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 for drawing Node mark details on the canvas.
/// </summary>
[Description("Gets or sets the background color used for drawing Node mark details on the canvas.")]
public Color MarkBackColor {
get { return _MarkBackColor; }
set {
_MarkBackColor = value;
this.Invalidate();
}
}
private Color _MagnetColor = Color.Lime;
/// <summary>
/// Get or set the magnet mark color when moving 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 selected rectangular area in the canvas.
/// </summary>
[Description("Gets or sets the color of the selected rectangular area in the canvas."), DefaultValue(typeof(Color), "DodgerBlue")]
public Color SelectedRectangleColor {
get { return _SelectedRectangleColor; }
set { _SelectedRectangleColor = value; }
}
private Color _HighLineColor = Color.Cyan;
/// <summary>
/// Get or set 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>
/// Gets or sets the foreground color of the hint area at the edge of the canvas.
/// </summary>
[Description("Gets or sets the foreground color of the hint area at the edge of 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>
/// Gets or sets the background color of the edge position prompt area in the canvas.
/// </summary>
[Description("Gets or sets the background color of the edge position prompt 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("Gets or sets the color that should be used in the canvas 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 Option data type in Node in the canvas.
/// </summary>
[Browsable(false)]
public Dictionary<Type, Color> TypeColor {
get { return _TypeColor; }
}
private bool mRequireCtrlForZooming = true;
public bool RequireCtrlForZooming
{
get { return mRequireCtrlForZooming; }
set { mRequireCtrlForZooming = value; }
}
private int mRoundedCornerRadius = -1; // -1 Means no rounded corner rendering.
public int RoundedCornerRadius
{
get { return mRoundedCornerRadius; }
set { mRoundedCornerRadius = value; }
}
private bool mAllowNodeGraphLoops = false;
public bool AllowNodeGraphLoops
{
get { return mAllowNodeGraphLoops; }
set { mAllowNodeGraphLoops = value; }
}
private bool mResetViewWhenEmpty = true;
public bool ResetViewWhenEmpty
{
get { return mResetViewWhenEmpty; }
set { mResetViewWhenEmpty = value; }
}
// Added for Basis. Allow connecting an output option of type
// typeof(object) to a properly typed input option.
private bool mAllowUntypedToTypedConnections = false;
public bool AllowUntypedToTypedConnections
{
get { return mAllowUntypedToTypedConnections; }
set { mAllowUntypedToTypedConnections = value; }
}
#endregion
#region protected properties ----------------------------------------------------------------------------------------
/// <summary>
/// The current real-time position of the mouse in the control.
/// </summary>
protected Point m_pt_in_control;
/// <summary>
/// The current real-time position of the 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 to move the canvas when the mouse is clicked, and the coordinate position of the canvas when the mouse is clicked.
/// </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 under the mouse point in the connection process Option When MouseUP determines whether to connect to 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;
private Pen m_pen_border;
private Pen m_pen_border_hover;
private Pen m_pen_border_selected;
private Pen m_pen_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 selected node when the mouse is clicked
private Dictionary<STNode, Point> m_dic_pt_selected = new Dictionary<STNode, Point>();
//Used for magnet effect When moving nodes, the statistics of non-selected nodes need to participate in the coordinates of the magnet effect. View->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 when moving the node, the active selection node counts the coordinates that need to participate in the magnet effect View->CheckMagnet()
private List<int> m_lst_magnet_mx = new List<int>();
private List<int> m_lst_magnet_my = new List<int>();
//It is used to calculate the time trigger interval during mouse scrolling. According to the interval, the displacement produced by the canvas is different. View->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 mouse-related events downwards (Node or NodeControls), such as disconnection-related operations should not be passed downwards
private bool m_is_buildpath; //Used to determine whether to re-establish the cache connection path during the redrawing 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 hovering
private GraphicsPath m_gp_hover; //The current connection path where the mouse is hovering
private StringFormat m_sf = new StringFormat(); //Text format Used to set the text format when Mark draws
//Save the node relationship corresponding to each connecting line
private Dictionary<GraphicsPath, ConnectionInfo> m_dic_gp_info = new Dictionary<GraphicsPath, ConnectionInfo>();
//Save the position of the Node beyond the visual area
private List<Point> m_lst_node_out = new List<Point>();
//The Node type loaded in 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 hovering node changes.
/// </summary>
[Description("Occurs when the hovering 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 a node is added.
/// </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 the origin of the canvas is moved.")]
public event EventHandler CanvasMoved;
/// <summary>
/// Occurs when the canvas is zoomed.
/// </summary>
[Description("Occurs when the canvas is zoomed.")]
public event EventHandler CanvasZoomed;
/// <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 is disconnected.
/// </summary>
[Description("Occurs when the node is disconnected.")]
public event STNodeEditorOptionEventHandler OptionDisconnected;
/// <summary>
/// Occurs when the node option is being disconnected.
/// </summary>
[Description("Occurs when the node option is being disconnected.")]
public event STNodeEditorOptionEventHandler OptionDisconnecting;
/// <summary>
/// Occurs when one or more nodes have been moved around the canvas.
/// </summary>
[Description("Occurs when one or more nodes have been moved around the canvas.")]
public event STNodesMovedEventHandler NodesMoved;
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 OnCanvasZoomed(EventArgs e) {
if (this.CanvasZoomed != null) this.CanvasZoomed(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);
}
protected internal virtual void OnNodesMoved(STNodesMovedEventArgs e) {
if (this.NodesMoved != null) this.NodesMoved(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);
m_pen_border = new Pen(new SolidBrush(Color.FromArgb(50, this._BorderColor)), 4.0f);
m_pen_border_active = new Pen(new SolidBrush(Color.FromArgb(150, this._BorderActiveColor)), 2.0f);
m_pen_border_hover = new Pen(new SolidBrush(Color.FromArgb(150, this._BorderHoverColor)), 2.0f);
m_pen_border_selected = new Pen(new SolidBrush(Color.FromArgb(150, this._BorderSelectedColor)), 2.0f);
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 {
if (m.Msg == WM_MOUSEHWHEEL) { //Get horizontal scrolling message
Point pt = new Point(((int)m.LParam) >> 16, (ushort)m.LParam);
pt = this.PointToClient(pt);
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 you are 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 that other than the nodes, the decoration related drawing should not be drawn in the Canvas coordinate system, but the coordinates of the control should be used for drawing, 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 marker 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 MouseClick and MouseUp to pass down
return;
}
NodeFindInfo nfi = this.FindNodeFromPoint(m_pt_down_in_canvas);
if (!string.IsNullOrEmpty(nfi.Mark)) { //If the click is marked information
m_ca = CanvasAction.DrawMarkDetails;
this.Invalidate();
return;
}
if (nfi.NodeOption != null) { //If you click the option connection 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));
if (e.Button == MouseButtons.Left)
{
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 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. It will be useful if you need to move the selected node.
m_dic_pt_selected.Add(n, n.Location);
}
m_ca = CanvasAction.MoveNode; //If you click the title of the node, you can move the node
if (this._ShowMagnet && this._ActiveNode != null) this.BuildMagnetLocation(); //It will be useful to establish the coordinates required for the magnet if you need to move the selected node
}
}
else
m_node_down = nfi.Node;
}
} else {
if (e.Button == MouseButtons.Left)
{
this.SetActiveNode(null);
foreach (var n in m_hs_node_selected.ToArray()) n.SetSelected(false, false);//Did not click anything to clear the selected node
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 (e.Button == MouseButtons.Middle)
{ //Move the canvas with the middle mouse button
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 (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.Left) { //If the left mouse button is clicked, the behavior is judged
m_gp_hover = null;
switch (m_ca) {
case CanvasAction.MoveNode: this.MoveNode(e.Location); return; //Current mobile node
case CanvasAction.ConnectOption: this.Invalidate(); return; //Currently connecting
case CanvasAction.SelectRectangle: //Currently 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, judge whether there are other objects under the mouse
NodeFindInfo nfi = this.FindNodeFromPoint(m_pt_in_canvas);
bool bRedraw = false;
if (this._HoverNode != nfi.Node) { //Hover 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 is hovering 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) { //Judging behavior when the mouse is raised
case CanvasAction.MoveNode: //If you are moving Node, send NodesMoved event and re-record the current position
{
List<NodeMovement> movements = new List<NodeMovement>();
bool didNodesReallyMove = false;
foreach (STNode n in m_dic_pt_selected.Keys.ToList())
{
NodeMovement movement = new NodeMovement();
movement.Node = n;
movement.OldLocation = m_dic_pt_selected[n];
movement.NewLocation = n.Location;
movements.Add(movement);
if (movement.OldLocation != movement.NewLocation)
didNodesReallyMove = true;
}
if (didNodesReallyMove)
OnNodesMoved(new STNodesMovedEventArgs(movements.ToArray()));
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; //The current disconnection operation does not carry out event delivery, 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);
bool zoomInput = !mRequireCtrlForZooming || ((Control.ModifierKeys & Keys.Control) == Keys.Control);
if (zoomInput) {
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;