From d6b8033b464c5090ea1d42ee0a62d955a362a62d Mon Sep 17 00:00:00 2001 From: Marc Zinnschlag Date: Sat, 12 Mar 2016 13:19:51 +0100 Subject: [PATCH] handle cell transitions when moving objects --- apps/opencs/model/world/cellcoordinates.cpp | 12 ++++++++++++ apps/opencs/model/world/cellcoordinates.hpp | 5 +++++ apps/opencs/model/world/ref.cpp | 9 +++++---- apps/opencs/view/render/object.cpp | 12 ++++++++++++ 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/apps/opencs/model/world/cellcoordinates.cpp b/apps/opencs/model/world/cellcoordinates.cpp index 3ef3e6c69..80ec9dc04 100644 --- a/apps/opencs/model/world/cellcoordinates.cpp +++ b/apps/opencs/model/world/cellcoordinates.cpp @@ -1,5 +1,7 @@ #include "cellcoordinates.hpp" +#include + #include #include @@ -7,6 +9,9 @@ CSMWorld::CellCoordinates::CellCoordinates() : mX (0), mY (0) {} CSMWorld::CellCoordinates::CellCoordinates (int x, int y) : mX (x), mY (y) {} +CSMWorld::CellCoordinates::CellCoordinates (const std::pair& coordinates) +: mX (coordinates.first), mY (coordinates.second) {} + int CSMWorld::CellCoordinates::getX() const { return mX; @@ -49,6 +54,13 @@ std::pair CSMWorld::CellCoordinates::fromId ( return std::make_pair (CellCoordinates(), false); } +std::pair CSMWorld::CellCoordinates::coordinatesToCellIndex (float x, float y) +{ + const int cellSize = 8192; + + return std::make_pair (std::floor (x/cellSize), std::floor (y/cellSize)); +} + bool CSMWorld::operator== (const CellCoordinates& left, const CellCoordinates& right) { return left.getX()==right.getX() && left.getY()==right.getY(); diff --git a/apps/opencs/model/world/cellcoordinates.hpp b/apps/opencs/model/world/cellcoordinates.hpp index 63db60c29..f8851a6d9 100644 --- a/apps/opencs/model/world/cellcoordinates.hpp +++ b/apps/opencs/model/world/cellcoordinates.hpp @@ -3,6 +3,7 @@ #include #include +#include #include @@ -19,6 +20,8 @@ namespace CSMWorld CellCoordinates (int x, int y); + CellCoordinates (const std::pair& coordinates); + int getX() const; int getY() const; @@ -34,6 +37,8 @@ namespace CSMWorld /// /// \note The worldspace part of \a id is ignored static std::pair fromId (const std::string& id); + + static std::pair coordinatesToCellIndex (float x, float y); }; bool operator== (const CellCoordinates& left, const CellCoordinates& right); diff --git a/apps/opencs/model/world/ref.cpp b/apps/opencs/model/world/ref.cpp index 638f7ec9c..e6345ce71 100644 --- a/apps/opencs/model/world/ref.cpp +++ b/apps/opencs/model/world/ref.cpp @@ -2,6 +2,10 @@ #include +#include + +#include "cellcoordinates.hpp" + CSMWorld::CellRef::CellRef() { mRefNum.mIndex = 0; @@ -10,8 +14,5 @@ CSMWorld::CellRef::CellRef() std::pair CSMWorld::CellRef::getCellIndex() const { - const int cellSize = 8192; - - return std::make_pair ( - std::floor (mPos.pos[0]/cellSize), std::floor (mPos.pos[1]/cellSize)); + return CellCoordinates::coordinatesToCellIndex (mPos.pos[0], mPos.pos[1]); } diff --git a/apps/opencs/view/render/object.cpp b/apps/opencs/view/render/object.cpp index f7bb338cd..cc151ead9 100644 --- a/apps/opencs/view/render/object.cpp +++ b/apps/opencs/view/render/object.cpp @@ -20,6 +20,7 @@ #include "../../model/world/commands.hpp" #include "../../model/world/universalid.hpp" #include "../../model/world/commandmacro.hpp" +#include "../../model/world/cellcoordinates.hpp" #include #include @@ -542,6 +543,17 @@ void CSVRender::Object::apply (CSMWorld::CommandMacro& commands) commands.push (new CSMWorld::ModifyCommand (*model, model->index (recordIndex, column), mPositionOverride.pos[i])); } + + int column = collection.findColumnIndex (static_cast ( + CSMWorld::Columns::ColumnId_Cell)); + + std::pair cellIndex = collection.getRecord (recordIndex).get().getCellIndex(); + + /// \todo figure out worldspace (not important until multiple worldspaces are supported) + std::string cellId = CSMWorld::CellCoordinates (cellIndex).getId (""); + + commands.push (new CSMWorld::ModifyCommand (*model, + model->index (recordIndex, column), QString::fromUtf8 (cellId.c_str()))); } if (mOverrideFlags & Override_Rotation)