Skip to content

Commit 2b8c0d7

Browse files
committed
Add StepSize and SnapTo10s to ETS2LA.State. Bump 3.3.2.
1 parent 2beadde commit 2b8c0d7

5 files changed

Lines changed: 84 additions & 8 deletions

File tree

ETS2LA.State/Program.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using ETS2LA.Telemetry;
55
using ETS2LA.Settings.Global;
66
using ETS2LA.Game;
7-
using ETS2LA.Notifications;
7+
using ETS2LA.Logging;
88

99
namespace ETS2LA.State;
1010

@@ -151,6 +151,31 @@ private void RoundToNearestUnit()
151151
DesiredSpeed = (float)Math.Round(DesiredSpeed); // Round to nearest m/s
152152
break;
153153
}
154+
155+
if (DesiredSpeed < 0)
156+
DesiredSpeed = 0;
157+
}
158+
159+
private float SnapTo10s(float increase)
160+
{
161+
if (!stateSettings.SnapTo10s)
162+
return DesiredSpeed + increase;
163+
164+
float currentSpeedInDisplayUnits = UnitConversions.FromScientificUnits(UnitType.Speed, DesiredSpeed, DisplayUnits);
165+
float newSpeedInDisplayUnits = currentSpeedInDisplayUnits + UnitConversions.FromScientificUnits(UnitType.Speed, increase, DisplayUnits);
166+
// When increasing by 2 from 37 we go:
167+
// 37 -> 39 -> 40 -> 42 -> 44
168+
float currentSpeed10s = (float)(Math.Floor((currentSpeedInDisplayUnits + 0.1f) / 10) * 10);
169+
float newSpeed10s = (float)(Math.Floor((newSpeedInDisplayUnits + 0.1f) / 10) * 10);
170+
171+
if (currentSpeed10s != newSpeed10s && newSpeed10s > currentSpeed10s)
172+
{
173+
return UnitConversions.ToScientificUnits(UnitType.Speed, newSpeed10s, DisplayUnits);
174+
}
175+
else
176+
{
177+
return DesiredSpeed + increase;
178+
}
154179
}
155180

156181
private void HandleSet(object sender, ControlChangeEventArgs e)
@@ -191,13 +216,15 @@ private void HandleIncrease(object sender, ControlChangeEventArgs e)
191216
switch (DisplayUnits)
192217
{
193218
case Units.Metric:
194-
DesiredSpeed += UnitConversions.ToScientificUnits(UnitType.Speed, 1.0f, Units.Metric); // 1 km/h in m/s
219+
float increaseMetric = UnitConversions.ToScientificUnits(UnitType.Speed, stateSettings.SpeedControlStepSize, Units.Metric);
220+
DesiredSpeed = SnapTo10s(increaseMetric);
195221
break;
196222
case Units.Imperial:
197-
DesiredSpeed += UnitConversions.ToScientificUnits(UnitType.Speed, 1.0f, Units.Imperial); // 1 mph in m/s
223+
float increaseImperial = UnitConversions.ToScientificUnits(UnitType.Speed, stateSettings.SpeedControlStepSize, Units.Imperial);
224+
DesiredSpeed = SnapTo10s(increaseImperial);
198225
break;
199226
case Units.Scientific:
200-
DesiredSpeed += 1f; // 1 m/s
227+
DesiredSpeed += stateSettings.SpeedControlStepSize;
201228
break;
202229
}
203230

@@ -219,13 +246,15 @@ private void HandleDecrease(object sender, ControlChangeEventArgs e)
219246
switch (DisplayUnits)
220247
{
221248
case Units.Metric:
222-
DesiredSpeed -= UnitConversions.ToScientificUnits(UnitType.Speed, 1.0f, Units.Metric); // 1 km/h in m/s
249+
float decreaseMetric = UnitConversions.ToScientificUnits(UnitType.Speed, stateSettings.SpeedControlStepSize, Units.Metric);
250+
DesiredSpeed = SnapTo10s(-decreaseMetric);
223251
break;
224252
case Units.Imperial:
225-
DesiredSpeed -= UnitConversions.ToScientificUnits(UnitType.Speed, 1.0f, Units.Imperial); // 1 mph in m/s
253+
float decreaseImperial = UnitConversions.ToScientificUnits(UnitType.Speed, stateSettings.SpeedControlStepSize, Units.Imperial);
254+
DesiredSpeed = SnapTo10s(-decreaseImperial);
226255
break;
227256
case Units.Scientific:
228-
DesiredSpeed -= 1f; // 1 m/s
257+
DesiredSpeed -= stateSettings.SpeedControlStepSize;
229258
break;
230259
}
231260

ETS2LA.State/Settings.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ namespace ETS2LA.State;
77
public class StateSettings
88
{
99
public Units DisplayUnits = Units.Metric;
10+
public int SpeedControlStepSize = 2;
11+
public bool SnapTo10s = true;
1012
}
1113

1214
public class StateSettingsHandler

ETS2LA.UI/Views/Settings/AssistanceSettings.axaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@
4343
</TabStrip>
4444
</StackPanel>
4545

46+
<StackPanel Spacing="10">
47+
<Grid ColumnDefinitions="*, Auto">
48+
<TextBlock Text="Snap to 10 units" VerticalAlignment="Center" />
49+
<ToggleSwitch Grid.Column="1" IsChecked="{Binding SnapTo10Units}" Classes="primary-toggle" />
50+
</Grid>
51+
<TextBlock Text="When changing the speed, snap to the nearest 10 units. i.e. 37 -> 39 -> 40 -> 42..." FontSize="12" Classes="Description" />
52+
</StackPanel>
53+
54+
<StackPanel Spacing="12">
55+
<StackPanel Orientation="Horizontal" Spacing="6">
56+
<TextBlock Text="Speed Control Step Size" />
57+
<TextBlock Text="{Binding ElementName=SpeedControlStepSizeSlider, Path=Value, StringFormat='{}{0:0} unit(s)'}" />
58+
</StackPanel>
59+
<Slider Name="SpeedControlStepSizeSlider" Minimum="1" Maximum="5" TickFrequency="1" IsSnapToTickEnabled="True" Value="{Binding SpeedControlStepSize}" />
60+
</StackPanel>
61+
4662
<Separator />
4763

4864
<StackPanel Spacing="10">

ETS2LA.UI/Views/Settings/AssistanceSettings.axaml.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using ETS2LA.Settings.Global;
55
using System.Runtime.CompilerServices;
66
using System.Collections.ObjectModel;
7+
using ETS2LA.State;
78

89
namespace ETS2LA.UI.Views.Settings;
910

@@ -29,6 +30,34 @@ public bool SeparateCruiseAndSteering
2930
}
3031
}
3132

33+
public int SpeedControlStepSize
34+
{
35+
get => (int)StateSettingsHandler.Current.GetSettings().SpeedControlStepSize;
36+
set
37+
{
38+
if (StateSettingsHandler.Current.GetSettings().SpeedControlStepSize != (int)value)
39+
{
40+
StateSettingsHandler.Current.GetSettings().SpeedControlStepSize = (int)value;
41+
StateSettingsHandler.Current.Save();
42+
}
43+
OnPropertyChanged(nameof(SpeedControlStepSize));
44+
}
45+
}
46+
47+
public bool SnapTo10Units
48+
{
49+
get => StateSettingsHandler.Current.GetSettings().SnapTo10s;
50+
set
51+
{
52+
if (StateSettingsHandler.Current.GetSettings().SnapTo10s != value)
53+
{
54+
StateSettingsHandler.Current.GetSettings().SnapTo10s = value;
55+
StateSettingsHandler.Current.Save();
56+
}
57+
OnPropertyChanged(nameof(SnapTo10Units));
58+
}
59+
}
60+
3261
public int SelectedAccelerationOption { get; set; }
3362
public int SelectedSteeringSensitivityOption { get; set; }
3463
public int SelectedFollowingDistanceOption { get; set; }

ETS2LA/ETS2LA.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
<Title>ETS2LA</Title>
1010
<Description>Self driving for ETS2 and ATS.</Description>
11-
<Version>3.3.1</Version>
11+
<Version>3.3.2</Version>
1212
</PropertyGroup>
1313

1414
<ItemGroup>

0 commit comments

Comments
 (0)