Cleanup, put duplicated code in function

This commit is contained in:
Aesylwinn 2016-05-16 17:00:58 -04:00
parent da6a742beb
commit 55656d68ef
2 changed files with 65 additions and 49 deletions

View file

@ -41,6 +41,7 @@ namespace CSVRender
Pathgrid::Pathgrid(CSMWorld::Data& data, osg::Group* parent, const std::string& pathgridId, Pathgrid::Pathgrid(CSMWorld::Data& data, osg::Group* parent, const std::string& pathgridId,
const CSMWorld::CellCoordinates& coordinates) const CSMWorld::CellCoordinates& coordinates)
: mData(data) : mData(data)
, mPathgridCollection(mData.getPathgrids())
, mId(pathgridId) , mId(pathgridId)
, mCoords(coordinates) , mCoords(coordinates)
, mParent(parent) , mParent(parent)
@ -97,15 +98,13 @@ namespace CSVRender
{ {
mSelected.clear(); mSelected.clear();
const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mData.getPathgrids(); const CSMWorld::Pathgrid* source = getPathgridSource();
int pathgridIndex = pathgrids.searchId(mId); if (source)
if (pathgridIndex != -1)
{ {
const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); for (unsigned short i = 0; i < static_cast<unsigned short>(source->mPoints.size()); ++i)
for (unsigned short i = 0; i < static_cast<unsigned short>(source.mPoints.size()); ++i)
mSelected.push_back(i); mSelected.push_back(i);
recreateSelectedGeometry(source); recreateSelectedGeometry(*source);
} }
else else
{ {
@ -130,21 +129,19 @@ namespace CSVRender
void Pathgrid::invertSelected() void Pathgrid::invertSelected()
{ {
NodeList temp = NodeList(mSelected.begin(), mSelected.end()); NodeList temp = NodeList(mSelected);
mSelected.clear(); mSelected.clear();
const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mData.getPathgrids(); const CSMWorld::Pathgrid* source = getPathgridSource();
int pathgridIndex = pathgrids.searchId(mId); if (source)
if (pathgridIndex != -1)
{ {
const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); for (unsigned short i = 0; i < static_cast<unsigned short>(source->mPoints.size()); ++i)
for (unsigned short i = 0; i < static_cast<unsigned short>(source.mPoints.size()); ++i)
{ {
if (std::find(temp.begin(), temp.end(), i) == temp.end()) if (std::find(temp.begin(), temp.end(), i) == temp.end())
mSelected.push_back(i); mSelected.push_back(i);
} }
recreateSelectedGeometry(source); recreateSelectedGeometry(*source);
} }
else else
{ {
@ -170,41 +167,44 @@ namespace CSVRender
void Pathgrid::applyPosition(CSMWorld::CommandMacro& commands) void Pathgrid::applyPosition(CSMWorld::CommandMacro& commands)
{ {
const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mData.getPathgrids(); const CSMWorld::Pathgrid* source = getPathgridSource();
int pathgridIndex = pathgrids.searchId(mId); if (source)
if (pathgridIndex != -1)
{ {
const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get();
osg::Vec3d localCoords = mSelectedNode->getPosition(); osg::Vec3d localCoords = mSelectedNode->getPosition();
int oX = static_cast<int>(localCoords.x()); int offsetX = static_cast<int>(localCoords.x());
int oY = static_cast<int>(localCoords.y()); int offsetY = static_cast<int>(localCoords.y());
int oZ = static_cast<int>(localCoords.z()); int offsetZ = static_cast<int>(localCoords.z());
CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& 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 recordIndex = mPathgridCollection.getIndex(mId);
int parentColumn = collection.findColumnIndex(CSMWorld::Columns::ColumnId_PathgridPoints); 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); QModelIndex parent = model->index(recordIndex, parentColumn);
for (size_t i = 0; i < mSelected.size(); ++i) for (size_t i = 0; i < mSelected.size(); ++i)
{ {
const CSMWorld::Pathgrid::Point& point = source.mPoints[mSelected[i]]; const CSMWorld::Pathgrid::Point& point = source->mPoints[mSelected[i]];
int row = mSelected[i]; int row = static_cast<int>(mSelected[i]);
// X commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posXColumn, parent),
int column = collection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosX); point.mX + offsetX));
commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, column, parent), point.mX + oX));
// Y commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posYColumn, parent),
column = collection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosY); point.mY + offsetY));
commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, column, parent), point.mY + oY));
// Z commands.push(new CSMWorld::ModifyCommand(*model, model->index(row, posZColumn, parent),
column = collection.searchNestedColumnIndex(parentColumn, CSMWorld::Columns::ColumnId_PathgridPosZ); point.mZ + offsetZ));
commands.push (new CSMWorld::ModifyCommand(*model, model->index(row, column, parent), point.mZ + oZ));
} }
} }
@ -228,30 +228,29 @@ namespace CSVRender
void Pathgrid::recreateGeometry() void Pathgrid::recreateGeometry()
{ {
removePathgridGeometry();
removeSelectedGeometry();
// Make new // Make new
const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mData.getPathgrids(); const CSMWorld::Pathgrid* source = getPathgridSource();
int pathgridIndex = pathgrids.searchId(mId); if (source)
if (pathgridIndex != -1)
{ {
const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); removePathgridGeometry();
mPathgridGeometry = SceneUtil::createPathgridGeometry(source); mPathgridGeometry = SceneUtil::createPathgridGeometry(*source);
mPathgridGeode->addDrawable(mPathgridGeometry); mPathgridGeode->addDrawable(mPathgridGeometry);
recreateSelectedGeometry(source); recreateSelectedGeometry(*source);
}
else
{
removePathgridGeometry();
removeSelectedGeometry();
} }
} }
void Pathgrid::recreateSelectedGeometry() void Pathgrid::recreateSelectedGeometry()
{ {
const CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& pathgrids = mData.getPathgrids(); const CSMWorld::Pathgrid* source = getPathgridSource();
int pathgridIndex = pathgrids.searchId(mId); if (source)
if (pathgridIndex != -1)
{ {
const CSMWorld::Pathgrid& source = pathgrids.getRecord(pathgridIndex).get(); recreateSelectedGeometry(*source);
recreateSelectedGeometry(source);
} }
else else
{ {
@ -283,4 +282,15 @@ namespace CSVRender
mSelectedGeometry.release(); mSelectedGeometry.release();
} }
} }
const CSMWorld::Pathgrid* Pathgrid::getPathgridSource()
{
int index = mPathgridCollection.searchId(mId);
if (index != -1)
{
return &mPathgridCollection.getRecord(index).get();
}
return 0;
}
} }

View file

@ -7,6 +7,8 @@
#include <osg/ref_ptr> #include <osg/ref_ptr>
#include "../../model/world/cellcoordinates.hpp" #include "../../model/world/cellcoordinates.hpp"
#include "../../model/world/idcollection.hpp"
#include "../../model/world/subcellcollection.hpp"
#include "tagbase.hpp" #include "tagbase.hpp"
@ -80,6 +82,7 @@ namespace CSVRender
private: private:
CSMWorld::Data& mData; CSMWorld::Data& mData;
CSMWorld::SubCellCollection<CSMWorld::Pathgrid>& mPathgridCollection;
std::string mId; std::string mId;
CSMWorld::CellCoordinates mCoords; CSMWorld::CellCoordinates mCoords;
@ -99,6 +102,9 @@ namespace CSVRender
void recreateSelectedGeometry(const CSMWorld::Pathgrid& source); void recreateSelectedGeometry(const CSMWorld::Pathgrid& source);
void removePathgridGeometry(); void removePathgridGeometry();
void removeSelectedGeometry(); void removeSelectedGeometry();
const CSMWorld::Pathgrid* getPathgridSource();
}; };
} }