-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Drawing.cs
1221 lines (1093 loc) · 54.8 KB
/
Drawing.cs
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 Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Attributes;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
namespace Sunglasses
{
public static class Drawing
{
#region Fields
private static float _zoomFactor;
private static Grasshopper.GUI.GH_FadeAnimation _fade;
private static Grasshopper.GUI.GH_FadeAnimation _fadeGroups;
private static Dictionary<Guid, Tuple<Size, string, FontFamily, FontStyle, float>> _groupsFontSizeCache = new Dictionary<Guid, Tuple<Size, string, FontFamily, FontStyle, float>>();
#endregion
#region Properties
public static float ZoomFactor
{
get
{
return _zoomFactor;
}
internal set
{
_zoomFactor = (System.Math.Min(ZoomObjectsDisplayMax, System.Math.Max(ZoomObjectsDisplayMin, value)) - ZoomObjectsDisplayMin) / (ZoomObjectsDisplayMax - ZoomObjectsDisplayMin);
}
}
public static Grasshopper.GUI.GH_FadeAnimation Fade
{
get
{
if (_fade == null)
_fade = new Grasshopper.GUI.GH_FadeAnimation(GH_Viewport.ZoomDefault * ZoomObjectsDisplayMin);
return _fade;
}
}
public static Grasshopper.GUI.GH_FadeAnimation FadeGroups
{
get
{
if (_fadeGroups == null)
_fadeGroups = new Grasshopper.GUI.GH_FadeAnimation(GH_Viewport.ZoomDefault * 0.5f);
return _fadeGroups;
}
}
public static float BoxBorderWidth
{
get
{
return 0.1f / Grasshopper.GUI.GH_GraphicsUtil.UiScale;
}
}
public static float ZoomObjectsDisplayMin { get { return 6f; } }
public static float ZoomObjectsDisplayMax { get { return 10f; } }
public static float GroupNicknameFontSizeMin { get { return 6f; } }
#endregion
#region Utils
public static Color FadeColor(Color color)
{
return Color.FromArgb(Fade.FadeAlpha, color);
}
public static float ScaleSize(float size)
{
return size / Grasshopper.GUI.GH_GraphicsUtil.UiScale;
}
public static float Remap(float V, float A, float B, float C, float D)
{
return (V - A) / (B - A) * (D - C) + C;
}
public static RectangleF Snap(RectangleF rec, float snapping)
{
return new RectangleF(
rec.X - rec.X % snapping,
rec.Y - rec.Y % snapping,
rec.Width - rec.Width % snapping,
rec.Height - rec.Height % snapping
);
}
public static Font AdjustFontHeight(Graphics graphics, Font font, string text, RectangleF bounds, float minSize = 0.1f)
{
if (font.Size <= minSize)
return font;
var h = graphics.MeasureString(text, font, (int)Math.Ceiling(bounds.Width)).Height;
if (h < bounds.Height)
return font;
var ratio = bounds.Height / h;
var delta = Math.Max(0.1f, font.Size - font.Size * ratio);
var newSize = Math.Max(0.1f, font.Size - 0.5f * delta);
return AdjustFontHeight(graphics, new Font(font.Name, newSize, font.Style), text, bounds);
}
public static float CalculateFittedFontSize(Graphics graphics, Font font, float size, string text, int limitWidth, float height, StringFormat sf, bool oneWord, int counter = 0)
{
//if (true)
//{
var f = new Font(font.FontFamily, size);
var s = graphics.MeasureString(text, f, oneWord ? int.MaxValue : limitWidth, sf);
f.Dispose();
var eh = height - s.Height;
var ew = limitWidth - s.Width;
if (Math.Abs(eh) < 1f && ew >= 0)
return size;
var ds = 1.0f;
if ((oneWord || ew < 0) && eh >= 0)
{
ds = limitWidth / s.Width;
}
else
{
ds = height / s.Height;
}
var t = ((1f - counter / 1000f) * 0.8f + 0.1f);
var newSize = size + t * ((size * ds) - size);
if (ds >= 1f && Math.Abs(size - newSize) < 0.01f)
return newSize;
if (counter++ > 1000 && s.Height < height)
return size;
return CalculateFittedFontSize(graphics, font, newSize, text, limitWidth, height, sf, oneWord, counter);
//}
//else
//{
// var f = new Font(font.FontFamily, size);
// var h = graphics.MeasureString(text, f, limitWidth, sf).Height;
// f.Dispose();
// var eh = height - h;
// if (Math.Abs(eh) < 1f)
// return size;
// var ds = height / h;
// if (Math.Abs(1f - ds) < 0.1f)
// return size;
// var t = ((1f - counter / 500f) * 0.6f + 0.1f);
// var newSize = size + t * ((size * ds) - size);
// if (eh > 0)
// counter++;
// if (counter > 500)
// return size;
// return CalculateFittedFontSize(graphics, font, newSize, text, limitWidth, height, sf, oneWord, counter);
//}
}
public static IEnumerable<IGH_DocumentObject> GetVisibleObjects(GH_Canvas canvas, IEnumerable<IGH_DocumentObject> objects)
{
foreach (var obj in objects)
{
RectangleF bnd = obj.Attributes.Bounds;
if (canvas.Viewport.IsVisible(ref bnd, 20))
{
yield return obj;
//if (obj is IGH_Component && obj.Attributes.GetType() == typeof(GH_ComponentAttributes))
//{
// yield return new RichedDisplayComponent(obj as IGH_Component) { ShowParameterIcons = ShowComponentParameterIcons };
//}
//else if (obj is IGH_Param && Util.IsPersistantParameter(obj))
//{
// yield return new RichedDisplayParameter(obj as IGH_Param) { };
//}
}
}
}
internal static List<string> GetDataDescription(Grasshopper.Kernel.Data.IGH_Structure data, Graphics graphics, Font font, SizeF limit, out float height)
{
var list = new List<string>();
var totalHeight = 0f;
var pathCount = data.PathCount;
for (int i = 0; i < pathCount; i++)
{
var path = data.Paths[i]; ;
if (!Add($" {path}"))
break;
var branch = data.get_Branch(path);
int spacing = branch.Count.ToString().Length + 2;
for (int j = 0; j < branch.Count; j++)
{
if (branch[j] == null)
{
if (!Add($"{Indexing(j, spacing)}<null>"))
break;
continue;
}
string text = branch[j].ToString();
if (string.IsNullOrEmpty(text))
{
if (!Add($"{Indexing(j, spacing)}<empty>"))
break;
}
else
{
if (!Add($"{Indexing(j, spacing)}{text}"))
break;
}
}
if (i < pathCount - 1)
if (!Add(Environment.NewLine))
break;
}
height = totalHeight;
return list;
bool Add(string text)
{
list.Add(text);
totalHeight += graphics.MeasureString(text, font, (int)Math.Ceiling(limit.Width)).Height;
return totalHeight < limit.Height;
}
string Indexing(int i, int s)
{
var text = $"{i}. ";
int num = s - text.Length;
if (num > 0)
{
text += new string(' ', num);
}
return text;
}
}
#endregion
#region Paint
public static void PaintNames(Graphics graphics, IEnumerable<IGH_DocumentObject> objects)
{
if (Settings.HideOnLowZoom && GH_Canvas.ZoomFadeLow < 5)
return;
try
{
var alpha = Settings.HideOnLowZoom ? GH_Canvas.ZoomFadeLow : 255;
var size = Settings.Font.Size;
var infl = size * 25;
var hght = size * 2f;
var nicknames = Settings.DisplayNicknames || Settings.DisplayCustomNicknames;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
var sf = Grasshopper.GUI.GH_TextRenderingConstants.CenterCenter;
sf.FormatFlags |= StringFormatFlags.NoClip;
foreach (IGH_DocumentObject obj in objects)
{
if (obj.Attributes == null)
obj.CreateAttributes();
RectangleF box = obj.Attributes.Bounds;
GH_Palette palette = obj is IGH_ActiveObject a ? GH_CapsuleRenderEngine.GetImpliedPalette(a) : GH_Palette.Normal;
GH_PaletteStyle style = GH_CapsuleRenderEngine.GetImpliedStyle(palette, obj.Attributes);
using (Brush brh = new SolidBrush(Color.FromArgb(alpha, style.Edge)))
{
graphics.DrawString(nicknames ? obj.NickName : obj.Name, Settings.Font, brh, box.X + box.Width/2f, box.Y - hght / 2f-2, sf);
}
}
}
catch (Exception e)
{
Rhino.RhinoApp.WriteLine(e.ToString());
}
}
public static void PaintGroupNickname(GH_Canvas canvas, IEnumerable<Grasshopper.Kernel.Special.GH_Group> groups)
{
if (groups == null || !groups.Any())
return;
var graphics = canvas.Graphics;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
var alpha = 255 - GH_Canvas.ZoomFadeLow;
GH_Palette palette = GH_Palette.Hidden;
var sf = new StringFormat() {
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center,
Trimming = StringTrimming.Word,
// FormatFlags = StringFormatFlags.LineLimit
};
var sFont = Settings.Font;
foreach (var group in groups)
{
if (string.IsNullOrEmpty(group.NickName))
continue;
if (group.Attributes == null)
group.CreateAttributes();
var box = group.Attributes.Bounds;
var sbox = Snap(box, 0.1f);
var ph = canvas.Viewport.ProjectRectangle(sbox).Height;
if (ph < 10)
continue;
var sboxSize = GH_Convert.ToSize(sbox.Size);
var fontSize = sFont.Size;
if(_groupsFontSizeCache.ContainsKey(group.InstanceGuid))
{
var cache = _groupsFontSizeCache[group.InstanceGuid];
if(cache.Item1.Equals(sboxSize) && cache.Item2.Equals(group.NickName) &&
cache.Item3.Equals(sFont.FontFamily) && cache.Item4.Equals(sFont.Style))
{
fontSize = cache.Item5;
}
else
{
fontSize = CalculateFittedFontSize(graphics, sFont, ph, group.NickName, (int)Math.Ceiling(box.Width), box.Height, sf, IsOneWord(group.NickName));
_groupsFontSizeCache[group.InstanceGuid] = new Tuple<Size, string, FontFamily, FontStyle, float>(GH_Convert.ToSize(sbox.Size), group.NickName, sFont.FontFamily, sFont.Style, fontSize);
}
}
else
{
fontSize = CalculateFittedFontSize(graphics, sFont, ph, group.NickName, (int)Math.Ceiling(box.Width), box.Height, sf, IsOneWord(group.NickName));
_groupsFontSizeCache.Add(group.InstanceGuid, new Tuple<Size, string, FontFamily, FontStyle, float>(sboxSize, group.NickName, sFont.FontFamily, sFont.Style, fontSize));
}
if (fontSize < GroupNicknameFontSizeMin)
continue;
using (var font = new Font(sFont.FontFamily, fontSize))
{
var style = GH_CapsuleRenderEngine.GetImpliedStyle(palette, group.Attributes);
var color = Grasshopper.GUI.GH_GraphicsUtil.BlendColour(Grasshopper.GUI.GH_GraphicsUtil.ForegroundColour(group.Colour, 150), group.Colour.GetBrightness() < 0.5 ? Color.White : Color.Black, 0.3);
using (Brush brh = new SolidBrush(Color.FromArgb(alpha, color)))
graphics.DrawString(group.NickName, font, brh, box, sf);
}
}
sf.Dispose();
bool IsOneWord(string text)
{
return !System.Text.RegularExpressions.Regex.IsMatch(text, "\\w+\\W+\\w");
}
}
public static void PaintRichedCapsules(GH_Canvas canvas, IEnumerable<IGH_DocumentObject> objects)
{
Fade.Evaluate(canvas, true);
if (Fade.FadeAlpha < 1)
return;
ZoomFactor = canvas.Viewport.Zoom;
if (ZoomFactor == 0)
return;
try
{
var graphics = canvas.Graphics;
foreach (var obj in objects)
{
if (!(obj is IGH_ActiveObject aObj))
continue;
if (obj.Attributes == null)
obj.CreateAttributes();
var objAtt = obj.Attributes;
var bounds = objAtt.Bounds;
if (!canvas.Viewport.IsVisible(ref bounds, 0))
continue;
RichedCapsule rObj = null;
if (objAtt is GH_ComponentAttributes cAtt)
{
rObj = new RichedCapsuleComponent(cAtt);
}
else if (objAtt is GH_FloatingParamAttributes pAtt && (aObj.IconDisplayMode == GH_IconDisplayMode.icon || aObj.IconDisplayMode == GH_IconDisplayMode.application && Grasshopper.CentralSettings.CanvasObjectIcons))
{
rObj = new RichedCapsuleParameter(pAtt);
}
if (rObj == null)
continue;
rObj.RenderRichedCapsule(graphics, canvas);
if (objAtt is GH_ComponentAttributes cAtt2)
{
typeof(GH_ComponentAttributes).GetMethod("RenderVariableParameterUI",
System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.Invoke(cAtt2, new object[] { canvas, graphics });
}
}
}
catch (Exception e)
{
Rhino.RhinoApp.WriteLine(" :( Sunglasses exception painting riched capsules! try with another font ):");
Rhino.RhinoApp.WriteLine(e.ToString());
}
}
#endregion
#region Capsule
internal static float PaintCapsuleParameterData(Graphics graphics, RectangleF bounds, IGH_Param param, GH_PaletteStyle style)
{
if (bounds.Width <= 2 || bounds.Height <= 1 || ZoomFactor < 0.5f)
return 0f;
var font = Settings.FontCapsuleParameterData;
var dd = GetDataDescription(param.VolatileData, graphics, font, new SizeF(bounds.Width - graphics.MeasureString(param.VolatileDataCount.ToString(), font).Width, bounds.Height), out _);
var count = dd.Count;
var color = FadeColor(style.Edge);
Color c0 = Color.FromArgb(20, color);
Color c1 = Color.FromArgb(50, color);
Color c2 = Color.FromArgb(100, color);
var border = BoxBorderWidth;
var border2 = border / 2f;
var padding = ScaleSize(0.5f);
var top = bounds.Y + padding / 2f;
var bottom = bounds.Bottom - padding;
var height = bottom - top;
var left = bounds.X + padding;
var right = bounds.Right - padding;
var width = right - left;
var fontTreeInfo = GH_FontServer.NewFont(font, font.Size * 0.9f);
var fontItem = GH_FontServer.NewFont(font, FontStyle.Regular);
var fontBranchCount = GH_FontServer.NewFont(font, FontStyle.Regular);
var treeInfo = string.Empty;
var empty = false;
if (param.VolatileDataCount == 0)
{
treeInfo = "Empty parameter";
empty = true;
}
else if (param.VolatileData.PathCount == 1)
{
int cnt = param.VolatileData.get_Branch(0).Count;
if (cnt == 1)
{
treeInfo = "Branch with 1 item";
}
else
{
treeInfo = string.Format("Branch with {0} items", cnt.ToString());
}
}
else
{
treeInfo = string.Format("Tree with {0} branches and {1} items", param.VolatileData.PathCount.ToString(), param.VolatileData.DataCount.ToString());
}
var cellHeight = graphics.MeasureString(treeInfo, font).Height;
var cellHeight1 = cellHeight;
var totalHeight = cellHeight;
var currentY = top + cellHeight;
if (currentY > bounds.Bottom)
{
return 0f;
}
PaintCapsuleBoxBackground(graphics, bounds, style);
var textBrush = new SolidBrush(style.Text);
if (bounds.Height < cellHeight * 3f)
{
graphics.DrawString(treeInfo, font, textBrush, bounds, Grasshopper.GUI.GH_TextRenderingConstants.CenterCenter);
}
else
{
if (empty)
{
graphics.DrawString(treeInfo, font, textBrush, bounds, Grasshopper.GUI.GH_TextRenderingConstants.CenterCenter);
}
else
{
graphics.DrawString(treeInfo, font, textBrush, new RectangleF(left, top, width, cellHeight), Grasshopper.GUI.GH_TextRenderingConstants.FarCenter);
int maxCP = 0;
for (int i = 0; i < param.VolatileData.PathCount; i++)
{
int cp = param.VolatileData.get_Branch(i).Count;
if (cp > maxCP)
maxCP = cp;
}
var widthIndex = graphics.MeasureString(maxCP.ToString(), font, (int)width).Width + ScaleSize(1f);
var widthItem = bounds.Width - widthIndex;
var column2X = bounds.X + widthIndex;
var column2width = bounds.Right - column2X;
var iter = 0;
var pathid = 0;
for (var i = 0; i < count; i++)
{
var s = dd[i].TrimStart();
var isB = s.StartsWith("{");
cellHeight = graphics.MeasureString(s, font, (int)width).Height;
totalHeight += cellHeight;
var nomore = false;
if (totalHeight >= height && !string.IsNullOrEmpty(s))
{
graphics.DrawString("...", font, textBrush, new RectangleF(column2X, bounds.Bottom - cellHeight1, widthItem, cellHeight1), Grasshopper.GUI.GH_TextRenderingConstants.CenterCenter);
nomore = true;
}
totalHeight -= cellHeight;
cellHeight = Math.Min(cellHeight, bounds.Bottom - currentY);
totalHeight += cellHeight;
if ((iter % 2 == 0 || isB) && !string.IsNullOrWhiteSpace(s.Replace("\r", "").Replace("\n", "")))
{
using (Brush brh = new SolidBrush(isB ? c1 : c0))
graphics.FillRectangle(brh, bounds.X, currentY, bounds.Width, cellHeight);
iter = 0;
}
if (isB)
{
using (Pen pen = new Pen(c2, border2) { StartCap = System.Drawing.Drawing2D.LineCap.Square, EndCap = System.Drawing.Drawing2D.LineCap.Square })
graphics.DrawLine(pen, bounds.X, currentY, bounds.Right, currentY);
if (!nomore)
graphics.DrawString(s, font, textBrush, new RectangleF(left, currentY, width, cellHeight), Grasshopper.GUI.GH_TextRenderingConstants.FarCenter);
float w1 = graphics.MeasureString(s, font).Width;
string listCount = $"N = {param.VolatileData.get_Branch(pathid).Count}";
float w2 = graphics.MeasureString(listCount, font).Width + padding;
if (w1 + w2 < width)
using (Brush brh = new SolidBrush(Color.FromArgb(200, style.Text)))
graphics.DrawString(listCount, fontBranchCount, brh, new RectangleF(column2X + padding, currentY, column2width, cellHeight), Grasshopper.GUI.GH_TextRenderingConstants.NearCenter);
pathid++;
}
else
{
if (currentY + cellHeight < bounds.Bottom || i == count - 1)
{
string[] split = s.Split(new[] { '.' }, 2);
if (split.Length > 0)
graphics.DrawString(split[0], font, textBrush, new RectangleF(bounds.X, currentY, widthIndex, cellHeight), Grasshopper.GUI.GH_TextRenderingConstants.CenterCenter);
if (split.Length > 1)
graphics.DrawString(split[1], fontItem, textBrush, new RectangleF(column2X, currentY, column2width, cellHeight), Grasshopper.GUI.GH_TextRenderingConstants.CenterCenter);
}
}
if (nomore)
break;
currentY += cellHeight;
iter++;
}
using (Pen pen = new Pen(c2, border2) { StartCap = System.Drawing.Drawing2D.LineCap.Flat, EndCap = System.Drawing.Drawing2D.LineCap.Square })
{
graphics.DrawLine(pen, bounds.X + widthIndex, top + cellHeight1, bounds.X + widthIndex, bounds.Bottom);
}
}
}
textBrush.Dispose();
fontBranchCount.Dispose();
fontItem.Dispose();
fontTreeInfo.Dispose();
PaintCapsuleBox(graphics, bounds, style);
return bounds.Height;
}
internal static void PaintCapsuleBoxBackground(Graphics graphics, RectangleF bounds, GH_PaletteStyle style)
{
using (var brh = new SolidBrush(Color.FromArgb(15, FadeColor(Color.Black))))
graphics.FillRectangle(brh, bounds);
}
internal static void PaintCapsuleBox(Graphics graphics, RectangleF bounds, GH_PaletteStyle style)
{
var shadowColor = Color.FromArgb(60, FadeColor(Color.Black));
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
///Grasshopper.GUI.GH_GraphicsUtil.ShadowRectangle(graphics, bounds, 1, 60);
var size = ZoomFactor >= 0.5f ? (1f - 0.5f * ZoomFactor) : 1f;
float left = (float)Math.Floor(bounds.X);
float top = (float)Math.Floor(bounds.Y);
float right = (float)Math.Ceiling(bounds.Right);
float bottom = (float)Math.Ceiling(bounds.Bottom);
var rec = new RectangleF(left, top, right - left, bottom - top);
//PaintShadow(graphics, new RectangleF(bounds.X, bounds.Y, bounds.Width, size), shadowColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical, false);
/////(graphics, new RectangleF(bounds.X, bounds.Bottom - size, bounds.Width, size), shadowColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical, true);
// PaintShadow(graphics, new RectangleF(bounds.X, bounds.Y, size, bounds.Height), shadowColor, System.Drawing.Drawing2D.LinearGradientMode.Horizontal, false);
//PaintShadow(graphics, new RectangleF(bounds.Right - size, bounds.Y, size, bounds.Height), shadowColor, System.Drawing.Drawing2D.LinearGradientMode.Horizontal, true);
using (var brh = new System.Drawing.Drawing2D.LinearGradientBrush(rec, Color.FromArgb(0, shadowColor), shadowColor, System.Drawing.Drawing2D.LinearGradientMode.Horizontal))
{
brh.WrapMode = System.Drawing.Drawing2D.WrapMode.TileFlipXY;
brh.Blend = new System.Drawing.Drawing2D.Blend()
{
Positions = new float[] {
0f,
Remap(bounds.X, left, right, 0f, 1f),
Remap(bounds.X + size, left, right, 0f, 1f),
Remap(bounds.Right - size, left, right, 0f, 1f),
Remap(bounds.Right, left, right, 0f, 1f),
1f
},
Factors = new float[] { 1f, 1f, 0f, 0f, 1f, 1f }
};
graphics.FillRectangle(brh, bounds);
}
using (var brh = new System.Drawing.Drawing2D.LinearGradientBrush(rec, Color.FromArgb(0, shadowColor), shadowColor, System.Drawing.Drawing2D.LinearGradientMode.Vertical))
{
brh.WrapMode = System.Drawing.Drawing2D.WrapMode.TileFlipXY;
brh.Blend = new System.Drawing.Drawing2D.Blend()
{
Positions = new float[] {
0f,
Remap(bounds.Y, top, bottom, 0f, 1f),
Remap(bounds.Y + size, top, bottom, 0f, 1f),
Remap(bounds.Bottom - size, top, bottom, 0f, 1f),
Remap(bounds.Bottom , top, bottom, 0f, 1f),
1f
},
Factors = new float[] { 1f, 1f, 0f, 0f, 1f, 1f }
};
graphics.FillRectangle(brh, bounds);
}
using (var pen = new Pen(FadeColor(style.Edge), BoxBorderWidth))
graphics.DrawRectangle(pen, bounds.X, bounds.Y, bounds.Width, bounds.Height);
}
internal static void PaintRedBorders(Graphics graphics, RectangleF bounds)
{
using (var pen = new Pen(Color.Red, BoxBorderWidth))
graphics.DrawRectangle(pen, bounds.X, bounds.Y, bounds.Width, bounds.Height);
}
internal abstract class RichedCapsule
{
#region Fields
private float _padding;
#endregion
#region Properties
public IGH_Attributes Attributes { get; }
public IGH_ActiveObject Object { get; }
public RectangleF Bounds { get; }
public GH_Capsule Capsule { get; }
public GH_PaletteStyle Style { get; }
public float Padding
{
get
{
if (_padding == 0f)
_padding = ScaleSize(1f);
return _padding;
}
}
#endregion
#region Constructors
public RichedCapsule(IGH_Attributes att)
{
Attributes = att;
Object = att.GetTopLevel.DocObject as IGH_ActiveObject;
Bounds = CreateBounds();
var palette = GH_CapsuleRenderEngine.GetImpliedPalette(Object);
Capsule = GH_Capsule.CreateCapsule(att.Bounds, palette);
Capsule.SetJaggedEdges(!Attributes.HasInputGrip, !Attributes.HasOutputGrip);
var defStyle = GH_CapsuleRenderEngine.GetImpliedStyle(palette, att);
var alpha = (int)(255 * System.Math.Pow(Fade.FadeAlpha / 255.0, 0.1));
Style = new GH_PaletteStyle(
defStyle.Fill,
Color.FromArgb(alpha, defStyle.Edge),
defStyle.Text);
}
#endregion
#region Methods
protected virtual RectangleF CreateBounds()
{
return Attributes.Bounds;
}
public virtual Region GetClip()
{
return new Region(Bounds);
}
public abstract void Render(Graphics graphics, GH_Canvas canvas);
public void RenderRichedCapsule(Graphics graphics, GH_Canvas canvas)
{
var clip = graphics.Clip;
graphics.SetClip(GetClip(), System.Drawing.Drawing2D.CombineMode.Replace);
Capsule.Render(graphics, Style);
Render(graphics, canvas);
graphics.SetClip(clip, System.Drawing.Drawing2D.CombineMode.Replace);
}
#endregion
}
internal class RichedCapsuleComponent : RichedCapsule
{
private bool _iconMode;
internal RichedCapsuleComponentInfo Info { get; }
internal List<RichedCapsuleComponentParameter> Parameters { get; }
public RichedCapsuleComponent(GH_ComponentAttributes att) : base(att)
{
_iconMode = Grasshopper.CentralSettings.CanvasObjectIcons;
if (_iconMode)
Info = new RichedCapsuleComponentInfo(att);
Parameters = new List<RichedCapsuleComponentParameter>();
foreach (var param in att.Owner.Params)
{
if (param.Attributes is GH_LinkedParamAttributes pAtt)
{
Parameters.Add(new RichedCapsuleComponentParameter(pAtt));
}
}
}
public override void Render(Graphics graphics, GH_Canvas canvas)
{
///Rhino.RhinoApp.WriteLine();
if (_iconMode)
{
//var start = DateTime.Now;
Info.Render(graphics, canvas);
//var time = (DateTime.Now - start).TotalMilliseconds;
///Rhino.RhinoApp.WriteLine("Info " + time.ToString());
}
foreach (var p in Parameters)
{
//var start = DateTime.Now;
p.Render(graphics, canvas);
//var time = (DateTime.Now - start).TotalMilliseconds;
//Rhino.RhinoApp.WriteLine((p.IsInput ? "Input " : "Output " )+ time.ToString());
}
}
public override Region GetClip()
{
var clip = new Region(Bounds);
if (_iconMode)
clip.Xor(Info.Bounds);
foreach (var p in Parameters)
clip.Xor(p.Bounds);
clip.Complement(Bounds);
return clip;
}
}
internal class RichedCapsuleComponentParameter : RichedCapsule
{
public IGH_Component Component { get; }
public IGH_Param Param { get; }
public bool IsInput { get; }
public RichedCapsuleComponentParameter(GH_LinkedParamAttributes att) : base(att)
{
Component = Attributes.GetTopLevel.DocObject as IGH_Component;
Param = Attributes.DocObject as IGH_Param;
IsInput = Component.Params.IsInputParam(Param);
}
protected override RectangleF CreateBounds()
{
var att = Attributes as GH_LinkedParamAttributes;
var box = att.Bounds;
GH_StateTagList stl = typeof(GH_LinkedParamAttributes)
.GetField("m_renderTags", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
.GetValue(att) as GH_StateTagList;
if (stl == null || stl.Count == 0)
return box;
var sBox = stl.BoundingBox;
if (att.HasInputGrip)
{
return new RectangleF(sBox.Right, box.Y, box.Width - sBox.Width, box.Height);
}
else
{
return new RectangleF(box.X, box.Y, box.Width - sBox.Width - 1, box.Height);
}
}
public override void Render(Graphics graphics, GH_Canvas canvas)
{
var align = IsInput ? StringAlignment.Far : StringAlignment.Near;
var nameFormat = new StringFormat() { Alignment = align, LineAlignment = StringAlignment.Center };
string nameText = Grasshopper.CentralSettings.CanvasFullNames ? Param.Name : Param.NickName;
var nameFontSize = Remap(ZoomFactor, 0, 1, GH_FontServer.StandardAdjusted.Size, ScaleSize(1.8f));
var nameFont = GH_FontServer.NewFont(Settings.FontCapsuleParameterName, nameFontSize);
var nameTextHeight = graphics.MeasureString(nameText, nameFont, (int)Math.Ceiling(Bounds.Width), nameFormat).Height;
var nameBox = new RectangleF(
Bounds.X,
Bounds.Y,
Bounds.Width,
Remap(ZoomFactor, 0, 1, Bounds.Height, nameTextHeight + 1f)
);
var descText = Param.Description;
switch (Param.Access)
{
case GH_ParamAccess.item:
//text = $"(as item) {text}";
break;
case GH_ParamAccess.list:
descText = $"(as list) {descText}";
break;
case GH_ParamAccess.tree:
descText = $"(as tree) {descText}";
break;
}
var descFormat = new StringFormat() { Alignment = align, LineAlignment = StringAlignment.Near };
var descFontSize = Settings.FontCapsuleDescription.Size * (1f - 0.4f * ZoomFactor);
var descFont = GH_FontServer.NewFont(Settings.FontCapsuleDescription, descFontSize, FontStyle.Italic);
var descBox = new RectangleF(
Bounds.X + Padding / 4f,
(nameBox.Y + nameBox.Height / 2f) + nameTextHeight / 2f,
Bounds.Width,
graphics.MeasureString(descText, descFont, (int)Math.Ceiling(Bounds.Width), descFormat).Height
);
var descVisible = ZoomFactor > 0.2f && descBox.Bottom <= Bounds.Bottom;
var instFont = Settings.FontCapsuleParameterData;
//var instText = GetDataDescription(Param.VolatileData, graphics, instFont, new SizeF(descBox.Width, Bounds.Height - nameBox.Height - Padding - descBox.Height - Padding), out float intsTextHeight);
var instTextHeightLine = graphics.MeasureString("Qq", instFont).Height;
var instTextLineCount = Param.VolatileDataCount + Param.VolatileData.PathCount + 2;
var intsTextHeight = instTextLineCount * instTextHeightLine;
var instBox = new RectangleF(
descBox.X,
descBox.Bottom,
descBox.Width - Padding,
intsTextHeight + Padding
);
var instVisible = descVisible && (instBox.Bottom <= Bounds.Bottom || ZoomFactor > 0.5f);
var iconSize = ZoomFactor >= 1f ? Math.Min(ScaleSize(3f), nameBox.Height) : 0f;
var iconBox = new RectangleF(
IsInput ? descBox.X : descBox.Right - iconSize - Padding / 2f,
nameBox.Y + nameBox.Height / 2f - iconSize / 2f,
iconSize,
iconSize
);
var iconVisible = iconSize >= 1;
if (iconVisible)
{
if (IsInput)
{
nameBox.X += iconSize;
}
nameBox.Width -= iconSize;
}
//if (!string.IsNullOrEmpty(Param.NickName) && Param.Name != Param.NickName)
//{
// var nameTextComplete = $"{Param.Name} ({Param.NickName})";
// if (graphics.MeasureString(nameTextComplete, nameFont).Width < nameBox.Width)
// {
// nameText = nameTextComplete;
// }
// else
// {
// if (!Grasshopper.CentralSettings.CanvasFullNames && ZoomFactor > 0.5f && graphics.MeasureString(Param.Name, nameFont).Width < nameBox.Width)
// {
// nameText = Param.Name;
// }
// }
//}
//else
//{
// if (!Grasshopper.CentralSettings.CanvasFullNames && ZoomFactor > 0.5f && graphics.MeasureString(Param.Name, nameFont).Width < nameBox.Width)
// {
// nameText = Param.Name;
// }
//}
if (!Grasshopper.CentralSettings.CanvasFullNames && ZoomFactor > 0.5f && graphics.MeasureString(Param.Name, nameFont).Width < nameBox.Width)
{
nameText = Param.Name;
}
var totalHeight = nameBox.Height + Padding + descBox.Height + (instVisible ? Padding + Math.Min(instBox.Height, (Bounds.Height - nameBox.Height - Padding - descBox.Height - Padding)) : 0f);
var dy = ZoomFactor * ((Bounds.Y + Bounds.Height / 2f) - (nameBox.Y + (totalHeight - nameBox.Height / 2f + nameTextHeight / 2f) / 2f));
iconBox.Offset(0, dy);
nameBox.Offset(0, dy);
descBox.Offset(0, dy);
instBox.Offset(0, dy);
if (nameBox.Y < Bounds.Y)
{
dy = Bounds.Y - nameBox.Y;
iconBox.Offset(0, dy);
nameBox.Offset(0, dy);
descBox.Offset(0, dy);
instBox.Offset(0, dy);
}
if (ZoomFactor > 0.5f)
{
descVisible = true;
var currentDescBoxHeight = descBox.Height;
descBox.Height = Math.Min(descBox.Height, Bounds.Bottom - descBox.Y - Padding / 2f - Math.Min(instBox.Height, ScaleSize(4f)));
if (descBox.Height < currentDescBoxHeight)
{
descFont = AdjustFontHeight(graphics, descFont, descText, descBox);
}
descVisible = true;
instBox.Y = descBox.Bottom + Padding;
instBox.Height = Bounds.Bottom - instBox.Y - Padding / 2f;
instVisible = instBox.Bottom <= Bounds.Bottom;
}
else if (ZoomFactor > 0.2f)
{
var currentDescBoxHeight = descBox.Height;
descBox.Height = Bounds.Bottom - descBox.Y - Padding / 2f;
if (descBox.Height < currentDescBoxHeight)
{
descFont = AdjustFontHeight(graphics, descFont, descText, descBox);
}
descVisible = true;
instBox.Y = descBox.Bottom + Padding;
}
instVisible &= ZoomFactor >= 0.5;
if (instVisible)
{
//var lineHeight = graphics.MeasureString("Qy", instFont, (int)Math.Ceiling(descBox.Width)).Height;
///var lineCount = Param.VolatileData.DataDescription(false, true).Split(new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None).Length;
instBox.Height = Math.Min(instBox.Height, instTextHeightLine * Math.Max(3, instTextLineCount));
}
totalHeight = nameBox.Height + (descVisible ? Padding + descBox.Height : 0f) + (instVisible ? Padding + instBox.Height : 0f);
//if (totalHeight < Bounds.Height)
//{
//dy = ZoomFactor * ((Bounds.Y + Bounds.Height / 2f - totalHeight/2f) - (nameBox.Y + nameBox.Height /2f - nameTextHeight/2f));
//iconBox.Offset(0, dy);
//nameBox.Offset(0, dy);
//descBox.Offset(0, dy);
//instBox.Offset(0, dy);
//}
var brushText = new SolidBrush(Style.Text);
var brushTextFaded = new SolidBrush(FadeColor(Style.Text));
graphics.DrawString(nameText, nameFont, brushText, nameBox, nameFormat);
if (iconVisible)
graphics.DrawImage(Param.Locked ? Param.Icon_24x24_Locked : Param.Icon_24x24, iconBox);
if (descVisible)
graphics.DrawString(descText, descFont, brushTextFaded, descBox, descFormat);
if (instVisible)
PaintCapsuleParameterData(graphics, instBox, Param, Style);
/*
PaintRedBorders(graphics, iconBox);
PaintRedBorders(graphics, nameBox);
PaintRedBorders(graphics, descBox);
PaintRedBorders(graphics, instBox);
*/
nameFont.Dispose();
descFont.Dispose();
nameFormat.Dispose();
brushTextFaded.Dispose();
}
}
internal class RichedCapsuleComponentInfo : RichedCapsule
{
public IGH_Component Component { get; }
public RichedCapsuleComponentInfo(GH_ComponentAttributes att) : base(att)
{
Component = Attributes.DocObject as IGH_Component;
}
protected override RectangleF CreateBounds()
{
var box = ((GH_ComponentAttributes)Attributes).ContentBox;
box.X = (float)Math.Floor(box.X);
if (Attributes.DocObject.IconDisplayMode != GH_IconDisplayMode.icon)
box.Inflate(1, 1);
return box;
}
public override void Render(Graphics graphics, GH_Canvas canvas)
{
var textFormat = new StringFormat() { Alignment = StringAlignment.Center, LineAlignment = StringAlignment.Near };
var iconBox = new RectangleF(
Bounds.X + Bounds.Width / 2f - Component.Icon_24x24.Width / 2f,
Bounds.Y + Bounds.Height / 2f - Component.Icon_24x24.Height / 2f,
Component.Icon_24x24.Width,
Component.Icon_24x24.Height
);
var nameText = Component.Name;
if (!string.IsNullOrEmpty(Component.NickName) && Component.Name != Component.NickName)
{
nameText = $"{nameText} ({Component.NickName})";