-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathGHCustomAttributes.cs
More file actions
353 lines (278 loc) · 13 KB
/
GHCustomAttributes.cs
File metadata and controls
353 lines (278 loc) · 13 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
using Grasshopper;
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Attributes;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
Original work Copyright (c) 2021 Ali Torabi (ali@parametriczoo.com)
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.
*/
namespace GHCustomControls
{
public class GHCustomAttributes : GH_ComponentAttributes
{
/// <summary>
/// the amount of the offset for the bounds of this componenet from the largest sub-control
/// </summary>
float _offset = 2;
/// <summary>
/// false when expireLayout() is called , true when Layout() is called
/// this is to avoid multiple call to Layout()
/// </summary>
bool _layout;
public GHCustomAttributes(GHCustomComponent owner) : base(owner)
{
}
/// <summary>
/// redraw the layout
/// </summary>
internal void Redraw()
{
if (_layout)
{
_layout = false;
ExpireLayout();
}
}
#region Custom layout logic
protected override void Layout()
{
_layout = true;
// draw basic layout
base.Layout();
PointF p = Pivot;
RectangleF b = Bounds;
GHCustomComponent owner = Owner as GHCustomComponent;
if (owner.CustomControls.Values.Count == 0)
return;
// we add the custom constrols to the buttom of each other in the same order they have been added to the dictionary
// the value buttom records the buttom postion of the controls as they are being added
float buttom = Bounds.Bottom;
// to find the overal widht of the component we find the maximum width of the customcontrols
float maxWidth = Bounds.Width;
foreach (GHControl item in owner.CustomControls.Values)
{
float w = (item.IsVisible)? item.GetWidth():0;
if (maxWidth < w)
maxWidth = w;
}
foreach (GHControl item in owner.CustomControls.Values)
{
float h = (item.IsVisible)? item.GetHeight():0;
// set the item bounds , we expand the bounds by 2 units from each side
item.Bounds = new RectangleF(Bounds.X, buttom, maxWidth, h);
buttom += h+_offset; // update the buttom value
}
float currentWidth = Bounds.Width;
var corner = new PointF(Bounds.X - _offset, Bounds.Y - _offset);
Bounds = new RectangleF(corner, new SizeF(maxWidth + 2 * _offset, Bounds.Height + buttom - Bounds.Bottom + 2 * _offset));
if (maxWidth > currentWidth)
{
// // update the output parameter layout
// int paramWidth = Owner.Params.Output.Max(
// p => GH_FontServer.StringWidth(
// (CentralSettings.CanvasFullNames) ? p.Name : p.NickName
// , SmallFont
// )
// );
//LayoutOutputParams(Owner, new RectangleF(Bounds.X , Bounds.Y, maxWidth, Bounds.Height));
foreach (var param in Owner.Params.Output)
{
RectangleF rec = param.Attributes.Bounds;
rec.Offset(Bounds.Right - param.Attributes.Bounds.Right, 0);
param.Attributes.Bounds = rec;
}
}
//Pivot = new PointF((Bounds.X + Bounds.Width) / 2 -12, Pivot.Y);
}
#endregion
#region Custom Mouse handling
public override GH_ObjectResponse RespondToMouseDown(GH_Canvas sender, GH_CanvasMouseEvent e)
{
GHCustomComponent owner = Owner as GHCustomComponent;
if (Bounds.Contains(e.CanvasLocation))
{
// click is inside the component
// check which control is clicked
GHMouseEventResult result = GHMouseEventResult.None;
foreach (GHControl control in owner.CustomControls.Values)
{
if (control.Bounds.Contains(e.CanvasLocation))
{
// click happened in this control
if (e.Button == System.Windows.Forms.MouseButtons.Left)
control.MouseLeftClick( sender, owner, e, ref result);
else if (e.Button == System.Windows.Forms.MouseButtons.Right)
control.MouseRightClick( sender, owner, e, ref result);
if (!control.UpdateSolution)
{
result &= ~GHMouseEventResult.UpdateSolution;
}
}
}
if (result.HasFlag(GHMouseEventResult.Invalidated))
sender.Invalidate();
if (result.HasFlag(GHMouseEventResult.UpdateSolution))
owner.ExpireSolution(true);
if (result.HasFlag(GHMouseEventResult.Handled))
return GH_ObjectResponse.Handled;
}
return base.RespondToMouseDown(sender, e);
}
public override GH_ObjectResponse RespondToMouseMove(GH_Canvas sender, GH_CanvasMouseEvent e)
{
GHCustomComponent owner = Owner as GHCustomComponent;
GHMouseEventResult result = GHMouseEventResult.None;
var backup = sender.Cursor;
if (Bounds.Contains(e.CanvasLocation))
{
foreach (GHControl item in owner.CustomControls.Values)
{
if (!item.Enabled)
continue;
if (item.Bounds.Contains(e.CanvasLocation))
{
item.MouseOver( sender, owner, e, ref result);
}
else
{
item.MouseLeave( sender, owner, e, ref result);
}
if (item is GHParameter && !item.UpdateSolution)
{
result &= ~GHMouseEventResult.UpdateSolution;
}
}
if (result.HasFlag(GHMouseEventResult.Invalidated))
sender.Invalidate();
if (result.HasFlag(GHMouseEventResult.UpdateSolution))
owner.ExpireSolution(true);
if (result.HasFlag(GHMouseEventResult.Handled))
return GH_ObjectResponse.Ignore;
}
return base.RespondToMouseMove(sender, e);
}
public override GH_ObjectResponse RespondToMouseUp(GH_Canvas sender, GH_CanvasMouseEvent e)
{
GHCustomComponent owner = Owner as GHCustomComponent;
GHMouseEventResult result = GHMouseEventResult.None;
var backup = sender.Cursor;
if (Bounds.Contains(e.CanvasLocation))
{
foreach (GHControl item in owner.CustomControls.Values)
{
if (!item.Enabled)
continue;
if (item.Bounds.Contains(e.CanvasLocation))
{
item.MouseKeyUp( sender, owner, e, ref result);
if (item is GHParameter && !item.UpdateSolution)
{
result &= ~GHMouseEventResult.UpdateSolution;
}
if (result.HasFlag(GHMouseEventResult.Handled))
break;
}
}
if (result.HasFlag(GHMouseEventResult.Invalidated))
sender.Invalidate();
if (result.HasFlag(GHMouseEventResult.UpdateSolution) )
owner.ExpireSolution(true);
if (result.HasFlag(GHMouseEventResult.Handled))
return GH_ObjectResponse.Handled;
}
return base.RespondToMouseUp(sender, e);
}
public override void SetupTooltip(PointF canvasPoint, GH_TooltipDisplayEventArgs e)
{
GHCustomComponent owner = Owner as GHCustomComponent;
foreach (GHControl item in owner.CustomControls.Values)
{
if (item.Bounds.Contains(canvasPoint) && item.Enabled)
{
item.SetupToolTip(canvasPoint,e);
return;
}
}
base.SetupTooltip(canvasPoint, e);
}
public override GH_ObjectResponse RespondToMouseDoubleClick(GH_Canvas sender, GH_CanvasMouseEvent e)
{
GHCustomComponent owner = Owner as GHCustomComponent;
GHMouseEventResult result = GHMouseEventResult.None;
var backup = sender.Cursor;
if (Bounds.Contains(e.CanvasLocation))
{
foreach (GHControl item in owner.CustomControls.Values)
{
if (!item.Enabled)
continue;
if (item.Bounds.Contains(e.CanvasLocation))
{
item.MouseRightClick(sender, owner, e, ref result);
}
if (item is GHParameter && !item.UpdateSolution)
{
result &= ~GHMouseEventResult.UpdateSolution;
}
}
if (result.HasFlag(GHMouseEventResult.Invalidated))
sender.Invalidate();
if (result.HasFlag(GHMouseEventResult.UpdateSolution))
owner.ExpireSolution(true);
if (result.HasFlag(GHMouseEventResult.Handled))
return GH_ObjectResponse.Ignore;
}
return base.RespondToMouseDoubleClick(sender, e);
}
#endregion
#region Custom Render logic
protected override void Render(GH_Canvas canvas, System.Drawing.Graphics graphics, GH_CanvasChannel channel)
{
switch (channel)
{
case GH_CanvasChannel.Objects:
//We need to draw everything ourselves.
base.RenderComponentCapsule(canvas, graphics, true, true, false, true, true,true);
//if (Owner.IconDisplayMode == GH_IconDisplayMode.icon)
//{
// PointF iconPos = new PointF((Bounds.Left + Bounds.Right) / 2 - 12, Pivot.Y);
// graphics.DrawImage(Owner.Icon_24x24, new RectangleF(iconPos, new Size(24,24)));
//}
if (canvas.Viewport.Zoom < 0.5)
{
return;
}
GHCustomComponent owner = Owner as GHCustomComponent;
foreach (GHControl item in owner.CustomControls.Values)
{
//if (!item.Bounds.Contains(canvas.CursorCanvasPosition))
// item.MouseLeave(owner,new GH_CanvasMouseEvent(),ref invalidate);
if (item.IsVisible)
item.Render(graphics, canvas.CursorCanvasPosition, this.Selected, Owner.Locked || !item.Enabled, Owner.Hidden);
}
break;
default:
base.Render(canvas, graphics, channel);
break;
}
}
#endregion
#region helpers
#endregion
}
}