From 95468901f2e490bff17ce20045b4938a82429e90 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 8 May 2020 15:13:05 +1000 Subject: [PATCH] AnalogController: Add analog toggle button --- src/core/analog_controller.cpp | 57 +++++++++++++++++++++++++++------- src/core/analog_controller.h | 8 +++-- src/core/controller.cpp | 2 +- src/core/save_state_version.h | 2 +- 4 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/core/analog_controller.cpp b/src/core/analog_controller.cpp index 5b41d28f2..16bde3117 100644 --- a/src/core/analog_controller.cpp +++ b/src/core/analog_controller.cpp @@ -1,9 +1,11 @@ #include "analog_controller.h" #include "common/log.h" #include "common/state_wrapper.h" +#include "host_interface.h" +#include "system.h" Log_SetChannel(AnalogController); -AnalogController::AnalogController() +AnalogController::AnalogController(System* system) : m_system(system) { m_axis_state.fill(0x80); } @@ -28,6 +30,8 @@ bool AnalogController::DoState(StateWrapper& sw) if (!Controller::DoState(sw)) return false; + const bool old_analog_mode = m_analog_mode; + sw.Do(&m_analog_mode); sw.Do(&m_rumble_unlocked); sw.Do(&m_configuration_mode); @@ -41,6 +45,12 @@ bool AnalogController::DoState(StateWrapper& sw) { for (u8 i = 0; i < NUM_MOTORS; i++) SetMotorState(i, motor_state[i]); + + if (old_analog_mode != m_analog_mode) + { + m_system->GetHostInterface()->AddFormattedOSDMessage(2.0f, "Controller switched to %s mode.", + m_analog_mode ? "analog" : "digital"); + } } return true; } @@ -73,6 +83,25 @@ void AnalogController::SetAxisState(Axis axis, u8 value) void AnalogController::SetButtonState(Button button, bool pressed) { + if (button == Button::Analog) + { + // analog toggle + if (pressed) + { + if (m_analog_locked) + { + m_system->GetHostInterface()->AddFormattedOSDMessage(2.0f, "Controller is locked to %s mode by the game.", + m_analog_mode ? "analog" : "digital"); + } + else + { + SetAnalogMode(!m_analog_mode); + } + } + + return; + } + if (pressed) m_button_state &= ~(u16(1) << static_cast(button)); else @@ -120,7 +149,9 @@ void AnalogController::SetAnalogMode(bool enabled) if (m_analog_mode == enabled) return; - Log_InfoPrintf("Controller switched to %s mode", enabled ? "analog" : "digital"); + Log_InfoPrintf("Controller switched to %s mode.", enabled ? "analog" : "digital"); + m_system->GetHostInterface()->AddFormattedOSDMessage(2.0f, "Controller switched to %s mode.", + enabled ? "analog" : "digital"); m_analog_mode = enabled; } @@ -267,11 +298,9 @@ bool AnalogController::Transfer(const u8 data_in, u8* data_out) case State::SetAnalogModeVal: { - Log_DebugPrintf("analog mode val 0x%02x", data_in); - if (data_in == 0x00) - SetAnalogMode(false); - else if (data_in == 0x01) - SetAnalogMode(true); + Log_DevPrintf("analog mode val 0x%02x", data_in); + if (data_in == 0x00 || data_in == 0x01) + SetAnalogMode((data_in == 0x01)); *data_out = 0x00; m_state = State::SetAnalogModeSel; @@ -281,7 +310,10 @@ bool AnalogController::Transfer(const u8 data_in, u8* data_out) case State::SetAnalogModeSel: { - Log_WarningPrintf("analog mode sel 0x%02x", data_in); + Log_DevPrintf("analog mode lock 0x%02x", data_in); + if (data_in == 0x02 || data_in == 0x03) + m_analog_locked = (data_in == 0x03); + *data_out = 0x00; m_state = State::Pad4Bytes; ack = true; @@ -361,9 +393,9 @@ bool AnalogController::Transfer(const u8 data_in, u8* data_out) return ack; } -std::unique_ptr AnalogController::Create() +std::unique_ptr AnalogController::Create(System* system) { - return std::make_unique(); + return std::make_unique(system); } std::optional AnalogController::StaticGetAxisCodeByName(std::string_view axis_name) @@ -408,6 +440,7 @@ std::optional AnalogController::StaticGetButtonCodeByName(std::string_view BUTTON(Circle); BUTTON(Cross); BUTTON(Square); + BUTTON(Analog); return std::nullopt; @@ -432,8 +465,8 @@ Controller::ButtonList AnalogController::StaticGetButtonNames() { \ #n, static_cast < s32>(Button::n) \ } - return {B(Up), B(Down), B(Left), B(Right), B(Select), B(Start), B(Triangle), B(Cross), - B(Circle), B(Square), B(L1), B(L2), B(R1), B(R2), B(L3), B(R3)}; + return {B(Up), B(Down), B(Left), B(Right), B(Select), B(Start), B(Triangle), B(Cross), B(Circle), + B(Square), B(L1), B(L2), B(R1), B(R2), B(L3), B(R3), B(Analog)}; #undef B } diff --git a/src/core/analog_controller.h b/src/core/analog_controller.h index 74ba6fbbe..5f6eb1f4a 100644 --- a/src/core/analog_controller.h +++ b/src/core/analog_controller.h @@ -35,15 +35,16 @@ public: Circle = 13, Cross = 14, Square = 15, + Analog = 16, Count }; static constexpr u8 NUM_MOTORS = 2; - AnalogController(); + AnalogController(System* system); ~AnalogController() override; - static std::unique_ptr Create(); + static std::unique_ptr Create(System* system); static std::optional StaticGetAxisCodeByName(std::string_view axis_name); static std::optional StaticGetButtonCodeByName(std::string_view button_name); static AxisList StaticGetAxisNames(); @@ -128,7 +129,10 @@ private: void SetAnalogMode(bool enabled); void SetMotorState(u8 motor, u8 value); + System* m_system; + bool m_analog_mode = false; + bool m_analog_locked = false; bool m_rumble_unlocked = false; bool m_configuration_mode = false; u8 m_command_param = 0; diff --git a/src/core/controller.cpp b/src/core/controller.cpp index a64dc1343..c1894c114 100644 --- a/src/core/controller.cpp +++ b/src/core/controller.cpp @@ -46,7 +46,7 @@ std::unique_ptr Controller::Create(System* system, ControllerType ty return DigitalController::Create(); case ControllerType::AnalogController: - return AnalogController::Create(); + return AnalogController::Create(system); case ControllerType::NamcoGunCon: return NamcoGunCon::Create(system); diff --git a/src/core/save_state_version.h b/src/core/save_state_version.h index ab245349f..2f3b74633 100644 --- a/src/core/save_state_version.h +++ b/src/core/save_state_version.h @@ -2,7 +2,7 @@ #include "types.h" static constexpr u32 SAVE_STATE_MAGIC = 0x43435544; -static constexpr u32 SAVE_STATE_VERSION = 29; +static constexpr u32 SAVE_STATE_VERSION = 30; #pragma pack(push, 4) struct SAVE_STATE_HEADER