Skip to content

Commit e344c65

Browse files
author
RandomEngy
committed
Tweaked refire on number box to make a more gradual ramp up in speed.
1 parent ff036dc commit e344c65

4 files changed

Lines changed: 88 additions & 46 deletions

File tree

VidCoder/Controls/NumberBox.xaml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
<Style x:Key="Arrow" TargetType="Polygon">
77
<Setter Property="Fill" Value="#333" />
88
</Style>
9-
<Style x:Key="OrangeButton" TargetType="Button">
10-
<!--<Setter Property="OverridesDefaultStyle" Value="True"/>-->
9+
<Style x:Key="UpDownButton" TargetType="Button">
1110
<Setter Property="Margin" Value="0"/>
1211
<Setter Property="Background" >
1312
<Setter.Value>
@@ -90,7 +89,7 @@
9089
<RowDefinition Height="*" />
9190
</Grid.RowDefinitions>
9291
<Button
93-
Style="{StaticResource OrangeButton}"
92+
Style="{StaticResource UpDownButton}"
9493
Grid.Row="0"
9594
FontSize="4"
9695
PreviewMouseLeftButtonDown="UpButtonMouseLeftButtonDown"
@@ -100,7 +99,7 @@
10099
Points="4,0 0,4 8,4" />
101100
</Button>
102101
<Button
103-
Style="{StaticResource OrangeButton}"
102+
Style="{StaticResource UpDownButton}"
104103
Grid.Row="1"
105104
FontSize="4"
106105
PreviewMouseLeftButtonDown="DownButtonMouseLeftButtonDown"

VidCoder/Controls/NumberBox.xaml.cs

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,7 @@ namespace VidCoder.Controls
1212
/// </summary>
1313
public partial class NumberBox : UserControl
1414
{
15-
private const int RefireInitialDelayMsec = 800;
16-
private const int MinimumRefireDelayMsec = 50;
17-
private const int RefireAccelerationMsec = 250;
18-
19-
private int currentRefireDelayMsec;
20-
private Timer refireTimer;
15+
private RefireControl refireControl;
2116

2217
private string noneCaption;
2318

@@ -164,54 +159,24 @@ private void RefreshNumberBox()
164159

165160
private void UpButtonMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
166161
{
167-
this.BeginRefire(this.IncrementNumber);
162+
this.refireControl = new RefireControl(this.IncrementNumber);
163+
this.refireControl.Begin();
168164
}
169165

170166
private void UpButtonMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
171167
{
172-
this.StopRefire();
168+
this.refireControl.Stop();
173169
}
174170

175171
private void DownButtonMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
176172
{
177-
this.BeginRefire(this.DecrementNumber);
173+
this.refireControl = new RefireControl(this.DecrementNumber);
174+
this.refireControl.Begin();
178175
}
179176

180177
private void DownButtonMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
181178
{
182-
this.StopRefire();
183-
}
184-
185-
private void BeginRefire(Action refireAction)
186-
{
187-
refireAction();
188-
189-
this.currentRefireDelayMsec = RefireInitialDelayMsec;
190-
this.refireTimer = new Timer(
191-
obj =>
192-
{
193-
this.Dispatcher.BeginInvoke(refireAction);
194-
195-
if (this.currentRefireDelayMsec > MinimumRefireDelayMsec)
196-
{
197-
this.currentRefireDelayMsec -= RefireAccelerationMsec;
198-
}
199-
200-
this.currentRefireDelayMsec = Math.Max(this.currentRefireDelayMsec, MinimumRefireDelayMsec);
201-
202-
this.refireTimer.Change(this.currentRefireDelayMsec, this.currentRefireDelayMsec);
203-
},
204-
null,
205-
RefireInitialDelayMsec,
206-
this.currentRefireDelayMsec);
207-
}
208-
209-
private void StopRefire()
210-
{
211-
if (this.refireTimer != null)
212-
{
213-
this.refireTimer.Dispose();
214-
}
179+
this.refireControl.Stop();
215180
}
216181

217182
private void IncrementNumber()
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Threading;
5+
using System.Windows;
6+
7+
namespace VidCoder
8+
{
9+
public class RefireControl
10+
{
11+
/// <summary>
12+
/// How long stages last.
13+
/// </summary>
14+
private const int StageDurationMsec = 900;
15+
16+
// At each stage the refire rate increases.
17+
private static readonly List<int> Delays = new List<int> {500, 200, 100, 50, 20};
18+
19+
private Action refireAction;
20+
private Timer refireTimer;
21+
22+
private Stopwatch stopwatch;
23+
private object refireSync = new object();
24+
25+
public RefireControl(Action refireAction)
26+
{
27+
this.refireAction = refireAction;
28+
}
29+
30+
public void Begin()
31+
{
32+
this.stopwatch = Stopwatch.StartNew();
33+
34+
// Fire once immediately.
35+
refireAction();
36+
37+
this.refireTimer = new Timer(
38+
obj =>
39+
{
40+
lock (this.refireSync)
41+
{
42+
int stage = (int)(this.stopwatch.ElapsedMilliseconds / StageDurationMsec);
43+
int newDelay;
44+
45+
if (stage >= Delays.Count)
46+
{
47+
newDelay = Delays[Delays.Count - 1];
48+
}
49+
else
50+
{
51+
newDelay = Delays[stage];
52+
}
53+
54+
Application.Current.Dispatcher.BeginInvoke(refireAction);
55+
56+
this.refireTimer.Change(newDelay, newDelay);
57+
}
58+
},
59+
null,
60+
Delays[0],
61+
Delays[0]);
62+
}
63+
64+
public void Stop()
65+
{
66+
lock (this.refireSync)
67+
{
68+
this.stopwatch.Stop();
69+
70+
if (this.refireTimer != null)
71+
{
72+
this.refireTimer.Dispose();
73+
}
74+
}
75+
}
76+
}
77+
}

VidCoder/VidCoder.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
<Compile Include="Utilities\ListSynchronizer\MultiSelectorBehaviors.cs" />
278278
<Compile Include="Settings.cs" />
279279
<Compile Include="Utilities\ListSynchronizer\TwoListSynchronizer.cs" />
280+
<Compile Include="Utilities\RefireControl.cs" />
280281
<Compile Include="Utilities\SystemSleepManagement.cs" />
281282
<Compile Include="Utilities\Unity.cs" />
282283
<Compile Include="ViewModel\AboutDialogViewModel.cs" />

0 commit comments

Comments
 (0)