Skip to content

Commit

Permalink
1.3 - Automation pauses automatically and resumes if controller is mo…
Browse files Browse the repository at this point in the history
…ving
  • Loading branch information
dliedke committed Oct 20, 2024
1 parent 81ae5be commit d8d5390
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
7 changes: 7 additions & 0 deletions D4Automator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
<ApplicationIcon>app.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Reference Include="SharpDX, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>packages\SharpDX.4.2.0\lib\net45\SharpDX.dll</HintPath>
</Reference>
<Reference Include="SharpDX.XInput, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b4dcf0f35e5521f1, processorArchitecture=MSIL">
<HintPath>packages\SharpDX.XInput.4.2.0\lib\net45\SharpDX.XInput.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand Down Expand Up @@ -78,6 +84,7 @@
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<None Include="packages.config" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
Expand Down
71 changes: 71 additions & 0 deletions MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using System.Collections.Generic;
using System.Runtime.InteropServices;

using SharpDX.XInput;

namespace D4Automator
{
public partial class MainForm : Form
Expand Down Expand Up @@ -44,6 +46,11 @@ public partial class MainForm : Form
private string settingsPath;
private CancellationTokenSource cancellationTokenSource;

private Controller controller;
private System.Windows.Forms.Timer controllerCheckTimer;
private const float DEAD_ZONE = 0.1f; // Adjust as needed
private bool wasAutomationRunning = false;

public MainForm()
{
InitializeComponent();
Expand All @@ -54,6 +61,63 @@ public MainForm()
AttachEventHandlers();
SetFormTitle();
UpdateLabels();
InitializeControllerDetection();
}

private void InitializeControllerDetection()
{
// Assumes the first controller
controller = new Controller(UserIndex.One);

// Check if controller is connected
if (controller.IsConnected)
{
controllerCheckTimer = new System.Windows.Forms.Timer();
controllerCheckTimer.Interval = 50; // Check every 50ms
controllerCheckTimer.Tick += ControllerCheckTimer_Tick;
controllerCheckTimer.Start();
}
}

private void ControllerCheckTimer_Tick(object sender, EventArgs e)
{
if (controller.IsConnected)
{
var state = controller.GetState();

bool controllerMoved = IsControllerMoved(state);

if (controllerMoved)
{
if (isRunning)
{
StopAutomation();
wasAutomationRunning = true;
}
}
else if (wasAutomationRunning)
{
StartAutomation();
wasAutomationRunning = false;
}
}
}

private bool IsControllerMoved(State state)
{
// Check analog sticks
bool leftStickMoved = Math.Abs(state.Gamepad.LeftThumbX) > short.MaxValue * DEAD_ZONE ||
Math.Abs(state.Gamepad.LeftThumbY) > short.MaxValue * DEAD_ZONE;
bool rightStickMoved = Math.Abs(state.Gamepad.RightThumbX) > short.MaxValue * DEAD_ZONE ||
Math.Abs(state.Gamepad.RightThumbY) > short.MaxValue * DEAD_ZONE;

// Check digital pad
bool dPadPressed = state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadUp) ||
state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadDown) ||
state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadLeft) ||
state.Gamepad.Buttons.HasFlag(GamepadButtonFlags.DPadRight);

return leftStickMoved || rightStickMoved || dPadPressed;
}

private void InitializeKeyMappings()
Expand Down Expand Up @@ -204,13 +268,16 @@ private void ToggleAutomation()
if (!isRunning)
{
StartAutomation();
wasAutomationRunning = false; // Reset this flag when manually starting
}
else
{
StopAutomation();
wasAutomationRunning = false; // Ensure this is false when manually stopping
}
}


private async void StartAutomation()
{
isRunning = true;
Expand Down Expand Up @@ -410,6 +477,10 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
SaveSettings();
UnregisterGlobalHotkey();

controllerCheckTimer?.Stop();
controllerCheckTimer?.Dispose();
controller = null;
}

private void btnKeyConfig_Click(object sender, EventArgs e)
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.2.0.0")]
[assembly: AssemblyFileVersion("1.2.0.0")]
[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyFileVersion("1.3.0.0")]
5 changes: 5 additions & 0 deletions packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="SharpDX" version="4.2.0" targetFramework="net48" />
<package id="SharpDX.XInput" version="4.2.0" targetFramework="net48" />
</packages>

0 comments on commit d8d5390

Please sign in to comment.