forked from Torabi/GHCustomControls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGHControl.cs
264 lines (222 loc) · 8.82 KB
/
GHControl.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
using Grasshopper.GUI;
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*
Original work Copyright (c) 2021 Ali Torabi ([email protected])
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 enum GHMouseEventResult
{
None=0,
Invalidated=1,
UpdateSolution=2,
Handled=4,
Highlighted=8
}
public abstract class GHControl : IGHCustomControl
{
protected GHControl(string name ,string description,Bitmap toolTipDiagram = null)
{
Name = name;
Description = description;
ToolTipDiagram = toolTipDiagram;
}
internal static Font SmallFont =GH_FontServer.StandardAdjusted;
internal static Font StandardFont = GH_FontServer.LargeAdjusted;
internal GHCustomAttributes attributes;
internal abstract int GetHeight();
internal abstract int GetWidth();
public virtual RectangleF Bounds { get; set; }
internal abstract void Render(Graphics graphics,PointF cursorCanvasPosition, bool selected, bool locked, bool hidden);
//internal abstract void MouseOver(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e , ref GHMouseEventResult result);
/// <summary>
/// true when the cursor is inside the <c>ActiveZone</c>
/// </summary>
internal bool isMouseIn = false;
internal virtual void MouseOver(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
if (this is IGHPanel)
{
((IGHPanel)this).MouseOverChildren(sender, customComponent, e, ref result);
if (result.HasFlag(GHMouseEventResult.Invalidated | GHMouseEventResult.Handled))
return;
}
if (ActiveZone.Contains(e.CanvasLocation))
{
if (!isMouseIn)
{
isMouseIn = true;
result = result | GHMouseEventResult.Invalidated | GHMouseEventResult.Handled;
return;
}
}
else
{
if (isMouseIn )
{
isMouseIn = false;
result = result | GHMouseEventResult.Invalidated | GHMouseEventResult.Handled;
}
}
}
internal virtual RectangleF ActiveZone => Bounds;
internal virtual void MouseLeave(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e,ref GHMouseEventResult result)
{
if (isMouseIn)
{
isMouseIn = false;
result = result | GHMouseEventResult.Invalidated | GHMouseEventResult.Handled;
return;
}
if (this is IGHPanel)
{
((IGHPanel)this).MouseOverChildren(sender, customComponent, e, ref result);
if (result.HasFlag(GHMouseEventResult.Invalidated | GHMouseEventResult.Handled))
return;
}
}
internal abstract void MouseLeftClick(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result);
internal abstract void MouseRightClick(GH_Canvas sender,GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result);
internal virtual void SetupToolTip(PointF canvasPoint,GH_TooltipDisplayEventArgs e)
{
if (!Enabled)
return;
if (ToolTipDiagram == null)
{
e.Title = Name;
e.Description = Description;
}
else
{
e.Text = Name;
e.Description = Description;
e.Diagram = ToolTipDiagram;
}
}
internal virtual void MouseKeyUp(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
if (this is IGHPanel)
{
IGHPanel panel = (IGHPanel)this;
foreach (GHControl control in panel.Items)
{
if (control.Bounds.Contains(e.CanvasLocation))
{
control.MouseKeyUp(sender, customComponent, e, ref result);
if (result.HasFlag(GHMouseEventResult.Handled))
return;
}
}
}
}
/// <summary>
/// name of the control also it is used as the title in UI
/// </summary>
public string Name { get; set; }
public string Description { get; set; }
/// <summary>
/// a bitmap shown in the detail area of the tooltip
/// </summary>
public Bitmap ToolTipDiagram { get ; set; }
/// <summary>
/// the amount of the offset for the sub-controls inside this control
/// </summary>
public abstract int Offset { get; }
/// <summary>
/// used to react to the mouse
/// </summary>
//public int Highlighted { get; set; } = -1;
bool _visible=true;
/// <summary>
/// return true if the control is visible
/// </summary>
public bool IsVisible { get=>_visible;
set
{
if (_visible != value)
{
attributes?.Redraw();
_visible = value;
}
}
}
private bool _enabled = true;
/// <summary>
/// when control is not enabled do not responds to mouse event and it renderes as locked
/// </summary>
public bool Enabled {
get=>_enabled;
set
{
if (_enabled != value)
Refresh();
_enabled = value;
if(this is IGHPanel)
{
IGHPanel panel = (IGHPanel)this;
foreach (IGHCustomControl control in panel.Items)
control.Enabled = _enabled;
}
}
}
/// <summary>
/// set the attribute of this control and all its children
/// </summary>
/// <param name="att"></param>
internal void SetAttribute(GHCustomAttributes att)
{
attributes = (GHCustomAttributes)att;
if (this is IGHPanel)
{
IGHPanel panel = (IGHPanel)this;
foreach (GHControl child in panel.Items)
{
child.SetAttribute(attributes);
}
}
}
/// <summary>
/// return the defualt brush based on the control state (enable or disable)
/// </summary>
/// <param name="control"></param>
/// <returns></returns>
public Brush ActiveBrush()
{
return (Enabled) ? Brushes.Black : Brushes.Gray;
}
internal Point PointToScreen(GH_Canvas canvas,PointF point)
{
var p = canvas.Viewport.ProjectPoint(point); // in the grasshopper window
return canvas.PointToScreen(new System.Drawing.Point((int)p.X, (int)p.Y));
}
/// <summary>
/// updates the control graphics at the next paint operation
/// </summary>
public void Refresh()
{
if (!Bounds.IsEmpty)
Grasshopper.Instances.ActiveCanvas.Invalidate(Rectangle.Round( Bounds));
//if (this is IGHPanel)
//{
// IGHPanel panel = (IGHPanel)this;
// foreach (IGHCustomControl control in panel.Items)
// control.Refresh();
//}
}
}
}