Skip to content

Commit

Permalink
Qt: Implement mouse wheel binding
Browse files Browse the repository at this point in the history
  • Loading branch information
stenzek authored and refractionpcsx2 committed Jul 5, 2022
1 parent f5e3d79 commit 6e706b3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
7 changes: 2 additions & 5 deletions pcsx2-qt/DisplayWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,15 +330,12 @@ bool DisplayWidget::event(QEvent* event)

case QEvent::Wheel:
{
// wheel delta is 120 as in winapi
const QPoint delta_angle(static_cast<QWheelEvent*>(event)->angleDelta());
constexpr float DELTA = 120.0f;

const float dx = std::clamp(static_cast<float>(delta_angle.x()) / DELTA, -1.0f, 1.0f);
const float dx = std::clamp(static_cast<float>(delta_angle.x()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
if (dx != 0.0f)
InputManager::UpdatePointerRelativeDelta(0, InputPointerAxis::WheelX, dx);

const float dy = std::clamp(static_cast<float>(delta_angle.y()) / DELTA, -1.0f, 1.0f);
const float dy = std::clamp(static_cast<float>(delta_angle.y()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
if (dy != 0.0f)
InputManager::UpdatePointerRelativeDelta(0, InputPointerAxis::WheelY, dy);

Expand Down
3 changes: 3 additions & 0 deletions pcsx2-qt/QtUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class QUrl;

namespace QtUtils
{
/// Wheel delta is 120 as in winapi.
static constexpr float MOUSE_WHEEL_DELTA = 120.0f;

/// Marks an action as the "default" - i.e. makes the text bold.
void MarkActionAsDefault(QAction* action);

Expand Down
28 changes: 28 additions & 0 deletions pcsx2-qt/Settings/InputBindingDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QtCore/QTimer>
#include <QtGui/QKeyEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>

// _BitScanForward()
#include "pcsx2/GS/GSIntrin.h"
Expand Down Expand Up @@ -77,6 +78,33 @@ bool InputBindingDialog::eventFilter(QObject* watched, QEvent* event)
m_new_bindings.push_back(InputManager::MakePointerButtonKey(0, button_index));
return true;
}
else if (event_type == QEvent::Wheel)
{
const QPoint delta_angle(static_cast<QWheelEvent*>(event)->angleDelta());
const float dx = std::clamp(static_cast<float>(delta_angle.x()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
if (dx != 0.0f)
{
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::WheelX));
key.negative = (dx < 0.0f);
m_new_bindings.push_back(key);
}

const float dy = std::clamp(static_cast<float>(delta_angle.y()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
if (dy != 0.0f)
{
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::WheelY));
key.negative = (dy < 0.0f);
m_new_bindings.push_back(key);
}

if (dx != 0.0f || dy != 0.0f)
{
addNewBinding();
stopListeningForInput();
}

return true;
}
else if (event_type == QEvent::MouseMove && m_mouse_mapping_enabled)
{
// if we've moved more than a decent distance from the center of the widget, bind it.
Expand Down
28 changes: 28 additions & 0 deletions pcsx2-qt/Settings/InputBindingWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <QtCore/QTimer>
#include <QtGui/QKeyEvent>
#include <QtGui/QMouseEvent>
#include <QtGui/QWheelEvent>
#include <QtWidgets/QInputDialog>
#include <QtWidgets/QMessageBox>
#include <cmath>
Expand Down Expand Up @@ -131,6 +132,33 @@ bool InputBindingWidget::eventFilter(QObject* watched, QEvent* event)
m_new_bindings.push_back(InputManager::MakePointerButtonKey(0, button_index));
return true;
}
else if (event_type == QEvent::Wheel)
{
const QPoint delta_angle(static_cast<QWheelEvent*>(event)->angleDelta());
const float dx = std::clamp(static_cast<float>(delta_angle.x()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
if (dx != 0.0f)
{
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::WheelX));
key.negative = (dx < 0.0f);
m_new_bindings.push_back(key);
}

const float dy = std::clamp(static_cast<float>(delta_angle.y()) / QtUtils::MOUSE_WHEEL_DELTA, -1.0f, 1.0f);
if (dy != 0.0f)
{
InputBindingKey key(InputManager::MakePointerAxisKey(0, InputPointerAxis::WheelY));
key.negative = (dy < 0.0f);
m_new_bindings.push_back(key);
}

if (dx != 0.0f || dy != 0.0f)
{
setNewBinding();
stopListeningForInput();
}

return true;
}
else if (event_type == QEvent::MouseMove && m_mouse_mapping_enabled)
{
// if we've moved more than a decent distance from the center of the widget, bind it.
Expand Down

0 comments on commit 6e706b3

Please sign in to comment.