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 IGamepadReader? GetReader() {
Console.WriteLine("Write a number to select controller type:");
Console.WriteLine("[1] - JoyCon");
Console.WriteLine("[2] - Wiimote");
private static readonly List<(string name, IGamepadReader reader)> Readers = [
#if JoyCon
("JoyCon", new JoyConRead()),
#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();
if (line == null) {
Console.WriteLine("Invalid input.");
@ -15,13 +37,11 @@ public static class ControllerSelector {
Console.WriteLine("Invalid number.");
return null;
}
if (id <= 0 || id >= Readers.Count) {
Console.WriteLine("Invalid number");
return null;
}
return GetReader(id);
return id - 1;
}
private static IGamepadReader? GetReader(int id) => id switch {
1 => new JoyConRead(),
2 => new WiimoteRead(),
_ => null
};
}