Skip to content

Commit 7830e3f

Browse files
committed
Basic mouse tracing
1 parent 4e81a0e commit 7830e3f

File tree

5 files changed

+53
-31
lines changed

5 files changed

+53
-31
lines changed

QtLive2d/LAppDelegate.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ void LAppDelegate::InitializeCubism()
158158
}
159159

160160
void LAppDelegate::mouseMoveEvent(QMouseEvent * event){
161-
_mouseX = static_cast<float>(event->pos().x()*QLive2dWidget::ratio);
162-
_mouseY = static_cast<float>(event->pos().y()*QLive2dWidget::ratio);
161+
_mouseX = static_cast<float>(event->pos().x()*QLive2dWidget::ratio_x);
162+
_mouseY = static_cast<float>(event->pos().y()*QLive2dWidget::ratio_y);
163163
// _mouseX= static_cast<float>(_mouseX*2.0/_screenWidth-1.0);
164164
// _mouseY= static_cast<float>(1.0-_mouseY*2.0/_screenHeight);
165165
if (!_captured)
@@ -175,8 +175,8 @@ void LAppDelegate::mouseMoveEvent(QMouseEvent * event){
175175
}
176176

177177
void LAppDelegate::mousePressEvent(QMouseEvent * event){
178-
_mouseX = static_cast<float>(event->pos().x()*QLive2dWidget::ratio);
179-
_mouseY = static_cast<float>(event->pos().y()*QLive2dWidget::ratio);
178+
_mouseX = static_cast<float>(event->pos().x()*QLive2dWidget::ratio_x);
179+
_mouseY = static_cast<float>(event->pos().y()*QLive2dWidget::ratio_y);
180180
// _mouseX= static_cast<float>(_mouseX*2.0/_screenWidth-1.0);
181181
// _mouseY= static_cast<float>(1.0-_mouseY*2.0/_screenHeight);
182182

@@ -194,17 +194,17 @@ void LAppDelegate::mousePressEvent(QMouseEvent * event){
194194
}
195195

196196
void LAppDelegate::rawMouseMoveEvent(QPoint pos) {
197-
_mouseX = static_cast<float>(pos.x() * QLive2dWidget::ratio);
198-
_mouseY = static_cast<float>(pos.y() * QLive2dWidget::ratio);
197+
_mouseX = static_cast<float>(pos.x());
198+
_mouseY = static_cast<float>(pos.y());
199199
if (_view == NULL) {
200200
return;
201201
}
202202
_view->OnTouchesMoved(_mouseX, _mouseY);
203203
}
204204

205205
void LAppDelegate::mouseReleaseEvent(QMouseEvent * event){
206-
_mouseX = static_cast<float>(event->pos().x()*QLive2dWidget::ratio);
207-
_mouseY = static_cast<float>(event->pos().y()*QLive2dWidget::ratio);
206+
_mouseX = static_cast<float>(event->pos().x()*QLive2dWidget::ratio_x);
207+
_mouseY = static_cast<float>(event->pos().y()*QLive2dWidget::ratio_y);
208208
// _mouseX= static_cast<float>(_mouseX*2.0/_screenWidth-1.0);
209209
// _mouseY= static_cast<float>(1.0-_mouseY*2.0/_screenHeight);
210210
if (_view == NULL)

QtLive2d/QLive2dWidget.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,27 @@ QLive2dWidget::QLive2dWidget(QWidget *parent):
2222

2323
// the OpenGL in OpenGLWidget won't scale by HDPI setting, we need to scale manually.
2424
ratio = parent->devicePixelRatio();
25+
calcRatios();
2526
LAppLive2DManager::GetInstance()->OnUpdate();
2627
}
28+
29+
void QLive2dWidget::calcRatios() {
30+
QWidget* parent = this->parentWidget();
31+
if (parent != nullptr) {
32+
QSize parentSize = parent->size();
33+
QSize thisSize = this->size();
34+
ratio_x = thisSize.width() / parentSize.width();
35+
ratio_y = thisSize.height() / parentSize.height();
36+
ratio_x *= ratio;
37+
ratio_y *= ratio;
38+
} else {
39+
ratio_x = ratio_y = ratio;
40+
}
41+
cout << "ratio_x: " << ratio_x << endl;
42+
cout << "ratio_y: " << ratio_y << endl;
43+
cout << "ratio: " << ratio << endl;
44+
}
45+
2746
void QLive2dWidget::initializeGL()
2847
{
2948
this->makeCurrent();

QtLive2d/QLive2dWidget.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include <QOpenGLWidget>
44
#include <QOpenGLFunctions>
55
#include <QElapsedTimer>
6+
#include <QGuiApplication>
7+
#include <QScreen>
8+
#include <QList>
9+
#include <iostream>
610

711
// #define QCOREEVENT_H // Mask qcoreevents.h to prevent the confusing errors from qcoreevent.h. What the fuck is happening to the MOC Compiler???
812
// Mask this header will cause some problems when using qevent. I'll remove it as soon as I find the mistake lead to these errors.
@@ -24,6 +28,8 @@ class QLive2dWidget :
2428
void mouseMove(QPoint rel);
2529

2630
inline static float ratio;
31+
inline static float ratio_x = 1;
32+
inline static float ratio_y = 1;
2733
inline static QElapsedTimer elapsedTimer;
2834

2935
protected:
@@ -33,6 +39,8 @@ class QLive2dWidget :
3339

3440
private slots:
3541
void updateMotions();
42+
private:
43+
void calcRatios();
3644

3745
signals:
3846
void initialized(QLive2dWidget *wid);

test/MainWindow.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
#include "MainWindow.h"
66
#include <QThread>
7-
#include <QDebug>
7+
8+
89

910
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
1011
this->live2DWidget = new QLive2dWidget(this);
@@ -17,9 +18,9 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
1718
// this->live2DWidget->setResDir("Resources");
1819
// this->live2DWidget->setModel("WY6");
1920
// this->live2DWidget->setAttribute(Qt::WA_TransparentForMouseEvents);
20-
// MouseEventThread *th = new MouseEventThread(this->geometry(), this->winId(), this);
21-
// connect(th, SIGNAL(mouseEvent(QPoint, QPoint)), this, SLOT(mouseEvent(QPoint, QPoint)), Qt::QueuedConnection);
22-
// th->start();
21+
MouseEventThread *th = new MouseEventThread(this->geometry(), this->winId(), this);
22+
connect(th, SIGNAL(mouseEvent(QPoint, QPoint)), this, SLOT(mouseEvent(QPoint, QPoint)), Qt::QueuedConnection);
23+
th->start();
2324
}
2425

2526
MainWindow::~MainWindow() {
@@ -28,17 +29,9 @@ MainWindow::~MainWindow() {
2829

2930
void MainWindow::initialized(QLive2dWidget *wid) {
3031
wid->setModel("WY6");
31-
qDebug()<<"Initialize finished";
32+
cout<<"Initialize finished";
3233
}
3334

34-
//void MainWindow::mouseEvent(QPoint relPosition, QPoint absPosition) {
35-
// const int x = relPosition.x();
36-
// const int y = relPosition.y();
37-
// QMouseEvent *mouseEvent = new QMouseEvent(QEvent::MouseMove,
38-
// relPosition,
39-
// absPosition,
40-
// Qt::MouseButton::NoButton,
41-
// Qt::MouseButton::NoButton,
42-
// Qt::KeyboardModifier::NoModifier);
43-
// this->live2DWidget->mouseMoveEvent(mouseEvent);
44-
//}
35+
void MainWindow::mouseEvent(QPoint relPosition, QPoint absPosition) {
36+
this->live2DWidget->mouseMove(relPosition);
37+
}

test/MainWindow.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,29 @@
55
#ifndef QTLIVE2D_MAINWINDOW_H
66
#define QTLIVE2D_MAINWINDOW_H
77

8+
#define QCOREEVENT_H
9+
#define QVARIANT_H
10+
#define QTEXTSTREAM_H
11+
812
#include <QMainWindow>
913
#include <QWidget>
1014
#include <QtX11Extras/QX11Info>
1115
#include <QPoint>
12-
#include <QMouseEvent>
13-
//#include <X11/extensions/shape.h>
14-
//#include "mouseEventThread.h"
16+
#include <X11/extensions/shape.h>
17+
#include "mouseEventThread.h"
1518
#include "../QtLive2d/QLive2dWidget.hpp"
19+
#include "iostream"
20+
using namespace std;
1621

1722
class MainWindow : public QMainWindow{
1823
Q_OBJECT
1924
public:
2025
QLive2dWidget *live2DWidget;
2126
MainWindow(QWidget *parent = nullptr);
2227
~MainWindow();
23-
protected:
24-
inline void mouseMoveEvent(QMouseEvent *ev) override {
25-
this->live2DWidget->mouseMove(ev);
26-
}
2728
public slots:
2829
void initialized(QLive2dWidget *wid);
30+
void mouseEvent(QPoint rel, QPoint abs);
2931
// void mouseEvent(QPoint relPosition, QPoint absPosition);
3032
};
3133

0 commit comments

Comments
 (0)