Wed, 26 Feb 2020 22:26:05 +0200
PartRenderer::modelToScreenCoordinates FINALLY WORKS
47 | 1 | #include <QMouseEvent> |
2 | #include "canvas.h" | |
3 | ||
4 | Canvas::Canvas( | |
5 | Model* model, | |
6 | DocumentManager* documents, | |
7 | const ldraw::ColorTable& colorTable, | |
8 | QWidget* parent) : | |
9 | PartRenderer{model, documents, colorTable, parent} | |
10 | { | |
11 | this->setMouseTracking(true); | |
12 | } | |
13 | ||
51 | 14 | void Canvas::handleSelectionChange(const QSet<ldraw::Id>& selectedIds, const QSet<ldraw::Id>& deselectedIds) |
15 | { | |
52
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
16 | Q_ASSERT(not selectedIds.contains(ldraw::NULL_ID)); |
51 | 17 | this->selection.subtract(deselectedIds); |
18 | this->selection.unite(selectedIds); | |
19 | this->compiler->setSelectedObjects(this->selection); | |
20 | this->update(); | |
21 | } | |
22 | ||
47 | 23 | void Canvas::mouseMoveEvent(QMouseEvent* event) |
24 | { | |
55 | 25 | #if 0 |
26 | std::optional<glm::vec3> p = this->cameraToGrid(event->pos()); | |
27 | if (p.has_value()) | |
28 | { | |
29 | this->newStatusText("Position: (%1, %2, %3)"_q.arg(p->x).arg(p->y).arg(p->z)); | |
30 | } | |
31 | else | |
32 | { | |
33 | this->newStatusText("Position: <none>"_q); | |
34 | } | |
35 | #else | |
56
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
36 | const QPointF originAtCamera = this->modelToScreenCoordinates({1, 1, 1}); |
fad4a5dd8dee
PartRenderer::modelToScreenCoordinates FINALLY WORKS
Teemu Piippo <teemu@hecknology.net>
parents:
55
diff
changeset
|
37 | this->newStatusText("{1,1,1} is at (%1, %2), cursor at (%3, %4)"_q.arg(originAtCamera.x()).arg(originAtCamera.y()).arg(event->pos().x()).arg(event->pos().y())); |
55 | 38 | #endif |
47 | 39 | const ldraw::Id id = this->pick(event->pos()); |
51 | 40 | this->highlighted = id; |
41 | this->totalMouseMove += (event->pos() - this->lastMousePosition).manhattanLength(); | |
42 | this->lastMousePosition = event->pos(); | |
47 | 43 | PartRenderer::mouseMoveEvent(event); |
44 | } | |
51 | 45 | |
46 | void Canvas::mousePressEvent(QMouseEvent* event) | |
47 | { | |
48 | this->totalMouseMove = 0; | |
49 | this->lastMousePosition = event->pos(); | |
50 | PartRenderer::mousePressEvent(event); | |
51 | } | |
52 | ||
53 | void Canvas::mouseReleaseEvent(QMouseEvent* event) | |
54 | { | |
55 | if (this->totalMouseMove < (2.0 / sqrt(2)) * 5.0) | |
56 | { | |
52
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
57 | if (this->highlighted == ldraw::NULL_ID) |
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
58 | { |
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
59 | this->selection = {}; |
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
60 | } |
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
61 | else |
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
62 | { |
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
63 | this->selection = {this->highlighted}; |
eee644f88e93
avoid having the null id in the selection
Teemu Piippo <teemu@hecknology.net>
parents:
51
diff
changeset
|
64 | } |
51 | 65 | this->compiler->setSelectedObjects(this->selection); |
66 | emit selectionChanged(this->selection); | |
67 | this->update(); | |
68 | } | |
69 | PartRenderer::mouseReleaseEvent(event); | |
70 | } |