mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2025-02-18 11:55:38 +00:00
Pad: Add button name -> code lookup functions
This commit is contained in:
parent
8930383c96
commit
c1710482df
|
@ -72,3 +72,33 @@ std::shared_ptr<DigitalController> DigitalController::Create()
|
||||||
{
|
{
|
||||||
return std::make_shared<DigitalController>();
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "pad_device.h"
|
#include "pad_device.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <optional>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
class DigitalController final : public PadDevice
|
class DigitalController final : public PadDevice
|
||||||
{
|
{
|
||||||
|
@ -29,6 +31,7 @@ public:
|
||||||
~DigitalController() override;
|
~DigitalController() override;
|
||||||
|
|
||||||
static std::shared_ptr<DigitalController> Create();
|
static std::shared_ptr<DigitalController> Create();
|
||||||
|
static std::optional<s32> GetButtonCodeByName(std::string_view button_name);
|
||||||
|
|
||||||
void SetButtonState(Button button, bool pressed);
|
void SetButtonState(Button button, bool pressed);
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include "pad_device.h"
|
#include "pad_device.h"
|
||||||
#include "common/state_wrapper.h"
|
#include "common/state_wrapper.h"
|
||||||
|
#include "digital_controller.h"
|
||||||
|
|
||||||
PadDevice::PadDevice() = default;
|
PadDevice::PadDevice() = default;
|
||||||
|
|
||||||
|
@ -19,3 +20,19 @@ bool PadDevice::Transfer(const u8 data_in, u8* data_out)
|
||||||
*data_out = 0xFF;
|
*data_out = 0xFF;
|
||||||
return false;
|
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 {};
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
#include <optional>
|
||||||
|
#include <string_view>
|
||||||
|
|
||||||
class StateWrapper;
|
class StateWrapper;
|
||||||
|
|
||||||
|
@ -17,5 +19,10 @@ public:
|
||||||
|
|
||||||
// Returns the value of ACK, as well as filling out_data.
|
// Returns the value of ACK, as well as filling out_data.
|
||||||
virtual bool Transfer(const u8 data_in, u8* data_out);
|
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);
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue