Pad: Add button name -> code lookup functions

This commit is contained in:
Connor McLaughlin 2019-12-09 00:46:04 +10:00
parent 8930383c96
commit c1710482df
4 changed files with 58 additions and 1 deletions

View file

@ -72,3 +72,33 @@ std::shared_ptr<DigitalController> DigitalController::Create()
{
return std::make_shared<DigitalController>();
}
std::optional<s32> DigitalController::GetButtonCodeByName(std::string_view button_name)
{
#define BUTTON(name) \
if (button_name == #name) \
{ \
return static_cast<s32>(ZeroExtend32(static_cast<u8>(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
}

View file

@ -1,6 +1,8 @@
#pragma once
#include "pad_device.h"
#include <memory>
#include <optional>
#include <string_view>
class DigitalController final : public PadDevice
{
@ -29,6 +31,7 @@ public:
~DigitalController() override;
static std::shared_ptr<DigitalController> Create();
static std::optional<s32> GetButtonCodeByName(std::string_view button_name);
void SetButtonState(Button button, bool pressed);

View file

@ -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> PadDevice::Create(std::string_view type_name)
{
if (type_name == "DigitalController")
return DigitalController::Create();
return {};
}
std::optional<s32> PadDevice::GetButtonCodeByName(std::string_view type_name, std::string_view button_name)
{
if (type_name == "DigitalController")
return DigitalController::GetButtonCodeByName(button_name);
return {};
}

View file

@ -1,5 +1,7 @@
#pragma once
#include "types.h"
#include <optional>
#include <string_view>
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<PadDevice> Create(std::string_view type_name);
/// Gets the integer code for a button in the specified controller type.
static std::optional<s32> GetButtonCodeByName(std::string_view type_name, std::string_view button_name);
};