# HG changeset patch # User Teemu Piippo # Date 1655287400 -10800 # Node ID 948867719906ca6c36f650ad2cc8631f23e65e02 # Parent 011600b3cf73e9c8b0203a1c31b31048ee4d72a1 refactor diff -r 011600b3cf73 -r 948867719906 src/document.cpp --- 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 points = modelActionPoints(action).value_or(std::vector{}); - 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(pensptr); + painter->setPen(pens.polygonPen); + for (const ModelAction& action : this->actions()) { + const std::vector points = modelActionPoints(action).value_or(std::vector{}); + 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) { diff -r 011600b3cf73 -r 948867719906 src/document.h --- 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 actions() const; void closeShape(); + void renderPreview(QPainter* painter, const void* pensptr); void removeLastPoint(); bool isCloseToExistingPoints() const; };