forked from Torabi/GHCustomControls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumericUpDown.cs
267 lines (227 loc) · 9.41 KB
/
NumericUpDown.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
using GH_IO.Types;
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;
using System.Timers;
/*
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 class NumericUpDown<T> : GHParameter,IGHPanel where T : struct, IFormattable, IComparable<T>
{
string FormatString = "";
//RectangleF _left, _right;
int _width, _height,_btnWidh,_labelWidth,_titleWidth;
PushButton _left, _right;
PushButton _label;
GHControl[] _controls;
int incerement = 0;
object _smallChange;
System.Timers.Timer timer;
T _min, _max;
public NumericUpDown(string name ,string description,T value, T min,T max, T smallChange, string formatString="", Bitmap toolTipDiagram = null) : base(name,description,value,toolTipDiagram)
{
FormatString = formatString;
_smallChange = smallChange;
_min = min;
_max = max;
_btnWidh = GH_FontServer.StringWidth("<", SmallFont);
_labelWidth = GH_FontServer.StringWidth("888.888", SmallFont);
_titleWidth = GH_FontServer.StringWidth(Name, SmallFont);
_width = _labelWidth + 2*_btnWidh + 2 * Offset+_titleWidth;
_height = GH_FontServer.MeasureString("A", SmallFont).Height + 2 * Offset;
_left = new PushButton("Up", "Increase", "<");
_right = new PushButton("Down", "Decrease", ">");
_label = new PushButton("Value", description, AsString(value), true,null, _labelWidth);
_left.UpdateSolution = false;_right.UpdateSolution = false; _label.UpdateSolution = false;
_left.OnValueChanged += _left_OnValueChanged;
_right.OnValueChanged += _right_OnValueChanged;
_label.OnValueChanged += _label_OnValueChanged;
_controls = new GHControl[] { _left, _label, _right };
timer = new System.Timers.Timer();
timer.Interval = 100;
timer.Elapsed += Timer_Elapsed;
}
public T Max
{
get => _max;
set {_max = value;}
}
public T Min
{
get => _min;
set { _min = value; }
}
void changeVlaue()
{
if (CurrentValue is double)
{
double temp = ((double)CurrentValue) + incerement * ((double)_smallChange);
if (temp >= Convert.ToDouble(_min) && temp <= Convert.ToDouble(_max))
CurrentValue = temp;
}
else if (CurrentValue is int)
{
int temp = ((int)CurrentValue) + incerement * ((int)_smallChange);
if (temp >= Convert.ToInt32(_min) && temp <= Convert.ToInt32(_max))
CurrentValue = temp;
}
else if (CurrentValue is float)
{
float temp = ((float)CurrentValue) + incerement * ((float)_smallChange);
if (temp >= (float)Convert.ToDecimal(_min) && temp <= (float)Convert.ToDecimal(_max))
CurrentValue = temp;
}
else
throw new Exception($"invalid type ({CurrentValue})");
_label.Text = AsString(CurrentValue);
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (incerement!=0)
{
changeVlaue();
Grasshopper.Instances.ActiveCanvas?.ScheduleRegen(100);
}
else
{
timer.Stop();
}
}
private void _right_OnValueChanged(object sender, ValueChangeEventArgumnet e)
{
if (((int)e.Value) == 1)
{
incerement = 1;
//changeVlaue();
timer.Start();
}
else
{
timer.Stop();
changeVlaue();
attributes.Owner.ExpireSolution(true);
incerement = 0;
}
}
private void _left_OnValueChanged(object sender, ValueChangeEventArgumnet e)
{
if (((int)e.Value)==1)
{
incerement = -1;
//changeVlaue();
timer.Start();
}
else
{
timer.Stop();
changeVlaue();
attributes.Owner.ExpireSolution(true);
incerement = 0;
}
}
private void _label_OnValueChanged(object sender, ValueChangeEventArgumnet e)
{
if (!attributes.ContentBox.IsEmpty && (int)e.Value == 0) // on muse up
{
T d = (T)CurrentValue;
NumericUpDownData<T> numeric = new NumericUpDownData<T>(d, _min, _max, FormatString);
//float s = GH_FontServer.MeasureString("A", SmallFont).Height * sender.Viewport.Zoom / 20;
if (numeric.GetInput(PointToScreen(Grasshopper.Instances.ActiveCanvas, new PointF((_label.Bounds.Left + _label.Bounds.Right) / 2, _label.Bounds.Top)), out T val))
{
CurrentValue = val;
_label.Text = AsString(CurrentValue);
attributes.Owner.ExpireSolution(true);
}
}
}
public override int Offset => 2;
public override GH_Types DataType
{
get
{
if (typeof(T).Equals(typeof(double)))
return GH_Types.gh_double;
else if (typeof(T).Equals(typeof(int)))
return GH_Types.gh_int32;
else if (typeof(T).Equals(typeof(float)))
return GH_Types.gh_decimal;
else
throw new Exception($"{typeof(T).ToString()} Type is not supported ");
}
}
public override GH_ParamAccess Access => GH_ParamAccess.item;
public GHControl[] Items { get => _controls; set { } }
public Orientation Orientation { get=> Orientation.Horizontal; set { } }
RectangleF _bounds;
public override RectangleF Bounds
{
get => _bounds;
set
{
_bounds = value;
this.SetChildrenBounds(0, 0, ContentAlignment.MiddleRight);
}
}
internal override int GetHeight() => _height;
internal override int GetWidth() => _width;
string AsString(object val)
{
if (val is double)
return (Convert.ToDouble(val)).ToString(FormatString);
else if (val is int)
return (Convert.ToInt32(val)).ToString(FormatString);
else if (val is float)
return (Convert.ToDecimal(val)).ToString(FormatString);
else
throw new Exception($"{val.ToString()} Type is not supported ");
}
internal override void MouseLeftClick(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
this.MouseClickChildren(sender, customComponent, e, ref result);
}
internal override void MouseRightClick(GH_Canvas sender, GHCustomComponent customComponent, GH_CanvasMouseEvent e, ref GHMouseEventResult result)
{
this.MouseClickChildren(sender, customComponent, e, ref result);
}
internal override void Render(Graphics graphics, PointF cursorCanvasPosition, bool selected, bool locked, bool hidden)
{
if (!IsVisible)
return;
Helpers.DrawFrame(Bounds, graphics, "", selected, locked, hidden);
using (StringFormat s = new StringFormat()
{
Alignment = StringAlignment.Near,
LineAlignment = StringAlignment.Near,
Trimming = StringTrimming.EllipsisCharacter
})
{
graphics.DrawString(
Name,
SmallFont,
(locked) ? Brushes.Gray : Brushes.Black,
Bounds.Left+Offset,
Bounds.Top+Offset,
s);
}
foreach (GHControl control in Items)
if (control.IsVisible)
control.Render(graphics, cursorCanvasPosition, selected, locked, hidden);
}
}
}