减低鼠标灵敏度

This commit is contained in:
Barry 2018-11-17 22:06:43 +08:00
parent 3dfec33144
commit aac1fa3fea
3 changed files with 94 additions and 38 deletions

View file

@ -4,6 +4,8 @@
#include "inputconvertgame.h"
#define CURSOR_POS_CHECK 50
InputConvertGame::InputConvertGame(QObject* parent) : QObject(parent)
{
@ -103,18 +105,15 @@ void InputConvertGame::sendTouchEvent(int id, QPointF pos, AndroidMotioneventAct
if (!controlEvent) {
return;
}
controlEvent->setTouchEventData(id, action, QRect(calcAbsolutePos(pos).toPoint(), m_frameSize));
controlEvent->setTouchEventData(id, action, QRect(calcFrameAbsolutePos(pos).toPoint(), m_frameSize));
sendControlEvent(controlEvent);
}
QPointF InputConvertGame::calcAbsolutePos(QPointF relativePos)
QPointF InputConvertGame::calcFrameAbsolutePos(QPointF relativePos)
{
QPointF absolutePos;
absolutePos.setX(m_showSize.width() * relativePos.x());
absolutePos.setY(m_showSize.height() * relativePos.y());
// convert pos
absolutePos.setX(absolutePos.x() * m_frameSize.width() / m_showSize.width());
absolutePos.setY(absolutePos.y() * m_frameSize.height() / m_showSize.height());
absolutePos.setX(m_frameSize.width() * relativePos.x());
absolutePos.setY(m_frameSize.height() * relativePos.y());
return absolutePos;
}
@ -375,28 +374,34 @@ bool InputConvertGame::processMouseMove(const QMouseEvent *from)
{
if (QEvent::MouseMove != from->type()) {
return false;
}
}
mouseMoveStartTouch(from);
startMouseMoveTimer();
// move
// pos
QPointF pos = from->localPos();
// convert pos
pos.setX(pos.x() / m_showSize.width());
pos.setY(pos.y() / m_showSize.height());
if (pos.x() < 0.1 || pos.x() > 0.9 || pos.y() < 0.1 || pos.y() > 0.9) {
mouseMoveStopTouch();
mouseMoveStartTouch(from);
if (checkCursorPos(from)) {
m_mouseMoveLastPos = QPointF(0.0f, 0.0f);
return true;
}
if (!m_mouseMoveLastPos.isNull()) {
pos = (m_mouseMoveLastPos + pos)/4;
QPointF distance = from->localPos() - m_mouseMoveLastPos;
distance /= 8;
mouseMoveStartTouch(from);
startMouseMoveTimer();
m_mouseMoveLastConverPos.setX(m_mouseMoveLastConverPos.x() + distance.x() / m_showSize.width());
m_mouseMoveLastConverPos.setY(m_mouseMoveLastConverPos.y() + distance.y() / m_showSize.height());
if (m_mouseMoveLastConverPos.x() < 0.1
|| m_mouseMoveLastConverPos.x() > 0.9
|| m_mouseMoveLastConverPos.y() < 0.1
|| m_mouseMoveLastConverPos.y() > 0.9) {
mouseMoveStopTouch();
mouseMoveStartTouch(from);
}
sendTouchMoveEvent(getTouchID(Qt::ExtraButton24), m_mouseMoveLastConverPos);
}
sendTouchMoveEvent(getTouchID(Qt::ExtraButton24), pos);
m_mouseMoveLastPos = pos;
m_mouseMoveLastPos = from->localPos();
return true;
}
@ -410,6 +415,15 @@ void InputConvertGame::moveCursorToStart(const QMouseEvent *from)
QCursor::setPos(globalPos);
}
void InputConvertGame::moveCursorTo(const QMouseEvent *from, const QPoint &pos)
{
QPoint posOffset = from->localPos().toPoint() - pos;
QPoint globalPos = from->globalPos();
globalPos -= posOffset;
QCursor::setPos(globalPos);
}
void InputConvertGame::startMouseMoveTimer()
{
stopMouseMoveTimer();
@ -427,9 +441,10 @@ void InputConvertGame::stopMouseMoveTimer()
void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from)
{
if (!m_mouseMovePress) {
moveCursorToStart(from);
//moveCursorToStart(from);
int id = attachTouchID(Qt::ExtraButton24);
sendTouchDownEvent(id, m_mouseMoveStartPos);
m_mouseMoveLastConverPos = m_mouseMoveStartPos;
m_mouseMovePress = true;
}
}
@ -437,8 +452,7 @@ void InputConvertGame::mouseMoveStartTouch(const QMouseEvent* from)
void InputConvertGame::mouseMoveStopTouch()
{
if (m_mouseMovePress) {
sendTouchUpEvent(getTouchID(Qt::ExtraButton24), m_mouseMoveLastPos);
m_mouseMoveLastPos = QPointF(0.0f, 0.0f);
sendTouchUpEvent(getTouchID(Qt::ExtraButton24), m_mouseMoveLastConverPos);
detachTouchID(Qt::ExtraButton24);
m_mouseMovePress = false;
}
@ -446,13 +460,9 @@ void InputConvertGame::mouseMoveStopTouch()
void InputConvertGame::switchGameMap()
{
m_gameMap = !m_gameMap;
grabCursor(m_gameMap);
}
void InputConvertGame::grabCursor(bool grab)
{
if(grab) {
m_gameMap = !m_gameMap;
emit grabCursor(m_gameMap);
if (m_gameMap) {
QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
} else {
mouseMoveStopTouch();
@ -460,6 +470,31 @@ void InputConvertGame::grabCursor(bool grab)
}
}
bool InputConvertGame::checkCursorPos(const QMouseEvent *from)
{
bool moveCursor = false;
QPoint pos = from->pos();
if (pos.x() < CURSOR_POS_CHECK) {
pos.setX(m_showSize.width() - CURSOR_POS_CHECK);
moveCursor = true;
} else if (pos.x() > m_showSize.width() - CURSOR_POS_CHECK) {
pos.setX(CURSOR_POS_CHECK);
moveCursor = true;
} else if (pos.y() < CURSOR_POS_CHECK) {
pos.setY(m_showSize.height() - CURSOR_POS_CHECK);
moveCursor = true;
} else if (pos.y() > m_showSize.height() - CURSOR_POS_CHECK) {
pos.setY(CURSOR_POS_CHECK);
moveCursor = true;
}
if (moveCursor) {
moveCursorTo(from, pos);
}
return moveCursor;
}
void InputConvertGame::timerEvent(QTimerEvent *event)
{
if (m_mouseMoveTimer == event->timerId()) {

View file

@ -16,13 +16,16 @@ public:
virtual void wheelEvent(const QWheelEvent* from, const QSize& frameSize, const QSize& showSize);
virtual void keyEvent(const QKeyEvent* from, const QSize& frameSize, const QSize& showSize);
signals:
void grabCursor(bool grab);
protected:
void updateSize(const QSize& frameSize, const QSize& showSize);
void sendTouchDownEvent(int id, QPointF pos);
void sendTouchMoveEvent(int id, QPointF pos);
void sendTouchUpEvent(int id, QPointF pos);
void sendTouchEvent(int id, QPointF pos, AndroidMotioneventAction action);
QPointF calcAbsolutePos(QPointF relativePos);
QPointF calcFrameAbsolutePos(QPointF relativePos);
// multi touch id
int attachTouchID(int key);
@ -42,13 +45,14 @@ protected:
bool processMouseClick(const QMouseEvent* from);
bool processMouseMove(const QMouseEvent* from);
void moveCursorToStart(const QMouseEvent* from);
void moveCursorTo(const QMouseEvent* from, const QPoint& pos);
void startMouseMoveTimer();
void stopMouseMoveTimer();
void mouseMoveStartTouch(const QMouseEvent* from);
void mouseMoveStopTouch();
void switchGameMap();
void grabCursor(bool grab);
void switchGameMap();
bool checkCursorPos(const QMouseEvent* from);
protected:
void timerEvent(QTimerEvent *event);
@ -77,7 +81,8 @@ private:
int m_steerWheelFirstTouchKey = 0;
// mouse move
QPointF m_mouseMoveStartPos = {0.57f, 0.26f};
QPointF m_mouseMoveStartPos = {0.57f, 0.26f};
QPointF m_mouseMoveLastConverPos = m_mouseMoveStartPos;
QPointF m_mouseMoveLastPos = {0.0f, 0.0f};
bool m_mouseMovePress = false;
int m_mouseMoveTimer = 0;

View file

@ -4,6 +4,9 @@
#include "videoform.h"
#include "ui_videoform.h"
#include <Windows.h>
#pragma comment(lib, "User32.lib")
VideoForm::VideoForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::videoForm)
@ -14,6 +17,19 @@ VideoForm::VideoForm(QWidget *parent) :
setMouseTracking(true);
ui->videoWidget->setMouseTracking(true);
connect(&m_inputConvert, &InputConvertGame::grabCursor, this, [this](bool grab){
if(grab) {
RECT mainRect; //windef.h中被定义
mainRect.left = (LONG)this->geometry().left();
mainRect.right = (LONG)this->geometry().right();
mainRect.top = (LONG)this->geometry().top();
mainRect.bottom = (LONG)this->geometry().bottom();
ClipCursor(&mainRect);
} else {
ClipCursor(Q_NULLPTR);
}
});
m_server = new Server();
m_frames.init();
m_decoder.setFrames(&m_frames);