GunCon: Add relative pointer binding
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
// SPDX-FileCopyrightText: 2019-2022 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-FileCopyrightText: 2019-2023 Connor McLaughlin <stenzek@gmail.com>
|
||||
// SPDX-License-Identifier: (GPL-3.0 OR CC-BY-NC-ND-4.0)
|
||||
|
||||
#include "playstation_mouse.h"
|
||||
@ -6,7 +6,6 @@
|
||||
#include "host.h"
|
||||
#include "system.h"
|
||||
|
||||
#include "util/input_manager.h"
|
||||
#include "util/state_wrapper.h"
|
||||
|
||||
#include "common/assert.h"
|
||||
@ -16,13 +15,11 @@
|
||||
|
||||
Log_SetChannel(PlayStationMouse);
|
||||
|
||||
static constexpr std::array<u8, static_cast<size_t>(PlayStationMouse::Button::Count)> s_button_indices = {{11, 10}};
|
||||
static constexpr std::array<u8, static_cast<size_t>(PlayStationMouse::Binding::ButtonCount)> s_button_indices = {
|
||||
{11, 10}};
|
||||
|
||||
PlayStationMouse::PlayStationMouse(u32 index) : Controller(index)
|
||||
{
|
||||
const auto& [x, y] = InputManager::GetPointerAbsolutePosition(0);
|
||||
m_last_host_position_x = static_cast<s32>(x);
|
||||
m_last_host_position_y = static_cast<s32>(y);
|
||||
}
|
||||
|
||||
PlayStationMouse::~PlayStationMouse() = default;
|
||||
@ -43,11 +40,21 @@ bool PlayStationMouse::DoState(StateWrapper& sw, bool apply_input_state)
|
||||
return false;
|
||||
|
||||
u16 button_state = m_button_state;
|
||||
u8 delta_x = m_delta_x;
|
||||
u8 delta_y = m_delta_y;
|
||||
float delta_x = m_delta_x;
|
||||
float delta_y = m_delta_y;
|
||||
sw.Do(&button_state);
|
||||
sw.Do(&delta_x);
|
||||
sw.Do(&delta_y);
|
||||
if (sw.GetVersion() >= 60)
|
||||
{
|
||||
sw.Do(&delta_x);
|
||||
sw.Do(&delta_y);
|
||||
}
|
||||
else
|
||||
{
|
||||
u8 dummy = 0;
|
||||
sw.Do(&dummy);
|
||||
sw.Do(&dummy);
|
||||
}
|
||||
|
||||
if (apply_input_state)
|
||||
{
|
||||
m_button_state = button_state;
|
||||
@ -70,8 +77,15 @@ float PlayStationMouse::GetBindState(u32 index) const
|
||||
|
||||
void PlayStationMouse::SetBindState(u32 index, float value)
|
||||
{
|
||||
if (index > s_button_indices.size())
|
||||
if (index >= s_button_indices.size())
|
||||
{
|
||||
if (index == static_cast<u32>(Binding::PointerX))
|
||||
m_delta_x += value;
|
||||
else if (index == static_cast<u32>(Binding::PointerY))
|
||||
m_delta_y += value;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (value >= 0.5f)
|
||||
m_button_state &= ~(u16(1) << s_button_indices[index]);
|
||||
@ -138,15 +152,22 @@ bool PlayStationMouse::Transfer(const u8 data_in, u8* data_out)
|
||||
|
||||
case TransferState::DeltaX:
|
||||
{
|
||||
UpdatePosition();
|
||||
*data_out = static_cast<u8>(m_delta_x);
|
||||
const float delta_x =
|
||||
std::clamp(std::floor(m_delta_x * m_sensitivity_x), static_cast<float>(std::numeric_limits<s8>::min()),
|
||||
static_cast<float>(std::numeric_limits<s8>::max()));
|
||||
m_delta_x -= delta_x / m_sensitivity_x;
|
||||
*data_out = static_cast<s8>(delta_x);
|
||||
m_transfer_state = TransferState::DeltaY;
|
||||
return true;
|
||||
}
|
||||
|
||||
case TransferState::DeltaY:
|
||||
{
|
||||
*data_out = static_cast<u8>(m_delta_y);
|
||||
const float delta_y =
|
||||
std::clamp(std::floor(m_delta_y * m_sensitivity_y), static_cast<float>(std::numeric_limits<s8>::min()),
|
||||
static_cast<float>(std::numeric_limits<s8>::max()));
|
||||
m_delta_y -= delta_y / m_sensitivity_x;
|
||||
*data_out = static_cast<s8>(delta_y);
|
||||
m_transfer_state = TransferState::Idle;
|
||||
return false;
|
||||
}
|
||||
@ -158,22 +179,12 @@ bool PlayStationMouse::Transfer(const u8 data_in, u8* data_out)
|
||||
}
|
||||
}
|
||||
|
||||
void PlayStationMouse::UpdatePosition()
|
||||
void PlayStationMouse::LoadSettings(SettingsInterface& si, const char* section)
|
||||
{
|
||||
// get screen coordinates
|
||||
const auto& [fmouse_x, fmouse_y] = InputManager::GetPointerAbsolutePosition(0);
|
||||
const s32 mouse_x = static_cast<s32>(fmouse_x);
|
||||
const s32 mouse_y = static_cast<s32>(fmouse_y);
|
||||
const s32 delta_x = mouse_x - m_last_host_position_x;
|
||||
const s32 delta_y = mouse_y - m_last_host_position_y;
|
||||
m_last_host_position_x = mouse_x;
|
||||
m_last_host_position_y = mouse_y;
|
||||
Controller::LoadSettings(si, section);
|
||||
|
||||
if (delta_x != 0 || delta_y != 0)
|
||||
Log_DevPrintf("dx=%d, dy=%d", delta_x, delta_y);
|
||||
|
||||
m_delta_x = static_cast<s8>(std::clamp<s32>(delta_x, std::numeric_limits<s8>::min(), std::numeric_limits<s8>::max()));
|
||||
m_delta_y = static_cast<s8>(std::clamp<s32>(delta_y, std::numeric_limits<s8>::min(), std::numeric_limits<s8>::max()));
|
||||
m_sensitivity_x = si.GetFloatValue(section, "SensitivityX", 1.0f);
|
||||
m_sensitivity_y = si.GetFloatValue(section, "SensitivityY", 1.0f);
|
||||
}
|
||||
|
||||
std::unique_ptr<PlayStationMouse> PlayStationMouse::Create(u32 index)
|
||||
@ -188,17 +199,20 @@ static const Controller::ControllerBindingInfo s_binding_info[] = {
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
BUTTON("Left", TRANSLATE_NOOP("PlayStationMouse", "Left Button"), PlayStationMouse::Button::Left, GenericInputBinding::Cross),
|
||||
BUTTON("Right", TRANSLATE_NOOP("PlayStationMouse", "Right Button"), PlayStationMouse::Button::Right, GenericInputBinding::Circle),
|
||||
{ "Pointer", TRANSLATE_NOOP("PlaystationMouse", "Pointer"), static_cast<u32>(PlayStationMouse::Binding::PointerX), InputBindingInfo::Type::Pointer, GenericInputBinding::Unknown },
|
||||
BUTTON("Left", TRANSLATE_NOOP("PlayStationMouse", "Left Button"), PlayStationMouse::Binding::Left, GenericInputBinding::Cross),
|
||||
BUTTON("Right", TRANSLATE_NOOP("PlayStationMouse", "Right Button"), PlayStationMouse::Binding::Right, GenericInputBinding::Circle),
|
||||
// clang-format on
|
||||
|
||||
#undef BUTTON
|
||||
};
|
||||
|
||||
static const SettingInfo s_settings[] = {
|
||||
{SettingInfo::Type::Boolean, "RelativeMouseMode", TRANSLATE_NOOP("PlayStationMouse", "Relative Mouse Mode"),
|
||||
TRANSLATE_NOOP("PlayStationMouse", "Locks the mouse cursor to the window, use for FPS games."), "false", nullptr,
|
||||
nullptr, nullptr, nullptr, nullptr, 0.0f},
|
||||
{SettingInfo::Type::Float, "SensitivityX", TRANSLATE_NOOP("PlayStationMouse", "Horizontal Sensitivity"),
|
||||
TRANSLATE_NOOP("PlayStationMouse", "Adjusts the correspondance between physical and virtual mouse movement."), "1.0",
|
||||
"0.01", "2.0", "0.01", "%.0f", nullptr, 100.0f},
|
||||
{SettingInfo::Type::Float, "SensitivityY", TRANSLATE_NOOP("PlayStationMouse", "Vertical Sensitivity"),
|
||||
TRANSLATE_NOOP("PlayStationMouse", "Adjusts the correspondance between physical and virtual mouse movement."), "1.0",
|
||||
"0.01", "2.0", "0.01", "%.0f", nullptr, 100.0f},
|
||||
};
|
||||
|
||||
const Controller::ControllerInfo PlayStationMouse::INFO = {ControllerType::PlayStationMouse,
|
||||
@ -208,17 +222,4 @@ const Controller::ControllerInfo PlayStationMouse::INFO = {ControllerType::PlayS
|
||||
countof(s_binding_info),
|
||||
s_settings,
|
||||
countof(s_settings),
|
||||
Controller::VibrationCapabilities::NoVibration};
|
||||
|
||||
void PlayStationMouse::LoadSettings(SettingsInterface& si, const char* section)
|
||||
{
|
||||
Controller::LoadSettings(si, section);
|
||||
|
||||
m_use_relative_mode = si.GetBoolValue(section, "RelativeMouseMode", false);
|
||||
}
|
||||
|
||||
bool PlayStationMouse::GetSoftwareCursor(std::string* image_path, float* image_scale, bool* relative_mode)
|
||||
{
|
||||
*relative_mode = m_use_relative_mode;
|
||||
return m_use_relative_mode;
|
||||
}
|
||||
Controller::VibrationCapabilities::NoVibration};
|
||||
Reference in New Issue
Block a user