-
Notifications
You must be signed in to change notification settings - Fork 11
/
MenuDropDown.cs
565 lines (479 loc) · 17.9 KB
/
MenuDropDown.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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using GH_IO.Serialization;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
namespace SandWorm
{
public class MenuDropDown : GH_Attr_Widget
{
private class MenuDropDownWindow : GH_Attr_Widget
{
private MenuDropDown _dropMenu;
private int _tempActive = -1;
private int _tempStart;
private int _maxLen;
private Rectangle _contentBox;
public MenuDropDownWindow(MenuDropDown parent)
: base(0, "")
{
_dropMenu = parent;
}
public void Update()
{
int count = _dropMenu.Items.Count;
if (_dropMenu.LastValidValue > count)
_dropMenu.Value = -1;
if (_dropMenu.Items.Count == 0)
_tempStart = 0;
_maxLen = count;
int num2 = count * 20;
base.Height = num2;
_contentBox = new Rectangle((int)base.CanvasPivot.X, (int)base.CanvasPivot.Y, (int)base.Width, num2);
}
public override SizeF ComputeMinSize()
{
return new SizeF(10f, 10f);
}
public override void Layout()
{
Update();
}
public override void Render(WidgetRenderArgs args)
{
if (args.Channel != WidgetChannel.Overlay)
{
return;
}
Graphics graphics = args.Canvas.Graphics;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
Pen pen = new Pen(Brushes.Gray);
System.Drawing.Drawing2D.GraphicsPath path = RoundedRect(_contentBox, 2);
graphics.DrawPath(pen, path);
graphics.FillPath(Brushes.White, path);
int num = 0;
for (int i = _tempStart; i < _tempStart + _maxLen; i++)
{
Brush white = Brushes.White;
Brush white2 = Brushes.White;
if (i == _tempActive)
{
white = new SolidBrush(Color.FromArgb(174, 213, 129));
white2 = Brushes.White;
}
else if (i == _dropMenu.Value)
{
white = new SolidBrush(Color.FromArgb(238, 238, 238));
white2 = new SolidBrush(Color.FromArgb(45, 45, 45));
}
else
{
white = new SolidBrush(Color.White);
white2 = new SolidBrush(Color.FromArgb(45, 45, 45));
}
System.Drawing.Drawing2D.GraphicsPath rect = RoundedRect(new Rectangle((int)base.Transform.X, (int)base.Transform.Y + 20 * num, (int)base.Width, 20), 2);
graphics.FillPath(white, rect);
graphics.DrawString(_dropMenu.Items[i].content, WidgetServer.Instance.DropdownFont, white2, base.Transform.X + base.Width / 2f, (int)base.Transform.Y + 20 * num + 5, stringFormat);
num++;
}
}
public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
{
return GH_ObjectResponse.Capture;
}
public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
{
if (Contains(e.CanvasLocation))
_tempActive = _tempStart + (int)((e.CanvasLocation.Y - base.Transform.Y) / 20f);
else
_tempActive = -1;
return GH_ObjectResponse.Capture;
}
public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
{
if (_contentBox.Contains((int)e.CanvasLocation.X, (int)e.CanvasLocation.Y))
{
_dropMenu.Value = _tempStart + (int)((e.CanvasLocation.Y - base.Transform.Y) / 20f);
_tempActive = -1;
_dropMenu.HideWindow(fire: true);
return GH_ObjectResponse.Release;
}
_dropMenu.HideWindow(fire: false);
return GH_ObjectResponse.Release;
}
public override bool Contains(PointF pt)
{
return canvasBounds.Contains(pt);
}
}
public class Entry
{
public string content;
public string name;
public string Header { get; set; }
public int index;
public object data;
public Entry(string name, string content, int ind)
{
this.content = content;
this.name = name;
index = ind;
}
public Entry(string name, string content, int ind, string header)
{
this.content = content;
this.name = name;
index = ind;
Header = header;
}
}
private MenuDropDownWindow _window;
public bool expanded;
private static int default_item_index = 0;
private int current_value;
private int last_valid_value;
private List<Entry> _items;
private string _emptyText = "empty";
public int Value
{
get
{
return current_value;
}
set
{
current_value = Math.Max(value, 0);
last_valid_value = ((value >= 0) ? value : 0);
}
}
public int LastValidValue => last_valid_value;
public List<Entry> Items => _items;
/// <summary>
/// Gets selected item
/// </summary>
public Entry Item => _items[Value];
private bool Empty => _items.Count == 0;
public event ValueChangeEventHandler ValueChanged;
public int FindIndex(string name)
{
for (int i = 0; i < _items.Count; i++)
{
if (_items[i].name.Equals(name))
{
return i;
}
}
return -1;
}
public override void PostUpdateBounds(out float outHeight)
{
_window.Width = base.Width;
outHeight = ComputeMinSize().Height;
}
/// <summary>
///
/// </summary>
/// <param name="index"></param>
/// <param name="id"></param>
/// <param name="tag">currently UNUSED</param>
public MenuDropDown(int index, string id, string tag = "")
: base(index, id)
{
_items = new List<Entry>();
_window = new MenuDropDownWindow(this);
_window.Parent = this;
}
/// <summary>
/// Use this one if you want to create a dropdown but keep the desriptions from the textinput.
/// </summary>
/// <param name="index"></param>
/// <param name="id"></param>
/// <param name="textInput"></param>
public MenuDropDown(MenuStaticText textInput, int index = 0, string id = "")
: base(index, id)
{
Header = textInput.Header;
Name = textInput.Text;
_items = new List<Entry>();
_window = new MenuDropDownWindow(this);
_window.Parent = this;
}
public MenuDropDown AddItem(string name, string cont, string header = "")
{
Entry item = new Entry(name, cont, _items.Count, header);
_items.Add(item);
Update();
return this; //fluent interface ftw
}
public MenuDropDown AddItem(string name, string cont, object data)
{
Entry entry = new Entry(name, cont, _items.Count);
entry.data = data;
_items.Add(entry);
Update();
return this; //fluent interface ftw
}
/// <summary>
/// Populate your dropdown menus from an Enum list. use typeof(yourEnum)
/// </summary>
/// <param name="enumType">input typeof(MyEnum)</param>
public MenuDropDown AddEnum(Type enumType)
{
if (!typeof(Enum).IsAssignableFrom(enumType))
throw new ArgumentException("enumType should describe an enum");
Array names = Enum.GetNames(enumType);
foreach (string name in names)
{
AddItem(name, name, name);
}
return this;
}
private void Update()
{
if (_items.Count == 0)
{
current_value = 0;
}
_window.Update();
}
public override void Layout()
{
_window.UpdateBounds(base.CanvasPivot, base.Width);
_window.Layout();
}
public void Clear()
{
_items.Clear();
Update();
}
public override SizeF ComputeMinSize()
{
int num = 0;
int num2 = 0;
if (Empty)
{
Size size = TextRenderer.MeasureText(_emptyText, WidgetServer.Instance.DropdownFont);
num = size.Width + 4 + 10;
num2 = size.Height + 2;
}
else
{
foreach (Entry item in _items)
{
Size size2 = TextRenderer.MeasureText(item.content, WidgetServer.Instance.DropdownFont);
int val = size2.Width + 4 + 10;
int val2 = size2.Height + 2;
num = Math.Max(num, val);
num2 = Math.Max(num2, val2);
}
}
return new SizeF(num, num2);
}
public override void Render(WidgetRenderArgs args)
{
GH_Canvas canvas = args.Canvas;
if (args.Channel == WidgetChannel.Overlay)
{
if (expanded)
{
_window.Render(args);
}
}
else if (args.Channel == WidgetChannel.Object)
{
Graphics graphics = canvas.Graphics;
float zoom = canvas.Viewport.Zoom;
int num = 255;
if (zoom < 1f)
{
float num2 = (zoom - 0.5f) * 2f;
num = (int)((float)num * num2);
}
if (num < 0)
{
num = 0;
}
num = GH_Canvas.ZoomFadeLow;
SolidBrush brush = new SolidBrush(Color.FromArgb(num, 90, 90, 90));
SolidBrush brush2 = new SolidBrush(Color.FromArgb(num, 190, 190, 190));
SolidBrush brush3 = new SolidBrush(Color.FromArgb(num, 45, 45, 45));
SolidBrush brush4 = new SolidBrush(Color.FromArgb(num, 255, 255, 255));
Pen pen = new Pen(brush2);
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
if (Empty)
{
PointF point = new PointF(base.CanvasPivot.X + base.Width / 2f, base.CanvasBounds.Y + 2f);
var path = RoundedRect(GH_Attr_Widget.Convert(base.CanvasBounds), 2);
graphics.DrawPath(pen, path);
graphics.FillPath(brush4, path);
graphics.DrawString(_emptyText, WidgetServer.Instance.DropdownFont, brush, point, stringFormat);
}
else
{
PointF point2 = new PointF(base.CanvasPivot.X + (base.Width - 13f) / 2f, base.CanvasBounds.Y + 2f);
var path = RoundedRect(GH_Attr_Widget.Convert(base.CanvasBounds), 2);
graphics.DrawPath(pen, path);
graphics.FillPath(brush4, path);
graphics.DrawString(_items[current_value].content, WidgetServer.Instance.DropdownFont, brush, point2, stringFormat);
PointF p1 = new PointF(base.CanvasPivot.X + base.Width - 13, base.CanvasPivot.Y + 6);
PointF p2 = new PointF(base.CanvasPivot.X + base.Width - 5, base.CanvasPivot.Y + 6);
PointF p3 = new PointF(base.CanvasPivot.X + base.Width - 9, base.CanvasPivot.Y + 12);
PointF[] curvePoints = { p1, p2, p3 };
//graphics.DrawPolygon(pen, curvePoints);
graphics.FillPolygon(brush3, curvePoints);
}
}
}
// Fixed tooltip in the folded dropdown menu - TODO: Fix it in the unfolded!
// It is hidden in the MenuScrollBar class, but I didn't manage to get the popup to work.
public override void TooltipSetup(PointF canvasPoint, GH_TooltipDisplayEventArgs e)
{
e.Icon = null;
e.Title = _name;
e.Text = _header;
if (_header != null)
{
e.Text += "\n";
}
e.Description = _description;
}
// This allowed us to get the static text tooltip :-)
public override GH_Attr_Widget IsTtipPoint(System.Drawing.PointF pt)
{
if (new System.Drawing.RectangleF(transfromation.X, transfromation.Y, base.Width, base.Height).Contains(pt))
{
return this;
}
return null;
}
public static System.Drawing.Drawing2D.GraphicsPath RoundedRect(Rectangle bounds, int radius)
{
int diameter = radius * 2;
Size size = new Size(diameter, diameter);
Rectangle arc = new Rectangle(bounds.Location, size);
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
if (radius == 0)
{
path.AddRectangle(bounds);
return path;
}
// top left arc
path.AddArc(arc, 180, 90);
// top right arc
arc.X = bounds.Right - diameter;
path.AddArc(arc, 270, 90);
// bottom right arc
arc.Y = bounds.Bottom - diameter;
path.AddArc(arc, 0, 90);
// bottom left arc
arc.X = bounds.Left;
path.AddArc(arc, 90, 90);
path.CloseFigure();
return path;
}
public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
{
if (Empty)
{
return GH_ObjectResponse.Release;
}
if (expanded)
{
return _window.RespondToMouseUp(sender, e);
}
return GH_ObjectResponse.Ignore;
}
public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
{
if (expanded)
{
return _window.RespondToMouseMove(sender, e);
}
return GH_ObjectResponse.Ignore;
}
public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
{
if (Empty)
{
return GH_ObjectResponse.Handled;
}
if (expanded)
{
if (_window.Contains(e.CanvasLocation))
{
return _window.RespondToMouseDown(sender, e);
}
HideWindow(fire: false);
return GH_ObjectResponse.Release;
}
ShowWindow();
return GH_ObjectResponse.Capture;
}
public void ShowWindow()
{
if (!expanded)
{
expanded = true;
TopCollection.ActiveWidget = this;
Update();
}
}
public void HideWindow(bool fire)
{
if (expanded)
{
expanded = false;
TopCollection.ActiveWidget = null;
TopCollection.MakeAllInActive();
if (fire && this.ValueChanged != null)
{
this.ValueChanged(this, new EventArgs());
}
}
}
public override bool Contains(PointF pt)
{
return base.CanvasBounds.Contains(pt);
}
public override bool Write(GH_IWriter writer)
{
writer.CreateChunk("MenuDropDown", Index).SetInt32("ActiveItemIndex", current_value);
return true;
}
public override bool Read(GH_IReader reader)
{
GH_IReader gH_IReader = reader.FindChunk("MenuDropDown", Index);
try
{
current_value = gH_IReader.GetInt32("ActiveItemIndex");
}
catch
{
current_value = default_item_index;
}
return true;
}
/// <summary>
/// updates the refered variable to be same enum as the dropdown. it does NOT update the document
/// </summary>
/// <typeparam name="T">double, int or enum</typeparam>
/// <param name="reference">Can be any field hosting an enum</param>
public MenuDropDown SetDefault<T>(ref T reference) where T : IConvertible
{
if (typeof(T).IsEnum)
{
reference = (T)Enum.Parse(typeof(T), Item.name);
}
else if (typeof(T) == typeof(String))
{
reference = (T)(object)Item.name;
}
return this; // return allows to fluent interface
}
}
}