-
Notifications
You must be signed in to change notification settings - Fork 0
/
Slider.cs
298 lines (256 loc) · 9.57 KB
/
Slider.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
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
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using Ephemera.NBagOfTricks;
namespace Ephemera.NBagOfUis
{
/// <summary>
/// Slider control.
/// </summary>
public class Slider : UserControl
{
// TODO log/db for volume use.
#region Fields
/// <summary>If user resets. This is the first value assigned to Value.</summary>
double _resetVal = double.NaN;
/// <summary>The brush.</summary>
readonly SolidBrush _brush = new(Color.White);
/// <summary>For drawing text.</summary>
readonly StringFormat _format = new() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center };
#endregion
#region Backing fields
double _resolution = 0.1;
double _minimum = 0.0;
double _maximum = 10.0;
double _value = 5.0;
#endregion
#region Properties
/// <summary>Optional label.</summary>
public string Label { get; set; } = "";
/// <summary>For styling.</summary>
public Color DrawColor { get { return _brush.Color; } set { _brush.Color = value; } }
/// <summary>Fader orientation</summary>
public Orientation Orientation { get; set; } = Orientation.Horizontal;
/// <summary>Per step resolution of this slider.</summary>
public double Resolution
{
get { return _resolution; }
set { _resolution = value; Rescale(); }
}
/// <summary>Minimum Value of the slider.</summary>
public double Minimum
{
get { return _minimum; }
set { _minimum = value; Rescale(); }
}
/// <summary>Maximum Value of the slider.</summary>
public double Maximum
{
get { return _maximum; }
set { _maximum = value; Rescale(); }
}
/// <summary>The current value of the slider.</summary>
public double Value
{
get { return _value; }
set { _value = MathUtils.Constrain(value, _minimum, _maximum, _resolution); if (double.IsNaN(_resetVal)) _resetVal = value; Invalidate(); }
}
#endregion
#region Events
/// <summary>Slider value changed event.</summary>
public event EventHandler? ValueChanged;
#endregion
#region Lifecycle
/// <summary>
/// Creates a new Slider control.
/// </summary>
public Slider()
{
SetStyle(ControlStyles.DoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
_brush.Dispose();
_format.Dispose();
}
base.Dispose(disposing);
}
/// <summary>
/// From Designer.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Slider
//
this.Name = "Slider";
this.Size = new System.Drawing.Size(150, 30);
this.ResumeLayout(false);
}
#endregion
#region Calcs
/// <summary>
/// If min or max or resolution changed by client.
/// </summary>
void Rescale()
{
_minimum = MathUtils.Constrain(_minimum, _minimum, _maximum, _resolution);
_maximum = MathUtils.Constrain(_maximum, _minimum, _maximum, _resolution);
_value = MathUtils.Constrain(_value, _minimum, _maximum, _resolution);
Invalidate();
}
#endregion
#region Drawing
/// <summary>
/// Draw the slider.
/// </summary>
protected override void OnPaint(PaintEventArgs pe)
{
// Setup.
pe.Graphics.Clear(BackColor);
// Draw the bar.
if (Orientation == Orientation.Horizontal)
{
double x = (_value - Minimum) / (Maximum - Minimum);
pe.Graphics.FillRectangle(_brush, ClientRectangle.Left, ClientRectangle.Top, ClientRectangle.Width * (float)x, ClientRectangle.Height);
}
else
{
double y = 1.0 - (_value - Minimum) / (Maximum - Minimum);
pe.Graphics.FillRectangle(_brush, ClientRectangle.Left, ClientRectangle.Height * (float)y, ClientRectangle.Width, ClientRectangle.Bottom);
}
// Text.
string sval = _value.ToString("#0." + new string('0', MathUtils.DecPlaces(_resolution)));
if (Label != "")
{
Rectangle r = new(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height / 2);
pe.Graphics.DrawString(Label, Font, Brushes.Black, r, _format);
r = new(ClientRectangle.X, ClientRectangle.Height / 2, ClientRectangle.Width, ClientRectangle.Height / 2);
pe.Graphics.DrawString(sval, Font, Brushes.Black, r, _format);
}
else
{
pe.Graphics.DrawString(sval, Font, Brushes.Black, ClientRectangle, _format);
}
}
#endregion
#region Mouse events
/// <summary>
/// Handle dragging.
/// </summary>
protected override void OnMouseMove(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
SetValueFromMouse(e);
}
base.OnMouseMove(e);
}
/// <summary>
/// Handle dragging.
/// </summary>
protected override void OnMouseDown(MouseEventArgs e)
{
switch(e.Button)
{
case MouseButtons.Left:
SetValueFromMouse(e);
break;
case MouseButtons.Right:
Value = _resetVal;
break;
}
base.OnMouseDown(e);
}
/// <summary>
/// ommon updater.
/// </summary>
/// <param name="e"></param>
void SetValueFromMouse(MouseEventArgs e)
{
double oldval = Value;
// Calculate the new value.
double newval = Orientation == Orientation.Horizontal ?
Minimum + e.X * (Maximum - Minimum) / Width :
Minimum + (Height - e.Y) * (Maximum - Minimum) / Height;
// This factors in the resolution.
Value = newval;
if(oldval != Value)
{
ValueChanged?.Invoke(this, EventArgs.Empty);
}
}
/// <summary>
/// Handle the nudge key.
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Control)
{
if (e.KeyCode == Keys.Down)
{
Value -= _resolution;
ValueChanged?.Invoke(this, EventArgs.Empty);
}
else if (e.KeyCode == Keys.Up)
{
Value += _resolution;
ValueChanged?.Invoke(this, EventArgs.Empty);
}
}
base.OnKeyDown(e);
}
#endregion
}
/// <summary>Simple toolstrip container for the slider.</summary>
[ToolStripItemDesignerAvailability(ToolStripItemDesignerAvailability.ToolStrip | ToolStripItemDesignerAvailability.StatusStrip)]
public class ToolStripSlider : ToolStripControlHost
{
#region Fields
/// <summary>Contained control.</summary>
readonly Slider _slider;
#endregion
#region Properties mapped to contained control
/// <summary>Optional label.</summary>
public string Label { get { return _slider.Label; } set { _slider.Label = value; } }
/// <summary>For styling.</summary>
public Color DrawColor { get { return _slider.DrawColor; } set { _slider.DrawColor = value; } }
/// <summary>Per step resolution of this slider.</summary>
public double Resolution { get { return _slider.Resolution; } set { _slider.Resolution = value; } }
/// <summary>Minimum Value of the slider.</summary>
public double Minimum { get { return _slider.Minimum; } set { _slider.Minimum = value; } }
/// <summary>Maximum Value of the slider.</summary>
public double Maximum { get { return _slider.Maximum; } set { _slider.Maximum = value; } }
/// <summary>The current value of the slider.</summary>
public double Value { get { return _slider.Value; } set { _slider.Value = value; } }
/// <summary>Optional border.</summary>
public BorderStyle BorderStyle { get { return _slider.BorderStyle; } set { _slider.BorderStyle = value; } }
#endregion
#region Events
/// <summary>Slider value changed event.</summary>
public event EventHandler? ValueChanged;
#endregion
/// <summary>
/// Make one.
/// </summary>
public ToolStripSlider() : base(new Slider())
{
_slider = (Slider)Control;
AutoSize = false;
Width = _slider.Width;
_slider.Orientation = Orientation.Horizontal;
_slider.ValueChanged += (_, __) => ValueChanged?.Invoke(this, EventArgs.Empty);
}
}
}