增加了Layer部分功能接口

dev-VirtualTexture
白封羽 2022-12-26 21:24:44 +08:00
parent d4e52d4ab8
commit fe6e10c882
4 changed files with 100 additions and 2 deletions

View File

@ -11,3 +11,49 @@ void LayerManager::paint(QPainter *painter) const
{ {
painter->drawPath(root->getCache()); painter->drawPath(root->getCache());
} }
bool LayerManager::singleSelectedCheck() const
{
if (selectedLayers.size() != 1)
return false;
return selectedLayers[0] != nullptr;
}
bool LayerManager::multipleSelectedCheck() const
{
if (selectedLayers.size() <= 1)
return false;
for (auto &layer : selectedLayers)
if (layer == nullptr)
return false;
return true;
}
bool LayerManager::rename(QString newName) const
{
if (!singleSelectedCheck())
return false;
auto &currenLayer = selectedLayers[0];
currenLayer->property.name = newName;
return true;
}
bool LayerManager::combine() const
{
if (!multipleSelectedCheck())
return false;
auto prevCommonFather = selectedLayers[0]->getParent();
for (auto &layer : selectedLayers)
if (layer->getParent() != prevCommonFather)
return false;
auto newCommonFather = new FolderLayerWrapper();
newCommonFather->setParent(prevCommonFather);
for (auto &layer : selectedLayers)
layer->setParent(newCommonFather);
return true;
}
bool LayerManager::changeParent(FolderLayerWrapper *newParent) const
{
if (!singleSelectedCheck())
return false;
if (selectedLayers[0] == root)
return false;
selectedLayers[0]->setParent(newParent);
return true;
}

View File

@ -9,6 +9,8 @@ using std::pair;
using std::vector; using std::vector;
class ElementManager; class ElementManager;
class LayerWrapper; class LayerWrapper;
class FolderLayerWrapper;
class LeafLayerWrapper;
class LayerManager class LayerManager
{ {
@ -17,8 +19,16 @@ class LayerManager
LayerWrapper *root; LayerWrapper *root;
LayerPtrs selectedLayers; LayerPtrs selectedLayers;
LayerPtrs involvedLeafLayersCache; LayerPtrs involvedLeafLayersCache;
bool singleSelectedCheck() const;
bool multipleSelectedCheck() const;
public: public:
LayerManager(QJsonObject source, ElementManager *elementManager); LayerManager(QJsonObject source, ElementManager *elementManager);
void paint(QPainter *painter) const; void paint(QPainter *painter) const;
bool rename(QString newName) const;
bool combine() const;
// bool seperate() const;
// bool erase() const;
// bool makeReference() const;
bool changeParent(FolderLayerWrapper *newParent) const;
}; };

View File

@ -18,6 +18,11 @@ void FolderLayerWrapper::removeChild(LayerWrapper *child)
} }
} }
LayerWrapper *LayerWrapper::getParent() const
{
return this == nullptr ? nullptr : this->parent.get();
}
QPainterPath LayerWrapper::getCache() QPainterPath LayerWrapper::getCache()
{ {
this->refresh(); this->refresh();
@ -26,6 +31,9 @@ QPainterPath LayerWrapper::getCache()
// TODO: undone // TODO: undone
LayerWrapper::LayerWrapper(QJsonObject json, LayerWrapper *parent) LayerWrapper::LayerWrapper(QJsonObject json, LayerWrapper *parent)
{ {
this->parent = shared_ptr<LayerWrapper>(parent);
auto offsetJson = json.value("offset").toObject();
property.offset = {offsetJson.value("x").toDouble(), offsetJson.value("y").toDouble()};
} }
FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent) FolderLayerWrapper::FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent)
@ -58,9 +66,18 @@ LeafLayerWrapper::LeafLayerWrapper(QJsonObject json, ElementManager *elementMana
int elementIndex = json.value("element").toInt(); int elementIndex = json.value("element").toInt();
wrappedElement = elementManager->getElementById(elementIndex); wrappedElement = elementManager->getElementById(elementIndex);
} }
void LayerWrapper::SimpleProperty::apply(QPainterPath &cache) const
{
QTransform trans;
trans.scale(zoom.x(), zoom.y());
trans.rotate(rotation);
trans.translate(offset.x(), offset.y());
cache = trans.map(cache);
// cache.translate(offset);
}
void LayerWrapper::refresh() void LayerWrapper::refresh()
{ {
property.apply(cache);
} }
void FolderLayerWrapper::refresh() void FolderLayerWrapper::refresh()
@ -68,6 +85,7 @@ void FolderLayerWrapper::refresh()
cache.clear(); cache.clear();
for (auto &child : children) for (auto &child : children)
cache.addPath(child.get()->getCache()); cache.addPath(child.get()->getCache());
LayerWrapper::refresh();
} }
void LeafLayerWrapper::refresh() void LeafLayerWrapper::refresh()
@ -75,4 +93,15 @@ void LeafLayerWrapper::refresh()
cache.clear(); cache.clear();
if (wrappedElement != nullptr) if (wrappedElement != nullptr)
cache.addPath(wrappedElement->getPaintObject()); cache.addPath(wrappedElement->getPaintObject());
LayerWrapper::refresh();
}
void LayerWrapper::setParent(LayerWrapper *newParent)
{
this->parent = shared_ptr<LayerWrapper>(newParent);
}
void FolderLayerWrapper::removeAllChild()
{
children.clear();
} }

View File

@ -26,8 +26,20 @@ class LayerWrapper
QPainterPath cache; QPainterPath cache;
public: public:
struct SimpleProperty
{
QString name = "";
QPointF zoom = {1.0, 1.0};
QPointF offset = {0, 0};
double rotation = 0;
bool flipHorizontally = 0;
bool flipVertically = 0;
void apply(QPainterPath &cache) const;
} property;
void setParent(LayerWrapper *newParent);
virtual void refresh(); virtual void refresh();
QPainterPath getCache(); // invoke by manager, then invoke parent's applyStyles QPainterPath getCache();
LayerWrapper *getParent() const; // invoke by manager, then invoke parent's applyStyles
LayerWrapper(QJsonObject json, LayerWrapper *parent); LayerWrapper(QJsonObject json, LayerWrapper *parent);
LayerWrapper() = default; LayerWrapper() = default;
}; };
@ -43,6 +55,7 @@ class FolderLayerWrapper : public LayerWrapper
FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent); FolderLayerWrapper(QJsonObject json, ElementManager *elementManager, LayerWrapper *parent);
void addChild(LayerWrapper *child); void addChild(LayerWrapper *child);
void removeChild(LayerWrapper *child); void removeChild(LayerWrapper *child);
void removeAllChild();
}; };
class LeafLayerWrapper : public LayerWrapper class LeafLayerWrapper : public LayerWrapper