Skip to content

Commit

Permalink
Fix cursor dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
feiy committed Dec 18, 2023
1 parent d7c1890 commit c896ee0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 36 deletions.
42 changes: 30 additions & 12 deletions QGameDev/XMLEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <QFileDialog>
#include <QMessageBox>
#include <QMouseEvent>
#include <QWindow>
#include <QJsonDocument>
#include <QJsonArray>
#include <QMenu>
Expand Down Expand Up @@ -272,18 +273,23 @@ void XMLEditor::OnMouseDown(QMouseEvent* event)
button = 4;
}


QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 1;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseDown(args.button, args.clicks, args.delta, args.x, args.y);

if (event->button() == Qt::MouseButton::LeftButton)
{
x_down = event->x();
y_down = event->y();
x_down = args.x;
y_down = args.y;
press_timer.start();
}
}
Expand Down Expand Up @@ -315,12 +321,16 @@ void XMLEditor::OnMouseUp(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 0;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseUp(args.button, args.clicks, args.delta, args.x, args.y);

if (press_timer.isActive())
Expand Down Expand Up @@ -356,18 +366,22 @@ void XMLEditor::OnMouseMove(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 0;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseMove(args.button, args.clicks, args.delta, args.x, args.y);

if (press_timer.isActive())
{
int x = event->x();
int y = event->y();
int x = args.x;
int y = args.y;

int dx = x - x_down;
int dy = y - y_down;
Expand All @@ -384,12 +398,16 @@ void XMLEditor::OnWheel(QWheelEvent* event)
if (m_game_player == nullptr) return;
m_ui.glControl->makeCurrent();

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = -1;
args.clicks = 0;
args.delta = event->angleDelta().y();
args.x = qRound(event->position().x());
args.y = qRound(event->position().y());
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseWheel(args.button, args.clicks, args.delta, args.x, args.y);
}

Expand Down
41 changes: 29 additions & 12 deletions QGamePlayer/PlayerWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <QJsonObject>
#include <QJsonArray>
#include <QMouseEvent>
#include <QWindow>
#include "PlayerWindow.h"

PlayerWindow::PlayerWindow(const char* path_proj, int idx)
Expand Down Expand Up @@ -155,18 +156,22 @@ void PlayerWindow::OnMouseDown(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 1;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseDown(args.button, args.clicks, args.delta, args.x, args.y);

if (event->button() == Qt::MouseButton::LeftButton)
{
x_down = event->x();
y_down = event->y();
x_down = args.x;
y_down = args.y;
press_timer.start();
}
}
Expand Down Expand Up @@ -198,12 +203,16 @@ void PlayerWindow::OnMouseUp(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 0;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseUp(args.button, args.clicks, args.delta, args.x, args.y);

if (press_timer.isActive())
Expand Down Expand Up @@ -239,18 +248,22 @@ void PlayerWindow::OnMouseMove(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 0;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseMove(args.button, args.clicks, args.delta, args.x, args.y);

if (press_timer.isActive())
{
int x = event->x();
int y = event->y();
int x = args.x;
int y = args.y;

int dx = x - x_down;
int dy = y - y_down;
Expand All @@ -267,12 +280,16 @@ void PlayerWindow::OnWheel(QWheelEvent* event)
if (m_game_player == nullptr) return;
m_ui.glControl->makeCurrent();

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = -1;
args.clicks = 0;
args.delta = event->angleDelta().y();
args.x = qRound(event->position().x());
args.y = qRound(event->position().y());
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseWheel(args.button, args.clicks, args.delta, args.x, args.y);
}

Expand Down
41 changes: 29 additions & 12 deletions QGamePlayer/QGamePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <QFileInfo>
#include <QFileDialog>
#include <QMouseEvent>
#include <QWindow>
#include "QGamePlayer.h"

QGamePlayer::QGamePlayer()
Expand Down Expand Up @@ -114,18 +115,22 @@ void QGamePlayer::OnMouseDown(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 1;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseDown(args.button, args.clicks, args.delta, args.x, args.y);

if (event->button() == Qt::MouseButton::LeftButton)
{
x_down = event->x();
y_down = event->y();
x_down = args.x;
y_down = args.y;
press_timer.start();
}
}
Expand Down Expand Up @@ -157,12 +162,16 @@ void QGamePlayer::OnMouseUp(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 0;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseUp(args.button, args.clicks, args.delta, args.x, args.y);

if (press_timer.isActive())
Expand Down Expand Up @@ -198,18 +207,22 @@ void QGamePlayer::OnMouseMove(QMouseEvent* event)
button = 4;
}

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = button;
args.clicks = 0;
args.delta = 0;
args.x = event->x();
args.y = event->y();
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseMove(args.button, args.clicks, args.delta, args.x, args.y);

if (press_timer.isActive())
{
int x = event->x();
int y = event->y();
int x = args.x;
int y = args.y;

int dx = x - x_down;
int dy = y - y_down;
Expand All @@ -226,12 +239,16 @@ void QGamePlayer::OnWheel(QWheelEvent* event)
if (m_game_player == nullptr) return;
m_ui.glControl->makeCurrent();

QWindow* win = m_ui.glControl->windowHandle();
double ratio = win->devicePixelRatio();
QPointF pos = event->position();

MouseEventArgs args;
args.button = -1;
args.clicks = 0;
args.delta = event->angleDelta().y();
args.x = qRound(event->position().x());
args.y = qRound(event->position().y());
args.x = pos.x() * ratio;
args.y = pos.y() * ratio;
m_game_player->OnMouseWheel(args.button, args.clicks, args.delta, args.x, args.y);
}

Expand Down

0 comments on commit c896ee0

Please sign in to comment.