From 200dccff4657087f0cb4b5c5c64844fb44d23714 Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Thu, 16 Mar 2023 20:16:03 +0800 Subject: [PATCH] =?UTF-8?q?[stroke]=20=E5=AE=8C=E6=88=90=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E5=92=8C=E5=8F=8D=E5=BA=8F=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StrokeStyleWidget.cpp | 11 +++-- .../src/Editor/LayerStyle.cpp | 42 +++++++++++++++---- .../src/Editor/LayerStyle.h | 16 +++++-- .../src/Editor/LayerWrapper.cpp | 5 +++ 4 files changed, 57 insertions(+), 17 deletions(-) diff --git a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp index 00283ca..f504d92 100644 --- a/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp +++ b/ArchitectureColoredPainting/src/Editor/EditorWidgetComponent/StrokeStyleWidget.cpp @@ -122,7 +122,7 @@ void StrokeStyleWidget::initTable(std::shared_ptrrbegin(); newWidth = lastPair->first + 0.01; } - Renderer::Material newMaterial = { QColor::fromRgb(0,0,0), 0.f, .8f }; + Renderer::Material newMaterial(QColor::fromRgb(0, 0, 0)); (*materialMap)[newWidth] = newMaterial; int newRow = this->strokeTable->rowCount() - 1; this->strokeTable->insertRow(newRow); @@ -153,18 +153,17 @@ void StrokeStyleWidget::setTableRow(int row, float width, Renderer::Material& ma }); QTableWidgetItem* metallicItem = new QTableWidgetItem; - metallicItem->setData(Qt::EditRole, material.metallic); + metallicItem->setData(Qt::EditRole, material.metallicF()); strokeTable->setItem(row, COLUMN_METALLIC, metallicItem); QTableWidgetItem* roughnessItem = new QTableWidgetItem; - roughnessItem->setData(Qt::EditRole, material.roughness); + roughnessItem->setData(Qt::EditRole, material.roughnessF()); strokeTable->setItem(row, COLUMN_ROUGHNESS, roughnessItem); QtMaterialRaisedButton* removeButton = new QtMaterialRaisedButton("-", strokeTable); removeButton->setFixedSize(20, 20); strokeTable->setCellWidget(row, COLUMN_OPERATIONS, removeButton); connect(removeButton, &QtMaterialRaisedButton::clicked, [this, row]() { - if (this->strokeTable->rowCount() <= 1) return; radialStroke(this->stroke)->materialMap.erase(this->strokeTable->item(row, COLUMN_WIDTH)->text().toFloat()); this->strokeTable->removeRow(row); }); @@ -204,12 +203,12 @@ void StrokeStyleWidget::onCellChanged(int row, int column) } case COLUMN_METALLIC: { - radialStroke(stroke)->materialMap[changedWidth].metallic = changedItemValue; + radialStroke(stroke)->materialMap[changedWidth].setMetallicF(changedItemValue); break; } case COLUMN_ROUGHNESS: { - radialStroke(stroke)->materialMap[changedWidth].roughness = changedItemValue; + radialStroke(stroke)->materialMap[changedWidth].setRoughnessF(changedItemValue); break; } } diff --git a/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp b/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp index 02e3b73..215ec15 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerStyle.cpp @@ -51,7 +51,6 @@ QWidget* StrokeElementLayerStyle::getInputWidget() if (this->strokePair.first == nullptr) { auto materialMap = std::map(); - materialMap[1.0] = Renderer::Material{ QColor(0, 0, 0), 0.f, .8f }; this->strokePair.first = std::shared_ptr(new Renderer::MaterialStyleStroke( 15, Renderer::StrokeType::kLeftSide, Renderer::StrokeEndType::kFlat, @@ -63,7 +62,6 @@ QWidget* StrokeElementLayerStyle::getInputWidget() if (this->strokePair.second == nullptr) { auto materialMap = std::map(); - materialMap[1.0] = Renderer::Material{ QColor(0, 0, 0), 0.f, .8f }; this->strokePair.second = std::shared_ptr(new Renderer::MaterialStyleStroke( 15, Renderer::StrokeType::kRightSide, Renderer::StrokeEndType::kFlat, @@ -108,6 +106,14 @@ QWidget* StrokeElementLayerStyle::getListDisplayWidget() const return w; } +StrokeElementLayerStyle::StrokeElementLayerStyle(PMaterialStyleStroke left, PMaterialStyleStroke right) +{ + this->strokePair.first = left; + this->strokePair.second = right ? right : std::dynamic_pointer_cast( + std::shared_ptr(std::move(left->clone())) + ); +} + StrokeElementLayerStyle::StrokeElementLayerStyle(const StrokeElementLayerStyle& other) { strokePair.first = std::dynamic_pointer_cast( @@ -121,11 +127,12 @@ StrokeElementLayerStyle::StrokeElementLayerStyle(const StrokeElementLayerStyle& QJsonObject StrokeElementLayerStyle::toJson() const { + // todo: 修改打开逻辑 QJsonObject json; - json.insert("type", "stroke"); - json.insert("enableEachSideIndependent", enableEachSideIndependent); - json.insert("left", EncodeUtil::toBase64(strokePair.first->encoded())); - json.insert("right", EncodeUtil::toBase64(strokePair.second->encoded())); + json["type"] = getTypeName(); + json["enableEachSideIndependent"] = enableEachSideIndependent; + json["left"] = EncodeUtil::toBase64(strokePair.first->encoded()); + json["right"] = EncodeUtil::toBase64(strokePair.second->encoded()); return json; } @@ -184,5 +191,26 @@ std::unique_ptr FillElementLayerStyle::clone() const std::unique_ptr LayerStyle::fromJson(const QJsonObject& json) { - return std::unique_ptr(); + QString type = json["type"].toString(); + if (type == StrokeElementLayerStyle::getTypeName()) + { + auto ptr = std::make_unique( + std::dynamic_pointer_cast( + std::shared_ptr(std::move(Renderer::MaterialStyle::decoded(EncodeUtil::fromBase64(json["left"].toString())))) + ), + std::dynamic_pointer_cast( + std::shared_ptr(std::move(Renderer::MaterialStyle::decoded(EncodeUtil::fromBase64(json["right"].toString())))) + ) + ); + ptr->enableEachSideIndependent = json["enableEachSideIndependent"].toBool(); + return ptr; + } + else if (type == FillElementLayerStyle::getTypeName()) + { + return std::make_unique(); + } + else + { + return nullptr; + } } diff --git a/ArchitectureColoredPainting/src/Editor/LayerStyle.h b/ArchitectureColoredPainting/src/Editor/LayerStyle.h index 6717299..636201d 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerStyle.h +++ b/ArchitectureColoredPainting/src/Editor/LayerStyle.h @@ -13,6 +13,8 @@ using Renderer::MaterialStyle; using Renderer::MaterialStyleStroke; +#define STYLE_TYPENAME(name) static QString getTypeName() { return name; } + /** * 在进行Style的添加时,首先创建空对象,然后直接调用getInputWidget()方法 * 在进行Style的修改时,直接调用getInputWidget()方法 @@ -36,17 +38,22 @@ public: class StrokeElementLayerStyle : public LayerStyle { + using PMaterialStyleStroke = std::shared_ptr; private: - std::pair, std::shared_ptr> strokePair; + std::pair strokePair; public: + STYLE_TYPENAME("stroke") + + StrokeElementLayerStyle() = default; + StrokeElementLayerStyle(PMaterialStyleStroke left, PMaterialStyleStroke right = nullptr); + StrokeElementLayerStyle(const StrokeElementLayerStyle& other); + ~StrokeElementLayerStyle() = default; + std::vector toBaseStyles() const override; QString getStyleName() const override; QWidget* getInputWidget() override; QWidget* getListDisplayWidget() const override; - StrokeElementLayerStyle() = default; - StrokeElementLayerStyle(const StrokeElementLayerStyle& other); - ~StrokeElementLayerStyle() = default; QJsonObject toJson() const override; std::unique_ptr clone() const override; @@ -56,6 +63,7 @@ public: class FillElementLayerStyle : public LayerStyle { public: + STYLE_TYPENAME("fill") std::vector toBaseStyles() const override; QString getStyleName() const override; QWidget* getInputWidget() override; diff --git a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp index d5229e9..febc24a 100644 --- a/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp +++ b/ArchitectureColoredPainting/src/Editor/LayerWrapper.cpp @@ -79,6 +79,11 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementMana qDebug() << json.value("name").toString() << " " << this; int elementIndex = json.value("element").toInt(); wrappedElement = elementManager->getElementById(elementIndex); + QJsonArray stylesArray = json.value("styles").toArray(); + for (const auto& style : stylesArray) + { + styles.push_back(LayerStyle::fromJson(style.toObject())); + } } void LayerWrapper::SimpleProperty::apply(PixelPath&cache)