From c1710482df281a0d204a8fb2dd51b9c062873e45 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Mon, 9 Dec 2019 00:46:04 +1000 Subject: [PATCH] Pad: Add button name -> code lookup functions --- src/core/digital_controller.cpp | 30 ++++++++++++++++++++++++++++++ src/core/digital_controller.h | 3 +++ src/core/pad_device.cpp | 17 +++++++++++++++++ src/core/pad_device.h | 9 ++++++++- 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/core/digital_controller.cpp b/src/core/digital_controller.cpp index f5b5b29c8..8b78ad478 100644 --- a/src/core/digital_controller.cpp +++ b/src/core/digital_controller.cpp @@ -72,3 +72,33 @@ std::shared_ptr DigitalController::Create() { return std::make_shared(); } + +std::optional DigitalController::GetButtonCodeByName(std::string_view button_name) +{ +#define BUTTON(name) \ + if (button_name == #name) \ + { \ + return static_cast(ZeroExtend32(static_cast(Button::name))); \ + } + + BUTTON(Select); + BUTTON(L3); + BUTTON(R3); + BUTTON(Start); + BUTTON(Up); + BUTTON(Right); + BUTTON(Down); + BUTTON(Left); + BUTTON(L2); + BUTTON(R2); + BUTTON(L1); + BUTTON(R1); + BUTTON(Triangle); + BUTTON(Circle); + BUTTON(Cross); + BUTTON(Square); + + return std::nullopt; + +#undef BUTTON +} diff --git a/src/core/digital_controller.h b/src/core/digital_controller.h index 510bf544a..15d1a14c5 100644 --- a/src/core/digital_controller.h +++ b/src/core/digital_controller.h @@ -1,6 +1,8 @@ #pragma once #include "pad_device.h" #include +#include +#include class DigitalController final : public PadDevice { @@ -29,6 +31,7 @@ public: ~DigitalController() override; static std::shared_ptr Create(); + static std::optional GetButtonCodeByName(std::string_view button_name); void SetButtonState(Button button, bool pressed); diff --git a/src/core/pad_device.cpp b/src/core/pad_device.cpp index 3e4c9818f..b66324a01 100644 --- a/src/core/pad_device.cpp +++ b/src/core/pad_device.cpp @@ -1,5 +1,6 @@ #include "pad_device.h" #include "common/state_wrapper.h" +#include "digital_controller.h" PadDevice::PadDevice() = default; @@ -19,3 +20,19 @@ bool PadDevice::Transfer(const u8 data_in, u8* data_out) *data_out = 0xFF; return false; } + +std::shared_ptr PadDevice::Create(std::string_view type_name) +{ + if (type_name == "DigitalController") + return DigitalController::Create(); + + return {}; +} + +std::optional PadDevice::GetButtonCodeByName(std::string_view type_name, std::string_view button_name) +{ + if (type_name == "DigitalController") + return DigitalController::GetButtonCodeByName(button_name); + + return {}; +} diff --git a/src/core/pad_device.h b/src/core/pad_device.h index 61b41399b..6c21cc5da 100644 --- a/src/core/pad_device.h +++ b/src/core/pad_device.h @@ -1,5 +1,7 @@ #pragma once #include "types.h" +#include +#include class StateWrapper; @@ -17,5 +19,10 @@ public: // Returns the value of ACK, as well as filling out_data. virtual bool Transfer(const u8 data_in, u8* data_out); -}; + /// Creates a new controller of the specified type. + static std::shared_ptr Create(std::string_view type_name); + + /// Gets the integer code for a button in the specified controller type. + static std::optional GetButtonCodeByName(std::string_view type_name, std::string_view button_name); +};