Avoid storing ref, dynamic cast worldspacewidget for safety

This commit is contained in:
unelsson 2021-08-22 15:33:40 +03:00
parent af3b9187b4
commit b84e41bd27
7 changed files with 53 additions and 12 deletions

View file

@ -1,19 +1,40 @@
#include "commands.hpp" #include "commands.hpp"
#include <components/debug/debuglog.hpp>
#include <components/esm/loadland.hpp> #include <components/esm/loadland.hpp>
#include "editmode.hpp"
#include "terrainselection.hpp" #include "terrainselection.hpp"
#include "terrainshapemode.hpp"
#include "terraintexturemode.hpp"
#include "worldspacewidget.hpp"
CSVRender::DrawTerrainSelectionCommand::DrawTerrainSelectionCommand(TerrainSelection& terrainSelection, QUndoCommand* parent) CSVRender::DrawTerrainSelectionCommand::DrawTerrainSelectionCommand(WorldspaceWidget* worldspaceWidget, QUndoCommand* parent)
: mTerrainSelection(terrainSelection) : mWorldspaceWidget(worldspaceWidget)
{ } { }
void CSVRender::DrawTerrainSelectionCommand::redo() void CSVRender::DrawTerrainSelectionCommand::redo()
{ {
mTerrainSelection.update(); if (CSVRender::WorldspaceWidget* worldspaceWidget = dynamic_cast<CSVRender::WorldspaceWidget *> (mWorldspaceWidget))
{
if (CSVRender::TerrainShapeMode* terrainMode = dynamic_cast<CSVRender::TerrainShapeMode *> (worldspaceWidget->getEditMode()) )
{
terrainMode->getTerrainSelection()->update();
return;
}
}
Log(Debug::Warning) << "Error in redoing terrain selection";
} }
void CSVRender::DrawTerrainSelectionCommand::undo() void CSVRender::DrawTerrainSelectionCommand::undo()
{ {
mTerrainSelection.update(); if (CSVRender::WorldspaceWidget* worldspaceWidget = dynamic_cast<CSVRender::WorldspaceWidget *> (mWorldspaceWidget))
{
if (CSVRender::TerrainShapeMode* terrainMode = dynamic_cast<CSVRender::TerrainShapeMode *> (worldspaceWidget->getEditMode()) )
{
terrainMode->getTerrainSelection()->update();
return;
}
}
Log(Debug::Warning) << "Error in undoing terrain selection";
} }

View file

@ -3,6 +3,8 @@
#include <QUndoCommand> #include <QUndoCommand>
#include "worldspacewidget.hpp"
namespace CSVRender namespace CSVRender
{ {
class TerrainSelection; class TerrainSelection;
@ -21,11 +23,13 @@ namespace CSVRender
*/ */
class DrawTerrainSelectionCommand : public QUndoCommand class DrawTerrainSelectionCommand : public QUndoCommand
{ {
friend class WorldspaceWidget;
private: private:
TerrainSelection& mTerrainSelection; WorldspaceWidget *mWorldspaceWidget;
public: public:
DrawTerrainSelectionCommand(TerrainSelection& terrainSelection, QUndoCommand* parent = nullptr); DrawTerrainSelectionCommand(WorldspaceWidget* worldspaceWidget, QUndoCommand* parent = nullptr);
void redo() override; void redo() override;
void undo() override; void undo() override;

View file

@ -287,7 +287,7 @@ void CSVRender::TerrainShapeMode::applyTerrainEditChanges()
undoStack.beginMacro ("Edit shape and normal records"); undoStack.beginMacro ("Edit shape and normal records");
// One command at the beginning of the macro for redrawing the terrain-selection grid when undoing the changes. // One command at the beginning of the macro for redrawing the terrain-selection grid when undoing the changes.
undoStack.push(new DrawTerrainSelectionCommand(*mTerrainShapeSelection)); undoStack.push(new DrawTerrainSelectionCommand(&getWorldspaceWidget()));
for(CSMWorld::CellCoordinates cellCoordinates: mAlteredCells) for(CSMWorld::CellCoordinates cellCoordinates: mAlteredCells)
{ {
@ -358,7 +358,7 @@ void CSVRender::TerrainShapeMode::applyTerrainEditChanges()
pushNormalsEditToCommand(landNormalsNew, document, landTable, cellId); pushNormalsEditToCommand(landNormalsNew, document, landTable, cellId);
} }
// One command at the end of the macro for redrawing the terrain-selection grid when redoing the changes. // One command at the end of the macro for redrawing the terrain-selection grid when redoing the changes.
undoStack.push(new DrawTerrainSelectionCommand(*mTerrainShapeSelection)); undoStack.push(new DrawTerrainSelectionCommand(&getWorldspaceWidget()));
undoStack.endMacro(); undoStack.endMacro();
clearTransientEdits(); clearTransientEdits();
@ -1444,6 +1444,11 @@ void CSVRender::TerrainShapeMode::mouseMoveEvent (QMouseEvent *event)
mBrushDraw->hide(); mBrushDraw->hide();
} }
std::shared_ptr<CSVRender::TerrainSelection> CSVRender::TerrainShapeMode::getTerrainSelection()
{
return mTerrainShapeSelection;
}
void CSVRender::TerrainShapeMode::setBrushSize(int brushSize) void CSVRender::TerrainShapeMode::setBrushSize(int brushSize)
{ {
mBrushSize = brushSize; mBrushSize = brushSize;

View file

@ -92,6 +92,8 @@ namespace CSVRender
void dragMoveEvent (QDragMoveEvent *event) override; void dragMoveEvent (QDragMoveEvent *event) override;
void mouseMoveEvent (QMouseEvent *event) override; void mouseMoveEvent (QMouseEvent *event) override;
std::shared_ptr<TerrainSelection> getTerrainSelection();
private: private:
/// Remove duplicates and sort mAlteredCells, then limitAlteredHeights forward and reverse /// Remove duplicates and sort mAlteredCells, then limitAlteredHeights forward and reverse
@ -176,7 +178,7 @@ namespace CSVRender
int mDragMode = InteractionType_None; int mDragMode = InteractionType_None;
osg::Group* mParentNode; osg::Group* mParentNode;
bool mIsEditing = false; bool mIsEditing = false;
std::unique_ptr<TerrainSelection> mTerrainShapeSelection; std::shared_ptr<TerrainSelection> mTerrainShapeSelection;
int mTotalDiffY = 0; int mTotalDiffY = 0;
std::vector<CSMWorld::CellCoordinates> mAlteredCells; std::vector<CSMWorld::CellCoordinates> mAlteredCells;
osg::Vec3d mEditingPos; osg::Vec3d mEditingPos;

View file

@ -712,6 +712,11 @@ void CSVRender::TerrainTextureMode::mouseMoveEvent (QMouseEvent *event)
mBrushDraw->hide(); mBrushDraw->hide();
} }
std::shared_ptr<CSVRender::TerrainSelection> CSVRender::TerrainTextureMode::getTerrainSelection()
{
return mTerrainTextureSelection;
}
void CSVRender::TerrainTextureMode::setBrushSize(int brushSize) void CSVRender::TerrainTextureMode::setBrushSize(int brushSize)
{ {

View file

@ -85,6 +85,8 @@ namespace CSVRender
void mouseMoveEvent (QMouseEvent *event) override; void mouseMoveEvent (QMouseEvent *event) override;
std::shared_ptr<TerrainSelection> getTerrainSelection();
private: private:
/// \brief Handle brush mechanics, maths regarding worldspace hit etc. /// \brief Handle brush mechanics, maths regarding worldspace hit etc.
void editTerrainTextureGrid (const WorldspaceHitResult& hit); void editTerrainTextureGrid (const WorldspaceHitResult& hit);
@ -115,7 +117,7 @@ namespace CSVRender
int mDragMode; int mDragMode;
osg::Group* mParentNode; osg::Group* mParentNode;
bool mIsEditing; bool mIsEditing;
std::unique_ptr<TerrainSelection> mTerrainTextureSelection; std::shared_ptr<TerrainSelection> mTerrainTextureSelection;
const int cellSize {ESM::Land::REAL_SIZE}; const int cellSize {ESM::Land::REAL_SIZE};
const int landTextureSize {ESM::Land::LAND_TEXTURE_SIZE}; const int landTextureSize {ESM::Land::LAND_TEXTURE_SIZE};

View file

@ -49,6 +49,8 @@ namespace CSVRender
{ {
Q_OBJECT Q_OBJECT
friend class DrawTerrainSelectionCommand;
CSVWidget::SceneToolToggle2 *mSceneElements; CSVWidget::SceneToolToggle2 *mSceneElements;
CSVWidget::SceneToolRun *mRun; CSVWidget::SceneToolRun *mRun;
CSMDoc::Document& mDocument; CSMDoc::Document& mDocument;