Skip to content

Commit

Permalink
Retirer le timer remplacer par un event ajout de graphique mise a jou…
Browse files Browse the repository at this point in the history
…r du readme.
  • Loading branch information
Nirad committed Sep 18, 2024
1 parent 13a75f4 commit 7a9b5f4
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Xbox Controller Cursor Control
![Alt text](screenshot/iconne.png?raw=true "Xbox Controller Cursor Control")

![Alt text]screenshot/app.png?raw=true "Xbox Controller Cursor Control")
## Introduction

This program allows you to control the mouse cursor using an Xbox controller. Use the left joystick to move the cursor, and buttons A and B to simulate mouse clicks. The right joystick simulates mouse wheel scrolling.
Expand Down
Binary file added Xbox Controller Cursor Control/Images/boutton.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions Xbox Controller Cursor Control/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
Width="250"
WindowStyle="None"
AllowsTransparency="True"
Background="Transparent" Loaded="Window_Loaded">
<Grid Background="DarkGray">
Background="Transparent"
Loaded="Window_Loaded">
<Grid Background="#FF1C1C1C">
<!-- Custom title bar -->
<Border Background="Black" Height="20" VerticalAlignment="Top" HorizontalAlignment="Stretch"
MouseLeftButtonDown="Border_MouseLeftButtonDown">
Expand All @@ -39,8 +40,9 @@
<!-- Main content area -->
<Label x:Name="lblStatus" Content="" Margin="0,35,0,5" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="Auto"/>
<Slider x:Name="sliderMouseSptep" HorizontalAlignment="Left" Margin="10,46,0,0" VerticalAlignment="Top" Width="120" Maximum="20" Minimum="5" SmallChange="1" ValueChanged="Slider_ValueChanged" Value="10" IsEnabled="True"/>
<Label Content="Cursor Speed" HorizontalAlignment="Left" Margin="32,20,0,0" VerticalAlignment="Top"/>
<Label Content="Cursor Speed" HorizontalAlignment="Left" Margin="32,20,0,0" VerticalAlignment="Top" Foreground="#FFF6EFEF"/>
<Slider x:Name="sliderScrollSpeed" HorizontalAlignment="Left" Margin="10,95,0,0" VerticalAlignment="Top" Width="120" Maximum="250" Minimum="50" SmallChange="10" ValueChanged="sliderScrollSpeed_ValueChanged" TickFrequency="10" Value="100" IsEnabled="True"/>
<Label Content="Scroll Speed" HorizontalAlignment="Left" Margin="32,69,0,0" VerticalAlignment="Top"/>
<Label Content="Scroll Speed" HorizontalAlignment="Left" Margin="32,69,0,0" VerticalAlignment="Top" Foreground="#FFF6EFEF"/>
<Image Margin="141,34,10,30" Source="/Images/boutton.png" Stretch="Fill"/>
</Grid>
</Window>
16 changes: 6 additions & 10 deletions Xbox Controller Cursor Control/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,23 @@ public MainWindow()
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Init();
// Configurez un Timer ou un autre mécanisme pour mettre à jour l'interface utilisateur
DispatcherTimer timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1); // Mettez à jour toutes les secondes
timer.Tick += OnTimerTick;
timer.Start();
}

private void OnTimerTick(object? sender, EventArgs e)
{
UpdateStatus();
}

private void Init()
{
xController.ControllerStatusChanged += OnControllerStatusChanged!;
Task.Run(() => xController.Start());
}

private void OnControllerStatusChanged(object sender, EventArgs e)
{
Dispatcher.Invoke(UpdateStatus); // Met à jour l'UI sur le thread principal
}
private void UpdateStatus()
{
// Exécuter sur le thread principal pour mettre à jour l'interface utilisateur
if (xController.success)
if (xController.isConnected)
{
lblStatus.Content = "Controller connected";
lblStatus.Foreground = Brushes.Green;
Expand Down
27 changes: 20 additions & 7 deletions Xbox Controller Cursor Control/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,40 @@ private struct XINPUT_GAMEPAD

public int WHEEL_DELTA = 100; // Valeur standard pour un "tick" de la molette de défilement
private const float DEADZONE = 0.15f; // Zone morte pour les joysticks
public bool success;
public bool isConnected = false;
public int moveStep = 10; // Nombre de pixels à déplacer à chaque mise à jour
public event EventHandler<EventArgs>? ControllerStatusChanged;
public XController()
{
ControllerStatusChanged = null;
}
public void Start()
{
bool isButtonAPressed = false; // État actuel du bouton "A"
bool wasButtonAPressed = false; // État précédent du bouton "A"

bool isButtonBPressed = false; // État actuel du bouton "B"
bool wasButtonBPressed = false; // État précédent du bouton "B"

while (true)
{
XINPUT_STATE state;
int result = XInputGetState(0, out state); // 0 pour le premier contrôleur

if (result != 0)
{
Console.WriteLine("Controller not connected.");
success = false;
if (isConnected)
{
isConnected = false;
OnControllerStatusChanged(); // Déclenche l'événement de déconnexion
}
}
else
{
success = true;
if (!isConnected)
{
isConnected = true;
OnControllerStatusChanged(); // Déclenche l'événement de connexion
}
}

float leftThumbX = state.Gamepad.sThumbLX;
float leftThumbY = state.Gamepad.sThumbLY;
float rightThumbY = state.Gamepad.sThumbRY;
Expand Down Expand Up @@ -181,5 +190,9 @@ public void Start()
System.Threading.Thread.Sleep(10);
}
}
protected virtual void OnControllerStatusChanged()
{
ControllerStatusChanged?.Invoke(this, EventArgs.Empty);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
<ApplicationIcon>app-icone.ico</ApplicationIcon>
</PropertyGroup>

<ItemGroup>
<None Remove="Images\boutton.png" />
</ItemGroup>

<ItemGroup>
<Content Include="app-icone.ico" />
<Content Include="Images\boutton.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>

</Project>
Binary file added Xbox Controller Cursor Control/app-icone.ico
Binary file not shown.
Binary file added screenshot/app.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshot/iconne.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7a9b5f4

Please sign in to comment.