refactor: preprocessors for wiimote and joycon compilation

This commit is contained in:
Gerard Gascón 2024-06-28 15:18:56 +02:00
parent 65d562ee74
commit 72750c3388
11 changed files with 63 additions and 32 deletions

View file

@ -1,11 +1,33 @@
namespace SwitchSlidePresenter; using ControllerSlidePresenter.GamepadReader;
namespace ControllerSlidePresenter;
public static class ControllerSelector { public static class ControllerSelector {
public static IGamepadReader? GetReader() { private static readonly List<(string name, IGamepadReader reader)> Readers = [
Console.WriteLine("Write a number to select controller type:"); #if JoyCon
Console.WriteLine("[1] - JoyCon"); ("JoyCon", new JoyConRead()),
Console.WriteLine("[2] - Wiimote"); #endif
#if Wiimote
("Wiimote", new WiimoteRead())
#endif
];
public static IGamepadReader? GetReader() {
if (Readers.Count == 1)
return Readers[0].reader;
Console.WriteLine("Write a number to select controller type:");
for (int i = 0; i < Readers.Count; i++)
Console.WriteLine($"[{i+1}] - {Readers[i].name}");
int? id = GetReaderIndex();
if (id == null)
return null;
return Readers[id.Value].reader;
}
private static int? GetReaderIndex() {
string? line = Console.ReadLine(); string? line = Console.ReadLine();
if (line == null) { if (line == null) {
Console.WriteLine("Invalid input."); Console.WriteLine("Invalid input.");
@ -15,13 +37,11 @@ public static class ControllerSelector {
Console.WriteLine("Invalid number."); Console.WriteLine("Invalid number.");
return null; return null;
} }
if (id <= 0 || id >= Readers.Count) {
return GetReader(id); Console.WriteLine("Invalid number");
return null;
} }
private static IGamepadReader? GetReader(int id) => id switch { return id - 1;
1 => new JoyConRead(), }
2 => new WiimoteRead(),
_ => null
};
} }

View file

@ -5,22 +5,26 @@
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<RootNamespace>SwitchSlidePresenter</RootNamespace>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))"> <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
<DefineConstants>OS_WINDOWS</DefineConstants> <DefineConstants>$(DefineConstants);OS_WINDOWS</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))"> <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
<DefineConstants>OS_LINUX</DefineConstants> <DefineConstants>$(DefineConstants);OS_LINUX</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))"> <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('OSX'))">
<DefineConstants>OS_MAC</DefineConstants> <DefineConstants>$(DefineConstants);OS_MAC</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<DefineConstants>$(DefineConstants);JoyCon</DefineConstants>
<DefineConstants>$(DefineConstants);Wiimote</DefineConstants>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="JoyCon.NET" Version="1.0.1" /> <PackageReference Include="JoyCon.NET" Version="1.0.1" Condition="$(DefineConstants.Contains(JoyCon))" />
<PackageReference Include="WiimoteLib.NetCore" Version="1.0.0" /> <PackageReference Include="WiimoteLib.NetCore" Version="1.0.0" Condition="$(DefineConstants.Contains(Wiimote))" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View file

@ -1,4 +1,4 @@
namespace SwitchSlidePresenter; namespace ControllerSlidePresenter.GamepadReader;
public interface IGamepadReader { public interface IGamepadReader {
public event Action NextSlide; public event Action NextSlide;

View file

@ -1,11 +1,12 @@
using System.Text; #if JoyCon
using System.Text;
using HidSharp; using HidSharp;
using wtf.cluster.JoyCon; using wtf.cluster.JoyCon;
using wtf.cluster.JoyCon.ExtraData; using wtf.cluster.JoyCon.ExtraData;
using wtf.cluster.JoyCon.InputData; using wtf.cluster.JoyCon.InputData;
using wtf.cluster.JoyCon.InputReports; using wtf.cluster.JoyCon.InputReports;
namespace SwitchSlidePresenter; namespace ControllerSlidePresenter.GamepadReader;
public class JoyConRead : IGamepadReader { public class JoyConRead : IGamepadReader {
public event Action NextSlide; public event Action NextSlide;
@ -90,3 +91,4 @@ public class JoyConRead : IGamepadReader {
return input.ZLorZR || input.Down; return input.ZLorZR || input.Down;
} }
} }
#endif

View file

@ -1,6 +1,7 @@
using WiimoteLib.NetCore; #if Wiimote
using WiimoteLib.NetCore;
namespace SwitchSlidePresenter; namespace ControllerSlidePresenter.GamepadReader;
public class WiimoteRead : IGamepadReader { public class WiimoteRead : IGamepadReader {
public event Action NextSlide; public event Action NextSlide;
@ -47,3 +48,4 @@ public class WiimoteRead : IGamepadReader {
return input.A || input.Right; return input.A || input.Right;
} }
} }
#endif

View file

@ -1,4 +1,4 @@
namespace SwitchSlidePresenter.InputSender; namespace ControllerSlidePresenter.InputSender;
public interface IInputSender { public interface IInputSender {
void NextSlide(); void NextSlide();

View file

@ -1,5 +1,5 @@
#if OS_LINUX #if OS_LINUX
namespace SwitchSlidePresenter.InputSender; namespace ControllerSlidePresenter.InputSender;
public class LinuxInputSender : IInputSender { public class LinuxInputSender : IInputSender {
public void NextSlide() { public void NextSlide() {

View file

@ -1,5 +1,5 @@
#if OS_MAC #if OS_MAC
namespace SwitchSlidePresenter.InputSender; namespace ControllerSlidePresenter.InputSender;
public class MacInputSender : IInputSender { public class MacInputSender : IInputSender {
public void NextSlide() { public void NextSlide() {

View file

@ -2,7 +2,7 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Win32Api; using Win32Api;
namespace SwitchSlidePresenter.InputSender; namespace ControllerSlidePresenter.InputSender;
public class WindowsInputSender : IInputSender { public class WindowsInputSender : IInputSender {
private const uint INPUT_KEYBOARD = 1; private const uint INPUT_KEYBOARD = 1;

View file

@ -1,4 +1,6 @@
namespace SwitchSlidePresenter { using ControllerSlidePresenter.GamepadReader;
namespace ControllerSlidePresenter {
internal abstract class Program { internal abstract class Program {
private static async Task Main() { private static async Task Main() {
IGamepadReader? reader = ControllerSelector.GetReader(); IGamepadReader? reader = ControllerSelector.GetReader();

View file

@ -1,6 +1,7 @@
using SwitchSlidePresenter.InputSender; using ControllerSlidePresenter.GamepadReader;
using ControllerSlidePresenter.InputSender;
namespace SwitchSlidePresenter; namespace ControllerSlidePresenter;
public class SlideSwitcher : IDisposable { public class SlideSwitcher : IDisposable {
private readonly IGamepadReader? _reader; private readonly IGamepadReader? _reader;