From d41671fff5122e6df9cc41cb6776ac2faf7bc740 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Tue, 3 Feb 2026 21:09:58 -0800 Subject: [PATCH] Cleanup lcModelAction. --- common/lc_model.cpp | 2 +- common/lc_modelaction.cpp | 353 +++++++++++++++++++------------------- common/lc_modelaction.h | 19 +- 3 files changed, 187 insertions(+), 187 deletions(-) diff --git a/common/lc_model.cpp b/common/lc_model.cpp index 2519bae8..9f4f060a 100644 --- a/common/lc_model.cpp +++ b/common/lc_model.cpp @@ -1988,7 +1988,7 @@ void lcModel::EndActionSequence(const QString& Description) for (const std::unique_ptr& ModelAction : mUndoHistory.front()->ModelActions) { - if (const lcModelActionSelection* ModelActionSelection = dynamic_cast(ModelAction.get())) + if (dynamic_cast(ModelAction.get())) { gMainWindow->UpdateSelectedObjects(true); break; diff --git a/common/lc_modelaction.cpp b/common/lc_modelaction.cpp index 1cdf8627..48652481 100644 --- a/common/lc_modelaction.cpp +++ b/common/lc_modelaction.cpp @@ -6,182 +6,6 @@ #include "light.h" #include "group.h" -bool lcModelAction::SaveHistoryBuffer(QByteArray& Buffer, const lcModel* Model) -{ - QDataStream Stream(&Buffer, QIODevice::WriteOnly); - - const std::vector>& Groups = Model->GetGroups(); - const std::vector>& Pieces = Model->GetPieces(); - const std::vector>& Cameras = Model->GetCameras(); - const std::vector>& Lights = Model->GetLights(); - - uint64_t ObjectCount[4] = { Groups.size(), Pieces.size(), Cameras.size(), Lights.size() }; - - if (Stream.writeRawData(reinterpret_cast(ObjectCount), sizeof(ObjectCount)) != sizeof(ObjectCount)) - return false; - - for (size_t GroupIndex : mGroupIndices) - { - const lcGroup* Group = Groups[GroupIndex].get(); - uint64_t ParentIndex = UINT64_MAX; - - if (Group->mGroup) - for (ParentIndex = 0; ParentIndex < Groups.size(); ParentIndex++) - if (Group->mGroup == Groups[ParentIndex].get()) - break; - - Stream << Group->mName; - Stream << ParentIndex; - } - - for (size_t PieceIndex : mPieceIndices) - if (!Pieces[PieceIndex]->SaveUndoData(Stream, Model)) - return false; - - for (size_t CameraIndex : mCameraIndices) - if (!Cameras[CameraIndex]->SaveUndoData(Stream, Model)) - return false; - - for (size_t LightIndex : mLightIndices) - if (!Lights[LightIndex]->SaveUndoData(Stream, Model)) - return false; - - return true; -} - -bool lcModelAction::LoadHistoryBuffer(const QByteArray& Buffer, lcModel* Model, bool CreateObjects) const -{ - QDataStream Stream(const_cast(&Buffer), QIODevice::ReadOnly); - - const std::vector>& Groups = Model->GetGroups(); - const std::vector>& Pieces = Model->GetPieces(); - const std::vector>& Cameras = Model->GetCameras(); - const std::vector>& Lights = Model->GetLights(); - - uint64_t ObjectCount[4]; - - if (Stream.readRawData(reinterpret_cast(ObjectCount), sizeof(ObjectCount)) != sizeof(ObjectCount)) - return false; - - if (CreateObjects) - { - ObjectCount[0] -= mGroupIndices.size(); - ObjectCount[1] -= mPieceIndices.size(); - ObjectCount[2] -= mCameraIndices.size(); - ObjectCount[3] -= mLightIndices.size(); - } - - if (ObjectCount[0] != Groups.size() || ObjectCount[1] != Pieces.size() || ObjectCount[2] != Cameras.size() || ObjectCount[3] != Lights.size()) - return false; - - if (CreateObjects) - { - for (size_t GroupIndex : mGroupIndices) - { - std::unique_ptr Group(new lcGroup()); - QString Name; - uint64_t ParentIndex; - - Stream >> Name; - Stream >> ParentIndex; - - Group->mName = Name; - Group->mGroup = ParentIndex < Groups.size() ? Groups[ParentIndex].get() : nullptr; - - Model->AddGroup(std::move(Group), GroupIndex); - } - - for (size_t PieceIndex : mPieceIndices) - { - if (PieceIndex > Pieces.size()) - return false; - - std::unique_ptr Piece(new lcPiece(nullptr)); - - if (!Piece->LoadUndoData(Stream, Model)) - return false; - - Model->AddPiece(std::move(Piece), PieceIndex); - } - - for (size_t CameraIndex : mCameraIndices) - { - if (CameraIndex > Cameras.size()) - return false; - - std::unique_ptr Camera(new lcCamera(false)); - - if (!Camera->LoadUndoData(Stream, Model)) - return false; - - Model->AddCamera(std::move(Camera), CameraIndex); - } - - for (size_t LightIndex : mLightIndices) - { - if (LightIndex > Lights.size()) - return false; - - std::unique_ptr Light(new lcLight(lcVector3(0.0f, 0.0f, 0.0f), lcLightType::Point)); - - if (!Light->LoadUndoData(Stream, Model)) - return false; - - Model->AddLight(std::move(Light), LightIndex); - } - } - else - { - for (size_t GroupIndex : mGroupIndices) - { - lcGroup* Group = Groups[GroupIndex].get(); - QString Name; - uint64_t ParentIndex; - - Stream >> Name; - Stream >> ParentIndex; - - Group->mName = Name; - Group->mGroup = ParentIndex < Groups.size() ? Groups[ParentIndex].get() : nullptr; - } - - for (size_t PieceIndex : mPieceIndices) - { - if (PieceIndex >= Pieces.size()) - return false; - - const std::unique_ptr& Piece = Pieces[PieceIndex]; - - if (!Piece->LoadUndoData(Stream, Model)) - return false; - } - - for (size_t CameraIndex : mCameraIndices) - { - if (CameraIndex >= Cameras.size()) - return false; - - const std::unique_ptr& Camera = Cameras[CameraIndex]; - - if (!Camera->LoadUndoData(Stream, Model)) - return false; - } - - for (size_t LightIndex : mLightIndices) - { - if (LightIndex >= Lights.size()) - return false; - - const std::unique_ptr& Light = Lights[LightIndex]; - - if (!Light->LoadUndoData(Stream, Model)) - return false; - } - } - - return true; -} - void lcModelActionSelection::SaveStartState(const lcModel* Model) { SaveState(mStartState, Model); @@ -323,6 +147,183 @@ lcModelActionObjectEdit::lcModelActionObjectEdit(lcModelActionObjectEditMode Mod { } + +bool lcModelActionObjectEdit::SaveHistoryBuffer(QByteArray& Buffer, const lcModel* Model) +{ + QDataStream Stream(&Buffer, QIODevice::WriteOnly); + + const std::vector>& Groups = Model->GetGroups(); + const std::vector>& Pieces = Model->GetPieces(); + const std::vector>& Cameras = Model->GetCameras(); + const std::vector>& Lights = Model->GetLights(); + + uint64_t ObjectCount[4] = { Groups.size(), Pieces.size(), Cameras.size(), Lights.size() }; + + if (Stream.writeRawData(reinterpret_cast(ObjectCount), sizeof(ObjectCount)) != sizeof(ObjectCount)) + return false; + + for (size_t GroupIndex : mGroupIndices) + { + const lcGroup* Group = Groups[GroupIndex].get(); + uint64_t ParentIndex = UINT64_MAX; + + if (Group->mGroup) + for (ParentIndex = 0; ParentIndex < Groups.size(); ParentIndex++) + if (Group->mGroup == Groups[ParentIndex].get()) + break; + + Stream << Group->mName; + Stream << ParentIndex; + } + + for (size_t PieceIndex : mPieceIndices) + if (!Pieces[PieceIndex]->SaveUndoData(Stream, Model)) + return false; + + for (size_t CameraIndex : mCameraIndices) + if (!Cameras[CameraIndex]->SaveUndoData(Stream, Model)) + return false; + + for (size_t LightIndex : mLightIndices) + if (!Lights[LightIndex]->SaveUndoData(Stream, Model)) + return false; + + return true; +} + +bool lcModelActionObjectEdit::LoadHistoryBuffer(const QByteArray& Buffer, lcModel* Model, bool CreateObjects) const +{ + QDataStream Stream(const_cast(&Buffer), QIODevice::ReadOnly); + + const std::vector>& Groups = Model->GetGroups(); + const std::vector>& Pieces = Model->GetPieces(); + const std::vector>& Cameras = Model->GetCameras(); + const std::vector>& Lights = Model->GetLights(); + + uint64_t ObjectCount[4]; + + if (Stream.readRawData(reinterpret_cast(ObjectCount), sizeof(ObjectCount)) != sizeof(ObjectCount)) + return false; + + if (CreateObjects) + { + ObjectCount[0] -= mGroupIndices.size(); + ObjectCount[1] -= mPieceIndices.size(); + ObjectCount[2] -= mCameraIndices.size(); + ObjectCount[3] -= mLightIndices.size(); + } + + if (ObjectCount[0] != Groups.size() || ObjectCount[1] != Pieces.size() || ObjectCount[2] != Cameras.size() || ObjectCount[3] != Lights.size()) + return false; + + if (CreateObjects) + { + for (size_t GroupIndex : mGroupIndices) + { + std::unique_ptr Group(new lcGroup()); + QString Name; + uint64_t ParentIndex; + + Stream >> Name; + Stream >> ParentIndex; + + Group->mName = Name; + Group->mGroup = ParentIndex < Groups.size() ? Groups[ParentIndex].get() : nullptr; + + Model->AddGroup(std::move(Group), GroupIndex); + } + + for (size_t PieceIndex : mPieceIndices) + { + if (PieceIndex > Pieces.size()) + return false; + + std::unique_ptr Piece(new lcPiece(nullptr)); + + if (!Piece->LoadUndoData(Stream, Model)) + return false; + + Model->AddPiece(std::move(Piece), PieceIndex); + } + + for (size_t CameraIndex : mCameraIndices) + { + if (CameraIndex > Cameras.size()) + return false; + + std::unique_ptr Camera(new lcCamera(false)); + + if (!Camera->LoadUndoData(Stream, Model)) + return false; + + Model->AddCamera(std::move(Camera), CameraIndex); + } + + for (size_t LightIndex : mLightIndices) + { + if (LightIndex > Lights.size()) + return false; + + std::unique_ptr Light(new lcLight(lcVector3(0.0f, 0.0f, 0.0f), lcLightType::Point)); + + if (!Light->LoadUndoData(Stream, Model)) + return false; + + Model->AddLight(std::move(Light), LightIndex); + } + } + else + { + for (size_t GroupIndex : mGroupIndices) + { + lcGroup* Group = Groups[GroupIndex].get(); + QString Name; + uint64_t ParentIndex; + + Stream >> Name; + Stream >> ParentIndex; + + Group->mName = Name; + Group->mGroup = ParentIndex < Groups.size() ? Groups[ParentIndex].get() : nullptr; + } + + for (size_t PieceIndex : mPieceIndices) + { + if (PieceIndex >= Pieces.size()) + return false; + + const std::unique_ptr& Piece = Pieces[PieceIndex]; + + if (!Piece->LoadUndoData(Stream, Model)) + return false; + } + + for (size_t CameraIndex : mCameraIndices) + { + if (CameraIndex >= Cameras.size()) + return false; + + const std::unique_ptr& Camera = Cameras[CameraIndex]; + + if (!Camera->LoadUndoData(Stream, Model)) + return false; + } + + for (size_t LightIndex : mLightIndices) + { + if (LightIndex >= Lights.size()) + return false; + + const std::unique_ptr& Light = Lights[LightIndex]; + + if (!Light->LoadUndoData(Stream, Model)) + return false; + } + } + + return true; +} + bool lcModelActionObjectEdit::SaveStartState(const lcModel* Model, const lcCamera* Camera) { const std::vector>& Pieces = Model->GetPieces(); diff --git a/common/lc_modelaction.h b/common/lc_modelaction.h index e64b3f6c..755e566d 100644 --- a/common/lc_modelaction.h +++ b/common/lc_modelaction.h @@ -8,15 +8,6 @@ class lcModelAction public: lcModelAction() = default; virtual ~lcModelAction() = default; - -protected: - bool SaveHistoryBuffer(QByteArray& Buffer, const lcModel* Model); - bool LoadHistoryBuffer(const QByteArray& Buffer, lcModel* Model, bool CreateObjects) const; - - std::vector mGroupIndices; - std::vector mPieceIndices; - std::vector mCameraIndices; - std::vector mLightIndices; }; struct lcModelActionSelectionState @@ -25,7 +16,7 @@ struct lcModelActionSelectionState std::vector CameraSelection; std::vector LightSelection; size_t FocusIndex = SIZE_MAX; - uint32_t FocusSection = 0; + uint32_t FocusSection = ~0U; lcObjectType FocusObjectType = static_cast(~0); bool operator!=(const lcModelActionSelectionState& Other) const @@ -86,6 +77,14 @@ public: } protected: + bool SaveHistoryBuffer(QByteArray& Buffer, const lcModel* Model); + bool LoadHistoryBuffer(const QByteArray& Buffer, lcModel* Model, bool CreateObjects) const; + + std::vector mGroupIndices; + std::vector mPieceIndices; + std::vector mCameraIndices; + std::vector mLightIndices; + lcModelActionObjectEditMode mMode; QByteArray mStartBuffer; QByteArray mEndBuffer;