Qt: Make GBA widgets movable with left click in borderless mode

This commit is contained in:
Bonta 2021-07-22 18:02:34 +02:00
parent 63ff86adff
commit 501868ace0
2 changed files with 31 additions and 1 deletions

View File

@ -15,6 +15,7 @@
#include <QImage>
#include <QMenu>
#include <QMimeData>
#include <QMouseEvent>
#include <QPainter>
#include "AudioCommon/AudioCommon.h"
@ -63,7 +64,7 @@ GBAWidget::GBAWidget(std::weak_ptr<HW::GBA::Core> core, const HW::GBA::CoreInfo&
const std::optional<NetPlay::PadDetails>& netplay_pad)
: QWidget(nullptr, LoadWindowFlags(netplay_pad ? netplay_pad->local_pad : info.device_number)),
m_core(std::move(core)), m_core_info(info), m_local_pad(info.device_number),
m_is_local_pad(true), m_volume(0), m_muted(false), m_force_disconnect(false)
m_is_local_pad(true), m_volume(0), m_muted(false), m_force_disconnect(false), m_moving(false)
{
bool visible = true;
if (netplay_pad)
@ -399,6 +400,29 @@ void GBAWidget::mouseDoubleClickEvent(QMouseEvent* event)
SetBorderless(!IsBorderless());
}
void GBAWidget::mousePressEvent(QMouseEvent* event)
{
if (event->button() != Qt::MouseButton::LeftButton ||
!windowFlags().testFlag(Qt::FramelessWindowHint))
return;
m_moving = true;
m_move_pos = event->pos();
}
void GBAWidget::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() != Qt::MouseButton::LeftButton)
return;
m_moving = false;
}
void GBAWidget::mouseMoveEvent(QMouseEvent* event)
{
if (!m_moving)
return;
move(event->globalPos() - m_move_pos - (geometry().topLeft() - pos()));
}
void GBAWidget::paintEvent(QPaintEvent* event)
{
QPainter painter(this);

View File

@ -9,6 +9,7 @@
#include <string_view>
#include <vector>
#include <QPoint>
#include <QWidget>
#include "Common/CommonTypes.h"
@ -68,6 +69,9 @@ private:
void closeEvent(QCloseEvent* event) override;
void contextMenuEvent(QContextMenuEvent* event) override;
void mouseDoubleClickEvent(QMouseEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
void mouseMoveEvent(QMouseEvent* event) override;
void paintEvent(QPaintEvent* event) override;
void dragEnterEvent(QDragEnterEvent* event) override;
@ -82,6 +86,8 @@ private:
int m_volume;
bool m_muted;
bool m_force_disconnect;
bool m_moving;
QPoint m_move_pos;
};
class GBAWidgetController : public QObject