diff --git a/apps/opencs/model/world/commands.cpp b/apps/opencs/model/world/commands.cpp index f8c72a3909..602d32f0f0 100644 --- a/apps/opencs/model/world/commands.cpp +++ b/apps/opencs/model/world/commands.cpp @@ -8,9 +8,11 @@ #include #include +#include "idcollection.hpp" #include "idtable.hpp" #include "idtree.hpp" #include "nestedtablewrapper.hpp" +#include "pathgrid.hpp" CSMWorld::ModifyCommand::ModifyCommand (QAbstractItemModel& model, const QModelIndex& index, const QVariant& new_, QUndoCommand* parent) @@ -235,6 +237,35 @@ void CSMWorld::CloneCommand::undo() mModel.removeRow (mModel.getModelIndex (mId, 0).row()); } +CSMWorld::CreatePathgridCommand::CreatePathgridCommand(IdTable& model, const std::string& id, QUndoCommand *parent) + : CreateCommand(model, id, parent) +{ + setType(UniversalId::Type_Pathgrid); +} + +void CSMWorld::CreatePathgridCommand::redo() +{ + CreateCommand::redo(); + + Record record = static_cast& >(mModel.getRecord(mId)); + record.get().blank(); + record.get().mCell = mId; + + if (!mId.empty() && mId[0]=='#') + { + int x, y; + char ignore; + + std::istringstream stream (mId); + if (stream >> ignore >> x >> y) + { + record.get().mData.mX = x; + record.get().mData.mY = y; + } + } + + mModel.setRecord(mId, record, mType); +} CSMWorld::UpdateCellCommand::UpdateCellCommand (IdTable& model, int row, QUndoCommand *parent) : QUndoCommand (parent), mModel (model), mRow (row) diff --git a/apps/opencs/model/world/commands.hpp b/apps/opencs/model/world/commands.hpp index 23ffccbd7e..b54a1d5acb 100644 --- a/apps/opencs/model/world/commands.hpp +++ b/apps/opencs/model/world/commands.hpp @@ -153,6 +153,15 @@ namespace CSMWorld virtual void undo(); }; + class CreatePathgridCommand : public CreateCommand + { + public: + + CreatePathgridCommand(IdTable& model, const std::string& id, QUndoCommand *parent = 0); + + virtual void redo(); + }; + /// \brief Update cell ID according to x/y-coordinates /// /// \note The new value will be calculated in the first call to redo instead of the diff --git a/apps/opencs/view/render/pathgrid.cpp b/apps/opencs/view/render/pathgrid.cpp index 7c6c8eb9e7..368fde5ee8 100644 --- a/apps/opencs/view/render/pathgrid.cpp +++ b/apps/opencs/view/render/pathgrid.cpp @@ -241,6 +241,10 @@ namespace CSVRender void Pathgrid::applyPoint(CSMWorld::CommandMacro& commands, const osg::Vec3d& worldPos) { + + CSMWorld::IdTree* model = dynamic_cast(mData.getTableModel( + CSMWorld::UniversalId::Type_Pathgrids)); + const CSMWorld::Pathgrid* source = getPathgridSource(); if (source) { @@ -250,9 +254,6 @@ namespace CSVRender int posY = clampToCell(static_cast(localCoords.y())); int posZ = clampToCell(static_cast(localCoords.z())); - CSMWorld::IdTree* model = dynamic_cast(mData.getTableModel( - CSMWorld::UniversalId::Type_Pathgrids)); - int recordIndex = mPathgridCollection.getIndex (mId); int parentColumn = mPathgridCollection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridPoints); @@ -269,14 +270,15 @@ namespace CSVRender int row = static_cast(source->mPoints.size()); // Add node - commands.push (new CSMWorld::AddNestedCommand(*model, mId, row, parentColumn)); - commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, posXColumn, parent), posX)); - commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, posYColumn, parent), posY)); - commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, posZColumn, parent), posZ)); + commands.push(new CSMWorld::AddNestedCommand(*model, mId, row, parentColumn)); + commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posXColumn, parent), posX)); + commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posYColumn, parent), posY)); + commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posZColumn, parent), posZ)); } else { - // Create pathgrid TODO + CSMWorld::CreatePathgridCommand* createCmd = new CSMWorld::CreatePathgridCommand(*model, mId); + commands.push(createCmd); } }