-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MenuItem.cs
458 lines (391 loc) · 19.4 KB
/
MenuItem.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using Grasshopper.GUI;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Special;
namespace Sunglasses
{
public class SunglassesMenuItem : ToolStripMenuItem
{
private ToolStripMenuItem _menuNicknames;
private ToolStripMenuItem _menuCustomNicknames;
private ToolStripMenuItem _menuFilter;
public SunglassesMenuItem()
: base("Sunglasses", Properties.Resources.sunglasses, ClickHandler)
{
Name = "SunglassesMenuItem";
Size = new Size(200, 30);
ToolTipText = "Draw the name or nickname above the component, so that it can be used in icon mode.\n\rVersion " + typeof(SunglassesMenuItem).Assembly.GetName().Version;
Checked = Settings.DisplayNames;
_menuNicknames = GH_DocumentObject.Menu_AppendItem(DropDown, "Display nicknames",
DisplayNicknamesHandler, true, Settings.DisplayNicknames);
_menuNicknames.ToolTipText = "Draw the nickname instead of the name of the objects.";
_menuCustomNicknames = GH_DocumentObject.Menu_AppendItem(DropDown, "Display only custom nicknames",
DisplayCustomNicknamesHandler, true, Settings.DisplayCustomNicknames);
_menuCustomNicknames.ToolTipText = "Draw just the user defined nicknames.";
_menuFilter = GH_DocumentObject.Menu_AppendItem(DropDown, "Filter objects");
_menuFilter.ToolTipText = "Select in which objects to draw the name.";
GH_DocumentObject.Menu_AppendItem(_menuFilter.DropDown, "Components", FilterComponentHandler, true, Settings.FilterComponents)
.ToolTipText = "If checked, draw the name of components.";
GH_DocumentObject.Menu_AppendItem(_menuFilter.DropDown, "Parameters", FilterParameterHandler, true, Settings.FilterParameters)
.ToolTipText = "If checked, draw the name of parameters.";
GH_DocumentObject.Menu_AppendItem(_menuFilter.DropDown, "Special", FilterSpecialHandler, true, Settings.FilterSpecial)
.ToolTipText = "If checked, draw the name of special objects, like Number Slider, Panel, Gradient...";
GH_DocumentObject.Menu_AppendItem(_menuFilter.DropDown, "Graphics", FilterGraphicHandler, true, Settings.FilterGraphic)
.ToolTipText = "If checked, draw the name of graphic objects, like Group, Scribble, Jump...";
var customFilter = GH_DocumentObject.Menu_AppendItem(_menuFilter.DropDown, "Exclusions");
customFilter.ToolTipText = "Insert the names or nicknames of the objects, separated by commas, that you want to exclude.";
//GH_DocumentObject.Menu_AppendTextItem(customFilter.DropDown, Utils.FilterCustom, FilterCustomKeyDownHandler, FilterCustomTextChangedHandler, true);
var textBox = new ToolStripTextBox()
{
Size = new Size(200, 24),
Text = Settings.FilterCustom,
BorderStyle = BorderStyle.FixedSingle
};
textBox.TextChanged += TextBox_TextChanged;
customFilter.DropDownItems.Add(textBox);
GH_DocumentObject.Menu_AppendSeparator(DropDown);
var riched = GH_DocumentObject.Menu_AppendItem(DropDown, "Riched capsules", DisplayRichedAttributesHandler, true, Settings.DisplayRichedCapsules);
riched.ToolTipText = "Shows all the information within a component when zoomed in. ";
GH_DocumentObject.Menu_AppendItem(DropDown, "Hide on low zoom", HideOnLowZoomHandler, true, Settings.HideOnLowZoom)
.ToolTipText = "If checked, the name disappears when the canvas zoom is very low.";
GH_DocumentObject.Menu_AppendItem(DropDown, "Big nicknames on groups", GroupsOnLowZoomHandler, true, Settings.GroupsOnLowZoom)
.ToolTipText = "If checked, the nickname of the groups is drawn with low zoom.";
GH_DocumentObject.Menu_AppendSeparator(DropDown);
if (Rhino.Runtime.HostUtils.RunningOnWindows)
{
GH_DocumentObject.Menu_AppendItem(DropDown, "Select font", SelectFontHandler);
}
else
{
var slider = new Grasshopper.GUI.GH_DigitScroller
{
MinimumValue = 3,
MaximumValue = 256,
DecimalPlaces = 1,
Value = (decimal)Settings.Font.Size,
Size = new Size(200, 24),
Name = "Font size"
};
slider.ValueChanged += Slider_ValueChanged;
GH_DocumentObject.Menu_AppendCustomItem(DropDown, slider);
}
UpdateCustomNicknameMenuEntryState();
}
private void SelectFontHandler(object sender, EventArgs e)
{
var form = GH_FontPicker.CreateFontPickerWindow(Settings.Font);
form.CreateControl();
var picker = form.Controls.OfType<GH_FontPicker>().FirstOrDefault();
if (picker == null)
return;
var panel = form.Controls.OfType<Panel>().FirstOrDefault();
if (panel == null)
return;
var pickerType = typeof(GH_FontPicker);
var sizeScroller = pickerType.GetField("_SizeScroller", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(picker) as Grasshopper.GUI.GH_DigitScroller;
var boldChecker = pickerType.GetField("_BoldCheck", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(picker) as CheckBox;
var italicChecker = pickerType.GetField("_ItalicCheck", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(picker) as CheckBox;
var fontScroller = pickerType.GetField("_FontList", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(picker) as GH_FontList;
sizeScroller.ValueChanged += PreviewFontChangedHandler;
boldChecker.CheckedChanged += PreviewFontChangedHandler;
italicChecker.CheckedChanged += PreviewFontChangedHandler;
fontScroller.MouseClick += PreviewFontChangedHandler;
var defaultButton = new Button()
{
Text = "Default",
Width = Grasshopper.Global_Proc.UiAdjust(80),
Dock = DockStyle.Right,
DialogResult = DialogResult.Yes
};
defaultButton.Click += DefaultFontHandler;
panel.Controls.Add(defaultButton);
var editor = Grasshopper.Instances.DocumentEditor;
GH_WindowsFormUtil.CenterFormOnWindow(form, editor, true);
var result = form.ShowDialog(editor);
if (result == DialogResult.OK)
{
var font = form.Tag as Font;
if (font != null)
{
Settings.ResetFonts();
Settings.Font = font;
}
}
else if (result == DialogResult.Yes)
{
Settings.ResetFonts();
Settings.Font = GH_FontServer.StandardItalic;
}
Grasshopper.Instances.ActiveCanvas?.Refresh();
sizeScroller.ValueChanged -= PreviewFontChangedHandler;
boldChecker.CheckedChanged -= PreviewFontChangedHandler;
italicChecker.CheckedChanged -= PreviewFontChangedHandler;
fontScroller.MouseClick -= PreviewFontChangedHandler;
defaultButton.Click -= DefaultFontHandler;
void PreviewFontChangedHandler(object s, EventArgs args)
{
var font = picker.SelectedFont;
if (font != null)
{
var currentFont = Settings.Font;
Settings.Font = font;
Grasshopper.Instances.ActiveCanvas?.Refresh();
Settings.Font = currentFont;
}
}
void DefaultFontHandler(object s, EventArgs args)
{
}
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
var item = (ToolStripTextBox)sender;
if (item == null)
return;
if (IsValidFilterCustom(item.Text))
{
item.ForeColor = Color.Black;
Settings.FilterCustom = item.Text;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
else
{
item.ForeColor = Color.IndianRed;
}
}
private void FilterComponentHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.FilterComponents = !Settings.FilterComponents;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void FilterParameterHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.FilterParameters = !Settings.FilterParameters;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void FilterGraphicHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.FilterGraphic = !Settings.FilterGraphic;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void FilterSpecialHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.FilterSpecial = !Settings.FilterSpecial;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private bool IsValidFilterCustom(string text)
{
if (string.IsNullOrEmpty(text))
return true;
var split = text.Split(',')
.Select(t => t.TrimStart().TrimEnd())
.Where(t => !string.IsNullOrWhiteSpace(t))
.ToArray();
return split.All(t => Exists(t));
bool Exists(string key)
{
foreach(var p in Grasshopper.Instances.ComponentServer.ObjectProxies)
{
if (p.Exposure == GH_Exposure.hidden)
continue;
if (p.Desc.Name.Equals(key, StringComparison.OrdinalIgnoreCase) ||
p.Desc.NickName.Equals(key, StringComparison.OrdinalIgnoreCase))
return true;
}
if (Grasshopper.Instances.ActiveCanvas.IsDocument)
{
foreach (var obj in Grasshopper.Instances.ActiveCanvas.Document.Objects)
{
if (obj.Name.Equals(key, StringComparison.OrdinalIgnoreCase) ||
obj.NickName.Equals(key, StringComparison.OrdinalIgnoreCase))
return true;
}
}
return false;
}
}
private void UpdateCustomNicknameMenuEntryState()
{
if (!Settings.DisplayNicknames)
{
// Set Checked to false doesn't automatically provoke the corresponding event.
// Set the setting yourself.
_menuCustomNicknames.Checked = false;
Settings.DisplayCustomNicknames = false;
}
_menuCustomNicknames.Enabled = Settings.DisplayNicknames;
}
private void DisplayNicknamesHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.DisplayNicknames = !Settings.DisplayNicknames;
UpdateCustomNicknameMenuEntryState();
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void DisplayCustomNicknamesHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.DisplayCustomNicknames = !Settings.DisplayCustomNicknames;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void DisplayRichedAttributesHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.DisplayRichedCapsules = !Settings.DisplayRichedCapsules;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void HideOnLowZoomHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.HideOnLowZoom = !Settings.HideOnLowZoom;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void GroupsOnLowZoomHandler(object sender, EventArgs e)
{
((ToolStripMenuItem)sender).Checked = Settings.GroupsOnLowZoom = !Settings.GroupsOnLowZoom;
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private void Slider_ValueChanged(object sender, Grasshopper.GUI.Base.GH_DigitScrollerEventArgs e)
{
Settings.Font = new Font(Settings.Font.FontFamily, (float)e.Value);
Grasshopper.Instances.ActiveCanvas?.Refresh();
}
private static void ClickHandler(object sender, EventArgs e)
{
var item = (SunglassesMenuItem)sender;
item.Checked = !item.Checked;
}
const string NameOfClusterComponent = "Cluster";
private bool ObjectFilter(IGH_DocumentObject obj)
{
if (Settings.DisplayNicknames || Settings.DisplayCustomNicknames)
{
if (obj is GH_Panel || obj is GH_Group || obj is GH_NumberSlider)
return false;
}
if (Settings.IsFilterCustomEnabled &&
Settings.ShouldExcludeObject(obj))
return false;
if (!Settings.FilterComponents &&
(obj is GH_Cluster || obj is IGH_Component || obj.ComponentGuid == galapagosID))
return false;
if (!Settings.FilterSpecial &&
IsSpecialObject(obj))
return false;
if (!Settings.FilterParameters &&
obj is IGH_Param)
return false;
if (!Settings.FilterGraphic &&
!(obj is IGH_ActiveObject))
return false;
if (Settings.DisplayCustomNicknames)
{
if(obj is IGH_Param param && param.Kind == GH_ParamKind.floating &&
(param.IconDisplayMode == GH_IconDisplayMode.name ||
param.IconDisplayMode == GH_IconDisplayMode.application && !Grasshopper.CentralSettings.CanvasObjectIcons))
{
return false;
}
var code = string.Join(".", obj.Category, obj.SubCategory, obj.Name);
var id = obj.ComponentGuid;
if (!_nicknamesCache.TryGetValue(code, out var defNick))
{
IGH_ObjectProxy proxy = null;
if (id == clusterID || id == vbID || id == csID || id == pyID || id == Guid.Empty)
{
proxy = Grasshopper.Instances.ComponentServer.FindObjectByName(obj.Name, true, true);
}
else
{
proxy = Grasshopper.Instances.ComponentServer.EmitObjectProxy(id);
}
if (proxy != null)
{
defNick = proxy.Desc.NickName;
_nicknamesCache.Add(code, defNick);
}
else
{
if (obj is GH_Cluster)
{
return obj.NickName != NameOfClusterComponent;
}
return true;
}
}
return !defNick.Equals(obj.NickName, StringComparison.OrdinalIgnoreCase);
}
return true;
}
private static Dictionary<Guid, bool> _specialObjectGuids = new Dictionary<Guid, bool>();
private static bool IsSpecialObject(IGH_DocumentObject obj)
{
if (obj is GH_Cluster)
return false;
var guid = obj.ComponentGuid;
if (_specialObjectGuids.TryGetValue(guid, out var result)) return result;
return _specialObjectGuids[guid] = obj.GetType().Namespace == "Grasshopper.Kernel.Special";
}
protected override void OnCheckedChanged(EventArgs e)
{
base.OnCheckedChanged(e);
Settings.DisplayNames = Checked;
var canvas = Grasshopper.Instances.ActiveCanvas;
if (canvas == null)
return;
if (Checked)
{
canvas.CanvasPrePaintObjects -= Canvas_CanvasPrePaintObjects;
canvas.CanvasPrePaintObjects += Canvas_CanvasPrePaintObjects;
canvas.CanvasPostPaintObjects -= Canvas_CanvasPostPaintObjects;
canvas.CanvasPostPaintObjects += Canvas_CanvasPostPaintObjects;
}
else
{
canvas.CanvasPrePaintObjects -= Canvas_CanvasPrePaintObjects;
canvas.CanvasPostPaintObjects -= Canvas_CanvasPostPaintObjects;
}
canvas.Refresh();
}
private IGH_DocumentObject[] _visibleObjects;
private IGH_DocumentObject[] _filteredObjects;
private SortedDictionary<string, string> _nicknamesCache = new SortedDictionary<string, string>();
private void Canvas_CanvasPrePaintObjects(Grasshopper.GUI.Canvas.GH_Canvas sender)
{
if (!sender.IsDocument || !Settings.DisplayNames)
return;
_visibleObjects = null;
_filteredObjects = null;
if (!Settings.DisplayNames && !Settings.DisplayRichedCapsules || !sender.IsDocument)
return;
_visibleObjects = Drawing.GetVisibleObjects(sender, sender.Document.Objects).ToArray();
if (_visibleObjects == null || _visibleObjects.Length == 0)
return;
_filteredObjects = _visibleObjects.Where(obj => ObjectFilter(obj)).ToArray();
if (_filteredObjects == null || _visibleObjects.Length == 0)
return;
Drawing.PaintNames(sender.Graphics, _filteredObjects);
}
private void Canvas_CanvasPostPaintObjects(Grasshopper.GUI.Canvas.GH_Canvas sender)
{
if (!sender.IsDocument || _visibleObjects == null || _visibleObjects.Length == 0)
return;
if(Settings.GroupsOnLowZoom && sender.Viewport.Zoom <= 0.5f)
{
var groups = _visibleObjects.OfType<GH_Group>();
if (Settings.IsFilterCustomEnabled)
{
groups = groups.Where(g => !Settings.ShouldExcludeObject(g));
}
Drawing.PaintGroupNickname(sender, groups);
}
if (Settings.DisplayRichedCapsules)
Drawing.PaintRichedCapsules(sender, _visibleObjects);
}
private Guid galapagosID = new Guid("E6DD2904-14BC-455b-8376-948BF2E3A7BC");
private Guid clusterID = new Guid("f31d8d7a-7536-4ac8-9c96-fde6ecda4d0a");
private Guid vbID = new Guid("079bd9bd-54a0-41d4-98af-db999015f63d");
private Guid csID = new Guid("a9a8ebd2-fff5-4c44-a8f5-739736d129ba");
private Guid pyID = new Guid("410755b1-224a-4c1e-a407-bf32fb45ea7e");
}
}