UI: Massive revamp, new features and improvements

This commit is contained in:
Connor McLaughlin
2022-07-11 23:03:29 +10:00
parent 3fb61865e5
commit b42b5501f6
425 changed files with 39701 additions and 29487 deletions

View File

@ -1,15 +1,14 @@
#include "analog_joystick.h"
#include "common/log.h"
#include "common/string_util.h"
#include "host_interface.h"
#include "host.h"
#include "system.h"
#include "util/state_wrapper.h"
#include <cmath>
Log_SetChannel(AnalogJoystick);
AnalogJoystick::AnalogJoystick(u32 index)
AnalogJoystick::AnalogJoystick(u32 index) : Controller(index)
{
m_index = index;
m_axis_state.fill(0x80);
Reset();
}
@ -50,82 +49,101 @@ bool AnalogJoystick::DoState(StateWrapper& sw, bool apply_input_state)
if (sw.IsReading() && (old_analog_mode != m_analog_mode))
{
g_host_interface->AddFormattedOSDMessage(
5.0f,
m_analog_mode ? g_host_interface->TranslateString("AnalogJoystick", "Controller %u switched to analog mode.") :
g_host_interface->TranslateString("AnalogJoystick", "Controller %u switched to digital mode."),
m_index + 1u);
Host::AddFormattedOSDMessage(5.0f,
m_analog_mode ?
Host::TranslateString("AnalogJoystick", "Controller %u switched to analog mode.") :
Host::TranslateString("AnalogJoystick", "Controller %u switched to digital mode."),
m_index + 1u);
}
return true;
}
std::optional<s32> AnalogJoystick::GetAxisCodeByName(std::string_view axis_name) const
float AnalogJoystick::GetBindState(u32 index) const
{
return StaticGetAxisCodeByName(axis_name);
}
std::optional<s32> AnalogJoystick::GetButtonCodeByName(std::string_view button_name) const
{
return StaticGetButtonCodeByName(button_name);
}
float AnalogJoystick::GetAxisState(s32 axis_code) const
{
if (axis_code < 0 || axis_code >= static_cast<s32>(Axis::Count))
return 0.0f;
// 0..255 -> -1..1
const float value = (((static_cast<float>(m_axis_state[static_cast<s32>(axis_code)]) / 255.0f) * 2.0f) - 1.0f);
return std::clamp(value / m_axis_scale, -1.0f, 1.0f);
}
void AnalogJoystick::SetAxisState(s32 axis_code, float value)
{
if (axis_code < 0 || axis_code >= static_cast<s32>(Axis::Count))
return;
// -1..1 -> 0..255
const float scaled_value = std::clamp(value * m_axis_scale, -1.0f, 1.0f);
const u8 u8_value = static_cast<u8>(std::clamp(std::round(((scaled_value + 1.0f) / 2.0f) * 255.0f), 0.0f, 255.0f));
SetAxisState(static_cast<Axis>(axis_code), u8_value);
}
void AnalogJoystick::SetAxisState(Axis axis, u8 value)
{
if (m_axis_state[static_cast<u8>(axis)] != value)
System::SetRunaheadReplayFlag();
m_axis_state[static_cast<u8>(axis)] = value;
}
bool AnalogJoystick::GetButtonState(s32 button_code) const
{
if (button_code < 0 || button_code >= static_cast<s32>(Button::Count))
return false;
const u16 bit = u16(1) << static_cast<u8>(button_code);
return ((m_button_state & bit) == 0);
}
void AnalogJoystick::SetButtonState(Button button, bool pressed)
{
if (button == Button::Mode)
if (index >= static_cast<u32>(Button::Count))
{
if (pressed)
const u32 sub_index = index - static_cast<u32>(Button::Count);
if (sub_index >= static_cast<u32>(m_half_axis_state.size()))
return 0.0f;
return static_cast<float>(m_half_axis_state[sub_index]) * (1.0f / 255.0f);
}
else if (index < static_cast<u32>(Button::Mode))
{
return static_cast<float>(((m_button_state >> index) & 1u) ^ 1u);
}
else
{
return 0.0f;
}
}
void AnalogJoystick::SetBindState(u32 index, float value)
{
if (index == static_cast<s32>(Button::Mode))
{
// analog toggle
if (value >= 0.5f)
ToggleAnalogMode();
return;
}
else if (index >= static_cast<u32>(Button::Count))
{
const u32 sub_index = index - static_cast<u32>(Button::Count);
if (sub_index >= static_cast<u32>(m_half_axis_state.size()))
return;
const u16 bit = u16(1) << static_cast<u8>(button);
value = ApplyAnalogDeadzoneSensitivity(m_analog_deadzone, m_analog_sensitivity, value);
const u8 u8_value = static_cast<u8>(std::clamp(value * 255.0f, 0.0f, 255.0f));
if (u8_value != m_half_axis_state[sub_index])
System::SetRunaheadReplayFlag();
if (pressed)
m_half_axis_state[sub_index] = u8_value;
#define MERGE(pos, neg) \
((m_half_axis_state[static_cast<u32>(pos)] != 0) ? (127u + ((m_half_axis_state[static_cast<u32>(pos)] + 1u) / 2u)) : \
(127u - (m_half_axis_state[static_cast<u32>(neg)] / 2u)))
switch (static_cast<HalfAxis>(sub_index))
{
case HalfAxis::LLeft:
case HalfAxis::LRight:
m_axis_state[static_cast<u8>(Axis::LeftX)] = MERGE(HalfAxis::LRight, HalfAxis::LLeft);
break;
case HalfAxis::LDown:
case HalfAxis::LUp:
m_axis_state[static_cast<u8>(Axis::LeftY)] = MERGE(HalfAxis::LDown, HalfAxis::LUp);
break;
case HalfAxis::RLeft:
case HalfAxis::RRight:
m_axis_state[static_cast<u8>(Axis::RightX)] = MERGE(HalfAxis::RRight, HalfAxis::RLeft);
break;
case HalfAxis::RDown:
case HalfAxis::RUp:
m_axis_state[static_cast<u8>(Axis::RightY)] = MERGE(HalfAxis::RDown, HalfAxis::RUp);
break;
default:
break;
}
#undef MERGE
return;
}
const u16 bit = u16(1) << static_cast<u8>(index);
if (value >= 0.5f)
{
if (m_button_state & bit)
System::SetRunaheadReplayFlag();
m_button_state &= ~bit;
m_button_state &= ~(bit);
}
else
{
@ -136,14 +154,6 @@ void AnalogJoystick::SetButtonState(Button button, bool pressed)
}
}
void AnalogJoystick::SetButtonState(s32 button_code, bool pressed)
{
if (button_code < 0 || button_code >= static_cast<s32>(Button::Count))
return;
SetButtonState(static_cast<Button>(button_code), pressed);
}
u32 AnalogJoystick::GetButtonStateBits() const
{
return m_button_state ^ 0xFFFF;
@ -173,11 +183,11 @@ void AnalogJoystick::ToggleAnalogMode()
m_analog_mode = !m_analog_mode;
Log_InfoPrintf("Joystick %u switched to %s mode.", m_index + 1u, m_analog_mode ? "analog" : "digital");
g_host_interface->AddFormattedOSDMessage(
5.0f,
m_analog_mode ? g_host_interface->TranslateString("AnalogJoystick", "Controller %u switched to analog mode.") :
g_host_interface->TranslateString("AnalogJoystick", "Controller %u switched to digital mode."),
m_index + 1u);
Host::AddFormattedOSDMessage(5.0f,
m_analog_mode ?
Host::TranslateString("AnalogJoystick", "Controller %u switched to analog mode.") :
Host::TranslateString("AnalogJoystick", "Controller %u switched to digital mode."),
m_index + 1u);
}
bool AnalogJoystick::Transfer(const u8 data_in, u8* data_out)
@ -272,104 +282,73 @@ std::unique_ptr<AnalogJoystick> AnalogJoystick::Create(u32 index)
return std::make_unique<AnalogJoystick>(index);
}
std::optional<s32> AnalogJoystick::StaticGetAxisCodeByName(std::string_view axis_name)
{
#define AXIS(name) \
if (axis_name == #name) \
static const Controller::ControllerBindingInfo s_binding_info[] = {
#define BUTTON(name, display_name, button, genb) \
{ \
return static_cast<s32>(ZeroExtend32(static_cast<u8>(Axis::name))); \
name, display_name, static_cast<u32>(button), Controller::ControllerBindingType::Button, genb \
}
#define AXIS(name, display_name, halfaxis, genb) \
{ \
name, display_name, static_cast<u32>(AnalogJoystick::Button::Count) + static_cast<u32>(halfaxis), \
Controller::ControllerBindingType::HalfAxis, genb \
}
AXIS(LeftX);
AXIS(LeftY);
AXIS(RightX);
AXIS(RightY);
BUTTON("Up", "D-Pad Up", AnalogJoystick::Button::Up, GenericInputBinding::DPadUp),
BUTTON("Right", "D-Pad Right", AnalogJoystick::Button::Right, GenericInputBinding::DPadRight),
BUTTON("Down", "D-Pad Down", AnalogJoystick::Button::Down, GenericInputBinding::DPadDown),
BUTTON("Left", "D-Pad Left", AnalogJoystick::Button::Left, GenericInputBinding::DPadLeft),
BUTTON("Triangle", "Triangle", AnalogJoystick::Button::Triangle, GenericInputBinding::Triangle),
BUTTON("Circle", "Circle", AnalogJoystick::Button::Circle, GenericInputBinding::Circle),
BUTTON("Cross", "Cross", AnalogJoystick::Button::Cross, GenericInputBinding::Cross),
BUTTON("Square", "Square", AnalogJoystick::Button::Square, GenericInputBinding::Square),
BUTTON("Select", "Select", AnalogJoystick::Button::Select, GenericInputBinding::Select),
BUTTON("Start", "Start", AnalogJoystick::Button::Start, GenericInputBinding::Start),
BUTTON("Mode", "Mode Toggle", AnalogJoystick::Button::Mode, GenericInputBinding::System),
BUTTON("L1", "L1", AnalogJoystick::Button::L1, GenericInputBinding::L1),
BUTTON("R1", "R1", AnalogJoystick::Button::R1, GenericInputBinding::R1),
BUTTON("L2", "L2", AnalogJoystick::Button::L2, GenericInputBinding::L2),
BUTTON("R2", "R2", AnalogJoystick::Button::R2, GenericInputBinding::R2),
BUTTON("L3", "L3", AnalogJoystick::Button::L3, GenericInputBinding::L3),
BUTTON("R3", "R3", AnalogJoystick::Button::R3, GenericInputBinding::R3),
return std::nullopt;
AXIS("LLeft", "Left Stick Left", AnalogJoystick::HalfAxis::LLeft, GenericInputBinding::LeftStickLeft),
AXIS("LRight", "Left Stick Right", AnalogJoystick::HalfAxis::LRight, GenericInputBinding::LeftStickRight),
AXIS("LDown", "Left Stick Down", AnalogJoystick::HalfAxis::LDown, GenericInputBinding::LeftStickDown),
AXIS("LUp", "Left Stick Up", AnalogJoystick::HalfAxis::LUp, GenericInputBinding::LeftStickUp),
AXIS("RLeft", "Right Stick Left", AnalogJoystick::HalfAxis::RLeft, GenericInputBinding::RightStickLeft),
AXIS("RRight", "Right Stick Right", AnalogJoystick::HalfAxis::RRight, GenericInputBinding::RightStickRight),
AXIS("RDown", "Right Stick Down", AnalogJoystick::HalfAxis::RDown, GenericInputBinding::RightStickDown),
AXIS("RUp", "Right Stick Up", AnalogJoystick::HalfAxis::RUp, GenericInputBinding::RightStickUp),
#undef AXIS
}
std::optional<s32> AnalogJoystick::StaticGetButtonCodeByName(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);
BUTTON(Mode);
return std::nullopt;
#undef BUTTON
}
};
Controller::AxisList AnalogJoystick::StaticGetAxisNames()
static const SettingInfo s_settings[] = {
{SettingInfo::Type::Float, "AnalogDeadzone", TRANSLATABLE("AnalogController", "Analog Deadzone"),
TRANSLATABLE("AnalogController",
"Sets the analog stick deadzone, i.e. the fraction of the stick movement which will be ignored.s"),
"1.00f", "0.00f", "1.00f", "0.01f"},
{SettingInfo::Type::Float, "AnalogSensitivity", TRANSLATABLE("AnalogController", "Analog Sensitivity"),
TRANSLATABLE(
"AnalogController",
"Sets the analog stick axis scaling factor. A value between 1.30 and 1.40 is recommended when using recent "
"controllers, e.g. DualShock 4, Xbox One Controller."),
"1.33f", "0.01f", "2.00f", "0.01f"}};
const Controller::ControllerInfo AnalogJoystick::INFO = {ControllerType::AnalogJoystick,
"AnalogJoystick",
TRANSLATABLE("ControllerType", "Analog Joystick"),
s_binding_info,
countof(s_binding_info),
s_settings,
countof(s_settings),
Controller::VibrationCapabilities::NoVibration};
void AnalogJoystick::LoadSettings(SettingsInterface& si, const char* section)
{
return {{TRANSLATABLE("AnalogJoystick", "LeftX"), static_cast<s32>(Axis::LeftX), AxisType::Full},
{TRANSLATABLE("AnalogJoystick", "LeftY"), static_cast<s32>(Axis::LeftY), AxisType::Full},
{TRANSLATABLE("AnalogJoystick", "RightX"), static_cast<s32>(Axis::RightX), AxisType::Full},
{TRANSLATABLE("AnalogJoystick", "RightY"), static_cast<s32>(Axis::RightY), AxisType::Full}};
}
Controller::ButtonList AnalogJoystick::StaticGetButtonNames()
{
return {{TRANSLATABLE("AnalogJoystick", "Up"), static_cast<s32>(Button::Up)},
{TRANSLATABLE("AnalogJoystick", "Down"), static_cast<s32>(Button::Down)},
{TRANSLATABLE("AnalogJoystick", "Left"), static_cast<s32>(Button::Left)},
{TRANSLATABLE("AnalogJoystick", "Right"), static_cast<s32>(Button::Right)},
{TRANSLATABLE("AnalogJoystick", "Select"), static_cast<s32>(Button::Select)},
{TRANSLATABLE("AnalogJoystick", "Start"), static_cast<s32>(Button::Start)},
{TRANSLATABLE("AnalogJoystick", "Triangle"), static_cast<s32>(Button::Triangle)},
{TRANSLATABLE("AnalogJoystick", "Cross"), static_cast<s32>(Button::Cross)},
{TRANSLATABLE("AnalogJoystick", "Circle"), static_cast<s32>(Button::Circle)},
{TRANSLATABLE("AnalogJoystick", "Square"), static_cast<s32>(Button::Square)},
{TRANSLATABLE("AnalogJoystick", "L1"), static_cast<s32>(Button::L1)},
{TRANSLATABLE("AnalogJoystick", "L2"), static_cast<s32>(Button::L2)},
{TRANSLATABLE("AnalogJoystick", "R1"), static_cast<s32>(Button::R1)},
{TRANSLATABLE("AnalogJoystick", "R2"), static_cast<s32>(Button::R2)},
{TRANSLATABLE("AnalogJoystick", "L3"), static_cast<s32>(Button::L3)},
{TRANSLATABLE("AnalogJoystick", "R3"), static_cast<s32>(Button::R3)},
{TRANSLATABLE("AnalogJoystick", "Analog"), static_cast<s32>(Button::Mode)}};
}
u32 AnalogJoystick::StaticGetVibrationMotorCount()
{
return 0;
}
Controller::SettingList AnalogJoystick::StaticGetSettings()
{
static constexpr std::array<SettingInfo, 1> settings = {
{{SettingInfo::Type::Float, "AxisScale", TRANSLATABLE("AnalogJoystick", "Analog Axis Scale"),
TRANSLATABLE(
"AnalogJoystick",
"Sets the analog stick axis scaling factor. A value between 1.30 and 1.40 is recommended when using recent "
"controllers, e.g. DualShock 4, Xbox One Controller."),
"1.00f", "0.01f", "1.50f", "0.01f"}}};
return SettingList(settings.begin(), settings.end());
}
void AnalogJoystick::LoadSettings(const char* section)
{
Controller::LoadSettings(section);
m_axis_scale = std::clamp(g_host_interface->GetFloatSettingValue(section, "AxisScale", 1.00f), 0.01f, 1.50f);
Controller::LoadSettings(si, section);
m_analog_deadzone = std::clamp(si.GetFloatValue(section, "AnalogDeadzone", DEFAULT_STICK_DEADZONE), 0.0f, 1.0f);
m_analog_sensitivity =
std::clamp(si.GetFloatValue(section, "AnalogSensitivity", DEFAULT_STICK_SENSITIVITY), 0.01f, 3.0f);
}