#include "stdafx.h" #include "ActionLayerDragDropItem.h" #include "SigmaDoc.h" #include "SigmaView.h" #include "Util.h" #include "Visitor.h" NAction::CActionLayerDragDropItem::CActionLayerDragDropItem() : CActionItem(nullptr, 0) { } NAction::CActionLayerDragDropItem::CActionLayerDragDropItem(CSigmaDoc* ppDoc, const CLayerLocation &oldLocation, const CLayerLocation &newLocation) : CActionItem(ppDoc, ActionTypeLayerDragDrop), m_oldLocation(oldLocation), m_newLocation(newLocation) { } void NAction::CActionLayerDragDropItem::Undo() { Move(m_newLocation, m_oldLocation); } POSITION NAction::CActionLayerDragDropItem::GetFirstChildLayerPosition(CString layerName) { auto pred = [&layerName](CLayer* pLayer) { CLayerName targetLayerName(layerName); CString name = pLayer->GetName(); return targetLayerName.IsChild(name); }; return GetMatchPosition(layerName, pred); } POSITION CActionLayerDragDropItem::GetLayerPosition(CString layerName) { auto isMatchingLayer = [&layerName](CLayer* pLayer) { CString currentName = pLayer->GetName(); CLayerName targetLayerName(layerName); return targetLayerName == currentName; }; return GetMatchPosition(layerName, isMatchingLayer); } POSITION CActionLayerDragDropItem::GetMatchPosition(CString strLayer, std::function pred) { CLayerName layerName(strLayer); CLayerList* pClass = GetDoc()->GetDraw()->GetClass(layerName.GetClassName()); if (pClass == nullptr) { return nullptr; } CListForEach(pos, *pClass) { CLayer *pLayer = pClass->GetAt(pos); if (pred(pLayer)) { return pos; } } return nullptr; } void NAction::CActionLayerDragDropItem::Move(const CLayerLocation& oldLocation, const CLayerLocation& newLocation) { CString oldLayer = oldLocation.layer; CString oldParent = GetParentLayerPath(oldLayer); CString oldPrev = oldLocation.prevLayer; CString newLayer = newLocation.layer; CString newParent = GetParentLayerPath(newLayer); CString newPrev = newLocation.prevLayer; if (oldParent == newParent) // 只是进行了同层移动 { if (newPrev.IsEmpty()) { POSITION pos = GetFirstChildLayerPosition(newParent); GetDoc()->GetDraw()->MoveLayerInSameClassSeries(oldLayer, TRUE, pos, 0); } else { POSITION pos = GetLayerPosition(newPrev); GetDoc()->GetDraw()->MoveLayerInSameClassSeries(oldLayer, TRUE, pos, 1); } } else // 跨层移动 { if (newPrev.IsEmpty()) { POSITION pos = GetFirstChildLayerPosition(newParent); GetDoc()->GetDraw()->MoveLayer(oldLayer, newParent, TRUE, pos, 0); } else { POSITION pos = GetLayerPosition(newPrev); GetDoc()->GetDraw()->MoveLayer(oldLayer, newParent, TRUE, pos, 1); } } } void NAction::CActionLayerDragDropItem::Redo() { Move(m_oldLocation, m_newLocation); } void NAction::CActionLayerDragDropItem::accept(CActionVisitor& visitor) { visitor.visit(*this); }