#include "analog_controller.h" #include "common/log.h" #include "common/state_wrapper.h" #include "common/string_util.h" #include "host_interface.h" #include "system.h" Log_SetChannel(AnalogController); AnalogController::AnalogController(u32 index) : m_index(index) { m_axis_state.fill(0x80); Reset(); } AnalogController::~AnalogController() = default; ControllerType AnalogController::GetType() const { return ControllerType::AnalogController; } void AnalogController::Reset() { m_analog_mode = false; m_rumble_unlocked = false; m_configuration_mode = false; m_command_param = 0; m_motor_state.fill(0); if (m_auto_enable_analog) SetAnalogMode(true); } 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); sw.Do(&m_command_param); sw.Do(&m_state); MotorState motor_state = m_motor_state; sw.Do(&motor_state); if (sw.IsReading()) { for (u8 i = 0; i < NUM_MOTORS; i++) SetMotorState(i, motor_state[i]); if (old_analog_mode != m_analog_mode) { g_host_interface->AddFormattedOSDMessage( 5.0f, m_analog_mode ? g_host_interface->TranslateString("AnalogController", "Controller %u switched to analog mode.") : g_host_interface->TranslateString("AnalogController", "Controller %u switched to digital mode."), m_index + 1u); } } return true; } std::optional AnalogController::GetAxisCodeByName(std::string_view axis_name) const { return StaticGetAxisCodeByName(axis_name); } std::optional AnalogController::GetButtonCodeByName(std::string_view button_name) const { return StaticGetButtonCodeByName(button_name); } void AnalogController::SetAxisState(s32 axis_code, float value) { if (axis_code < 0 || axis_code >= static_cast(Axis::Count)) return; // -1..1 -> 0..255 const u8 u8_value = static_cast(std::clamp(((value + 1.0f) / 2.0f) * 255.0f, 0.0f, 255.0f)); SetAxisState(static_cast(axis_code), u8_value); } void AnalogController::SetAxisState(Axis axis, u8 value) { m_axis_state[static_cast(axis)] = value; } void AnalogController::SetButtonState(Button button, bool pressed) { if (button == Button::Analog) { // analog toggle if (pressed) { if (m_analog_locked) { g_host_interface->AddFormattedOSDMessage( 5.0f, m_analog_mode ? g_host_interface->TranslateString("AnalogController", "Controller %u is locked to analog mode by the game.") : g_host_interface->TranslateString("AnalogController", "Controller %u is locked to digital mode by the game."), m_index + 1u); } else { SetAnalogMode(!m_analog_mode); } } return; } if (pressed) m_button_state &= ~(u16(1) << static_cast(button)); else m_button_state |= u16(1) << static_cast(button); } void AnalogController::SetButtonState(s32 button_code, bool pressed) { if (button_code < 0 || button_code >= static_cast(Button::Count)) return; SetButtonState(static_cast