From 0e62672b58456f0ffd51628a59d3b0b45b7fc728 Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Sun, 19 Mar 2023 01:27:42 +0800 Subject: [PATCH 1/4] =?UTF-8?q?[editor/style]=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E4=BA=86FillElementLayerStyle=20|=20#2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArchitectureColoredPainting.vcxproj | 2 + ...rchitectureColoredPainting.vcxproj.filters | 6 + .../EditorWidgetComponent/ColorPicker.h | 2 +- .../EditorWidgetComponent/FillStyleWidget.cpp | 48 +++++++ .../EditorWidgetComponent/FillStyleWidget.h | 12 ++ .../StrokeStyleWidget.cpp | 2 +- .../src/Editor/LayerStyle.cpp | 125 +++++++++++------- .../src/Editor/LayerStyle.h | 37 ++++-- .../src/Editor/RightBar/InfoDisplayWidget.cpp | 59 +-------- 9 files changed, 174 insertions(+), 119 deletions(-) create mode 100644 ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.cpp create mode 100644 ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.h diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj index ac1871e..948b53b 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj @@ -104,6 +104,7 @@ + @@ -200,6 +201,7 @@ + diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters index f60bea2..c738e52 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters @@ -240,6 +240,9 @@ Source Files + + Source Files + @@ -290,6 +293,9 @@ Header Files + + Header Files + diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/ColorPicker.h b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/ColorPicker.h index aca00ea..a8edd67 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/ColorPicker.h +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/ColorPicker.h @@ -6,7 +6,7 @@ class ColorPicker : public QPushButton private: QColor color; public: - ColorPicker(const QColor& color, QWidget* parent = nullptr); + ColorPicker(const QColor& color = QColor::fromRgb(0, 0, 0), QWidget* parent = nullptr); QColor getColor() const; public slots: void onClicked(); diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.cpp new file mode 100644 index 0000000..bf78dbc --- /dev/null +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.cpp @@ -0,0 +1,48 @@ +#include "FillStyleWidget.h" +#include "ColorPicker.h" +#include +#include +#include + +FillStyleWidget::FillStyleWidget(std::shared_ptr fill, QWidget* parent) +: QWidget(parent), fill(fill) +{ + auto* layout = new QGridLayout(this); + this->setLayout(layout); + + // 颜色 + auto* material = &plainFill(fill)->material; + auto* colorLabel = new QLabel(QStringLiteral("颜色"), this); + layout->addWidget(colorLabel, 0, 0); + auto* colorPicker = new ColorPicker(material->color); + connect(colorPicker, &ColorPicker::colorChanged, + [material](QColor color) + { + material->color = color; + }); + layout->addWidget(colorPicker, 0, 1); + + // 金属度 + auto* metallicLabel = new QLabel(QStringLiteral("金属度"), this); + layout->addWidget(metallicLabel, 1, 0); + auto* metallicInput = new QtMaterialTextField(this); + metallicInput->setText(QString::number(material->metallicF(), 'g', 3)); + connect(metallicInput, &QtMaterialTextField::textChanged, + [material](const QString& text) + { + material->setMetallicF(text.toFloat()); + }); + layout->addWidget(metallicInput, 1, 1); + + // 粗糙度 + auto* roughnessLabel = new QLabel(QStringLiteral("粗糙度"), this); + layout->addWidget(roughnessLabel, 2, 0); + auto* roughnessInput = new QtMaterialTextField(this); + roughnessInput->setText(QString::number(material->roughnessF(), 'g', 3)); + connect(roughnessInput, &QtMaterialTextField::textChanged, + [material](const QString& text) + { + material->setRoughnessF(text.toFloat()); + }); + layout->addWidget(roughnessInput, 2, 1); +} diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.h b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.h new file mode 100644 index 0000000..bff2724 --- /dev/null +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/FillStyleWidget.h @@ -0,0 +1,12 @@ +#pragma once +#include "../Renderer/Painting/MaterialStyleFill.h" +#include "LayerStyle.h" +#include +class FillStyleWidget : public QWidget +{ + Q_OBJECT +public: + FillStyleWidget(std::shared_ptr fill, QWidget* parent = nullptr); + std::shared_ptr fill; +}; + diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp index 670e185..e2c9ce7 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp @@ -11,7 +11,7 @@ constexpr int COLUMN_ROUGHNESS = 3; constexpr int COLUMN_OPERATIONS = 4; StrokeStyleWidget::StrokeStyleWidget( - std::shared_ptr stroke, + std::shared_ptr stroke, QWidget* parent ) : QWidget(parent), stroke(stroke) { diff --git a/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp b/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp index 5115658..7240d46 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp @@ -1,12 +1,11 @@ #include "LayerStyle.h" #include "./EditorWidgetComponent/StrokeStyleWidget.h" +#include "./EditorWidgetComponent/FillStyleWidget.h" #include "./util/EncodeUtil.hpp" #include #include -#include #include #include -#include #include #include #define _USE_JOIN_VIEW_INPUT_RANGE @@ -20,21 +19,19 @@ std::vector StrokeElementLayerStyle::toBaseStyles() const { if (!radialStroke(strokePair.first)->materialMap.empty()) { - baseStyles.push_back(Renderer::BaseStyle(std::make_shared(), - strokePair.first)); + baseStyles.push_back({ std::make_shared(), strokePair.first }); } if (!radialStroke(strokePair.second)->materialMap.empty()) { - baseStyles.push_back(Renderer::BaseStyle(std::make_shared(), - strokePair.second)); + baseStyles.push_back({ std::make_shared(), strokePair.second }); } } else if (!radialStroke(strokePair.first)->materialMap.empty()) { const auto material = std::shared_ptr(std::move(strokePair.first->clone())); - std::dynamic_pointer_cast(material)->strokeType = Renderer::StrokeType::kBothSides; + std::static_pointer_cast(material)->strokeType = Renderer::StrokeType::kBothSides; - baseStyles.push_back(Renderer::BaseStyle(std::make_shared(), material)); + baseStyles.push_back({ std::make_shared(), material }); } return baseStyles; } @@ -197,18 +194,23 @@ bool LayerStyleContainer::dropStyle(const QString& styleName) return true; } -float LayerStyleContainer::boundingBoxAffectValue() { +float LayerStyleContainer::boundingBoxAffectValue() const { float maxLineWidth = 0; - auto strokeStyle = styles[StrokeElementLayerStyle::displayName()]; - if (strokeStyle != nullptr) { - auto strokeElementLayerStyle = dynamic_cast(strokeStyle.get()); - if (strokeElementLayerStyle != nullptr) { - auto leftStyleStroke = strokeElementLayerStyle->strokePair.first; - auto rightStyleStroke = strokeElementLayerStyle->strokePair.second; - if (leftStyleStroke != nullptr) { + const auto strokeStyle = styles.at(StrokeElementLayerStyle::displayName()); + if (strokeStyle != nullptr) + { + if (const auto strokeElementLayerStyle = + std::dynamic_pointer_cast(strokeStyle); + strokeElementLayerStyle != nullptr) + { + const auto& leftStyleStroke = strokeElementLayerStyle->strokePair.first; + const auto& rightStyleStroke = strokeElementLayerStyle->strokePair.second; + if (leftStyleStroke != nullptr) + { maxLineWidth = std::max(maxLineWidth, leftStyleStroke->halfWidth); } - if (rightStyleStroke != nullptr) { + if (rightStyleStroke != nullptr) + { maxLineWidth = std::max(maxLineWidth, rightStyleStroke->halfWidth); } } @@ -216,11 +218,25 @@ float LayerStyleContainer::boundingBoxAffectValue() { return maxLineWidth; } -inline size_t LayerStyleContainer::getHash() const +size_t LayerStyleContainer::getHash() const { return hash; } +std::unique_ptr StrokeElementLayerStyle::fromJson(const QJsonObject& json) +{ + auto ptr = std::make_unique( + std::static_pointer_cast( + std::shared_ptr(std::move(MaterialStyle::decoded(EncodeUtil::fromBase64(json["left"].toString())))) + ), + std::static_pointer_cast( + std::shared_ptr(std::move(MaterialStyle::decoded(EncodeUtil::fromBase64(json["right"].toString())))) + ) + ); + ptr->enableEachSideIndependent = json["enableEachSideIndependent"].toBool(); + return ptr; +} + StrokeElementLayerStyle::StrokeElementLayerStyle() { const auto materialMap = std::map(); @@ -238,7 +254,7 @@ StrokeElementLayerStyle::StrokeElementLayerStyle() } -StrokeElementLayerStyle::StrokeElementLayerStyle(PMaterialStyleStroke left, PMaterialStyleStroke right) +StrokeElementLayerStyle::StrokeElementLayerStyle(const PMaterialStyleStroke& left, const PMaterialStyleStroke& right) { this->strokePair.first = left; this->strokePair.second = right ? right : std::static_pointer_cast( @@ -259,8 +275,7 @@ StrokeElementLayerStyle::StrokeElementLayerStyle(const StrokeElementLayerStyle& QJsonObject StrokeElementLayerStyle::toJson() const { - QJsonObject json; - json["type"] = typeName(); + auto json = LayerStyle::toJson(); json["enableEachSideIndependent"] = enableEachSideIndependent; json["left"] = EncodeUtil::toBase64(strokePair.first->encoded()); json["right"] = EncodeUtil::toBase64(strokePair.second->encoded()); @@ -272,18 +287,24 @@ std::unique_ptr StrokeElementLayerStyle::clone() const return std::make_unique(StrokeElementLayerStyle(*this)); } +std::unique_ptr FillElementLayerStyle::fromJson(const QJsonObject& json) +{ + auto ptr = std::make_unique( + std::static_pointer_cast( + std::shared_ptr(std::move(MaterialStyle::decoded(EncodeUtil::fromBase64(json["material"].toString())))) + ) + ); + return ptr; +} + std::vector FillElementLayerStyle::toBaseStyles() const { - // TODO: implement - return std::vector(); + return { {std::make_shared(), fillMaterialStyle} }; } QWidget* FillElementLayerStyle::getInputWidget() { - // TODO - auto* name = new QLineEdit; - name->setText(QStringLiteral("填充")); - return name; + return new FillStyleWidget(fillMaterialStyle); } QWidget* FillElementLayerStyle::getListDisplayWidget() const @@ -297,18 +318,29 @@ QWidget* FillElementLayerStyle::getListDisplayWidget() const return w; } +FillElementLayerStyle::FillElementLayerStyle(const PMaterialStyleFill& fillMaterialStyle) + : fillMaterialStyle(fillMaterialStyle) +{ + if (!fillMaterialStyle) + { + this->fillMaterialStyle = std::make_shared( + std::make_shared(Renderer::Material(QColor::fromRgb(0, 0, 0))) + ); + } +} + FillElementLayerStyle::FillElementLayerStyle(const FillElementLayerStyle& other) { - materialStyles = std::vector>(other.materialStyles.size()); - for (size_t i = 0; i < other.materialStyles.size(); i++) - { - materialStyles[i] = std::make_shared(*other.materialStyles[i]); - } + this->fillMaterialStyle = std::static_pointer_cast( + std::shared_ptr(std::move(other.fillMaterialStyle->clone())) + ); } QJsonObject FillElementLayerStyle::toJson() const { - return QJsonObject(); + auto json = LayerStyle::toJson(); + json["material"] = EncodeUtil::toBase64(fillMaterialStyle->encoded()); + return json; } std::unique_ptr FillElementLayerStyle::clone() const @@ -321,23 +353,18 @@ std::unique_ptr LayerStyle::fromJson(const QJsonObject& json) QString type = json["type"].toString(); if (type == StrokeElementLayerStyle::typeName()) { - auto ptr = std::make_unique( - std::static_pointer_cast( - std::shared_ptr(std::move(MaterialStyle::decoded(EncodeUtil::fromBase64(json["left"].toString())))) - ), - std::static_pointer_cast( - std::shared_ptr(std::move(MaterialStyle::decoded(EncodeUtil::fromBase64(json["right"].toString())))) - ) - ); - ptr->enableEachSideIndependent = json["enableEachSideIndependent"].toBool(); - return ptr; + return StrokeElementLayerStyle::fromJson(json); } - else if (type == FillElementLayerStyle::typeName()) + if (type == FillElementLayerStyle::typeName()) { - return std::make_unique(); - } - else - { - return nullptr; + return FillElementLayerStyle::fromJson(json); } + return nullptr; +} + +QJsonObject LayerStyle::toJson() const +{ + QJsonObject json; + json["type"] = this->getTypeName(); + return json; } diff --git a/ArchitectureColoredPainting/src/Editor/LayerStyle.h b/ArchitectureColoredPainting/src/Editor/LayerStyle.h index ed328bb..3d9b023 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerStyle.h +++ b/ArchitectureColoredPainting/src/Editor/LayerStyle.h @@ -12,26 +12,35 @@ using Renderer::MaterialStyle; using Renderer::MaterialStyleStroke; +using Renderer::MaterialStyleFill; #define STYLE_NAME(display_name, type_name) \ static QString displayName() { return QStringLiteral(display_name); } \ QString getDisplayName() const override { return QStringLiteral(display_name); } \ - static QString typeName() { return type_name; } + static QString typeName() { return type_name; } \ + QString getTypeName() const override { return type_name; } #define radialStroke(stroke) std::static_pointer_cast(stroke->materialStroke) +#define plainFill(fill) std::static_pointer_cast(fill->materialFill) class LayerStyle : public Renderer::ElementStyle { public: static std::unique_ptr fromJson(const QJsonObject& json); + virtual ~LayerStyle() = default; virtual QString getDisplayName() const = 0; + virtual QString getTypeName() const = 0; + virtual QWidget* getInputWidget() = 0; virtual QWidget* getListDisplayWidget() const = 0; - virtual ~LayerStyle() = default; - virtual QJsonObject toJson() const = 0; + + virtual QJsonObject toJson() const; virtual std::unique_ptr clone() const = 0; }; +/** + * LayerStyle的容器,在每次数据变更时需要手动调用computeNewHash()方法,否则哈希值不会更新 + */ class LayerStyleContainer : public Renderer::ElementStyle { using DisplayNameWithSupplier = std::map()>>; @@ -56,7 +65,7 @@ public: std::unique_ptr makeUnusedStyle(const QString& styleName) const; bool useStyle(const std::shared_ptr& style); bool dropStyle(const QString& styleName); - float boundingBoxAffectValue(); + float boundingBoxAffectValue() const; size_t getHash() const; /** @@ -70,10 +79,11 @@ class StrokeElementLayerStyle : public LayerStyle using PMaterialStyleStroke = std::shared_ptr; public: - STYLE_NAME("描边","stroke") + STYLE_NAME("描边", "stroke") + static std::unique_ptr fromJson(const QJsonObject& json); StrokeElementLayerStyle(); - StrokeElementLayerStyle(PMaterialStyleStroke left, PMaterialStyleStroke right = nullptr); + StrokeElementLayerStyle(const PMaterialStyleStroke& left, const PMaterialStyleStroke& right = nullptr); StrokeElementLayerStyle(const StrokeElementLayerStyle& other); ~StrokeElementLayerStyle() override = default; @@ -89,16 +99,21 @@ public: class FillElementLayerStyle : public LayerStyle { + using PMaterialStyleFill = std::shared_ptr; + public: - STYLE_NAME("填充","fill") + STYLE_NAME("填充", "fill") + static std::unique_ptr fromJson(const QJsonObject& json); + + FillElementLayerStyle(const PMaterialStyleFill& fillMaterialStyle = nullptr); + FillElementLayerStyle(const FillElementLayerStyle& other); + ~FillElementLayerStyle() override = default; std::vector toBaseStyles() const override; QWidget* getInputWidget() override; QWidget* getListDisplayWidget() const override; - FillElementLayerStyle() = default; - FillElementLayerStyle(const FillElementLayerStyle& other); - ~FillElementLayerStyle() override = default; - std::vector> materialStyles; QJsonObject toJson() const override; std::unique_ptr clone() const override; + + PMaterialStyleFill fillMaterialStyle; }; \ No newline at end of file diff --git a/ArchitectureColoredPainting/src/Editor/RightBar/InfoDisplayWidget.cpp b/ArchitectureColoredPainting/src/Editor/RightBar/InfoDisplayWidget.cpp index ca1e2ca..1db2940 100644 --- a/ArchitectureColoredPainting/src/Editor/RightBar/InfoDisplayWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/RightBar/InfoDisplayWidget.cpp @@ -102,7 +102,7 @@ void InfoDisplayWidget::generateLayerForm() else { connect(addStyleButton, &QPushButton::clicked, [&, leafP] { - auto* dialog = new LayerStyleDialog(leafP->styles); + auto* dialog = new LayerStyleDialog(leafP->styles, nullptr, this); dialog->exec(); if (dialog->layerStyle) { @@ -113,7 +113,6 @@ void InfoDisplayWidget::generateLayerForm() emit requireSelfRefresh(); emit requireRefreshElementWidget(); } - dialog->deleteLater(); }); } @@ -125,60 +124,7 @@ void InfoDisplayWidget::generateLayerForm() header->setFlags(Qt::NoItemFlags); styleList->addItem(header); styleList->setItemWidget(header, headerWidget); - //static vector styleNames = { "鏍蜂緥1", "鏍蜂緥2", "鏍蜂緥3" }; - // auto createStyleItem = [this, styleList](int index) { - // QListWidgetItem* item = new QListWidgetItem; - // QWidget* w = new QWidget; - // item->setSizeHint(QSize(50, 40)); - // QHBoxLayout* layout = new QHBoxLayout; - // QPushButton* deleteButton = new QPushButton(w); - // QPushButton* detailButton = new QPushButton(w); - // QLabel* name = new QLabel(w); - // name->setText(styleNames[index]); - // detailButton->setText("..."); - // detailButton->setFixedSize(QSize(20, 20)); - // deleteButton->setText("脳"); - // deleteButton->setFixedSize(QSize(20, 20)); - // connect(detailButton, &QPushButton::clicked, [styleList, item, this, index]() { - // QDialog dlg(this); - // dlg.setWindowTitle("鏍峰紡璇︽儏"); - // dlg.resize(400, 200); - // QGridLayout *contentLayout = new QGridLayout(&dlg); - // QLineEdit* name = new QLineEdit(styleNames[index], &dlg); - // auto buttonBox = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel); - // contentLayout->addWidget(buttonBox); - // connect(buttonBox, &QDialogButtonBox::accepted, &dlg, &QDialog::accept); - // connect(buttonBox, &QDialogButtonBox::rejected, &dlg, &QDialog::reject); - // bool updateStyle = dlg.exec(); - // if (updateStyle) { - // styleNames[index] = name->text(); - // qDebug() << name->text(); - // // 鍦ㄦ澶勪慨鏀规柊鏍峰紡淇℃伅鑷冲唴瀛 - // emit requireRefreshPreview(); - // emit requireSelfRefresh(); - // } - // }); - // connect(deleteButton, &QPushButton::clicked, [styleList,item,this]() { - // styleList->removeItemWidget(item); - // delete item; - // // 鍒犻櫎layer瀵瑰簲鏍峰紡 - // emit requireRefreshPreview(); - // emit requireSelfRefresh(); - // }); - // layout->addWidget(name); - // layout->addWidget(detailButton); - // layout->addWidget(deleteButton); - // w->setLayout(layout); - // styleList->addItem(item); - // styleList->setItemWidget(item, w); - // }; - // for (int i = 0; i < styleNames.size(); i++) - // createStyleItem(i); - /*if (leafP->styles.empty()) - { - leafP->styles.push_back(std::shared_ptr(new StrokeElementLayerStyle())); - }*/ auto* styles = &leafP->styles; for (auto styleIterator = styles->begin(); styleIterator != styles->end(); ++styleIterator) { @@ -200,7 +146,7 @@ void InfoDisplayWidget::generateLayerForm() [this, styles, styleIterator] { auto* dialog = - new LayerStyleDialog(*styles, styleIterator->second); + new LayerStyleDialog(*styles, styleIterator->second, this); dialog->exec(); if (dialog->layerStyle) @@ -212,7 +158,6 @@ void InfoDisplayWidget::generateLayerForm() emit requireSelfRefresh(); emit requireRefreshElementWidget(); } - dialog->deleteLater(); }); connect(removeButton, &QPushButton::clicked, this, From 12b62bf0397ec862710099d6a18d81d7e9726a5b Mon Sep 17 00:00:00 2001 From: wuyize Date: Sun, 19 Mar 2023 12:17:35 +0800 Subject: [PATCH 2/4] =?UTF-8?q?Fix:=20element.comp=E5=A1=AB=E5=85=85?= =?UTF-8?q?=E6=A0=B7=E5=BC=8F=E6=B8=B2=E6=9F=93=E9=94=99=E8=AF=AF=20|#13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../res/Shaders/element.comp | 6 +- UnitTest/ElementRendererTest.cpp | 79 ++++++++++++++++--- UnitTest/ElementRendererTest.h | 16 ++-- 3 files changed, 77 insertions(+), 24 deletions(-) diff --git a/ArchitectureColoredPainting/res/Shaders/element.comp b/ArchitectureColoredPainting/res/Shaders/element.comp index b44b514..fd09af8 100644 --- a/ArchitectureColoredPainting/res/Shaders/element.comp +++ b/ArchitectureColoredPainting/res/Shaders/element.comp @@ -1071,15 +1071,15 @@ void main() if (num_its % 2 == 1) { hitElement = true; - elementColor = vec4(1, 1, 0, 0); vec4 head = unpackUnorm4x8(floatBitsToUint(style[styleIndex])); if (head.z == 0) { - elementColor = vec4(unpackUnorm4x8(floatBitsToUint(style[++styleIndex])).rgb, 1); + elementColor = vec4(unpackUnorm4x8(floatBitsToUint(style[styleIndex+1])).rgb, 1); } + } - + styleIndex += 2; } else // Stroke { diff --git a/UnitTest/ElementRendererTest.cpp b/UnitTest/ElementRendererTest.cpp index ca1fbca..aba0474 100644 --- a/UnitTest/ElementRendererTest.cpp +++ b/UnitTest/ElementRendererTest.cpp @@ -3,7 +3,7 @@ #include #include "ElementRendererTest.h" #include "Renderer/Painting/ElementStyle.h" - +#include "Renderer/Painting/MaterialStyleFill.h" using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Renderer; @@ -12,13 +12,63 @@ namespace UnitTest { void messageHandler(QtMsgType type, const QMessageLogContext& context, const QString& msg); + TEST_CLASS(ElementRendererFillTest) + { + private: + char* argv[1]; + int argc; + QPainterPath path; + public: + ElementRendererFillTest() :argv{ (char*)"" }, argc(1) + { + QQuickSvgParser::parsePathDataFast("M100,100C-.5,100,0,100.5,0,0L40,.07C40,59.5,39.5,60,100,60Z", + path); + QTransform transform; + //transform.scale(10, 10); + path = transform.map(path); + } + + TEST_METHOD_INITIALIZE(initialize) + { + qInstallMessageHandler(messageHandler); + QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps); + QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); + } + TEST_METHOD(TestFillPlain) + { + QApplication a(argc, argv); + class Style : public Renderer::ElementStyle + { + virtual std::vector toBaseStyles() const override + { + + return { BaseStyle(std::make_shared(), + std::make_shared( + std::make_shared(Material(QColor(255,255,0))))) }; + } + } style; + TestGLWidget w(style, path); + w.show(); + a.exec(); + } + + }; + TEST_CLASS(ElementRendererStokeTypeTest) { private: char* argv[1]; int argc; + QPainterPath path; public: - ElementRendererStokeTypeTest() :argv{ (char*)"" }, argc(1) {} + ElementRendererStokeTypeTest() :argv{ (char*)"" }, argc(1) + { + QQuickSvgParser::parsePathDataFast("M292.82,107.78s0,0,0,0,0,3.59,0,7.62c0,3.85,0,5.78.06,6.43a19.94,19.94,0,0,0,2.87,7.58,15.85,15.85,0,0,0,6.61,6.23A14.75,14.75,0,0,0,310,137a11.69,11.69,0,0,0,7.59-2.92,11,11,0,0,0,3.2-6.84c.15-1.27.58-4.84-1.79-7.64a8.54,8.54,0,0,0-3.56-2.44c-1.32-.52-3.32-1.31-5.06-.33a5.41,5.41,0,0,0-2.14,3,3.48,3.48,0,0,0-.16,2.71c.78,1.86,3.36,2.14,3.47,2.15", path); + QTransform transform; + transform.scale(10, 10); + path = transform.map(path); + } TEST_METHOD_INITIALIZE(initialize) { @@ -44,7 +94,7 @@ namespace UnitTest std::make_shared(materialMap, false))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } @@ -65,7 +115,7 @@ namespace UnitTest std::make_shared(materialMap, false))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } @@ -86,7 +136,7 @@ namespace UnitTest std::make_shared(materialMap, false))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } @@ -107,7 +157,7 @@ namespace UnitTest std::make_shared(materialMap, false))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } @@ -128,7 +178,7 @@ namespace UnitTest std::make_shared(materialMap, false))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } @@ -139,8 +189,15 @@ namespace UnitTest private: char* argv[1]; int argc; + QPainterPath path; public: - ElementRendererStokeMaterialTest() :argv{ (char*)"" }, argc(1) {} + ElementRendererStokeMaterialTest() :argv{ (char*)"" }, argc(1) + { + QQuickSvgParser::parsePathDataFast("M292.82,107.78s0,0,0,0,0,3.59,0,7.62c0,3.85,0,5.78.06,6.43a19.94,19.94,0,0,0,2.87,7.58,15.85,15.85,0,0,0,6.61,6.23A14.75,14.75,0,0,0,310,137a11.69,11.69,0,0,0,7.59-2.92,11,11,0,0,0,3.2-6.84c.15-1.27.58-4.84-1.79-7.64a8.54,8.54,0,0,0-3.56-2.44c-1.32-.52-3.32-1.31-5.06-.33a5.41,5.41,0,0,0-2.14,3,3.48,3.48,0,0,0-.16,2.71c.78,1.86,3.36,2.14,3.47,2.15", path); + QTransform transform; + transform.scale(10, 10); + path = transform.map(path); + } TEST_METHOD(TestStrokePlain) { QApplication a(argc, argv); @@ -153,7 +210,7 @@ namespace UnitTest std::make_shared(QColor(255,255,255),1,1))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } @@ -174,7 +231,7 @@ namespace UnitTest std::make_shared(materialMap, false))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } @@ -195,7 +252,7 @@ namespace UnitTest std::make_shared(materialMap, true))) }; } } style; - TestGLWidget w(style); + TestGLWidget w(style, path); w.show(); a.exec(); } diff --git a/UnitTest/ElementRendererTest.h b/UnitTest/ElementRendererTest.h index 9271821..d709bfe 100644 --- a/UnitTest/ElementRendererTest.h +++ b/UnitTest/ElementRendererTest.h @@ -16,29 +16,25 @@ namespace UnitTest private: Renderer::ElementRenderer& renderer; Renderer::ElementStyle& style; + QPainterPath& path; public: - TestGLWidget(Renderer::ElementStyle& style, QWidget* parent = nullptr) - : QOpenGLWidget(parent), renderer(*Renderer::ElementRenderer::instance()), style(style) {}; + TestGLWidget(Renderer::ElementStyle& style, QPainterPath& path, QWidget* parent = nullptr) + : QOpenGLWidget(parent), renderer(*Renderer::ElementRenderer::instance()), style(style), path(path) {}; void initializeGL() override { initializeOpenGLFunctions(); }; void paintGL() override { - glClearColor(219/255., 78/255., 32/255., 1.0); + glClearColor(219 / 255., 78 / 255., 32 / 255., 1.0); glClear(GL_COLOR_BUFFER_BIT); - QPainterPath path; - QQuickSvgParser::parsePathDataFast("M292.82,107.78s0,0,0,0,0,3.59,0,7.62c0,3.85,0,5.78.06,6.43a19.94,19.94,0,0,0,2.87,7.58,15.85,15.85,0,0,0,6.61,6.23A14.75,14.75,0,0,0,310,137a11.69,11.69,0,0,0,7.59-2.92,11,11,0,0,0,3.2-6.84c.15-1.27.58-4.84-1.79-7.64a8.54,8.54,0,0,0-3.56-2.44c-1.32-.52-3.32-1.31-5.06-.33a5.41,5.41,0,0,0-2.14,3,3.48,3.48,0,0,0-.16,2.71c.78,1.86,3.36,2.14,3.47,2.15", path); - path = PainterPathUtil::monotonization(path); - QTransform transform; - transform.scale(10, 10); - path = transform.map(path); + float pixelRatio = devicePixelRatioF(); auto [img, pos] = renderer.drawElement(path, style, pixelRatio); QPainter painter(this); painter.setRenderHint(QPainter::SmoothPixmapTransform); painter.setRenderHint(QPainter::HighQualityAntialiasing); - painter.drawImage(QRectF(QPointF(0, 0), img.size()/pixelRatio), img); + painter.drawImage(QRectF(QPointF(0, 0), img.size() / pixelRatio), img); }; void resizeGL(int w, int h) override {}; }; From 4c95d6e36251bd664bd3ddbd62bb79936dfe8daf Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Sun, 19 Mar 2023 13:29:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=BE=AE=E8=B0=83=E4=BA=86=E7=AD=9B?= =?UTF-8?q?=E9=80=89=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ArchitectureColoredPainting.vcxproj | 1 + ...rchitectureColoredPainting.vcxproj.filters | 113 ++++++++++-------- 2 files changed, 65 insertions(+), 49 deletions(-) diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj index 948b53b..c403fad 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj @@ -202,6 +202,7 @@ + diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters index c738e52..5610888 100644 --- a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters @@ -71,6 +71,18 @@ {b9732a33-aa2e-4f8d-886f-1b1730c66519} + + {8d846557-8fd5-47d5-8edf-eb3eb77c226b} + + + {22d7f3ef-8185-476e-8fe1-aea24c4faacc} + + + {6fc32493-d5a2-44c3-a283-d2d3181330fb} + + + {e6de889e-8313-4846-8bdf-125b766eef59} + @@ -141,18 +153,6 @@ Source Files\Renderer\Painting - - Source Files\Editor - - - Source Files\Editor - - - Source Files\Editor - - - Source Files\Editor - Source Files\Editor @@ -201,12 +201,6 @@ Source Files\Editor - - Source Files - - - Source Files\Editor - Source Files @@ -225,23 +219,41 @@ Source Files\Editor - - Source Files\Editor - - - Source Files\Editor - Source Files\Editor Source Files\Editor + + Source Files\Editor + - Source Files + Source Files\Editor - Source Files + Source Files\Editor\Style + + + Source Files\Editor\Element + + + Source Files\Editor\Element + + + Source Files\Editor\Element + + + Source Files\Editor\Layer + + + Source Files\Editor\Layer + + + Source Files\Editor\Layer + + + Source Files\Editor\Layer @@ -257,9 +269,6 @@ Header Files\Editor - - Header Files\Editor - Header Files @@ -272,15 +281,9 @@ Header Files\Editor - - Header Files\Editor - Header Files\Editor - - Header Files\Editor - Header Files\Editor @@ -291,10 +294,19 @@ Header Files\Editor - Header Files + Header Files\Editor - Header Files + Header Files\Editor\Style + + + Header Files\Editor\Element + + + Header Files\Editor\Layer + + + Header Files\Editor\Layer @@ -426,15 +438,6 @@ Header Files\Renderer\Painting - - Header Files\Editor - - - Header Files\Editor - - - Header Files\Editor - Header Files\Renderer\Painting @@ -474,9 +477,6 @@ Header Files\Renderer\Preview - - Header Files - Header Files\Renderer @@ -498,6 +498,21 @@ Header Files\Editor\util + + Header Files\Editor\Element + + + Header Files\Editor\Element + + + Header Files\Editor\Layer + + + Header Files\Editor + + + Header Files\Editor\Layer + From 66cde802ecfbd64b5c919667048c930d3326e7b3 Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Sun, 19 Mar 2023 14:12:44 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=B8=BAGraphicElement=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E4=BA=86isClosed=E6=8E=A5=E5=8F=A3=20|=20#11?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Editor/GraphicElement.cpp | 15 ++++++++++++++- .../src/Editor/GraphicElement.h | 5 +++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp index 5c0acb0..4cc2ef3 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.cpp @@ -17,7 +17,10 @@ void SimpleElement::loadSvgFile(const QString& filePath) // TODO 样式问题 SvgFileLoader loader; loader.loadSvgFile(filePath, painterPath); - qDebug() << "load svg file success " << painterPath.elementCount(); + auto startPoint = static_cast(painterPath.elementAt(0)); + auto endPoint = static_cast(painterPath.elementAt(painterPath.elementCount() - 1)); + this->closed = startPoint == endPoint; + qDebug() << "load svg file success " << painterPath.elementCount() << (isClosed() ? "is" : "not") << "closed"; } SimpleElement::SimpleElement(QJsonObject jsonSource) : jsonSource(jsonSource) @@ -117,11 +120,21 @@ void SimpleElement::paint(QPainter* painter, QTransform transform, const LayerSt painter->restore(); } +bool SimpleElement::isClosed() const +{ + return closed; +} + void GroupElement::paint(QPainter* painter, QTransform transform, const LayerStyleContainer& styles) { sourceLayer->paint(painter, transform); } +bool GroupElement::isClosed() const +{ + return false; +} + QPixmap SimpleElement::getPreview(QSize size) { diff --git a/ArchitectureColoredPainting/src/Editor/GraphicElement.h b/ArchitectureColoredPainting/src/Editor/GraphicElement.h index d0500b2..377b9e5 100644 --- a/ArchitectureColoredPainting/src/Editor/GraphicElement.h +++ b/ArchitectureColoredPainting/src/Editor/GraphicElement.h @@ -28,11 +28,14 @@ public: virtual PixelPath getPaintObject() const = 0; virtual PixelPath getPaintObject(const LayerStyleContainer& styles) const = 0; virtual void paint(QPainter* painter, QTransform transform, const LayerStyleContainer& styles) = 0; + virtual bool isClosed() const = 0; virtual QPixmap getPreview(QSize size) = 0; }; class SimpleElement : public GraphicElement { +private: + bool closed; public: QJsonObject jsonSource; // TODO: 改为ComposedPainterPath @@ -48,6 +51,7 @@ public: PixelPath getPaintObject() const override; PixelPath getPaintObject(const LayerStyleContainer& styles) const override; void paint(QPainter* painter, QTransform transform, const LayerStyleContainer& styles) override; + bool isClosed() const override; QPixmap getPreview(QSize size) override; }; @@ -65,6 +69,7 @@ public: PixelPath getPaintObject(const LayerStyleContainer& styles) const override; void setSourceLayer(FolderLayerWrapper* sourceLayer); void paint(QPainter* painter, QTransform transform, const LayerStyleContainer& styles) override; + bool isClosed() const override; QPixmap getPreview(QSize size) override; void collectReachable(std::set& set) const; };