24 { |
24 { |
25 static const QString result = tr("Draw new elements into the model."); |
25 static const QString result = tr("Draw new elements into the model."); |
26 return result; |
26 return result; |
27 } |
27 } |
28 |
28 |
29 bool DrawTool::mouseClick(Document* document, Canvas* canvas) |
29 bool DrawTool::mouseClick(Document* document, Canvas* canvas, QMouseEvent* event) |
30 { |
30 { |
31 const auto& worldPosition = canvas->getWorldPosition(); |
31 if (event->button() == Qt::LeftButton) |
32 if (worldPosition.has_value()) |
|
33 { |
32 { |
34 const glm::vec3& pos = worldPosition.value(); |
33 const auto& worldPosition = canvas->getWorldPosition(); |
35 const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; |
34 if (worldPosition.has_value()) |
36 if (any(this->polygon, isCloseToPos)) |
|
37 { |
35 { |
38 this->closeShape(document); |
36 const glm::vec3& pos = worldPosition.value(); |
39 } |
37 const auto isCloseToPos = [&](const glm::vec3& x){return geom::isclose(x, pos);}; |
40 else |
38 if (any(this->polygon, isCloseToPos)) |
41 { |
|
42 this->polygon.push_back(pos); |
|
43 if (this->polygon.size() == 4) |
|
44 { |
39 { |
45 this->closeShape(document); |
40 this->closeShape(document); |
46 } |
41 } |
|
42 else |
|
43 { |
|
44 this->polygon.push_back(pos); |
|
45 if (this->polygon.size() == 4) |
|
46 { |
|
47 this->closeShape(document); |
|
48 } |
|
49 } |
47 } |
50 } |
|
51 this->previewPolygon = this->polygon; |
|
52 return true; |
48 } |
53 } |
49 this->previewPolygon = this->polygon; |
54 else if (event->button() == Qt::RightButton and this->polygon.size() > 0) |
50 return true; |
55 { |
|
56 this->polygon.erase(this->polygon.end() - 1); |
|
57 this->updatePreviewPolygon(); |
|
58 return true; |
|
59 } |
|
60 else |
|
61 { |
|
62 return false; |
|
63 } |
51 } |
64 } |
52 |
65 |
53 bool DrawTool::mouseMove(Document* document, Canvas* canvas) |
66 bool DrawTool::mouseMove(Document* document, Canvas* canvas, QMouseEvent *event) |
54 { |
67 { |
55 static_cast<void>(document); |
68 static_cast<void>(document); |
|
69 static_cast<void>(event); |
56 const auto& worldPosition = canvas->getWorldPosition(); |
70 const auto& worldPosition = canvas->getWorldPosition(); |
57 if (worldPosition.has_value()) |
71 if (worldPosition.has_value()) |
58 { |
72 { |
59 this->previewPoint = worldPosition.value(); |
73 this->previewPoint = worldPosition.value(); |
60 if (this->polygon.size() < 4) |
74 if (this->polygon.size() < 4) |
61 { |
75 { |
62 this->previewPolygon.resize(this->polygon.size() + 1); |
76 this->updatePreviewPolygon(); |
63 this->previewPolygon.back() = this->previewPoint; |
|
64 } |
77 } |
65 } |
78 } |
66 return false; |
79 return false; |
|
80 } |
|
81 |
|
82 void DrawTool::updatePreviewPolygon() |
|
83 { |
|
84 this->previewPolygon.resize(this->polygon.size() + 1); |
|
85 this->previewPolygon.back() = this->previewPoint; |
67 } |
86 } |
68 |
87 |
69 void DrawTool::reset() |
88 void DrawTool::reset() |
70 { |
89 { |
71 this->polygon.clear(); |
90 this->polygon.clear(); |