From 2f45cfd06a44465e9b5d612c475c33e0d40ce451 Mon Sep 17 00:00:00 2001 From: Lioncache Date: Tue, 16 Dec 2025 22:39:03 -0500 Subject: [PATCH] CDeleteLinksCommand: Move struct to internal implementation Allows ease of extending, etc without cascading rebuilds. --- src/Editor/Undo/CDeleteLinksCommand.cpp | 60 ++++++++++++++++--------- src/Editor/Undo/CDeleteLinksCommand.h | 16 +++---- 2 files changed, 43 insertions(+), 33 deletions(-) diff --git a/src/Editor/Undo/CDeleteLinksCommand.cpp b/src/Editor/Undo/CDeleteLinksCommand.cpp index 30940bf8..86466a1b 100644 --- a/src/Editor/Undo/CDeleteLinksCommand.cpp +++ b/src/Editor/Undo/CDeleteLinksCommand.cpp @@ -5,6 +5,18 @@ #include +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& rkIndices) : IUndoCommand(QCoreApplication::translate("CDeleteLinksCommand", "Delete Links")) , mpEditor(pEditor) @@ -15,14 +27,15 @@ CDeleteLinksCommand::CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *p { const CLink *pLink = pObject->Link(Type, index); - SDeletedLink DelLink; - DelLink.State = pLink->State(); - DelLink.Message = pLink->Message(); - DelLink.pSender = pLink->Sender(); - DelLink.pReceiver = pLink->Receiver(); - DelLink.SenderIndex = pLink->SenderIndex(); - DelLink.ReceiverIndex = pLink->ReceiverIndex(); - mLinks.push_back(DelLink); + mLinks.push_back({ + .State = pLink->State(), + .Message = pLink->Message(), + .pSender = pLink->Sender(), + .pReceiver = pLink->Receiver(), + .SenderIndex = pLink->SenderIndex(), + .ReceiverIndex = pLink->ReceiverIndex(), + }); + const auto& DelLink = mLinks.back(); if (Type == ELinkType::Outgoing) { @@ -37,38 +50,41 @@ CDeleteLinksCommand::CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *p } } +CDeleteLinksCommand::~CDeleteLinksCommand() = default; + void CDeleteLinksCommand::undo() { struct SNewLink { SDeletedLink *pDelLink; CLink *pLink; - }; - QList 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 NewLinks; for (SDeletedLink& rDelLink : mLinks) { - SNewLink Link; - Link.pDelLink = &rDelLink; - Link.pLink = new CLink(mpEditor->ActiveArea(), rDelLink.State, rDelLink.Message, rDelLink.pSender.InstanceID(), rDelLink.pReceiver.InstanceID()); - NewLinks.push_back(Link); + NewLinks.push_back({ + .pDelLink = &rDelLink, + .pLink = new CLink(mpEditor->ActiveArea(), rDelLink.State, rDelLink.Message, rDelLink.pSender.InstanceID(), rDelLink.pReceiver.InstanceID()), + }); } // Add to senders - std::sort(NewLinks.begin(), NewLinks.end(), [](const SNewLink& rLinkA, const SNewLink& rLinkB) { - return rLinkA.pDelLink->SenderIndex < rLinkB.pDelLink->SenderIndex; - }); - + std::sort(NewLinks.begin(), NewLinks.end(), &SNewLink::SenderIndexSorter); for (SNewLink& rNew : NewLinks) { rNew.pDelLink->pSender->AddLink(ELinkType::Outgoing, rNew.pLink, rNew.pDelLink->SenderIndex); } // Add to receivers - std::sort(NewLinks.begin(), NewLinks.end(), [](const SNewLink& rLinkA, const SNewLink& rLinkB) { - return rLinkA.pDelLink->ReceiverIndex < rLinkB.pDelLink->ReceiverIndex; - }); - + std::sort(NewLinks.begin(), NewLinks.end(), &SNewLink::ReceiverIndexSorter); for (SNewLink& rNew : NewLinks) { rNew.pDelLink->pReceiver->AddLink(ELinkType::Incoming, rNew.pLink, rNew.pDelLink->ReceiverIndex); diff --git a/src/Editor/Undo/CDeleteLinksCommand.h b/src/Editor/Undo/CDeleteLinksCommand.h index 1d58c5d9..69b14392 100644 --- a/src/Editor/Undo/CDeleteLinksCommand.h +++ b/src/Editor/Undo/CDeleteLinksCommand.h @@ -12,20 +12,14 @@ class CDeleteLinksCommand : public IUndoCommand CWorldEditor *mpEditor = nullptr; CInstancePtrList mAffectedInstances; - struct SDeletedLink - { - uint32 State; - uint32 Message; - CInstancePtr pSender; - CInstancePtr pReceiver; - uint32 SenderIndex; - uint32 ReceiverIndex; - }; + struct SDeletedLink; QList mLinks; public: - CDeleteLinksCommand() = default; - CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *pObject, ELinkType Type, const QList& rkIndices); + CDeleteLinksCommand(); + CDeleteLinksCommand(CWorldEditor *pEditor, CScriptObject *pObject, ELinkType Type, const QList& rkIndices); + ~CDeleteLinksCommand() override; + void undo() override; void redo() override; bool AffectsCleanState() const override { return true; }