-
Notifications
You must be signed in to change notification settings - Fork 0
/
MotionFilter.h
47 lines (39 loc) · 1.16 KB
/
MotionFilter.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef MOTIONFILTER_H
#define MOTIONFILTER_H
#include <iostream>
#include <QtWidgets/QGraphicsPolygonItem>
#include <QtWidgets/QGraphicsSceneMouseEvent>
#include <QtCore/QObject>
#include <QtCore/QEvent>
#include <QtWidgets/QGraphicsView>
#include <QtGui/QKeyEvent>
#include <QtWidgets/QScrollBar>
#include <set>
#include "box2dengine.h"
#include "PlayerShip.h"
// Would prefer to not require this class at all, but it seems
// required for handling scene-level mouse and touch events properly.
class SceneMotionFilter : public QObject
{
Q_OBJECT
public:
SceneMotionFilter(PlayerShip *ship):
_ship(ship) { }
protected:
bool eventFilter(QObject *obj, QEvent *event) {
if (event->type() == QEvent::GraphicsSceneMouseMove) {
QGraphicsSceneMouseEvent *mouseEvent = static_cast<QGraphicsSceneMouseEvent *>(event);
QPointF pos = mouseEvent->scenePos();
b2Vec2 sp = _ship->GetBody()->GetPosition();
b2Vec2 mp(pos.x(), pos.y());
mp -= sp;
mp *= 0.02;
_ship->GetBody()->ApplyForceToCenter(mp, true);
return true;
}
return QObject::eventFilter(obj, event);
}
private:
PlayerShip* _ship;
};
#endif // MOTIONFILTER_H