Skip to content

Commit

Permalink
ImGuiOverlays: Fix analog input display
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek committed Jan 17, 2024
1 parent 227049b commit 23b72d0
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 28 deletions.
32 changes: 23 additions & 9 deletions pcsx2/ImGui/ImGuiOverlays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <chrono>
#include <cmath>
#include <deque>
#include <limits>
#include <mutex>
#include <span>
#include <tuple>
Expand Down Expand Up @@ -509,14 +510,25 @@ void ImGuiManager::DrawInputsOverlay()
{
case InputBindingInfo::Type::Axis:
case InputBindingInfo::Type::HalfAxis:
{
// axes are only shown if not resting/past deadzone. values are normalized.
const float value = pad->GetEffectiveInput(bind);
const float abs_value = std::abs(value);
if (abs_value >= (254.0f / 255.0f))
text.append_fmt(" {}", bi.icon_name ? bi.icon_name : bi.name);
else if (abs_value >= (1.0f / 255.0f))
text.append_fmt(" {}: {:.2f}", bi.icon_name ? bi.icon_name : bi.name, value);
}
break;

case InputBindingInfo::Type::Button:
{
// axes are only shown if not resting/past deadzone
const u8 value = pad->GetEffectiveInput(bind);
if (value >= 254)
// buttons display the value from 0 through 255.
const float value = pad->GetEffectiveInput(bind);
if (value >= 254.0f)
text.append_fmt(" {}", bi.icon_name ? bi.icon_name : bi.name);
else if (value >= 1)
text.append_fmt(" {}: {}", bi.icon_name ? bi.icon_name : bi.name, value);
else if (value > 0.0f)
text.append_fmt(" {}: {:.0f}", bi.icon_name ? bi.icon_name : bi.name, value);
}
break;

Expand Down Expand Up @@ -554,7 +566,7 @@ void ImGuiManager::DrawInputsOverlay()
case InputBindingInfo::Type::Axis:
case InputBindingInfo::Type::HalfAxis:
{
// axes are always shown
// axes are only shown if not resting/past deadzone. values are normalized.
const float value = static_cast<float>(USB::GetDeviceBindValue(port, bi.bind_index));
if (value >= (254.0f / 255.0f))
text.append_fmt(" {}", bi.icon_name ? bi.icon_name : bi.name);
Expand All @@ -565,10 +577,12 @@ void ImGuiManager::DrawInputsOverlay()

case InputBindingInfo::Type::Button:
{
// buttons only shown when active
const float value = static_cast<float>(USB::GetDeviceBindValue(port, bi.bind_index));
if (value >= 0.5f)
// buttons display the value from 0 through 255. values are normalized, so denormalize them.
const float value = static_cast<float>(USB::GetDeviceBindValue(port, bi.bind_index)) * 255.0f;
if (value >= 254.0f)
text.append_fmt(" {}", bi.icon_name ? bi.icon_name : bi.name);
else if (value > 0.0f)
text.append_fmt(" {}: {:.0f}", bi.icon_name ? bi.icon_name : bi.name, value);
}
break;

Expand Down
2 changes: 1 addition & 1 deletion pcsx2/SIO/Pad/PadBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class PadBase
virtual void SetButtonDeadzone(float deadzone) = 0;
virtual void SetAnalogInvertL(bool x, bool y) = 0;
virtual void SetAnalogInvertR(bool x, bool y) = 0;
virtual u8 GetEffectiveInput(u32 index) const = 0;
virtual float GetEffectiveInput(u32 index) const = 0;
virtual u8 GetRawInput(u32 index) const = 0;
virtual std::tuple<u8, u8> GetRawLeftAnalog() const = 0;
virtual std::tuple<u8, u8> GetRawRightAnalog() const = 0;
Expand Down
18 changes: 9 additions & 9 deletions pcsx2/SIO/Pad/PadDualshock2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -785,36 +785,36 @@ void PadDualshock2::SetAnalogInvertR(bool x, bool y)
this->analogs.ryInvert = y;
}

u8 PadDualshock2::GetEffectiveInput(u32 index) const
float PadDualshock2::GetEffectiveInput(u32 index) const
{
if (!IsAnalogKey(index))
return GetRawInput(index);

switch (index)
{
case Inputs::PAD_L_LEFT:
return (analogs.lx > 0 && analogs.lx < 127) ? analogs.lx : 0;
return (analogs.lx < 127) ? -((127 - analogs.lx) / 127.0f) : 0;

case Inputs::PAD_L_RIGHT:
return (analogs.lx >= 128) ? analogs.lx : 0;
return (analogs.lx > 127) ? ((analogs.lx - 127) / 128.0f) : 0;

case Inputs::PAD_L_UP:
return (analogs.ly > 0 && analogs.ly < 127) ? analogs.ly : 0;
return (analogs.ly < 127) ? -((127 - analogs.ly) / 127.0f) : 0;

case Inputs::PAD_L_DOWN:
return (analogs.ly >= 128) ? analogs.ly : 0;
return (analogs.ly > 127) ? ((analogs.ly - 127) / 128.0f) : 0;

case Inputs::PAD_R_LEFT:
return (analogs.rx > 0 && analogs.rx < 127) ? analogs.rx : 0;
return (analogs.rx < 127) ? -((127 - analogs.rx) / 127.0f) : 0;

case Inputs::PAD_R_RIGHT:
return (analogs.rx >= 128) ? analogs.rx : 0;
return (analogs.rx > 127) ? ((analogs.rx - 127) / 128.0f) : 0;

case Inputs::PAD_R_UP:
return (analogs.ry > 0 && analogs.ry < 127) ? analogs.ry : 0;
return (analogs.ry < 127) ? -((127 - analogs.ry) / 127.0f) : 0;

case Inputs::PAD_R_DOWN:
return (analogs.ry >= 128) ? analogs.ry : 0;
return (analogs.ry > 127) ? ((analogs.ry - 127) / 128.0f) : 0;

default:
return 0;
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/SIO/Pad/PadDualshock2.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class PadDualshock2 final : public PadBase
void SetButtonDeadzone(float deadzone) override;
void SetAnalogInvertL(bool x, bool y) override;
void SetAnalogInvertR(bool x, bool y) override;
u8 GetEffectiveInput(u32 index) const override;
float GetEffectiveInput(u32 index) const override;
u8 GetRawInput(u32 index) const override;
std::tuple<u8, u8> GetRawLeftAnalog() const override;
std::tuple<u8, u8> GetRawRightAnalog() const override;
Expand Down
4 changes: 2 additions & 2 deletions pcsx2/SIO/Pad/PadGuitar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,9 @@ void PadGuitar::SetAnalogInvertR(bool x, bool y)
{
}

u8 PadGuitar::GetEffectiveInput(u32 index) const
float PadGuitar::GetEffectiveInput(u32 index) const
{
return GetRawInput(index);
return GetRawInput(index) / 255.0f;
}

u8 PadGuitar::GetRawInput(u32 index) const
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/SIO/Pad/PadGuitar.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class PadGuitar final : public PadBase
void SetButtonDeadzone(float deadzone) override;
void SetAnalogInvertL(bool x, bool y) override;
void SetAnalogInvertR(bool x, bool y) override;
u8 GetEffectiveInput(u32 index) const override;
float GetEffectiveInput(u32 index) const override;
u8 GetRawInput(u32 index) const override;
std::tuple<u8, u8> GetRawLeftAnalog() const override;
std::tuple<u8, u8> GetRawRightAnalog() const override;
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/SIO/Pad/PadNotConnected.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ void PadNotConnected::SetAnalogInvertR(bool x, bool y)

}

u8 PadNotConnected::GetEffectiveInput(u32 index) const
float PadNotConnected::GetEffectiveInput(u32 index) const
{
return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/SIO/Pad/PadNotConnected.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class PadNotConnected final : public PadBase
void SetButtonDeadzone(float deadzone) override;
void SetAnalogInvertL(bool x, bool y) override;
void SetAnalogInvertR(bool x, bool y) override;
u8 GetEffectiveInput(u32 index) const override;
float GetEffectiveInput(u32 index) const override;
u8 GetRawInput(u32 index) const override;
std::tuple<u8, u8> GetRawLeftAnalog() const override;
std::tuple<u8, u8> GetRawRightAnalog() const override;
Expand Down
4 changes: 2 additions & 2 deletions pcsx2/SIO/Pad/PadPopn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ void PadPopn::SetAnalogInvertR(bool x, bool y)
{
}

u8 PadPopn::GetEffectiveInput(u32 index) const
float PadPopn::GetEffectiveInput(u32 index) const
{
return GetRawInput(index);
return GetRawInput(index) / 255.0f;
}

u8 PadPopn::GetRawInput(u32 index) const
Expand Down
2 changes: 1 addition & 1 deletion pcsx2/SIO/Pad/PadPopn.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class PadPopn final : public PadBase
void SetButtonDeadzone(float deadzone) override;
void SetAnalogInvertL(bool x, bool y) override;
void SetAnalogInvertR(bool x, bool y) override;
u8 GetEffectiveInput(u32 index) const override;
float GetEffectiveInput(u32 index) const override;
u8 GetRawInput(u32 index) const override;
std::tuple<u8, u8> GetRawLeftAnalog() const override;
std::tuple<u8, u8> GetRawRightAnalog() const override;
Expand Down

0 comments on commit 23b72d0

Please sign in to comment.