Commit 72750c33 authored by Gerard Gascón's avatar Gerard Gascón
Browse files

refactor: preprocessors for wiimote and joycon compilation

parent 65d562ee
Loading
Loading
Loading
Loading
+30 −10
Original line number Diff line number Diff line
namespace SwitchSlidePresenter;
using ControllerSlidePresenter.GamepadReader;

namespace ControllerSlidePresenter;

public static class ControllerSelector {
	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:");
		Console.WriteLine("[1] - JoyCon");
		Console.WriteLine("[2] - Wiimote");
		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;
		}

		return GetReader(id);
		if (id <= 0 || id >= Readers.Count) {
			Console.WriteLine("Invalid number");
			return null;
		}

	private static IGamepadReader? GetReader(int id) => id switch {
		1 => new JoyConRead(),
		2 => new WiimoteRead(),
		_ => null
	};
		return id - 1;
	}
}
 No newline at end of file
+10 −6
Original line number Diff line number Diff line
@@ -5,22 +5,26 @@
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <RootNamespace>SwitchSlidePresenter</RootNamespace>
    </PropertyGroup>

    <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Windows'))">
        <DefineConstants>OS_WINDOWS</DefineConstants>
        <DefineConstants>$(DefineConstants);OS_WINDOWS</DefineConstants>
    </PropertyGroup>
    <PropertyGroup Condition="$([MSBuild]::IsOSPlatform('Linux'))">
        <DefineConstants>OS_LINUX</DefineConstants>
        <DefineConstants>$(DefineConstants);OS_LINUX</DefineConstants>
    </PropertyGroup>
    <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>
    
    <ItemGroup>
      <PackageReference Include="JoyCon.NET" Version="1.0.1" />
      <PackageReference Include="WiimoteLib.NetCore" Version="1.0.0" />
      <PackageReference Include="JoyCon.NET" Version="1.0.1" Condition="$(DefineConstants.Contains(JoyCon))" />
      <PackageReference Include="WiimoteLib.NetCore" Version="1.0.0" Condition="$(DefineConstants.Contains(Wiimote))" />
    </ItemGroup>

    <ItemGroup>
+1 −1
Original line number Diff line number Diff line
namespace SwitchSlidePresenter;
namespace ControllerSlidePresenter.GamepadReader;

public interface IGamepadReader {
	public event Action NextSlide;
+5 −3
Original line number Diff line number Diff line
using System.Text;
#if JoyCon
using System.Text;
using HidSharp;
using wtf.cluster.JoyCon;
using wtf.cluster.JoyCon.ExtraData;
using wtf.cluster.JoyCon.InputData;
using wtf.cluster.JoyCon.InputReports;

namespace SwitchSlidePresenter;
namespace ControllerSlidePresenter.GamepadReader;

public class JoyConRead : IGamepadReader {
	public event Action NextSlide;
@@ -90,3 +91,4 @@ public class JoyConRead : IGamepadReader {
		return input.ZLorZR || input.Down;
	}
}
#endif
+5 −3
Original line number Diff line number Diff line
using WiimoteLib.NetCore;
#if Wiimote
using WiimoteLib.NetCore;

namespace SwitchSlidePresenter;
namespace ControllerSlidePresenter.GamepadReader;

public class WiimoteRead : IGamepadReader {
	public event Action NextSlide;
@@ -47,3 +48,4 @@ public class WiimoteRead : IGamepadReader {
		return input.A || input.Right;
	}
}
#endif
Loading