Skip to content

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace BrickController2.Droid.PlatformServices.GameController
{
internal class GameControllerService : GameControllerServiceBase<GamepadController>
internal class GameControllerService : GameControllerServiceBase
{
private readonly InputManager _inputManager;

Expand Down Expand Up @@ -36,9 +36,9 @@ internal void MainActivityOnInputDeviceAdded(int deviceId)
/// <param name="deviceId">deviceId of InputDevice</param>
internal void MainActivityOnInputDeviceRemoved(int deviceId)
{
if (TryRemove(x => x.Gamepad.Id == deviceId, out var controller))
if (TryRemove<GamepadController>(x => x.ControllerDevice.Id == deviceId, out var controller))
{
_logger.LogInformation("Gamepad has been removed DeviceId:{id}, ControllerId:{controllerId}",
_logger.LogInformation("ControllerDevice has been removed DeviceId:{id}, ControllerId:{controllerId}",
deviceId, controller.ControllerId);
}
}
Expand All @@ -64,14 +64,14 @@ internal void MainActivityOnInputDeviceChanged(int deviceId)
else if (controller != null)
{
// handle change - remove and then add it again
TryRemove(x => x.Gamepad.Id == deviceId, out _);
TryRemove<GamepadController>(x => x.ControllerDevice.Id == deviceId, out _);
}
AddGameControllerDevice(device);
}
}
else if (TryRemove(x => x.Gamepad.Id == deviceId, out var controller))
else if (TryRemove<GamepadController>(x => x.ControllerDevice.Id == deviceId, out var controller))
{
_logger.LogInformation("Gamepad has been removed DeviceId:{id}, ControllerId:{controllerId}",
_logger.LogInformation("ControllerDevice has been removed DeviceId:{id}, ControllerId:{controllerId}",
deviceId, controller.ControllerId);
}
}
Expand Down Expand Up @@ -122,7 +122,7 @@ private void AddGameControllerDevice(InputDevice gamepad)
}

private bool TryGetControllerByDeviceId(int deviceId, [MaybeNullWhen(false)] out GamepadController controller)
=> TryGetController(x => x.Gamepad.Id == deviceId, out controller);
=> TryGetController(x => x.ControllerDevice.Id == deviceId, out controller);

private static bool TryGetGamepadDevice(int deviceId, [MaybeNullWhen(false)] out InputDevice device)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Android.Views;
using BrickController2.Droid.Extensions;
using BrickController2.PlatformServices.GameController;
using System;
using System.Collections.Generic;
Expand All @@ -25,11 +24,8 @@ public GamepadController(GameControllerService service, InputDevice gamePad)
{
// initialize properties
Name = GetDisplayName(gamePad);
VendorId = gamePad.VendorId;
ProductId = gamePad.ProductId;
ControllerNumber = gamePad.ControllerNumber;
ControllerId = GetControllerIdFromNumber(gamePad.ControllerNumber);
UniquePersistantDeviceId = gamePad.GetUniquePersistentDeviceId();
}

internal bool OnButtonEvent(KeyEvent e, float buttonValue)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace BrickController2.Windows.PlatformServices.GameController;

internal class GameControllerService : GameControllerServiceBase<GamepadController>, IGameControllerService
internal class GameControllerService : GameControllerServiceBase, IGameControllerService
{
private readonly IMainThreadService _mainThreadService;
private readonly IDispatcherProvider _dispatcherProvider;
Expand Down Expand Up @@ -53,9 +53,9 @@ private void Gamepad_GamepadRemoved(object? sender, Gamepad gamepad)
// ensure stopped in UI thread
_ = _mainThreadService.RunOnMainThread(() =>
{
if (TryRemove(x => x.Gamepad == gamepad, out var controller))
if (TryRemove<GamepadController>(x => x.ControllerDevice == gamepad, out var controller))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is type of GamepadController needed to be defined for TryRemove? What is the benefit of such change, if it has to be specified here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because we lost the generic in the base class - it's just the interface IGameController now

    /// <summary>
    /// Collection of available gamepads having <see cref="IGameController.ControllerId"/>
    /// </summary>
    private readonly List<IGameController> _availableControllers = [];

But to distinguish the controllers we neet to handle the real type to be able to access properties not defined in the interface.

If there is another remove-event for another controller type to handle - i.e. for the McpServer ;) - the generic paramater of TryRemove<> would be that special type of controller:

    private void McpServerRemoved(object? sender, McpServer e)
    {
        lock (_lockObject)
        {
            // ensure stopped in UI thread
            _ = _mainThreadService.RunOnMainThread(() =>
            {
                if (TryRemove<McpServerController>(x => x.ControllerDevice is McpServer, out var controller))
                {
                    _logger.LogInformation("McpServer has been removed");
                }
            });
        }
    }

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I d not like this desgin as:

  • it enforces hard coupling of MCP server to controller service for each platform
image * additionally it seems to me in your follow up (#165), MCP platform inplementations are nearly the same. Is it?

Shortly said, it should be done differently. May be there might be some game controller providers, which are providing different types of game controllers, the gamepad ones, MCP ones. Just a quick idea.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, if I understood you correctly, I see it that way too.
It would be a clean solution if there were a central instance that manages all types of input devices and handles event signaling (gamepad 🕹️ events, gyro events, mcp server events, etc.).
Then there should be providers for the different types of input devices that register and deregister the input devices with the central instance und call the event invoking methods...

If I find some time these days I'll suggest an implementation of that.

{
_logger.LogInformation("Gamepad has been removed ControllerId:{controllerId}", controller.ControllerId);
_logger.LogInformation("Controller device has been removed ControllerId:{controllerId}", controller.ControllerId);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,10 @@ internal class GamepadController : GamepadControllerBase<Gamepad>
public GamepadController(GameControllerService service, Gamepad gamepad, RawGameController rawController, int controllerNumber, IDispatcherTimer timer)
: base(service, gamepad)
{
Name = rawController.DisplayName;
ControllerNumber = controllerNumber;
ControllerId = GetControllerIdFromNumber(controllerNumber);

UniquePersistantDeviceId = rawController.NonRoamableId;
Name = rawController.DisplayName;
VendorId = rawController.HardwareVendorId;
ProductId = rawController.HardwareProductId;

_timer = timer;

_timer.Interval = DefaultInterval;
Expand All @@ -55,7 +51,7 @@ public override void Stop()

private void Timer_Tick(object? sender, object e)
{
var currentReading = Gamepad.GetCurrentReading();
var currentReading = ControllerDevice.GetCurrentReading();

var currentEvents = currentReading
.Enumerate()
Expand Down
Loading