Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Commit

Permalink
Merge from develop for release
Browse files Browse the repository at this point in the history
  • Loading branch information
Yamazaki93 committed Apr 30, 2020
2 parents 95d5f20 + 2774f6e commit e691dd3
Show file tree
Hide file tree
Showing 35 changed files with 541 additions and 577 deletions.
32 changes: 29 additions & 3 deletions TSWMod/InputHelpers.cs
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace TSWMod
{
Expand All @@ -18,7 +20,7 @@ public static class InputHelpers
/// <summary>
/// Virtual Keys
/// </summary>
public enum VirtualKeyStates : int
public enum VKCodes : int
{
VK_LBUTTON = 0x01,
VK_RBUTTON = 0x02,
Expand Down Expand Up @@ -253,12 +255,36 @@ public static IntPtr GetMainWindow(string processName)
return processes.Length > 0 ? processes[0].MainWindowHandle : IntPtr.Zero;
}

public static void KeyComboDown(IntPtr hWnd, VKCodes[] keys)
{
for (var i = 0; i < keys.Length; i++)
{
KeyDown(hWnd, keys[i]);
if (i == keys.Length - 1)
break;
Thread.Sleep(10);
}
}

public static void KeyComboUp(IntPtr hWnd, VKCodes[] keys)
{
keys = keys.Reverse().ToArray();
for (var i = 0; i < keys.Length; i++)
{
KeyUp(hWnd, keys[i]);
if (i == keys.Length - 1)
break;
Thread.Sleep(10);
}
}


/// <summary>
/// Sends a Keydown message(0x100) to the specified window with a Virtual Key
/// </summary>
/// <param name="winTitle">Window Title</param>
/// <param name="Key">Virtual Key to Send</param>
public static void KeyDown(IntPtr hWnd, VirtualKeyStates Key)
public static void KeyDown(IntPtr hWnd, VKCodes Key)
{
SendMessage(hWnd, 0x100, (int)Key, 0);
}
Expand All @@ -268,7 +294,7 @@ public static void KeyDown(IntPtr hWnd, VirtualKeyStates Key)
/// </summary>
/// <param name="winTitle">Window Title</param>
/// <param name="Key">Virtual Key to Send</param>
public static void KeyUp(IntPtr hWnd, VirtualKeyStates Key)
public static void KeyUp(IntPtr hWnd, VKCodes Key)
{
SendMessage(hWnd, 0x101, (int)Key, 0);
}
Expand Down
12 changes: 9 additions & 3 deletions TSWMod/RailDriver/RDThrottleDynamicBrake.cs
Expand Up @@ -25,20 +25,26 @@ public float TranslatedThrottleValue
return 0;
}

return ((float)CurrentValue - _idleMin) / ((float)Max - _idleMin);
return ((float) CurrentValue - _idleMin) / ((float) Max - _idleMin);
}
}

public float TranslatedDynamicBrakeValue
{
get
{
if (CurrentValue > _dynSetupMax)
var idleDynSetupLimit = (_dynSetupMin + _idleMin) / 2f;
if (CurrentValue > idleDynSetupLimit)
{
return 0;
}

return 1 - ((float)CurrentValue - Min) / ((float)_dynSetupMax - Min);
var dynamicRangeValue = 1 - ((float) CurrentValue - Min) / ((float) _dynSetupMax - Min);
if (dynamicRangeValue < 0.02)
{
return 0.02f;
}
return Math.Max(dynamicRangeValue, 0.03f);
}
}

Expand Down
2 changes: 1 addition & 1 deletion TSWMod/RailDriver/RailDriverLeverState.cs
Expand Up @@ -39,7 +39,7 @@ class RailDriverLeverState

public float ReverserTranslated { get; } // 0 = Reverse, 0.5 = Neutral, 1 = Forward
public float ThrottleTranslated { get; } // 0 - 1
public float DynamicBrakeTranslated { get; } // 0 - 1
public float DynamicBrakeTranslated { get; } // 0 = off, 0.02 = setup, 0.03 - 1 = brake range
public float AutoBrakeTranslated { get; } // 0 - 1 = Full auto brake range, 2 = emergency

public bool BailOff { get; }
Expand Down
25 changes: 13 additions & 12 deletions TSWMod/TSW/CSX/AC4400CW.cs
Expand Up @@ -9,17 +9,18 @@ namespace TSWMod.TSW.CSX
class AC4400CW : ILocomotive
{
public const string NamePartial = "AC4400CW";
private readonly IDictionary<int, InputHelpers.VirtualKeyStates> DefaultKeyMappings = new Dictionary<int, InputHelpers.VirtualKeyStates>
{
{ 36, InputHelpers.VirtualKeyStates.VK_BACK }, // Emergency brake
{ 37, InputHelpers.VirtualKeyStates.VK_BACK },
{ 38, InputHelpers.VirtualKeyStates.VK_Q },
{ 39, InputHelpers.VirtualKeyStates.VK_X },
{ 40, InputHelpers.VirtualKeyStates.VK_P },
{ 41, InputHelpers.VirtualKeyStates.VK_B },
{ 42, InputHelpers.VirtualKeyStates.VK_SPACE }, // Horn
{ 43, InputHelpers.VirtualKeyStates.VK_SPACE },
};
private IDictionary<int, InputHelpers.VKCodes[]> DefaultKeyMappings =>
new Dictionary<int, InputHelpers.VKCodes[]>
{
{36, KeyboardLayoutManager.Current.EmergencyBrake}, // Emergency brake
{37, KeyboardLayoutManager.Current.EmergencyBrake},
{38, KeyboardLayoutManager.Current.AlerterReset},
{39, KeyboardLayoutManager.Current.Sand},
{40, KeyboardLayoutManager.Current.PantographRaise},
{41, KeyboardLayoutManager.Current.Bell},
{42, KeyboardLayoutManager.Current.Horn1}, // Horn
{43, KeyboardLayoutManager.Current.Horn1},
};

public AC4400CW(Mem m, UIntPtr basePtr, IntPtr hWnd)
{
Expand Down Expand Up @@ -76,7 +77,7 @@ public void SetConfiguration(JObject config)
{
}

public IDictionary<int, InputHelpers.VirtualKeyStates> GetButtonMappings()
public IDictionary<int, InputHelpers.VKCodes[]> GetButtonMappings()
{
return DefaultKeyMappings;
}
Expand Down
34 changes: 3 additions & 31 deletions TSWMod/TSW/CSX/AC4400CWAutoBrake.cs
Expand Up @@ -9,7 +9,7 @@ namespace TSWMod.TSW.CSX
{
class AC4400CWAutoBrake : TSWLever
{
public AC4400CWAutoBrake(Mem m, IntPtr hWnd, UIntPtr basePtr) : base(m, hWnd, basePtr, false)
public AC4400CWAutoBrake(Mem m, IntPtr hWnd, UIntPtr basePtr) : base(m, hWnd, basePtr)
{
}

Expand Down Expand Up @@ -43,35 +43,7 @@ public AC4400CWAutoBrake(Mem m, IntPtr hWnd, UIntPtr basePtr) : base(m, hWnd, ba
return 0.85f; // > 0.85 = handle off
}

protected override void OnDifferentValue(float diff)
{
if (diff < 0)
{
if (_currentKeyDown == '"')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_7);
}
_currentKeyDown = ';';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_OEM_1);
}
else
{
if (_currentKeyDown == '"')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_1);
}
_currentKeyDown = ';';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_OEM_7);
}
}

protected override void OnSameValue()
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_1);
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_7);
}


private char _currentKeyDown = ' ';
protected override InputHelpers.VKCodes[] IncreaseKeys => KeyboardLayoutManager.Current.AutomaticBrakeIncrease;
protected override InputHelpers.VKCodes[] DecreaseKeys => KeyboardLayoutManager.Current.AutomaticBrakeDecrease;
}
}
45 changes: 8 additions & 37 deletions TSWMod/TSW/CSX/AC4400CWIndependentBrake.cs
Expand Up @@ -9,7 +9,7 @@ namespace TSWMod.TSW.CSX
{
class AC4400CWIndependentBrake : TSWLever
{
public AC4400CWIndependentBrake(Mem m, IntPtr hWnd, UIntPtr basePtr) : base(m, hWnd, basePtr, false)
public AC4400CWIndependentBrake(Mem m, IntPtr hWnd, UIntPtr basePtr) : base(m, hWnd, basePtr)
{
}

Expand All @@ -18,58 +18,29 @@ protected override float TargetValuePreTransform(float raw)
return 0.8f * raw + 0.2f;
}

protected override void OnDifferentValue(float diff)
{
if (_bailOff)
{
return;
}

if (diff < 0)
{
if (_currentKeyDown == ']')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_6);
}
_currentKeyDown = '[';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_OEM_4);
}
else
{
if (_currentKeyDown == '[')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_4);
}
_currentKeyDown = ']';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_OEM_6);
}
}

protected override void OnSameValue()
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_4);
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_6);
}

public void EngageBailOffIfNeeded()
{
if (!_bailOff && CurrentValue.AlmostEquals(0.2f))
{
_bailOff = true;
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_OEM_4);
InputHelpers.KeyComboDown(HWND, KeyboardLayoutManager.Current.IndependentBrakeDecrease);
}
}

public void DisengageBailOffIfNeeded()
{
if (_bailOff)
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_OEM_4);
InputHelpers.KeyComboUp(HWND, KeyboardLayoutManager.Current.IndependentBrakeDecrease);
_bailOff = false;
}
}

private char _currentKeyDown = ' ';
protected override InputHelpers.VKCodes[] IncreaseKeys =>
KeyboardLayoutManager.Current.IndependentBrakeIncrease;
protected override InputHelpers.VKCodes[] DecreaseKeys =>
KeyboardLayoutManager.Current.IndependentBrakeDecrease;

private bool _bailOff;
}
}
32 changes: 2 additions & 30 deletions TSWMod/TSW/CSX/AC4400CWThrottle.cs
Expand Up @@ -13,34 +13,6 @@ public AC4400CWThrottle(Mem m, IntPtr hWnd, UIntPtr basePtr) : base(m, hWnd, bas
{
}

protected override void OnDifferentValue(float diff)
{
if (diff < 0)
{
if (_currentKeyDown == 'a')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_A);
}
_currentKeyDown = 'd';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_D);
}
else
{
if (_currentKeyDown == 'd')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_D);
}
_currentKeyDown = 'a';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_A);
}
}

protected override void OnSameValue()
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_D);
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_A);
}

public float TranslateCombinedValue(float throttle, float dynamicBrake)
{
if (throttle > 0) // throttle is 0-8
Expand All @@ -55,7 +27,7 @@ public float TranslateCombinedValue(float throttle, float dynamicBrake)

return 0;
}

private char _currentKeyDown = ' ';
protected override InputHelpers.VKCodes[] IncreaseKeys => KeyboardLayoutManager.Current.ThrottleIncrease;
protected override InputHelpers.VKCodes[] DecreaseKeys => KeyboardLayoutManager.Current.ThrottleDecrease;
}
}
32 changes: 2 additions & 30 deletions TSWMod/TSW/CSX/EightNotchThrottle.cs
Expand Up @@ -17,35 +17,7 @@ protected override float TargetValuePreTransform(float raw)
{
return Convert.ToSingle(Math.Round(raw * 8f, 0));
}

protected override void OnDifferentValue(float diff)
{
if (diff < 0)
{
if (_currentKeyDown == 'a')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_A);
}
_currentKeyDown = 'd';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_D);
}
else
{
if (_currentKeyDown == 'd')
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_D);
}
_currentKeyDown = 'a';
InputHelpers.KeyDown(HWND, InputHelpers.VirtualKeyStates.VK_A);
}
}

protected override void OnSameValue()
{
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_D);
InputHelpers.KeyUp(HWND, InputHelpers.VirtualKeyStates.VK_A);
}

private char _currentKeyDown = ' ';
protected override InputHelpers.VKCodes[] IncreaseKeys => KeyboardLayoutManager.Current.ThrottleIncrease;
protected override InputHelpers.VKCodes[] DecreaseKeys => KeyboardLayoutManager.Current.ThrottleDecrease;
}
}

0 comments on commit e691dd3

Please sign in to comment.