-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathSTNodePropertyGrid.cs
More file actions
767 lines (720 loc) · 33.7 KB
/
STNodePropertyGrid.cs
File metadata and controls
767 lines (720 loc) · 33.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Reflection;
using System.Windows.Forms;
using System.ComponentModel;
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: 2021-01-28
* modify: 2021-03-02
* Author: Crystal_lz
* blog: http://st233.com
* Gitee: https://gitee.com/DebugST
* Github: https://github.com/DebugST
*/
namespace ST.Library.UI.NodeEditor
{
/// <summary>
/// STNode节点属性编辑器
/// </summary>
public class STNodePropertyGrid : SKControl
{
#region properties ==========
private STNode _STNode;
/// <summary>
/// 当前显示的STNode
/// </summary>
[Description("当前显示的STNode"), Browsable(false)]
public STNode STNode {
get { return _STNode; }
}
private Color _ItemHoverColor = Color.FromArgb(50, 125, 125, 125);
/// <summary>
/// 获取或设置属性选项被鼠标悬停时候背景色
/// </summary>
[Description("获取或设置属性选项被鼠标悬停时候背景色")]
public Color ItemHoverColor {
get { return _ItemHoverColor; }
set { _ItemHoverColor = value; }
}
private Color _ItemSelectedColor = Color.DodgerBlue;
/// <summary>
/// 获取或设置属性选项被选中时候背景色 当AutoColor被设置时此属性不能被设置
/// </summary>
[Description("获取或设置属性选项被选中时候背景色 当AutoColor被设置时此属性不能被设置"), DefaultValue(typeof(Color), "DodgerBlue")]
public Color ItemSelectedColor {
get { return _ItemSelectedColor; }
set {
if (this._AutoColor) return;
if (value == _ItemSelectedColor) return;
_ItemSelectedColor = value;
this.Invalidate();
}
}
private Color _ItemValueBackColor = Color.FromArgb(255, 80, 80, 80);
/// <summary>
/// 获取或设置属性选项值背景色
/// </summary>
[Description("获取或设置属性选项值背景色")]
public Color ItemValueBackColor {
get { return _ItemValueBackColor; }
set {
_ItemValueBackColor = value;
this.Invalidate();
}
}
private Color _TitleColor = Color.FromArgb(127, 0, 0, 0);
/// <summary>
/// 获取或设置默认标题背景色
/// </summary>
[Description("获取或设置默认标题背景色")]
public Color TitleColor {
get { return _TitleColor; }
set {
_TitleColor = value;
if (!this._ShowTitle) return;
this.Invalidate(m_rect_title);
}
}
private Color _ErrorColor = Color.FromArgb(200, Color.Brown);
/// <summary>
/// 获取或设置属性设置错误时候提示信息背景色
/// </summary>
[Description("获取或设置属性设置错误时候提示信息背景色")]
public Color ErrorColor {
get { return _ErrorColor; }
set { _ErrorColor = value; }
}
private Color _DescriptionColor = Color.FromArgb(200, Color.DarkGoldenrod);
/// <summary>
/// 获取或设置属性描述信息背景色
/// </summary>
[Description("获取或设置属性描述信息背景色")]
public Color DescriptionColor {
get { return _DescriptionColor; }
set { _DescriptionColor = value; }
}
private bool _ShowTitle = true;
/// <summary>
/// 获取或设置是否显示节点标题
/// </summary>
[Description("获取或设置是否显示节点标题")]
public bool ShowTitle {
get { return _ShowTitle; }
set {
_ShowTitle = value;
this.SetItemRectangle();
this.Invalidate();
}
}
private bool _AutoColor = true;
/// <summary>
/// 获取或设置是否根据STNode自动设置控件高亮颜色
/// </summary>
[Description("获取或设置是否根据STNode自动设置控件高亮颜色"), DefaultValue(true)]
public bool AutoColor {
get { return _AutoColor; }
set { _AutoColor = value; }
}
private bool _InfoFirstOnDraw;
/// <summary>
/// 获取或当节点被设置时候 是否优先绘制信息面板
/// </summary>
[Description("获取或设置当节点被设置时候 是否优先绘制信息面板"), DefaultValue(false)]
public bool InfoFirstOnDraw {
get { return _InfoFirstOnDraw; }
set { _InfoFirstOnDraw = value; }
}
private bool _ReadOnlyModel;
/// <summary>
/// 获取或设置当前属性编辑器是否处于只读模式
/// </summary>
[Description("获取或设置当前属性编辑器是否处于只读模式"), DefaultValue(false)]
public bool ReadOnlyModel {
get { return _ReadOnlyModel; }
set {
if (value == _ReadOnlyModel) return;
_ReadOnlyModel = value;
this.Invalidate(m_rect_title);
}
}
/// <summary>
/// 获取当前滚动条高度
/// </summary>
[Description("获取当前滚动条高度")]
public int ScrollOffset { get { return m_nOffsetY; } }
#endregion
#region protected fields ==========
/// <summary>
/// 作者链接地址区域
/// </summary>
protected Rectangle m_rect_link;
/// <summary>
/// 查看帮助按钮区域
/// </summary>
protected Rectangle m_rect_help;
/// <summary>
/// 编辑器标题区域
/// </summary>
protected Rectangle m_rect_title;
/// <summary>
/// 面板切换按钮区域
/// </summary>
protected Rectangle m_rect_switch;
/// <summary>
/// 控件在绘制过程中使用的垂直滚动偏移
/// </summary>
protected int m_nOffsetY;
/// <summary>
/// 保存的信息面板垂直滚动偏移
/// </summary>
protected int m_nInfoOffsetY;
/// <summary>
/// 保存的属性面板垂直滚动偏移
/// </summary>
protected int m_nPropertyOffsetY;
/// <summary>
/// 控件在绘制过程中使用的绘图区域总高度
/// </summary>
protected int m_nVHeight;
/// <summary>
/// 保存的信息面板需要的总高度
/// </summary>
protected int m_nInfoVHeight;
/// <summary>
/// 保存的属性面板需要的总高度
/// </summary>
protected int m_nPropertyVHeight;
/// <summary>
/// 信息面板中Key显示需要的水平宽度
/// </summary>
protected int m_nInfoLeft;
#endregion
private Type m_type;
private string[] m_KeysString = new string[] { "作者", "邮箱", "链接", "查看帮助" };
private int m_nTitleHeight = 20;
private int m_item_height = 30;
private Color m_clr_item_1 = Color.FromArgb(10, 0, 0, 0);
private Color m_clr_item_2 = Color.FromArgb(10, 255, 255, 255);
//所有属性列表保存在此List中
private List<STNodePropertyDescriptor> m_lst_item = new List<STNodePropertyDescriptor>();
private STNodePropertyDescriptor m_item_hover; //当前被鼠标悬停的选项
private STNodePropertyDescriptor m_item_hover_value; //当前值区域被鼠标悬停的选项
private STNodePropertyDescriptor m_item_down_value; //当前值区域被鼠标点击的选项
private STNodePropertyDescriptor m_item_selected; //当前选中的选项
private STNodeAttribute m_node_attribute; //节点参数信息
private bool m_b_hover_switch; //是否鼠标悬停在面板切换按钮上
private bool m_b_current_draw_info; //当前绘制的时候是信息面板
private Point m_pt_move; //鼠标在控件上的实时坐标
private Point m_pt_down; //上次鼠标在控件上点下的坐标
private string m_str_err; //当被设置时 绘制错误信息
private string m_str_desc; //当被设置时 绘制描述信息
private Pen m_pen;
private SolidBrush m_brush;
private StringFormat m_sf;
private DrawingTools m_dt;
private SKCanvas m_canvas;
/// <summary>
/// 构造一个节点属性编辑器
/// </summary>
public STNodePropertyGrid() {
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);
m_pen = new Pen(Color.Black, 1);
m_brush = new SolidBrush(Color.Black);
m_sf = new StringFormat();
m_sf.LineAlignment = StringAlignment.Center;
m_sf.FormatFlags = StringFormatFlags.NoWrap;
m_dt.Pen = m_pen;
m_dt.SolidBrush = m_brush;
this.ForeColor = Color.White;
this.BackColor = Color.FromArgb(255, 35, 35, 35);
this.MinimumSize = new Size(120, 50);
this.Size = new Size(200, 150);
}
#region private method ==========
private List<STNodePropertyDescriptor> GetProperties(STNode node) {
List<STNodePropertyDescriptor> lst = new List<STNodePropertyDescriptor>();
if (node == null) return lst;
Type t = node.GetType();
foreach (var p in t.GetProperties()) {
var attrs = p.GetCustomAttributes(true);
foreach (var a in attrs) {
if (!(a is STNodePropertyAttribute)) continue;
var attr = a as STNodePropertyAttribute;
object obj = Activator.CreateInstance(attr.DescriptorType);
if (!(obj is STNodePropertyDescriptor))
throw new ArgumentException("[STNodePropertyAttribute.DescriptorType]参数值必须为[STNodePropertyDescriptor]或者其子类的类型");
var desc = (STNodePropertyDescriptor)Activator.CreateInstance(attr.DescriptorType);
desc.Node = node;
desc.Name = attr.Name;
desc.Description = attr.Description;
desc.PropertyInfo = p;
desc.Control = this;
lst.Add(desc);
}
}
return lst;
}
private STNodeAttribute GetNodeAttribute(STNode node) {
if (node == null) return null;
Type t = node.GetType();
foreach (var v in t.GetCustomAttributes(true)) {
if (!(v is STNodeAttribute)) continue;
return (STNodeAttribute)v;
}
return null;
}
private void SetItemRectangle() {
int nw_p = 0, nw_h = 0;
using (var paint = new SKPaint { TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true }) {
foreach (var v in m_lst_item) {
float w = paint.MeasureText(v.Name ?? string.Empty);
if (w > nw_p) nw_p = (int)Math.Ceiling(w);
}
for (int i = 0; i < m_KeysString.Length - 1; i++) {
float w = paint.MeasureText(m_KeysString[i] ?? string.Empty);
if (w > nw_h) nw_h = (int)Math.Ceiling(w);
}
}
nw_p += 5; nw_h += 5;
nw_p = Math.Min(nw_p, this.Width >> 1);
m_nInfoLeft = Math.Min(nw_h, this.Width >> 1);
int nTitleHeight = this._ShowTitle ? m_nTitleHeight : 0;
for (int i = 0; i < m_lst_item.Count; i++) {
STNodePropertyDescriptor item = m_lst_item[i];
Rectangle rect = new Rectangle(0, i * m_item_height + nTitleHeight, this.Width, m_item_height);
item.Rectangle = rect;
rect.Width = nw_p;
item.RectangleL = rect;
rect.X = rect.Right;
rect.Width = this.Width - rect.Left - 1;
rect.Inflate(-4, -4);
item.RectangleR = rect;
item.OnSetItemLocation();
}
m_nPropertyVHeight = m_lst_item.Count * m_item_height;
if (this._ShowTitle) m_nPropertyVHeight += m_nTitleHeight;
}
#endregion
#region override ==========
/// <summary>
/// 当控件重绘时候发生
/// </summary>
/// <param name="e">事件参数</param>
protected override void OnPaintSurface(SKPaintSurfaceEventArgs e) {
base.OnPaintSurface(e);
m_canvas = e.Surface.Canvas;
m_canvas.Clear(SkiaDrawingHelper.ToSKColor(this.BackColor));
if (this._InfoFirstOnDraw) {
this.OnDrawInfo(m_dt);
} else {
for (int i = 0; i < m_lst_item.Count; i++) this.OnDrawPropertyItem(m_dt, m_lst_item[i], i);
}
if (this._ShowTitle) this.OnDrawTitle(m_dt);
if (!string.IsNullOrEmpty(m_str_err)) this.OnDrawErrorInfo(m_dt);
if (!string.IsNullOrEmpty(m_str_desc)) this.OnDrawDescription(m_dt);
m_canvas = null;
}
/// <summary>
/// 当鼠标在控件上移动时候发生
/// </summary>
/// <param name="e">事件参数</param>
protected override void OnMouseMove(MouseEventArgs e) {
base.OnMouseMove(e);
m_pt_move = e.Location;
bool bHover = this._ShowTitle && m_rect_switch.Contains(e.Location);
if (bHover != m_b_hover_switch) {
m_b_hover_switch = bHover;
this.Invalidate(m_rect_switch);
}
Point pt = new Point(e.X, e.Y - (int)m_nOffsetY);
MouseEventArgs mea = new MouseEventArgs(e.Button, e.Clicks, pt.X, pt.Y, e.Delta);
if (m_b_current_draw_info)
this.OnProcessHelpMouseMove(mea);
else
this.OnProcessPropertyMouseMove(mea);
}
/// <summary>
/// 当鼠标在控件上点下时候发生
/// </summary>
/// <param name="e">事件参数</param>
protected override void OnMouseDown(MouseEventArgs e) {
base.OnMouseDown(e);
m_pt_down = e.Location;
this.Focus();
bool bRedraw = false;
if (m_str_err != null) {
bRedraw = true;
m_str_err = null;
}
if (this._ShowTitle) {
if (m_rect_switch.Contains(e.Location)) {
if (m_node_attribute == null || m_lst_item.Count == 0) return;
m_b_current_draw_info = !m_b_current_draw_info;
this.Invalidate();
return;
} else if (m_rect_title.Contains(e.Location)) {
return;
}
}
if (this._ShowTitle && m_rect_switch.Contains(e.Location)) {
if (m_node_attribute == null || m_lst_item.Count == 0) return;
m_b_current_draw_info = !m_b_current_draw_info;
this.Invalidate();
return;
}
Point pt = new Point(e.X, e.Y - (int)m_nOffsetY);
MouseEventArgs mea = new MouseEventArgs(e.Button, e.Clicks, pt.X, pt.Y, e.Delta);
if (m_b_current_draw_info)
this.OnProcessInfoMouseDown(mea);
else
this.OnProcessPropertyMouseDown(mea);
if (bRedraw) this.Invalidate();
}
/// <summary>
/// 当鼠标在控件上抬起时候发生
/// </summary>
/// <param name="e">事件参数</param>
protected override void OnMouseUp(MouseEventArgs e) {
base.OnMouseUp(e);
m_str_desc = null;
if (m_item_down_value != null && !this._ReadOnlyModel) {
Point pt = new Point(e.X, e.Y - (int)m_nOffsetY);
MouseEventArgs mea = new MouseEventArgs(e.Button, e.Clicks, pt.X, pt.Y, e.Delta);
m_item_down_value.OnMouseUp(mea);
if (m_pt_down == e.Location && !this._ReadOnlyModel) {
m_item_down_value.OnMouseClick(mea);
}
}
m_item_down_value = null;
this.Invalidate();
}
/// <summary>
/// 当鼠标离开控件时候发生
/// </summary>
/// <param name="e">事件参数</param>
protected override void OnMouseLeave(EventArgs e) {
base.OnMouseLeave(e);
m_b_hover_switch = false;
if (m_item_hover_value != null && !this._ReadOnlyModel) m_item_hover_value.OnMouseLeave(e);
m_item_hover = null;
this.Invalidate();
}
/// <summary>
/// 当鼠标在控件上滚动滚轮时候发生
/// </summary>
/// <param name="e">事件参数</param>
protected override void OnMouseWheel(MouseEventArgs e) {
base.OnMouseWheel(e);
if (e.Delta > 0) {
if (m_nOffsetY == 0) return;
m_nOffsetY += m_item_height;
if (m_nOffsetY > 0) m_nOffsetY = 0;
} else {
if (this.Height - m_nOffsetY >= m_nVHeight) return;
m_nOffsetY -= m_item_height;
}
if (m_b_current_draw_info)
m_nInfoOffsetY = m_nOffsetY;
else
m_nPropertyOffsetY = m_nOffsetY;
this.Invalidate();
}
/// <summary>
/// 当设置控件矩形区域时候发生
/// </summary>
/// <param name="x">x坐标</param>
/// <param name="y">y坐标</param>
/// <param name="width">宽度</param>
/// <param name="height">高度</param>
/// <param name="specified">指定需要设置的标识</param>
//protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified) {
// if (width < 120) width = 120;
// if (height < 50) height = 50;
// base.SetBoundsCore(x, y, width, height, specified);
//}
/// <summary>
/// 当控件尺寸发生改变时候发生
/// </summary>
/// <param name="e">事件参数</param>
protected override void OnResize(EventArgs e) {
base.OnResize(e);
m_rect_title.Width = this.Width;
m_rect_title.Height = m_nTitleHeight;
if (this._ShowTitle)
m_rect_switch = new Rectangle(this.Width - m_nTitleHeight + 2, 2, m_nTitleHeight - 4, m_nTitleHeight - 4);
if (this._STNode != null) this.SetItemRectangle();
}
#endregion
#region virtual method ==========
/// <summary>
/// 当绘制属性选项时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
/// <param name="item">目标属性选项描述器</param>
/// <param name="nIndex">选项所在索引</param>
protected virtual void OnDrawPropertyItem(DrawingTools dt, STNodePropertyDescriptor item, int nIndex) {
if (m_canvas == null) return;
using (var fill = new SKPaint { Style = SKPaintStyle.Fill, IsAntialias = true })
using (var text = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this.ForeColor), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true }) {
fill.Color = SkiaDrawingHelper.ToSKColor((nIndex % 2 == 0) ? m_clr_item_1 : m_clr_item_2);
m_canvas.DrawRect(item.Rectangle.Left, item.Rectangle.Top, item.Rectangle.Width, item.Rectangle.Height, fill);
if (item == m_item_hover || item == m_item_selected) {
fill.Color = SkiaDrawingHelper.ToSKColor(this._ItemHoverColor);
m_canvas.DrawRect(item.Rectangle.Left, item.Rectangle.Top, item.Rectangle.Width, item.Rectangle.Height, fill);
}
if (m_item_selected == item) {
m_canvas.DrawRect(item.Rectangle.X, item.Rectangle.Y, 5, item.Rectangle.Height, fill);
fill.Color = SkiaDrawingHelper.ToSKColor((this._AutoColor && this._STNode != null) ? this._STNode.TitleColor : this._ItemSelectedColor);
m_canvas.DrawRect(item.Rectangle.X, item.Rectangle.Y + 4, 5, item.Rectangle.Height - 8, fill);
}
var fm = text.FontMetrics;
float y = item.RectangleL.Top + (item.RectangleL.Height - (fm.Descent - fm.Ascent)) / 2 - fm.Ascent;
float x = item.RectangleL.Right - text.MeasureText(item.Name ?? string.Empty) - 2;
m_canvas.DrawText(item.Name ?? string.Empty, x, y, text);
}
item.OnDrawValueRectangle(m_dt);
if (this._ReadOnlyModel && m_canvas != null) {
using (var overlay = new SKPaint { Color = new SKColor(125, 125, 125, 125), Style = SKPaintStyle.Fill, IsAntialias = true })
m_canvas.DrawRect(item.RectangleR.Left, item.RectangleR.Top, item.RectangleR.Width, item.RectangleR.Height, overlay);
}
}
/// <summary>
/// 绘制属性窗口标题
/// </summary>
/// <param name="dt">绘制工具</param>
protected virtual void OnDrawTitle(DrawingTools dt) {
if (m_canvas == null) return;
using (var fill = new SKPaint { Style = SKPaintStyle.Fill, IsAntialias = true })
using (var text = new SKPaint { TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true, Style = SKPaintStyle.Fill }) {
fill.Color = SkiaDrawingHelper.ToSKColor(this._AutoColor ? (this._STNode == null ? this._TitleColor : this._STNode.TitleColor) : this._TitleColor);
m_canvas.DrawRect(m_rect_title.Left, m_rect_title.Top, m_rect_title.Width, m_rect_title.Height, fill);
text.Color = SkiaDrawingHelper.ToSKColor(this._STNode == null ? this.ForeColor : this._STNode.ForeColor);
var t = this._STNode == null ? this.Text : this._STNode.Title;
var fm = text.FontMetrics;
float y = m_rect_title.Top + (m_rect_title.Height - (fm.Descent - fm.Ascent)) / 2 - fm.Ascent;
float x = m_rect_title.Left + (m_rect_title.Width - text.MeasureText(t ?? string.Empty)) / 2;
m_canvas.DrawText(t ?? string.Empty, x, y, text);
}
}
/// <summary>
/// 当需要绘制属性描述信息时发生
/// </summary>
/// <param name="dt">绘制工具</param>
protected virtual void OnDrawDescription(DrawingTools dt) {
if (string.IsNullOrEmpty(m_str_desc) || m_canvas == null) return;
using (var text = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this.ForeColor), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true })
using (var fill = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._DescriptionColor), Style = SKPaintStyle.Fill, IsAntialias = true })
using (var stroke = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._DescriptionColor), Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true }) {
var bounds = new SKRect();
text.MeasureText(m_str_desc, ref bounds);
Rectangle rect_desc = new Rectangle(0, this.Height - (int)Math.Ceiling(bounds.Height) - 8, this.Width, (int)Math.Ceiling(bounds.Height) + 8);
m_canvas.DrawRect(rect_desc.Left, rect_desc.Top, rect_desc.Width, rect_desc.Height, fill);
m_canvas.DrawRect(rect_desc.Left, rect_desc.Top, rect_desc.Width - 1, rect_desc.Height - 1, stroke);
var fm = text.FontMetrics;
float y = rect_desc.Top + (rect_desc.Height - (fm.Descent - fm.Ascent)) / 2 - fm.Ascent;
m_canvas.DrawText(m_str_desc, rect_desc.Left + 4, y, text);
}
}
/// <summary>
/// 当需要绘制错误信息时发生
/// </summary>
/// <param name="dt">绘制工具</param>
protected virtual void OnDrawErrorInfo(DrawingTools dt) {
if (string.IsNullOrEmpty(m_str_err) || m_canvas == null) return;
using (var text = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this.ForeColor), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true })
using (var fill = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._ErrorColor), Style = SKPaintStyle.Fill, IsAntialias = true })
using (var stroke = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this._ErrorColor), Style = SKPaintStyle.Stroke, StrokeWidth = 1, IsAntialias = true }) {
var bounds = new SKRect();
text.MeasureText(m_str_err, ref bounds);
Rectangle rect_desc = new Rectangle(0, 0, this.Width, (int)Math.Ceiling(bounds.Height) + 8);
m_canvas.DrawRect(rect_desc.Left, rect_desc.Top, rect_desc.Width, rect_desc.Height, fill);
m_canvas.DrawRect(rect_desc.Left, rect_desc.Top, rect_desc.Width - 1, rect_desc.Height - 1, stroke);
var fm = text.FontMetrics;
float y = rect_desc.Top + (rect_desc.Height - (fm.Descent - fm.Ascent)) / 2 - fm.Ascent;
m_canvas.DrawText(m_str_err, rect_desc.Left + 4, y, text);
}
}
/// <summary>
/// 当绘制节点信息时候发生
/// </summary>
/// <param name="dt">绘制工具</param>
protected virtual void OnDrawInfo(DrawingTools dt) {
if (m_node_attribute == null || m_canvas == null) return;
var attr = m_node_attribute;
using (var text = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(this.ForeColor), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true })
using (var textDim = new SKPaint { Color = SkiaDrawingHelper.ToSKColor(Color.FromArgb(this.ForeColor.A / 2, this.ForeColor)), TextSize = Math.Max(10f, this.Font.Size), IsAntialias = true })
using (var fill = new SKPaint { Style = SKPaintStyle.Fill, IsAntialias = true }) {
int top = this._ShowTitle ? m_nTitleHeight : 0;
int y = top;
void row(SKColor bg,string key,string val,SKPaint vp){
fill.Color=bg; m_canvas.DrawRect(0,y,this.Width,m_item_height,fill);
var fm=text.FontMetrics; float ty=y+(m_item_height-(fm.Descent-fm.Ascent))/2-fm.Ascent;
m_canvas.DrawText(key,2,ty,text); m_canvas.DrawText(val??string.Empty,m_nInfoLeft,ty,vp); y+=m_item_height;
}
row(SkiaDrawingHelper.ToSKColor(m_clr_item_2),m_KeysString[0],attr.Author,textDim);
row(SkiaDrawingHelper.ToSKColor(m_clr_item_1),m_KeysString[1],attr.Mail,textDim);
row(SkiaDrawingHelper.ToSKColor(m_clr_item_2),m_KeysString[2],attr.Link,new SKPaint{Color=SKColors.CornflowerBlue,TextSize=text.TextSize,IsAntialias=true});
m_nInfoVHeight = y + m_item_height;
}
}
/// <summary>
/// 当在属性面板鼠标点下时候发生
/// </summary>
/// <param name="e">鼠标事件参数</param>
protected virtual void OnProcessPropertyMouseDown(MouseEventArgs e) {
bool bRedraw = false;
if (m_item_selected != m_item_hover) {
m_item_selected = m_item_hover;
bRedraw = true;
}
m_item_down_value = null;
if (m_item_selected == null) {
if (bRedraw) this.Invalidate();
return;
}
if (m_item_selected.RectangleR.Contains(e.Location)) {
m_item_down_value = m_item_selected;
if (!this._ReadOnlyModel)
m_item_selected.OnMouseDown(e);
else {
return;
}
} else if (m_item_selected.RectangleL.Contains(e.Location)) {
m_str_desc = m_item_selected.Description;
bRedraw = true;
}
if (bRedraw) this.Invalidate();
}
/// <summary>
/// 当在信息面板鼠标点下时候发生
/// </summary>
/// <param name="e">鼠标事件参数</param>
protected virtual void OnProcessInfoMouseDown(MouseEventArgs e) {
try {
if (m_rect_link.Contains(e.Location)) {
System.Diagnostics.Process.Start(m_node_attribute.Link);
} else if (m_rect_help.Contains(e.Location)) {
STNodeAttribute.ShowHelp(m_type);
}
} catch (Exception ex) {
this.SetErrorMessage(ex.Message);
}
}
/// <summary>
/// 当在属性面板鼠标移动时候发生
/// </summary>
/// <param name="e">鼠标事件参数</param>
protected virtual void OnProcessPropertyMouseMove(MouseEventArgs e) {
if (m_item_down_value != null) {
m_item_down_value.OnMouseMove(e);
return;
}
STNodePropertyDescriptor item = null;
foreach (var v in m_lst_item) {
if (v.Rectangle.Contains(e.Location)) {
item = v;
break;
}
}
if (item != null) {
if (item.RectangleR.Contains(e.Location)) {
if (m_item_hover_value != item) {
if (m_item_hover_value != null) m_item_hover_value.OnMouseLeave(e);
m_item_hover_value = item;
m_item_hover_value.OnMouseEnter(e);
}
m_item_hover_value.OnMouseMove(e);
} else {
if (m_item_hover_value != null) m_item_hover_value.OnMouseLeave(e);
}
}
if (m_item_hover != item) {
m_item_hover = item;
this.Invalidate();
}
}
/// <summary>
/// 当在信息面板鼠标移动时候发生
/// </summary>
/// <param name="e">鼠标事件参数</param>
protected virtual void OnProcessHelpMouseMove(MouseEventArgs e) {
if (m_rect_link.Contains(e.Location) || m_rect_help.Contains(e.Location)) {
this.Cursor = Cursors.Hand;
} else this.Cursor = Cursors.Arrow;
}
#endregion
#region public ==========
/// <summary>
/// 设置需要显示的STNode节点
/// </summary>
/// <param name="node">目标节点</param>
public void SetNode(STNode node) {
if (node == this._STNode) return;
m_nInfoOffsetY = m_nPropertyOffsetY = 0;
m_nInfoVHeight = m_nPropertyVHeight = 0;
m_rect_link = m_rect_help = Rectangle.Empty;
m_str_desc = m_str_err = null;
this._STNode = node;
if (node != null) {
m_type = node.GetType();
m_lst_item = this.GetProperties(node);
m_node_attribute = this.GetNodeAttribute(node);
this.SetItemRectangle();
m_b_current_draw_info = m_lst_item.Count == 0 || this._InfoFirstOnDraw;
if (this._AutoColor) this._ItemSelectedColor = this._STNode.TitleColor;
} else {
m_type = null;
m_lst_item.Clear();
m_node_attribute = null;
}
this.Invalidate();
}
/// <summary>
/// 设置信息页面Key的显示文本
/// </summary>
/// <param name="strAuthor">作者</param>
/// <param name="strMail">邮箱</param>
/// <param name="strLink">连接</param>
/// <param name="strHelp">查看帮助</param>
public void SetInfoKey(string strAuthor, string strMail, string strLink, string strHelp) {
m_KeysString = new string[] { strAuthor, strMail, strLink, strHelp };
}
/// <summary>
/// 设置要显示的错误信息
/// </summary>
/// <param name="strText">错误信息</param>
public void SetErrorMessage(string strText) {
m_str_err = strText;
this.Invalidate();
}
#endregion
}
}