Wed, 15 Jun 2022 13:03:20 +0300
refactor
src/document.cpp | file | annotate | diff | comparison | revisions | |
src/document.h | file | annotate | diff | comparison | revisions |
--- a/src/document.cpp Wed Jun 15 12:41:57 2022 +0300 +++ b/src/document.cpp Wed Jun 15 13:03:20 2022 +0300 @@ -156,60 +156,38 @@ return result; } +namespace { +struct Pens +{ + const QBrush pointBrush; + const QPen pointPen; + const QPen polygonPen; + const QPen badPolygonPen; + const QBrush greenPolygonBrush; + const QBrush redPolygonBrush; +}; +} + +static const Pens brightPens{ + .pointBrush = {Qt::white}, + .pointPen = {QBrush{Qt::black}, 2.0}, + .polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}, + .greenPolygonBrush = {QColor{64, 255, 128, 192}}, + .redPolygonBrush = {QColor{255, 96, 96, 192}}, +}; + +static const Pens darkPens{ + .pointBrush = {Qt::black}, + .pointPen = {QBrush{Qt::white}, 2.0}, + .polygonPen = {QBrush{Qt::white}, 2.0, Qt::DashLine}, + .greenPolygonBrush = {QColor{64, 255, 128, 192}}, + .redPolygonBrush = {QColor{255, 96, 96, 192}}, +}; + void EditTools::overpaint(QPainter* painter) { - struct Pens - { - const QBrush pointBrush; - const QPen pointPen; - const QPen polygonPen; - const QPen badPolygonPen; - const QBrush greenPolygonBrush; - const QBrush redPolygonBrush; - }; - static const Pens brightPens{ - .pointBrush = {Qt::white}, - .pointPen = {QBrush{Qt::black}, 2.0}, - .polygonPen = {QBrush{Qt::black}, 2.0, Qt::DashLine}, - .greenPolygonBrush = {QColor{64, 255, 128, 192}}, - .redPolygonBrush = {QColor{255, 96, 96, 192}}, - }; - static const Pens darkPens{ - .pointBrush = {Qt::black}, - .pointPen = {QBrush{Qt::white}, 2.0}, - .polygonPen = {QBrush{Qt::white}, 2.0, Qt::DashLine}, - .greenPolygonBrush = {QColor{64, 255, 128, 192}}, - .redPolygonBrush = {QColor{255, 96, 96, 192}}, - }; const Pens& pens = (this->renderer->isDark() ? darkPens : brightPens); - switch(this->mode) { - case SelectMode: - break; - case DrawMode: - painter->setPen(pens.polygonPen); - for (const ModelAction& action : this->actions()) { - const std::vector<glm::vec3> points = modelActionPoints(action).value_or(std::vector<glm::vec3>{}); - if (points.size() == 2) { - drawWorldPolyline(painter, points, renderer); - } - else { - if (worldPolygonWinding(points, this->renderer) == Winding::Clockwise) { - painter->setBrush(pens.greenPolygonBrush); - } - else { - painter->setBrush(pens.redPolygonBrush); - } - drawWorldPolygon(painter, points, this->renderer); - } - } - //drawWorldPolyline(painter, this->previewPolygon, this->renderer); - painter->setBrush(pens.pointBrush); - painter->setPen(pens.pointPen); - for (const glm::vec3& point : this->polygon) { - drawWorldPoint(painter, point, this->renderer); - } - break; - } + this->renderPreview(painter, &pens); if (this->worldPosition.has_value()) { painter->setRenderHint(QPainter::Antialiasing); @@ -221,6 +199,32 @@ } } +void EditTools::renderPreview(QPainter* painter, const void* pensptr) +{ + const Pens& pens = *reinterpret_cast<const Pens*>(pensptr); + painter->setPen(pens.polygonPen); + for (const ModelAction& action : this->actions()) { + const std::vector<glm::vec3> points = modelActionPoints(action).value_or(std::vector<glm::vec3>{}); + if (points.size() == 2) { + drawWorldPolyline(painter, points, renderer); + } + else { + if (worldPolygonWinding(points, this->renderer) == Winding::Clockwise) { + painter->setBrush(pens.greenPolygonBrush); + } + else { + painter->setBrush(pens.redPolygonBrush); + } + drawWorldPolygon(painter, points, this->renderer); + } + } + painter->setBrush(pens.pointBrush); + painter->setPen(pens.pointPen); + for (const glm::vec3& point : this->polygon) { + drawWorldPoint(painter, point, this->renderer); + } +} + void EditTools::removeLastPoint() { if (this->polygon.size() > 1) {
--- a/src/document.h Wed Jun 15 12:41:57 2022 +0300 +++ b/src/document.h Wed Jun 15 13:03:20 2022 +0300 @@ -76,6 +76,7 @@ private: const std::vector<ModelAction> actions() const; void closeShape(); + void renderPreview(QPainter* painter, const void* pensptr); void removeLastPoint(); bool isCloseToExistingPoints() const; };