-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathHelpers.cs
234 lines (206 loc) · 8.65 KB
/
Helpers.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
using Grasshopper.GUI.Canvas;
using Grasshopper.Kernel;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using WPFNumericUpDown;
/*
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 static class Helpers
{
public static void DropShadow(Graphics G, Color c, Color backColor, GraphicsPath GP, int d)
{
Color[] colors = getColorVector(c, backColor, d).ToArray();
var t = G.Transform;
for (int i = 0; i < d; i++)
{
G.TranslateTransform(1f, 0.75f); // <== shadow vector!
using (Pen pen = new Pen(colors[i], 1.75f)) // <== pen width (*)
G.DrawPath(pen, GP);
}
G.Transform = t;
}
public static void InShadow(Graphics G, Color c, Color backColor, GraphicsPath GP, int d)
{
Color[] colors = getColorVector(c, backColor, d).ToArray();
RectangleF rec = GP.GetBounds();
for (int i = 0; i < d; i++)
{
G.TranslateTransform(1f, 1f); // <== shadow vector!
G.ScaleTransform(1f - 2f / rec.Width, 1f - 2f / rec.Height);
using (Pen pen = new Pen(colors[i], 1.75f)) // <== pen width (*)
G.DrawPath(pen, GP);
}
G.ResetTransform();
}
public static List<Color> getColorVector(Color fc, Color bc, int depth)
{
List<Color> cv = new List<Color>();
float dRed = 1f * (bc.R - fc.R) / depth;
float dGreen = 1f * (bc.G - fc.G) / depth;
float dBlue = 1f * (bc.B - fc.B) / depth;
for (int d = 1; d <= depth; d++)
cv.Add(Color.FromArgb(255, (int)(fc.R + dRed * d),
(int)(fc.G + dGreen * d), (int)(fc.B + dBlue * d)));
return cv;
}
public static GraphicsPath getRectPath(RectangleF R)
{
byte[] fm = new byte[3];
for (int b = 0; b < 3; b++) fm[b] = 1;
List<PointF> points = new List<PointF>();
points.Add(new PointF(R.Left, R.Bottom));
points.Add(new PointF(R.Right, R.Bottom));
points.Add(new PointF(R.Right, R.Top));
return new GraphicsPath(points.ToArray(), fm);
}
/// <summary>
/// Create new rectangeF by offseting this rectangle inside.(negative value offsets the rectangle outside)
/// </summary>
/// <param name="rectangle"></param>
/// <param name="f"></param>
/// <returns></returns>
public static RectangleF Offset(this RectangleF rectangle,float f)
{
return new RectangleF(rectangle.X + f, rectangle.Y + f, rectangle.Width - 2 * f, rectangle.Height - 2 * f);
}
/// <summary>
/// Create a new rectangleF in given size at the center of this rectangle
/// </summary>
/// <param name="rectangle"></param>
/// <param name="size"></param>
/// <returns></returns>
public static RectangleF Center(this RectangleF rectangle, Size size)
{
return new RectangleF(
rectangle.Left + (rectangle.Width - size.Width) / 2f,
rectangle.Top + (rectangle.Height - size.Height) / 2f,
size.Width,
size.Height);
}
/// <summary>
/// return the point in the center of the rectangle
/// </summary>
/// <param name="rectangle"></param>
/// <returns></returns>
public static PointF Middle(this RectangleF rectangle)
{
return new PointF(rectangle.X + rectangle.Width / 2f, rectangle.Y + rectangle.Height / 2f);
}
//public static PointF
public static void DrawFrame(RectangleF bound, Graphics G,string title, bool selected,bool locked,bool hidden)
{
GH_Capsule capsule = GH_Capsule.CreateCapsule(bound, GH_Palette.Transparent, 2, 0);
capsule.Render(G, selected, locked, hidden);
if (title == string.Empty)
{
capsule.Dispose();
return;
}
using (StringFormat s = new StringFormat()
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Near,
Trimming = StringTrimming.EllipsisCharacter
})
{
G.DrawString(
title,
GHControl.SmallFont,
(locked)?Brushes.Gray: Brushes.Black,
capsule.Box.Left + capsule.MaxRadius,
capsule.Box.Top + capsule.MaxRadius,
s);
}
if (title != string.Empty)
{
using (Pen p = new Pen(Brushes.DimGray, 0.5f))
{
G.DrawLine(
p,
capsule.Box.Left + 3,
capsule.Box.Top + GHControl.SmallFont.Height + 1,
capsule.Box.Right - 3,
capsule.Box.Top + GHControl.SmallFont.Height + 1);
}
}
capsule.Dispose();
}
public static bool GetInput<T>(System.Drawing.Point location, GenericNumber<T> number) where T : struct, IFormattable, IComparable<T>
{
NumericUpDownWindow window = new NumericUpDownWindow(new System.Windows.Point(location.X, location.Y));
T previous = number.Value;
window.SetGeneric<T>(number);
window.ShowDialog();
if (window.DialogResult.HasValue && window.DialogResult.Value)
{
if (previous.CompareTo( number.Value)==0)
return false;
else
return true;
}
return false;
}
public static bool GetIntegerInput(System.Drawing.Point location,IntegerNumber number)
{
NumericUpDownWindow window = new NumericUpDownWindow(new System.Windows.Point(location.X, location.Y+11));
window.DataContext=number;
int previous = number.Value;
window.SetInteger(number);
window.ShowDialog();
if (window.DialogResult.HasValue && window.DialogResult.Value)
{
if (previous == number.Value)
return false;
else
return true;
}
return false;
}
public static bool GetFloatInput(System.Drawing.Point location, DecimalNumber number)
{
NumericUpDownWindow window = new NumericUpDownWindow(new System.Windows.Point(location.X, location.Y+11));
decimal previous = number.Value;
window.SetFloat(number);
window.ShowDialog();
if (window.DialogResult.HasValue && window.DialogResult.Value)
{
if (previous == number.Value)
return false;
else
return true;
}
return false;
}
public static bool GetDoubleInput(System.Drawing.Point location,DoubleNumber number)
{
NumericUpDownWindow window = new NumericUpDownWindow(new System.Windows.Point(location.X, location.Y+11));
double previous = number.Value;
window.SetDouble(number);
window.ShowDialog();
if (window.DialogResult.HasValue && window.DialogResult.Value)
{
if (previous == number.Value)
return false;
else
return true;
}
return false;
}
}
}