-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirtualEncoder.cs
More file actions
311 lines (276 loc) · 11.5 KB
/
VirtualEncoder.cs
File metadata and controls
311 lines (276 loc) · 11.5 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SDRSharp.TuningDial
{
public partial class VirtualEncoder : UserControl
{
private int diameter = 0;
private Rectangle bezel = new Rectangle();
private Rectangle highlight = new Rectangle();
private Rectangle backGround = new Rectangle();
private Rectangle glow = new Rectangle();
private Rectangle dial = new Rectangle();
private Rectangle detent = new Rectangle();
private Point KnobCenter = new Point();
private bool _IsRotating = false;
private double _CurrentAngle = 270;
private double _OldAngle = 0;
private bool _showGlow = true;
private IlluminationColor glowType = IlluminationColor.Blue;
private Color glowColor;
private bool _autoStep;
private EncoderDirectionType EncoderDirection;
private bool _AllowStepChanges = false;
private int _stepSize = 1;
public delegate void DirectionChangedEventHandler(object sender, DirectionChangedEventArgs e);
public event DirectionChangedEventHandler DirectionChanged;
public VirtualEncoder()
{
InitializeComponent();
this.DoubleBuffered = true;
this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
this._IsRotating = false;
this._CurrentAngle = 270.0;
this.EncoderDirection = EncoderDirectionType.Zero;
GlowColor = IlluminationColor.Blue;
}
private void SetDimensions()
{
if (Width >= Height)
{
diameter = Height - 1;
}
else
{
diameter = Width - 1;
}
bezel = new Rectangle(0, 0, diameter, diameter);
highlight = new Rectangle(10, 10, diameter -20, diameter-20);
backGround = new Rectangle(12, 12, diameter - 24, diameter - 24);
glow = new Rectangle(10, 11, diameter - 20, diameter - 20);
dial = new Rectangle(14, 13, diameter - 27, diameter - 27);
//detent = new Rectangle(40, 40, (int)(diameter * 0.15), (int)(diameter * 0.15));
Size detentSize = new Size(20, 20);
KnobCenter = new Point(checked((int)Math.Round(unchecked((double)this.bezel.Left + (double)this.bezel.Width / 2.0 )-10)), checked((int)Math.Round(unchecked((double)this.bezel.Top + (double)this.bezel.Height / 2.0 )-10)));
Point detentLocation = this.LocatePointer();
detent = new Rectangle(detentLocation, detentSize);
}
protected virtual void OnDirectionChanged(DirectionChangedEventArgs e)
{
DirectionChangedEventHandler handler = DirectionChanged;
handler?.Invoke(this, e);
}
private void DrawControl(Graphics e)
{
e.Clear(this.BackColor);
using (Brush bezelBrush = (Brush)new LinearGradientBrush(bezel, Color.White, Color.Black, LinearGradientMode.ForwardDiagonal))
{
e.FillEllipse(bezelBrush, bezel);
};
using (var knobBrush = (Brush)new LinearGradientBrush(bezel, Color.Black, Color.White, LinearGradientMode.ForwardDiagonal))
{
e.FillEllipse(knobBrush, highlight);
};
e.FillEllipse(Brushes.Black, backGround);
if (ShowGlow)
{
using (var ellipsePath = new GraphicsPath())
{
ellipsePath.AddEllipse(glow);
using (var brush = new PathGradientBrush(ellipsePath))
{
brush.CenterPoint = new PointF(glow.Width / 2f, glow.Height / 1f);
brush.CenterColor = Color.Transparent;
brush.SurroundColors = new[] { Color.FromArgb(210, glowColor) };
brush.FocusScales = new PointF(0.75f, 1.0f);
e.FillRectangle(brush, glow);
}
}
}
using (var buttonBrush = (Brush)new LinearGradientBrush(dial, Color.FromArgb(200, Color.White), Color.Black, LinearGradientMode.ForwardDiagonal))
{
e.FillEllipse(buttonBrush, dial);
};
using (var detentBrush = (Brush)new LinearGradientBrush(detent, Color.Black, Color.Black, LinearGradientMode.BackwardDiagonal))
{
e.FillEllipse(detentBrush, detent);
};
e.DrawEllipse(Pens.Black, dial);
using (var detenttwoBrush = (Brush)new LinearGradientBrush(detent, Color.Black, Color.Silver, (float)-_CurrentAngle / 360))
{
e.FillEllipse(detenttwoBrush, detent);
};
}
private bool HitTestPointer(MouseEventArgs e)
{
return this.detent.Contains(e.X, e.Y);
}
private Point LocatePointer()
{
double num = (this._CurrentAngle - 180.0) * Math.PI / 180.0;
Point point = new Point
{
X = checked((int)Math.Round(unchecked((double)checked(this.KnobCenter.X) + Math.Cos(num) * ((double)checked(dial.Width-40) / 2.0)))),
Y = checked((int)Math.Round(unchecked((double)checked(this.KnobCenter.Y) + Math.Sin(num) * ((double)checked(dial.Width-40) / 2.0))))
};
return point;
}
protected override void OnPaintBackground(PaintEventArgs e)
{
e.Graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
SetDimensions();
DrawControl(e.Graphics);
}
protected override void OnMouseUp(MouseEventArgs e)
{
this._IsRotating = false;
this.Cursor = Cursors.Default;
this.EncoderDirection = EncoderDirectionType.Zero;
OnDirectionChanged(new DirectionChangedEventArgs(EncoderDirection));
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (this._IsRotating)
{
this.Cursor = Cursors.Hand;
this._CurrentAngle = Math.Atan2((double)checked(e.Y - this.KnobCenter.Y), (double)checked(e.X - this.KnobCenter.X)) * (180.0 / Math.PI) + 180.0;
if (this._CurrentAngle > this._OldAngle)
{
this.EncoderDirection = EncoderDirectionType.Positive;
}
else if (this._CurrentAngle < this._OldAngle)
{
this.EncoderDirection = EncoderDirectionType.Negative;
}
OnDirectionChanged(new DirectionChangedEventArgs(EncoderDirection));
this._OldAngle = this._CurrentAngle;
}
this.Refresh();
}
protected override void OnMouseDown(MouseEventArgs e)
{
if ((e.Button == MouseButtons.Left) && (HitTestPointer(e) == true))
{
this._IsRotating = true;
this._CurrentAngle = Math.Atan2((double)checked(e.Y - this.KnobCenter.Y), (double)checked(e.X - this.KnobCenter.X)) * (180.0 / Math.PI) + 180.0;
this._OldAngle = this._CurrentAngle;
}
this.Refresh();
}
public IlluminationColor GlowColor
{
get { return glowType; }
set
{
glowType = value;
redToolStripMenuItem.Checked = false;
orangeToolStripMenuItem.Checked = false;
yellowToolStripMenuItem.Checked = false;
greenToolStripMenuItem.Checked = false;
blueToolStripMenuItem.Checked = false;
purpleToolStripMenuItem.Checked = false;
switch (value)
{
case IlluminationColor.Red:
glowColor = Color.Red;
redToolStripMenuItem.Checked = true;
break;
case IlluminationColor.Orange:
glowColor = Color.Orange;
orangeToolStripMenuItem.Checked = true;
break;
case IlluminationColor.Yellow:
glowColor = Color.FromArgb(192, 192, 0);
yellowToolStripMenuItem.Checked = true;
break;
case IlluminationColor.Green:
glowColor = Color.LimeGreen;
greenToolStripMenuItem.Checked = true;
break;
case IlluminationColor.Blue:
blueToolStripMenuItem.Checked = true;
glowColor = Color.Blue;
break;
case IlluminationColor.Purple:
purpleToolStripMenuItem.Checked = true;
glowColor = Color.MediumPurple;
break;
default:
break;
}
this.Invalidate();
}
}
public bool ShowGlow
{
get { return _showGlow; }
set
{
_showGlow = value;
illuminiateDialToolStripMenuItem.Checked = value;
}
}
private void RedToolStripMenuItem_Click(object sender, EventArgs e)
{
GlowColor = IlluminationColor.Red;
}
private void OrangeToolStripMenuItem_Click(object sender, EventArgs e)
{
GlowColor = IlluminationColor.Orange;
}
private void YellowToolStripMenuItem_Click(object sender, EventArgs e)
{
GlowColor = IlluminationColor.Yellow;
}
private void GreenToolStripMenuItem_Click(object sender, EventArgs e)
{
GlowColor = IlluminationColor.Green;
}
private void BlueToolStripMenuItem_Click(object sender, EventArgs e)
{
GlowColor = IlluminationColor.Blue;
}
private void PurpleToolStripMenuItem_Click(object sender, EventArgs e)
{
GlowColor = IlluminationColor.Purple;
}
private void IlluminiateDialToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowGlow = !ShowGlow;
}
public bool AllowStepChanges
{
get { return _AllowStepChanges; }
set
{
_AllowStepChanges = value;
adjustStepSizesToolStripMenuItem.Visible = value;
toolStripMenuItem1.Visible = value;
}
}
public bool UseAutoStep
{
get { return _autoStep; }
set { _autoStep = value; }
}
public int CustomStep
{
get { return _stepSize; }
set { _stepSize = value; }
}
private void adjustStepSizesToolStripMenuItem_Click(object sender, EventArgs e)
{
StepAdjustForm frm = new StepAdjustForm(this);
frm.ShowDialog(this);
}
}
}