CDeleteLinksCommand: Move struct to internal implementation

Allows ease of extending, etc without cascading rebuilds.
This commit is contained in:
Lioncache
2025-12-16 22:39:03 -05:00
parent 8e3ab79ca0
commit 2f45cfd06a
2 changed files with 43 additions and 33 deletions

View File

@@ -5,6 +5,18 @@
#include <QCoreApplication> #include <QCoreApplication>
struct CDeleteLinksCommand::SDeletedLink
{
uint32 State;
uint32 Message;
CInstancePtr pSender;
CInstancePtr pReceiver;
uint32 SenderIndex;
uint32 ReceiverIndex;
};
CDeleteLinksCommand::CDeleteLinksCommand() = default;
CDeleteLinksCommand::CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *pObject, ELinkType Type, const QList<uint32>& rkIndices) CDeleteLinksCommand::CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *pObject, ELinkType Type, const QList<uint32>& rkIndices)
: IUndoCommand(QCoreApplication::translate("CDeleteLinksCommand", "Delete Links")) : IUndoCommand(QCoreApplication::translate("CDeleteLinksCommand", "Delete Links"))
, mpEditor(pEditor) , mpEditor(pEditor)
@@ -15,14 +27,15 @@ CDeleteLinksCommand::CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *p
{ {
const CLink *pLink = pObject->Link(Type, index); const CLink *pLink = pObject->Link(Type, index);
SDeletedLink DelLink; mLinks.push_back({
DelLink.State = pLink->State(); .State = pLink->State(),
DelLink.Message = pLink->Message(); .Message = pLink->Message(),
DelLink.pSender = pLink->Sender(); .pSender = pLink->Sender(),
DelLink.pReceiver = pLink->Receiver(); .pReceiver = pLink->Receiver(),
DelLink.SenderIndex = pLink->SenderIndex(); .SenderIndex = pLink->SenderIndex(),
DelLink.ReceiverIndex = pLink->ReceiverIndex(); .ReceiverIndex = pLink->ReceiverIndex(),
mLinks.push_back(DelLink); });
const auto& DelLink = mLinks.back();
if (Type == ELinkType::Outgoing) if (Type == ELinkType::Outgoing)
{ {
@@ -37,38 +50,41 @@ CDeleteLinksCommand::CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *p
} }
} }
CDeleteLinksCommand::~CDeleteLinksCommand() = default;
void CDeleteLinksCommand::undo() void CDeleteLinksCommand::undo()
{ {
struct SNewLink struct SNewLink
{ {
SDeletedLink *pDelLink; SDeletedLink *pDelLink;
CLink *pLink; CLink *pLink;
};
QList<SNewLink> NewLinks;
static bool SenderIndexSorter(const SNewLink& l, const SNewLink& r) {
return l.pDelLink->SenderIndex < r.pDelLink->SenderIndex;
}
static bool ReceiverIndexSorter(const SNewLink& l, const SNewLink& r) {
return l.pDelLink->ReceiverIndex < r.pDelLink->ReceiverIndex;
}
};
QList<SNewLink> NewLinks;
for (SDeletedLink& rDelLink : mLinks) for (SDeletedLink& rDelLink : mLinks)
{ {
SNewLink Link; NewLinks.push_back({
Link.pDelLink = &rDelLink; .pDelLink = &rDelLink,
Link.pLink = new CLink(mpEditor->ActiveArea(), rDelLink.State, rDelLink.Message, rDelLink.pSender.InstanceID(), rDelLink.pReceiver.InstanceID()); .pLink = new CLink(mpEditor->ActiveArea(), rDelLink.State, rDelLink.Message, rDelLink.pSender.InstanceID(), rDelLink.pReceiver.InstanceID()),
NewLinks.push_back(Link); });
} }
// Add to senders // Add to senders
std::sort(NewLinks.begin(), NewLinks.end(), [](const SNewLink& rLinkA, const SNewLink& rLinkB) { std::sort(NewLinks.begin(), NewLinks.end(), &SNewLink::SenderIndexSorter);
return rLinkA.pDelLink->SenderIndex < rLinkB.pDelLink->SenderIndex;
});
for (SNewLink& rNew : NewLinks) for (SNewLink& rNew : NewLinks)
{ {
rNew.pDelLink->pSender->AddLink(ELinkType::Outgoing, rNew.pLink, rNew.pDelLink->SenderIndex); rNew.pDelLink->pSender->AddLink(ELinkType::Outgoing, rNew.pLink, rNew.pDelLink->SenderIndex);
} }
// Add to receivers // Add to receivers
std::sort(NewLinks.begin(), NewLinks.end(), [](const SNewLink& rLinkA, const SNewLink& rLinkB) { std::sort(NewLinks.begin(), NewLinks.end(), &SNewLink::ReceiverIndexSorter);
return rLinkA.pDelLink->ReceiverIndex < rLinkB.pDelLink->ReceiverIndex;
});
for (SNewLink& rNew : NewLinks) for (SNewLink& rNew : NewLinks)
{ {
rNew.pDelLink->pReceiver->AddLink(ELinkType::Incoming, rNew.pLink, rNew.pDelLink->ReceiverIndex); rNew.pDelLink->pReceiver->AddLink(ELinkType::Incoming, rNew.pLink, rNew.pDelLink->ReceiverIndex);

View File

@@ -12,20 +12,14 @@ class CDeleteLinksCommand : public IUndoCommand
CWorldEditor *mpEditor = nullptr; CWorldEditor *mpEditor = nullptr;
CInstancePtrList mAffectedInstances; CInstancePtrList mAffectedInstances;
struct SDeletedLink struct SDeletedLink;
{
uint32 State;
uint32 Message;
CInstancePtr pSender;
CInstancePtr pReceiver;
uint32 SenderIndex;
uint32 ReceiverIndex;
};
QList<SDeletedLink> mLinks; QList<SDeletedLink> mLinks;
public: public:
CDeleteLinksCommand() = default; CDeleteLinksCommand();
CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *pObject, ELinkType Type, const QList<uint32>& rkIndices); CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *pObject, ELinkType Type, const QList<uint32_t>& rkIndices);
~CDeleteLinksCommand() override;
void undo() override; void undo() override;
void redo() override; void redo() override;
bool AffectsCleanState() const override { return true; } bool AffectsCleanState() const override { return true; }