mirror of
https://github.com/RetroDECK/Duckstation.git
synced 2024-11-27 08:05:41 +00:00
Controller: Support general axis input events
This commit is contained in:
parent
32d8b4dc84
commit
6e18e56089
|
@ -21,6 +21,8 @@ bool Controller::Transfer(const u8 data_in, u8* data_out)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Controller::SetAxisState(s32 axis_code, float value) {}
|
||||||
|
|
||||||
void Controller::SetButtonState(s32 button_code, bool pressed) {}
|
void Controller::SetButtonState(s32 button_code, bool pressed) {}
|
||||||
|
|
||||||
std::unique_ptr<Controller> Controller::Create(ControllerType type)
|
std::unique_ptr<Controller> Controller::Create(ControllerType type)
|
||||||
|
@ -38,6 +40,24 @@ std::unique_ptr<Controller> Controller::Create(ControllerType type)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<s32> Controller::GetAxisCodeByName(std::string_view button_name) const
|
||||||
|
{
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::optional<s32> Controller::GetAxisCodeByName(ControllerType type, std::string_view axis_name)
|
||||||
|
{
|
||||||
|
switch (type)
|
||||||
|
{
|
||||||
|
case ControllerType::DigitalController:
|
||||||
|
return DigitalController::StaticGetAxisCodeByName(axis_name);
|
||||||
|
|
||||||
|
case ControllerType::None:
|
||||||
|
default:
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<s32> Controller::GetButtonCodeByName(ControllerType type, std::string_view button_name)
|
std::optional<s32> Controller::GetButtonCodeByName(ControllerType type, std::string_view button_name)
|
||||||
{
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
|
|
|
@ -15,6 +15,9 @@ public:
|
||||||
/// Returns the type of controller.
|
/// Returns the type of controller.
|
||||||
virtual ControllerType GetType() const = 0;
|
virtual ControllerType GetType() const = 0;
|
||||||
|
|
||||||
|
/// Gets the integer code for an axis in the specified controller type.
|
||||||
|
virtual std::optional<s32> GetAxisCodeByName(std::string_view axis_name) const;
|
||||||
|
|
||||||
/// Gets the integer code for a button in the specified controller type.
|
/// Gets the integer code for a button in the specified controller type.
|
||||||
virtual std::optional<s32> GetButtonCodeByName(std::string_view button_name) const;
|
virtual std::optional<s32> GetButtonCodeByName(std::string_view button_name) const;
|
||||||
|
|
||||||
|
@ -27,12 +30,18 @@ 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);
|
||||||
|
|
||||||
|
/// Changes the specified axis state. Values are normalized from -1..1.
|
||||||
|
virtual void SetAxisState(s32 axis_code, float value);
|
||||||
|
|
||||||
/// Changes the specified button state.
|
/// Changes the specified button state.
|
||||||
virtual void SetButtonState(s32 button_code, bool pressed);
|
virtual void SetButtonState(s32 button_code, bool pressed);
|
||||||
|
|
||||||
/// Creates a new controller of the specified type.
|
/// Creates a new controller of the specified type.
|
||||||
static std::unique_ptr<Controller> Create(ControllerType type);
|
static std::unique_ptr<Controller> Create(ControllerType type);
|
||||||
|
|
||||||
|
/// Gets the integer code for an axis in the specified controller type.
|
||||||
|
static std::optional<s32> GetAxisCodeByName(ControllerType type, std::string_view axis_name);
|
||||||
|
|
||||||
/// Gets the integer code for a button in the specified controller type.
|
/// Gets the integer code for a button in the specified controller type.
|
||||||
static std::optional<s32> GetButtonCodeByName(ControllerType type, std::string_view button_name);
|
static std::optional<s32> GetButtonCodeByName(ControllerType type, std::string_view button_name);
|
||||||
};
|
};
|
||||||
|
|
|
@ -11,11 +11,18 @@ ControllerType DigitalController::GetType() const
|
||||||
return ControllerType::DigitalController;
|
return ControllerType::DigitalController;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<s32> DigitalController::GetAxisCodeByName(std::string_view axis_name) const
|
||||||
|
{
|
||||||
|
return StaticGetAxisCodeByName(axis_name);
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<s32> DigitalController::GetButtonCodeByName(std::string_view button_name) const
|
std::optional<s32> DigitalController::GetButtonCodeByName(std::string_view button_name) const
|
||||||
{
|
{
|
||||||
return StaticGetButtonCodeByName(button_name);
|
return StaticGetButtonCodeByName(button_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DigitalController::SetAxisState(s32 axis_code, float value) {}
|
||||||
|
|
||||||
void DigitalController::SetButtonState(Button button, bool pressed)
|
void DigitalController::SetButtonState(Button button, bool pressed)
|
||||||
{
|
{
|
||||||
if (pressed)
|
if (pressed)
|
||||||
|
@ -91,6 +98,11 @@ std::unique_ptr<DigitalController> DigitalController::Create()
|
||||||
return std::make_unique<DigitalController>();
|
return std::make_unique<DigitalController>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::optional<s32> DigitalController::StaticGetAxisCodeByName(std::string_view button_name)
|
||||||
|
{
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<s32> DigitalController::StaticGetButtonCodeByName(std::string_view button_name)
|
std::optional<s32> DigitalController::StaticGetButtonCodeByName(std::string_view button_name)
|
||||||
{
|
{
|
||||||
#define BUTTON(name) \
|
#define BUTTON(name) \
|
||||||
|
|
|
@ -32,17 +32,21 @@ public:
|
||||||
~DigitalController() override;
|
~DigitalController() override;
|
||||||
|
|
||||||
static std::unique_ptr<DigitalController> Create();
|
static std::unique_ptr<DigitalController> Create();
|
||||||
|
static std::optional<s32> StaticGetAxisCodeByName(std::string_view button_name);
|
||||||
static std::optional<s32> StaticGetButtonCodeByName(std::string_view button_name);
|
static std::optional<s32> StaticGetButtonCodeByName(std::string_view button_name);
|
||||||
|
|
||||||
ControllerType GetType() const override;
|
ControllerType GetType() const override;
|
||||||
|
std::optional<s32> GetAxisCodeByName(std::string_view axis_name) const override;
|
||||||
std::optional<s32> GetButtonCodeByName(std::string_view button_name) const override;
|
std::optional<s32> GetButtonCodeByName(std::string_view button_name) const override;
|
||||||
|
|
||||||
void SetButtonState(Button button, bool pressed);
|
void SetAxisState(s32 axis_code, float value) override;
|
||||||
void SetButtonState(s32 button_code, bool pressed);
|
void SetButtonState(s32 button_code, bool pressed) override;
|
||||||
|
|
||||||
void ResetTransferState() override;
|
void ResetTransferState() override;
|
||||||
bool Transfer(const u8 data_in, u8* data_out) override;
|
bool Transfer(const u8 data_in, u8* data_out) override;
|
||||||
|
|
||||||
|
void SetButtonState(Button button, bool pressed);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum class TransferState : u8
|
enum class TransferState : u8
|
||||||
{
|
{
|
||||||
|
|
|
@ -459,7 +459,7 @@ void SDLHostInterface::UpdateKeyboardControllerMapping()
|
||||||
{
|
{
|
||||||
m_keyboard_button_mapping.fill(-1);
|
m_keyboard_button_mapping.fill(-1);
|
||||||
|
|
||||||
const Controller* controller = m_system->GetController(0);
|
const Controller* controller = m_system ? m_system->GetController(0) : nullptr;
|
||||||
if (controller)
|
if (controller)
|
||||||
{
|
{
|
||||||
#define SET_BUTTON_MAP(action, name) \
|
#define SET_BUTTON_MAP(action, name) \
|
||||||
|
@ -490,7 +490,8 @@ bool SDLHostInterface::HandleSDLKeyEventForController(const SDL_Event* event)
|
||||||
Controller* controller;
|
Controller* controller;
|
||||||
|
|
||||||
#define DO_ACTION(action) \
|
#define DO_ACTION(action) \
|
||||||
if ((controller = m_system->GetController(0)) != nullptr && m_keyboard_button_mapping[static_cast<int>(action)]) \
|
if ((controller = m_system ? m_system->GetController(0) : nullptr) != nullptr && \
|
||||||
|
m_keyboard_button_mapping[static_cast<int>(action)]) \
|
||||||
{ \
|
{ \
|
||||||
controller->SetButtonState(m_keyboard_button_mapping[static_cast<int>(action)], pressed); \
|
controller->SetButtonState(m_keyboard_button_mapping[static_cast<int>(action)], pressed); \
|
||||||
}
|
}
|
||||||
|
@ -563,13 +564,22 @@ bool SDLHostInterface::HandleSDLKeyEventForController(const SDL_Event* event)
|
||||||
|
|
||||||
void SDLHostInterface::UpdateControllerControllerMapping()
|
void SDLHostInterface::UpdateControllerControllerMapping()
|
||||||
{
|
{
|
||||||
|
m_controller_axis_mapping.fill(-1);
|
||||||
m_controller_button_mapping.fill(-1);
|
m_controller_button_mapping.fill(-1);
|
||||||
|
|
||||||
const Controller* controller = m_system->GetController(0);
|
Controller* controller = m_system ? m_system->GetController(0) : nullptr;
|
||||||
if (controller)
|
if (controller)
|
||||||
{
|
{
|
||||||
#define SET_BUTTON_MAP(action, name) \
|
#define SET_AXIS_MAP(axis, name) m_controller_axis_mapping[axis] = controller->GetAxisCodeByName(name).value_or(-1)
|
||||||
m_controller_button_mapping[static_cast<int>(action)] = controller->GetButtonCodeByName(name).value_or(-1)
|
#define SET_BUTTON_MAP(button, name) \
|
||||||
|
m_controller_button_mapping[button] = controller->GetButtonCodeByName(name).value_or(-1)
|
||||||
|
|
||||||
|
SET_AXIS_MAP(SDL_CONTROLLER_AXIS_LEFTX, "LeftX");
|
||||||
|
SET_AXIS_MAP(SDL_CONTROLLER_AXIS_LEFTY, "LeftY");
|
||||||
|
SET_AXIS_MAP(SDL_CONTROLLER_AXIS_RIGHTX, "RightX");
|
||||||
|
SET_AXIS_MAP(SDL_CONTROLLER_AXIS_RIGHTY, "RightY");
|
||||||
|
SET_AXIS_MAP(SDL_CONTROLLER_AXIS_TRIGGERLEFT, "LeftTrigger");
|
||||||
|
SET_AXIS_MAP(SDL_CONTROLLER_AXIS_TRIGGERRIGHT, "RightTrigger");
|
||||||
|
|
||||||
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_DPAD_UP, "Up");
|
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_DPAD_UP, "Up");
|
||||||
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_DPAD_DOWN, "Down");
|
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_DPAD_DOWN, "Down");
|
||||||
|
@ -586,6 +596,7 @@ void SDLHostInterface::UpdateControllerControllerMapping()
|
||||||
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_START, "Start");
|
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_START, "Start");
|
||||||
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_BACK, "Select");
|
SET_BUTTON_MAP(SDL_CONTROLLER_BUTTON_BACK, "Select");
|
||||||
|
|
||||||
|
#undef SET_AXIS_MAP
|
||||||
#undef SET_BUTTON_MAP
|
#undef SET_BUTTON_MAP
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -593,10 +604,19 @@ void SDLHostInterface::UpdateControllerControllerMapping()
|
||||||
void SDLHostInterface::HandleSDLControllerAxisEventForController(const SDL_Event* ev)
|
void SDLHostInterface::HandleSDLControllerAxisEventForController(const SDL_Event* ev)
|
||||||
{
|
{
|
||||||
// Log_DevPrintf("axis %d %d", ev->caxis.axis, ev->caxis.value);
|
// Log_DevPrintf("axis %d %d", ev->caxis.axis, ev->caxis.value);
|
||||||
Controller* controller = m_system->GetController(0);
|
Controller* controller = m_system ? m_system->GetController(0) : nullptr;
|
||||||
if (!controller)
|
if (!controller)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// proper axis mapping
|
||||||
|
if (m_controller_axis_mapping[ev->caxis.axis] >= 0)
|
||||||
|
{
|
||||||
|
const float value = static_cast<float>(ev->caxis.value) / (ev->caxis.value < 0 ? 32768.0f : 32767.0f);
|
||||||
|
controller->SetAxisState(m_controller_axis_mapping[ev->caxis.axis], value);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// axis-as-button mapping
|
||||||
static constexpr int deadzone = 8192;
|
static constexpr int deadzone = 8192;
|
||||||
const bool negative = (ev->caxis.value < 0);
|
const bool negative = (ev->caxis.value < 0);
|
||||||
const bool active = (std::abs(ev->caxis.value) >= deadzone);
|
const bool active = (std::abs(ev->caxis.value) >= deadzone);
|
||||||
|
|
|
@ -125,6 +125,7 @@ private:
|
||||||
KeyboardControllerActionMap m_keyboard_button_mapping;
|
KeyboardControllerActionMap m_keyboard_button_mapping;
|
||||||
|
|
||||||
std::map<int, SDL_GameController*> m_sdl_controllers;
|
std::map<int, SDL_GameController*> m_sdl_controllers;
|
||||||
|
std::array<s32, SDL_CONTROLLER_AXIS_MAX> m_controller_axis_mapping;
|
||||||
std::array<s32, SDL_CONTROLLER_BUTTON_MAX> m_controller_button_mapping;
|
std::array<s32, SDL_CONTROLLER_BUTTON_MAX> m_controller_button_mapping;
|
||||||
|
|
||||||
u32 m_switch_gpu_renderer_event_id = 0;
|
u32 m_switch_gpu_renderer_event_id = 0;
|
||||||
|
|
Loading…
Reference in a new issue