From 6fbc10dbbadfe807efe56f0e96fd670571ff53d2 Mon Sep 17 00:00:00 2001 From: Aesylwinn Date: Mon, 16 May 2016 17:20:07 -0400 Subject: [PATCH] Add ability to get cell, pathgrid, and ability to add nodes. Also missing include. --- apps/opencs/view/render/cell.cpp | 5 +++ apps/opencs/view/render/cell.hpp | 3 ++ .../view/render/pagedworldspacewidget.cpp | 15 +++++++ .../view/render/pagedworldspacewidget.hpp | 2 + apps/opencs/view/render/pathgrid.cpp | 42 +++++++++++++++++++ apps/opencs/view/render/pathgrid.hpp | 1 + apps/opencs/view/render/pathgridmode.cpp | 32 ++++++++++++++ .../view/render/unpagedworldspacewidget.cpp | 5 +++ .../view/render/unpagedworldspacewidget.hpp | 2 + apps/opencs/view/render/worldspacewidget.hpp | 4 ++ 10 files changed, 111 insertions(+) diff --git a/apps/opencs/view/render/cell.cpp b/apps/opencs/view/render/cell.cpp index e383ccad38..eed394d0b4 100644 --- a/apps/opencs/view/render/cell.cpp +++ b/apps/opencs/view/render/cell.cpp @@ -123,6 +123,11 @@ CSVRender::Cell::~Cell() mCellNode->getParent(0)->removeChild(mCellNode); } +CSVRender::Pathgrid* CSVRender::Cell::getPathgrid() const +{ + return mPathgrid.get(); +} + bool CSVRender::Cell::referenceableDataChanged (const QModelIndex& topLeft, const QModelIndex& bottomRight) { diff --git a/apps/opencs/view/render/cell.hpp b/apps/opencs/view/render/cell.hpp index caf6c59a92..a2efc0077d 100644 --- a/apps/opencs/view/render/cell.hpp +++ b/apps/opencs/view/render/cell.hpp @@ -87,6 +87,9 @@ namespace CSVRender ~Cell(); + /// \note Returns the pathgrid representation which will exist as long as the cell exists + Pathgrid* getPathgrid() const; + /// \return Did this call result in a modification of the visual representation of /// this cell? bool referenceableDataChanged (const QModelIndex& topLeft, diff --git a/apps/opencs/view/render/pagedworldspacewidget.cpp b/apps/opencs/view/render/pagedworldspacewidget.cpp index b44d1d58ae..39e7f8361a 100644 --- a/apps/opencs/view/render/pagedworldspacewidget.cpp +++ b/apps/opencs/view/render/pagedworldspacewidget.cpp @@ -644,6 +644,21 @@ std::string CSVRender::PagedWorldspaceWidget::getCellId (const osg::Vec3f& point return cellCoordinates.getId (mWorldspace); } +CSVRender::Cell* CSVRender::PagedWorldspaceWidget::getCell(const osg::Vec3d& point) const +{ + const int cellSize = 8192; + + CSMWorld::CellCoordinates coords( + static_cast (std::floor (point.x()/cellSize)), + static_cast (std::floor (point.y()/cellSize))); + + std::map::const_iterator searchResult = mCells.find(coords); + if (searchResult != mCells.end()) + return searchResult->second; + else + return 0; +} + std::vector > CSVRender::PagedWorldspaceWidget::getSelection ( unsigned int elementMask) const { diff --git a/apps/opencs/view/render/pagedworldspacewidget.hpp b/apps/opencs/view/render/pagedworldspacewidget.hpp index 95bf0f8464..c7f035dd98 100644 --- a/apps/opencs/view/render/pagedworldspacewidget.hpp +++ b/apps/opencs/view/render/pagedworldspacewidget.hpp @@ -121,6 +121,8 @@ namespace CSVRender virtual std::string getCellId (const osg::Vec3f& point) const; + virtual Cell* getCell(const osg::Vec3d& point) const; + virtual std::vector > getSelection (unsigned int elementMask) const; diff --git a/apps/opencs/view/render/pathgrid.cpp b/apps/opencs/view/render/pathgrid.cpp index 4c3db2562c..4df9852cb4 100644 --- a/apps/opencs/view/render/pathgrid.cpp +++ b/apps/opencs/view/render/pathgrid.cpp @@ -13,6 +13,7 @@ #include "../../model/world/commands.hpp" #include "../../model/world/commandmacro.hpp" #include "../../model/world/data.hpp" +#include "../../model/world/idtree.hpp" namespace CSVRender { @@ -165,6 +166,47 @@ namespace CSVRender mSelectedNode->setPosition(osg::Vec3f(0,0,0)); } + void Pathgrid::applyPoint(CSMWorld::CommandMacro& commands, const osg::Vec3d& worldPos) + { + const CSMWorld::Pathgrid* source = getPathgridSource(); + if (source) + { + osg::Vec3d localCoords = worldPos - mBaseNode->getPosition(); + + int posX = static_cast(localCoords.x()); + int posY = static_cast(localCoords.y()); + int posZ = 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); + + 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); + 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)); + } + else + { + // Create pathgrid TODO + } + } + void Pathgrid::applyPosition(CSMWorld::CommandMacro& commands) { const CSMWorld::Pathgrid* source = getPathgridSource(); diff --git a/apps/opencs/view/render/pathgrid.hpp b/apps/opencs/view/render/pathgrid.hpp index 2fece6e0d8..a820417408 100644 --- a/apps/opencs/view/render/pathgrid.hpp +++ b/apps/opencs/view/render/pathgrid.hpp @@ -71,6 +71,7 @@ namespace CSVRender void moveSelected(const osg::Vec3d& offset); void resetMove(); + void applyPoint(CSMWorld::CommandMacro& commands, const osg::Vec3d& worldPos); void applyPosition(CSMWorld::CommandMacro& commands); void applyEdge(CSMWorld::CommandMacro& commands, unsigned short node1, unsigned short node2); void applyEdges(CSMWorld::CommandMacro& commands, unsigned short node); diff --git a/apps/opencs/view/render/pathgridmode.cpp b/apps/opencs/view/render/pathgridmode.cpp index de7b09018b..c06618e74a 100644 --- a/apps/opencs/view/render/pathgridmode.cpp +++ b/apps/opencs/view/render/pathgridmode.cpp @@ -9,6 +9,7 @@ #include "../../model/world/idtable.hpp" #include "../../model/world/idtree.hpp" +#include "cell.hpp" #include "mask.hpp" #include "pathgrid.hpp" #include "worldspacewidget.hpp" @@ -66,6 +67,37 @@ namespace CSVRender } void PathgridMode::primaryEditPressed(const WorldspaceHitResult& hitResult) + { + // Determine placement + osg::Vec3d position; + + if (hitResult.hit) + { + position = hitResult.worldPos; + } + else + { + const double DefaultDistance = 500.f; + + osg::Vec3d eye, center, up, offset; + getWorldspaceWidget().getCamera()->getViewMatrix().getLookAt (eye, center, up); + + osg::Vec3d direction = center - eye; + direction.normalize(); + position = eye + direction * DefaultDistance; + } + + // Get pathgrid cell + Cell* cell = getWorldspaceWidget().getCell (position); + if (cell) + { + // Add node + QUndoStack& undoStack = getWorldspaceWidget().getDocument().getUndoStack(); + QString description = "Connect node to selected nodes"; + + CSMWorld::CommandMacro macro(undoStack, description); + cell->getPathgrid()->applyPoint(macro, position); + } } void PathgridMode::secondaryEditPressed(const WorldspaceHitResult& hit) diff --git a/apps/opencs/view/render/unpagedworldspacewidget.cpp b/apps/opencs/view/render/unpagedworldspacewidget.cpp index df28f3f800..802194bea2 100644 --- a/apps/opencs/view/render/unpagedworldspacewidget.cpp +++ b/apps/opencs/view/render/unpagedworldspacewidget.cpp @@ -127,6 +127,11 @@ std::string CSVRender::UnpagedWorldspaceWidget::getCellId (const osg::Vec3f& poi return mCellId; } +CSVRender::Cell* CSVRender::UnpagedWorldspaceWidget::getCell(const osg::Vec3d& point) const +{ + return mCell.get(); +} + std::vector > CSVRender::UnpagedWorldspaceWidget::getSelection ( unsigned int elementMask) const { diff --git a/apps/opencs/view/render/unpagedworldspacewidget.hpp b/apps/opencs/view/render/unpagedworldspacewidget.hpp index 98cc3ef967..dd4a8a2f7f 100644 --- a/apps/opencs/view/render/unpagedworldspacewidget.hpp +++ b/apps/opencs/view/render/unpagedworldspacewidget.hpp @@ -61,6 +61,8 @@ namespace CSVRender virtual std::string getCellId (const osg::Vec3f& point) const; + virtual Cell* getCell(const osg::Vec3d& point) const; + virtual std::vector > getSelection (unsigned int elementMask) const; diff --git a/apps/opencs/view/render/worldspacewidget.hpp b/apps/opencs/view/render/worldspacewidget.hpp index 70ebae5f8d..59aacca291 100644 --- a/apps/opencs/view/render/worldspacewidget.hpp +++ b/apps/opencs/view/render/worldspacewidget.hpp @@ -33,6 +33,7 @@ namespace CSVWidget namespace CSVRender { class TagBase; + class Cell; class CellArrow; class EditMode; @@ -161,6 +162,9 @@ namespace CSVRender virtual std::string getCellId (const osg::Vec3f& point) const = 0; + /// \note Returns the cell if it exists, otherwise a null pointer + virtual Cell* getCell(const osg::Vec3d& point) const = 0; + virtual std::vector > getSelection (unsigned int elementMask) const = 0;