src/ui/canvas.cpp

Thu, 27 Feb 2020 11:56:41 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Thu, 27 Feb 2020 11:56:41 +0200
changeset 57
5c0005f63319
parent 56
fad4a5dd8dee
child 58
b7841cd31fb7
permissions
-rw-r--r--

use glm::unProject to implement screenToModelCoordinates

47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
1 #include <QMouseEvent>
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
2 #include <QPainter>
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
3 #include "canvas.h"
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
4
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
5 Canvas::Canvas(
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
6 Model* model,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
7 DocumentManager* documents,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
8 const ldraw::ColorTable& colorTable,
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
9 QWidget* parent) :
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
10 PartRenderer{model, documents, colorTable, parent}
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
11 {
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
12 this->setMouseTracking(true);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
13 }
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
14
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
15 void Canvas::handleSelectionChange(const QSet<ldraw::Id>& selectedIds, const QSet<ldraw::Id>& deselectedIds)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
16 {
52
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
17 Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID));
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
18 this->selection.subtract(deselectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
19 this->selection.unite(selectedIds);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
20 this->compiler->setSelectedObjects(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
21 this->update();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
22 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
23
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 void Canvas::mouseMoveEvent(QMouseEvent* event)
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 {
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
26 const ldraw::Id id = this->pick(event->pos());
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
27 this->highlighted = id;
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
28 this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength();
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
29 this->lastMousePosition = event->pos();
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
30 this->worldPosition = this->screenToModelCoordinates(this->lastMousePosition);
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
31 if (this->worldPosition.has_value())
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
32 {
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
33 this->worldPosition = glm::round(*this->worldPosition);
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
34 }
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
35 if (this->worldPosition.has_value())
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
36 {
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
37 this->newStatusText("Position: (%1, %2, %3)"_q
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
38 .arg(this->worldPosition->x)
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
39 .arg(this->worldPosition->y)
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
40 .arg(this->worldPosition->z));
55
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
41 }
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
42 else
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
43 {
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
44 this->newStatusText("Position: <none>"_q);
cb81ecb5fb23 grid stuff
Teemu Piippo <teemu@hecknology.net>
parents: 52
diff changeset
45 }
47
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 PartRenderer::mouseMoveEvent(event);
cd6704009eb9 picking works now
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
48
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
49 void Canvas::mousePressEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
50 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
51 this->totalMouseMove = 0;
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
52 this->lastMousePosition = event->pos();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
53 PartRenderer::mousePressEvent(event);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
54 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
55
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
56 void Canvas::mouseReleaseEvent(QMouseEvent* event)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
57 {
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
58 if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0)
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
59 {
52
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
60 if (this->highlighted == ldraw::NULL_ID)
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
61 {
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
62 this->selection = {};
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
63 }
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
64 else
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
65 {
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
66 this->selection = {this->highlighted};
eee644f88e93 avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents: 51
diff changeset
67 }
51
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
68 this->compiler->setSelectedObjects(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
69 emit selectionChanged(this->selection);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
70 this->update();
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
71 }
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
72 PartRenderer::mouseReleaseEvent(event);
1a9eac27698d selection works now
Teemu Piippo <teemu@hecknology.net>
parents: 48
diff changeset
73 }
57
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
74
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
75 void Canvas::paintGL()
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
76 {
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
77 PartRenderer::paintGL();
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
78 if (this->worldPosition.has_value())
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
79 {
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
80 QPainter painter{this};
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
81 painter.setRenderHint(QPainter::Antialiasing);
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
82 painter.setPen(Qt::black);
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
83 painter.setBrush(Qt::green);
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
84 painter.drawEllipse(this->modelToScreenCoordinates(*this->worldPosition), 10, 10);
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
85 }
5c0005f63319 use glm::unProject to implement screenToModelCoordinates
Teemu Piippo <teemu@hecknology.net>
parents: 56
diff changeset
86 }

mercurial