diff --git a/apps/opencs/view/render/pathgrid.cpp b/apps/opencs/view/render/pathgrid.cpp index bd25a1b28..4c3db2562 100644 --- a/apps/opencs/view/render/pathgrid.cpp +++ b/apps/opencs/view/render/pathgrid.cpp @@ -41,6 +41,7 @@ namespace CSVRender Pathgrid::Pathgrid(CSMWorld::Data& data, osg::Group* parent, const std::string& pathgridId, const CSMWorld::CellCoordinates& coordinates) : mData(data) + , mPathgridCollection(mData.getPathgrids()) , mId(pathgridId) , mCoords(coordinates) , mParent(parent) @@ -97,15 +98,13 @@ namespace CSVRender { mSelected.clear(); - const CSMWorld::SubCellCollection& pathgrids = mData.getPathgrids(); - int pathgridIndex = pathgrids.searchId(mId); - if (pathgridIndex != -1) + const CSMWorld::Pathgrid* source = getPathgridSource(); + if (source) { - const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); - for (unsigned short i = 0; i < static_cast(source.mPoints.size()); ++i) + for (unsigned short i = 0; i < static_cast(source->mPoints.size()); ++i) mSelected.push_back(i); - recreateSelectedGeometry(source); + recreateSelectedGeometry(*source); } else { @@ -130,21 +129,19 @@ namespace CSVRender void Pathgrid::invertSelected() { - NodeList temp = NodeList(mSelected.begin(), mSelected.end()); + NodeList temp = NodeList(mSelected); mSelected.clear(); - const CSMWorld::SubCellCollection& pathgrids = mData.getPathgrids(); - int pathgridIndex = pathgrids.searchId(mId); - if (pathgridIndex != -1) + const CSMWorld::Pathgrid* source = getPathgridSource(); + if (source) { - const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); - for (unsigned short i = 0; i < static_cast(source.mPoints.size()); ++i) + for (unsigned short i = 0; i < static_cast(source->mPoints.size()); ++i) { if (std::find(temp.begin(), temp.end(), i) == temp.end()) mSelected.push_back(i); } - recreateSelectedGeometry(source); + recreateSelectedGeometry(*source); } else { @@ -170,41 +167,44 @@ namespace CSVRender void Pathgrid::applyPosition(CSMWorld::CommandMacro& commands) { - const CSMWorld::SubCellCollection& pathgrids = mData.getPathgrids(); - int pathgridIndex = pathgrids.searchId(mId); - if (pathgridIndex != -1) + const CSMWorld::Pathgrid* source = getPathgridSource(); + if (source) { - const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); osg::Vec3d localCoords = mSelectedNode->getPosition(); - int oX = static_cast(localCoords.x()); - int oY = static_cast(localCoords.y()); - int oZ = static_cast(localCoords.z()); + int offsetX = static_cast(localCoords.x()); + int offsetY = static_cast(localCoords.y()); + int offsetZ = static_cast(localCoords.z()); - CSMWorld::SubCellCollection& collection = mData.getPathgrids(); - QAbstractItemModel* model = mData.getTableModel (CSMWorld::UniversalId::Type_Pathgrids); + QAbstractItemModel* model = mData.getTableModel(CSMWorld::UniversalId::Type_Pathgrids); - int recordIndex = collection.getIndex (mId); - int parentColumn = collection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridPoints); + int recordIndex = mPathgridCollection.getIndex(mId); + int parentColumn = mPathgridCollection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridPoints); + + int posXColumn = mPathgridCollection.searchNestedColumnIndex(parentColumn, + CSMWorld::Columns::ColumnId_PathgridPosX); + + int posYColumn = mPathgridCollection.searchNestedColumnIndex(parentColumn, + CSMWorld::Columns::ColumnId_PathgridPosY); + + int posZColumn = mPathgridCollection.searchNestedColumnIndex(parentColumn, + CSMWorld::Columns::ColumnId_PathgridPosZ); QModelIndex parent = model->index(recordIndex, parentColumn); for (size_t i = 0; i < mSelected.size(); ++i) { - const CSMWorld::Pathgrid::Point& point = source.mPoints[mSelected[i]]; - int row = mSelected[i]; + const CSMWorld::Pathgrid::Point& point = source->mPoints[mSelected[i]]; + int row = static_cast(mSelected[i]); - // X - int column = collection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosX); - commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, column, parent), point.mX + oX)); + commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posXColumn, parent), + point.mX + offsetX)); - // Y - column = collection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosY); - commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, column, parent), point.mY + oY)); + commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posYColumn, parent), + point.mY + offsetY)); - // Z - column = collection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosZ); - commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, column, parent), point.mZ + oZ)); + commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posZColumn, parent), + point.mZ + offsetZ)); } } @@ -228,30 +228,29 @@ namespace CSVRender void Pathgrid::recreateGeometry() { - removePathgridGeometry(); - removeSelectedGeometry(); - // Make new - const CSMWorld::SubCellCollection& pathgrids = mData.getPathgrids(); - int pathgridIndex = pathgrids.searchId(mId); - if (pathgridIndex != -1) + const CSMWorld::Pathgrid* source = getPathgridSource(); + if (source) { - const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); - mPathgridGeometry = SceneUtil::createPathgridGeometry(source); + removePathgridGeometry(); + mPathgridGeometry = SceneUtil::createPathgridGeometry(*source); mPathgridGeode->addDrawable(mPathgridGeometry); - recreateSelectedGeometry(source); + recreateSelectedGeometry(*source); + } + else + { + removePathgridGeometry(); + removeSelectedGeometry(); } } void Pathgrid::recreateSelectedGeometry() { - const CSMWorld::SubCellCollection& pathgrids = mData.getPathgrids(); - int pathgridIndex = pathgrids.searchId(mId); - if (pathgridIndex != -1) + const CSMWorld::Pathgrid* source = getPathgridSource(); + if (source) { - const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); - recreateSelectedGeometry(source); + recreateSelectedGeometry(*source); } else { @@ -283,4 +282,15 @@ namespace CSVRender mSelectedGeometry.release(); } } + + const CSMWorld::Pathgrid* Pathgrid::getPathgridSource() + { + int index = mPathgridCollection.searchId(mId); + if (index != -1) + { + return &mPathgridCollection.getRecord(index).get(); + } + + return 0; + } } diff --git a/apps/opencs/view/render/pathgrid.hpp b/apps/opencs/view/render/pathgrid.hpp index c1a8f9308..2fece6e0d 100644 --- a/apps/opencs/view/render/pathgrid.hpp +++ b/apps/opencs/view/render/pathgrid.hpp @@ -7,6 +7,8 @@ #include #include "../../model/world/cellcoordinates.hpp" +#include "../../model/world/idcollection.hpp" +#include "../../model/world/subcellcollection.hpp" #include "tagbase.hpp" @@ -80,6 +82,7 @@ namespace CSVRender private: CSMWorld::Data& mData; + CSMWorld::SubCellCollection& mPathgridCollection; std::string mId; CSMWorld::CellCoordinates mCoords; @@ -99,6 +102,9 @@ namespace CSVRender void recreateSelectedGeometry(const CSMWorld::Pathgrid& source); void removePathgridGeometry(); void removeSelectedGeometry(); + + const CSMWorld::Pathgrid* getPathgridSource(); + }; }