AnalogController: Add analog toggle button

This commit is contained in:
Connor McLaughlin 2020-05-08 15:13:05 +10:00
parent 5b389ae13d
commit 95468901f2
4 changed files with 53 additions and 16 deletions

View File

@ -1,9 +1,11 @@
#include "analog_controller.h" #include "analog_controller.h"
#include "common/log.h" #include "common/log.h"
#include "common/state_wrapper.h" #include "common/state_wrapper.h"
#include "host_interface.h"
#include "system.h"
Log_SetChannel(AnalogController); Log_SetChannel(AnalogController);
AnalogController::AnalogController() AnalogController::AnalogController(System* system) : m_system(system)
{ {
m_axis_state.fill(0x80); m_axis_state.fill(0x80);
} }
@ -28,6 +30,8 @@ bool AnalogController::DoState(StateWrapper& sw)
if (!Controller::DoState(sw)) if (!Controller::DoState(sw))
return false; return false;
const bool old_analog_mode = m_analog_mode;
sw.Do(&m_analog_mode); sw.Do(&m_analog_mode);
sw.Do(&m_rumble_unlocked); sw.Do(&m_rumble_unlocked);
sw.Do(&m_configuration_mode); sw.Do(&m_configuration_mode);
@ -41,6 +45,12 @@ bool AnalogController::DoState(StateWrapper& sw)
{ {
for (u8 i = 0; i < NUM_MOTORS; i++) for (u8 i = 0; i < NUM_MOTORS; i++)
SetMotorState(i, motor_state[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; return true;
} }
@ -73,6 +83,25 @@ void AnalogController::SetAxisState(Axis axis, u8 value)
void AnalogController::SetButtonState(Button button, bool pressed) 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) if (pressed)
m_button_state &= ~(u16(1) << static_cast<u8>(button)); m_button_state &= ~(u16(1) << static_cast<u8>(button));
else else
@ -120,7 +149,9 @@ void AnalogController::SetAnalogMode(bool enabled)
if (m_analog_mode == enabled) if (m_analog_mode == enabled)
return; 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; m_analog_mode = enabled;
} }
@ -267,11 +298,9 @@ bool AnalogController::Transfer(const u8 data_in, u8* data_out)
case State::SetAnalogModeVal: case State::SetAnalogModeVal:
{ {
Log_DebugPrintf("analog mode val 0x%02x", data_in); Log_DevPrintf("analog mode val 0x%02x", data_in);
if (data_in == 0x00) if (data_in == 0x00 || data_in == 0x01)
SetAnalogMode(false); SetAnalogMode((data_in == 0x01));
else if (data_in == 0x01)
SetAnalogMode(true);
*data_out = 0x00; *data_out = 0x00;
m_state = State::SetAnalogModeSel; m_state = State::SetAnalogModeSel;
@ -281,7 +310,10 @@ bool AnalogController::Transfer(const u8 data_in, u8* data_out)
case State::SetAnalogModeSel: 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; *data_out = 0x00;
m_state = State::Pad4Bytes; m_state = State::Pad4Bytes;
ack = true; ack = true;
@ -361,9 +393,9 @@ bool AnalogController::Transfer(const u8 data_in, u8* data_out)
return ack; return ack;
} }
std::unique_ptr<AnalogController> AnalogController::Create() std::unique_ptr<AnalogController> AnalogController::Create(System* system)
{ {
return std::make_unique<AnalogController>(); return std::make_unique<AnalogController>(system);
} }
std::optional<s32> AnalogController::StaticGetAxisCodeByName(std::string_view axis_name) std::optional<s32> AnalogController::StaticGetAxisCodeByName(std::string_view axis_name)
@ -408,6 +440,7 @@ std::optional<s32> AnalogController::StaticGetButtonCodeByName(std::string_view
BUTTON(Circle); BUTTON(Circle);
BUTTON(Cross); BUTTON(Cross);
BUTTON(Square); BUTTON(Square);
BUTTON(Analog);
return std::nullopt; return std::nullopt;
@ -432,8 +465,8 @@ Controller::ButtonList AnalogController::StaticGetButtonNames()
{ \ { \
#n, static_cast < s32>(Button::n) \ #n, static_cast < s32>(Button::n) \
} }
return {B(Up), B(Down), B(Left), B(Right), B(Select), B(Start), B(Triangle), B(Cross), return {B(Up), B(Down), B(Left), B(Right), B(Select), B(Start), B(Triangle), B(Cross), B(Circle),
B(Circle), B(Square), B(L1), B(L2), B(R1), B(R2), B(L3), B(R3)}; B(Square), B(L1), B(L2), B(R1), B(R2), B(L3), B(R3), B(Analog)};
#undef B #undef B
} }

View File

@ -35,15 +35,16 @@ public:
Circle = 13, Circle = 13,
Cross = 14, Cross = 14,
Square = 15, Square = 15,
Analog = 16,
Count Count
}; };
static constexpr u8 NUM_MOTORS = 2; static constexpr u8 NUM_MOTORS = 2;
AnalogController(); AnalogController(System* system);
~AnalogController() override; ~AnalogController() override;
static std::unique_ptr<AnalogController> Create(); static std::unique_ptr<AnalogController> Create(System* system);
static std::optional<s32> StaticGetAxisCodeByName(std::string_view axis_name); static std::optional<s32> StaticGetAxisCodeByName(std::string_view axis_name);
static std::optional<s32> StaticGetButtonCodeByName(std::string_view button_name); static std::optional<s32> StaticGetButtonCodeByName(std::string_view button_name);
static AxisList StaticGetAxisNames(); static AxisList StaticGetAxisNames();
@ -128,7 +129,10 @@ private:
void SetAnalogMode(bool enabled); void SetAnalogMode(bool enabled);
void SetMotorState(u8 motor, u8 value); void SetMotorState(u8 motor, u8 value);
System* m_system;
bool m_analog_mode = false; bool m_analog_mode = false;
bool m_analog_locked = false;
bool m_rumble_unlocked = false; bool m_rumble_unlocked = false;
bool m_configuration_mode = false; bool m_configuration_mode = false;
u8 m_command_param = 0; u8 m_command_param = 0;

View File

@ -46,7 +46,7 @@ std::unique_ptr<Controller> Controller::Create(System* system, ControllerType ty
return DigitalController::Create(); return DigitalController::Create();
case ControllerType::AnalogController: case ControllerType::AnalogController:
return AnalogController::Create(); return AnalogController::Create(system);
case ControllerType::NamcoGunCon: case ControllerType::NamcoGunCon:
return NamcoGunCon::Create(system); return NamcoGunCon::Create(system);

View File

@ -2,7 +2,7 @@
#include "types.h" #include "types.h"
static constexpr u32 SAVE_STATE_MAGIC = 0x43435544; 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) #pragma pack(push, 4)
struct SAVE_STATE_HEADER struct SAVE_STATE_HEADER