Skip to content

Commit

Permalink
feat: add drag delay
Browse files Browse the repository at this point in the history
  • Loading branch information
barry-ran committed Jun 28, 2021
1 parent 38199ec commit 6f33ea7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 6 deletions.
59 changes: 53 additions & 6 deletions QtScrcpy/device/controller/inputconvert/inputconvertgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,18 +422,65 @@ void InputConvertGame::processKeyClickMulti(const KeyMap::DelayClickNode *nodes,
}
}

void InputConvertGame::onDragTimer() {
if(m_dragDelayData.queuePos.empty()) {
return;
}
int id = getTouchID(m_dragDelayData.pressKey);
m_dragDelayData.currentPos = m_dragDelayData.queuePos.dequeue();
sendTouchMoveEvent(id, m_dragDelayData.currentPos);

if(m_dragDelayData.queuePos.empty()) {
delete m_dragDelayData.timer;
m_dragDelayData.timer = nullptr;

sendTouchUpEvent(id, m_dragDelayData.currentPos);
detachTouchID(m_dragDelayData.pressKey);

m_dragDelayData.currentPos = QPointF();
m_dragDelayData.pressKey = 0;
return;
}

if(!m_dragDelayData.queuePos.empty()) {
m_dragDelayData.timer->start(m_dragDelayData.queueTimer.dequeue());
}
}

void InputConvertGame::processKeyDrag(const QPointF &startPos, QPointF endPos, const QKeyEvent *from)
{
if (QEvent::KeyPress == from->type()) {
// stop last
if (m_dragDelayData.timer && m_dragDelayData.timer->isActive()) {
m_dragDelayData.timer->stop();
delete m_dragDelayData.timer;
m_dragDelayData.timer = nullptr;
m_dragDelayData.queuePos.clear();
m_dragDelayData.queueTimer.clear();

sendTouchUpEvent(getTouchID(m_dragDelayData.pressKey), m_dragDelayData.currentPos);
detachTouchID(m_dragDelayData.pressKey);

m_dragDelayData.currentPos = QPointF();
m_dragDelayData.pressKey = 0;
}

// start this
int id = attachTouchID(from->key());
sendTouchDownEvent(id, startPos);
sendTouchMoveEvent(id, endPos);
}

if (QEvent::KeyRelease == from->type()) {
int id = getTouchID(from->key());
sendTouchUpEvent(id, endPos);
detachTouchID(from->key());
m_dragDelayData.timer = new QTimer(this);
m_dragDelayData.timer->setSingleShot(true);
connect(m_dragDelayData.timer, &QTimer::timeout, this, &InputConvertGame::onDragTimer);
m_dragDelayData.pressKey = from->key();
m_dragDelayData.currentPos = startPos;
m_dragDelayData.queuePos.clear();
m_dragDelayData.queueTimer.clear();
getDelayQueue(startPos, endPos,
0.01f, 0.002f, 2.0f,
m_dragDelayData.queuePos,
m_dragDelayData.queueTimer);
m_dragDelayData.timer->start();
}
}

Expand Down
10 changes: 10 additions & 0 deletions QtScrcpy/device/controller/inputconvert/inputconvertgame.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class InputConvertGame : public InputConvertNormal

private slots:
void onSteerWheelTimer();
void onDragTimer();

private:
QSize m_frameSize;
Expand Down Expand Up @@ -110,6 +111,15 @@ private slots:
int timer = 0;
bool smallEyes = false;
} m_ctrlMouseMove;

// for drag delay
struct {
QPointF currentPos;
QTimer* timer = nullptr;
QQueue<QPointF> queuePos;
QQueue<double> queueTimer;
int pressKey = 0;
} m_dragDelayData;
};

#endif // INPUTCONVERTGAME_H

0 comments on commit 6f33ea7

Please sign in to comment.