From ff18595a86ef60fdcc2c4ec8ea355797b143a445 Mon Sep 17 00:00:00 2001 From: Nelsson Huotari Date: Sat, 21 Sep 2019 13:15:42 +0300 Subject: [PATCH] Reduce code duplification, add bool value to limiting --- apps/opencs/view/render/terrainshapemode.cpp | 104 +++++++++---------- apps/opencs/view/render/terrainshapemode.hpp | 8 +- 2 files changed, 53 insertions(+), 59 deletions(-) diff --git a/apps/opencs/view/render/terrainshapemode.cpp b/apps/opencs/view/render/terrainshapemode.cpp index cc20339a9..7c4a68ae5 100644 --- a/apps/opencs/view/render/terrainshapemode.cpp +++ b/apps/opencs/view/render/terrainshapemode.cpp @@ -877,7 +877,37 @@ void CSVRender::TerrainShapeMode::fixEdges(const CSMWorld::CellCoordinates& cell } } -void CSVRender::TerrainShapeMode::limitAlteredHeights(const CSMWorld::CellCoordinates& cellCoords, bool reverseMode) +void CSVRender::TerrainShapeMode::compareAndLimit(const CSMWorld::CellCoordinates& cellCoords, int inCellX, int inCellY, float* limitedAlteredHeightXAxis, float* limitedAlteredHeightYAxis, bool* steepnessIsWithinLimits) +{ + if (limitedAlteredHeightXAxis) + { + if (limitedAlteredHeightYAxis) + { + if(std::abs(*limitedAlteredHeightXAxis) >= std::abs(*limitedAlteredHeightYAxis)) + { + alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightXAxis, false); + *steepnessIsWithinLimits = false; + } + else + { + alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightYAxis, false); + *steepnessIsWithinLimits = false; + } + } + else + { + alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightXAxis, false); + *steepnessIsWithinLimits = false; + } + } + else if (limitedAlteredHeightYAxis) + { + alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightYAxis, false); + *steepnessIsWithinLimits = false; + } +} + +bool CSVRender::TerrainShapeMode::limitAlteredHeights(const CSMWorld::CellCoordinates& cellCoords, bool reverseMode) { CSMDoc::Document& document = getWorldspaceWidget().getDocument(); CSMWorld::IdTable& landTable = dynamic_cast ( @@ -889,31 +919,32 @@ void CSVRender::TerrainShapeMode::limitAlteredHeights(const CSMWorld::CellCoordi int limitHeightChange = 1016.0f; // Limited by save format bool noCell = document.getData().getCells().searchId (cellId) == -1; bool noLand = document.getData().getLand().searchId (cellId) == -1; + bool steepnessIsWithinLimits = true; if (!noCell && !noLand) { const CSMWorld::LandHeightsColumn::DataType landShapePointer = landTable.data(landTable.getModelIndex(cellId, landshapeColumn)).value(); + float thisHeight = 0.0f; + float thisAlteredHeight = 0.0f; + float leftHeight = 0.0f; + float leftAlteredHeight = 0.0f; + float upHeight = 0.0f; + float upAlteredHeight = 0.0f; + float rightHeight = 0.0f; + float rightAlteredHeight = 0.0f; + float downHeight = 0.0f; + float downAlteredHeight = 0.0f; + if (reverseMode == false) { for(int inCellY = 0; inCellY < landSize; ++inCellY) { for(int inCellX = 0; inCellX < landSize; ++inCellX) { - float thisHeight = 0.0f; - float thisAlteredHeight = 0.0f; - float leftHeight = 0.0f; - float leftAlteredHeight = 0.0f; - float upHeight = 0.0f; - float upAlteredHeight = 0.0f; - float rightHeight = 0.0f; - float rightAlteredHeight = 0.0f; - float downHeight = 0.0f; - float downAlteredHeight = 0.0f; float* limitedAlteredHeightXAxis = nullptr; float* limitedAlteredHeightYAxis = nullptr; - updateKeyHeightValues(cellCoords, inCellX, inCellY, &thisHeight, &thisAlteredHeight, &leftHeight, &leftAlteredHeight, &upHeight, &upAlteredHeight, &rightHeight, &rightAlteredHeight, &downHeight, &downAlteredHeight); @@ -930,25 +961,9 @@ void CSVRender::TerrainShapeMode::limitAlteredHeights(const CSMWorld::CellCoordi limitedAlteredHeightYAxis = new float(upHeight + limitHeightChange - (thisHeight - thisAlteredHeight)); // Limit altered height value based on x or y, whichever is the smallest - if (limitedAlteredHeightXAxis) - { - if (limitedAlteredHeightYAxis) - { - if(std::abs(*limitedAlteredHeightXAxis) >= std::abs(*limitedAlteredHeightYAxis)) - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightXAxis, false); - else - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightYAxis, false); - } - else - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightXAxis, false); - } - else if (limitedAlteredHeightYAxis) - { - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightYAxis, false); - } + compareAndLimit(cellCoords, inCellX, inCellY, limitedAlteredHeightXAxis, limitedAlteredHeightYAxis, &steepnessIsWithinLimits); delete limitedAlteredHeightXAxis; - delete limitedAlteredHeightYAxis; - } + delete limitedAlteredHeightYAxis; } } } @@ -958,19 +973,8 @@ void CSVRender::TerrainShapeMode::limitAlteredHeights(const CSMWorld::CellCoordi { for(int inCellX = landSize - 1; inCellX >= 0; --inCellX) { - float thisHeight = 0.0f; - float thisAlteredHeight = 0.0f; - float leftHeight = 0.0f; - float leftAlteredHeight = 0.0f; - float upHeight = 0.0f; - float upAlteredHeight = 0.0f; - float rightHeight = 0.0f; - float rightAlteredHeight = 0.0f; - float downHeight = 0.0f; - float downAlteredHeight = 0.0f; float* limitedAlteredHeightXAxis = nullptr; float* limitedAlteredHeightYAxis = nullptr; - updateKeyHeightValues(cellCoords, inCellX, inCellY, &thisHeight, &thisAlteredHeight, &leftHeight, &leftAlteredHeight, &upHeight, &upAlteredHeight, &rightHeight, &rightAlteredHeight, &downHeight, &downAlteredHeight); @@ -987,27 +991,13 @@ void CSVRender::TerrainShapeMode::limitAlteredHeights(const CSMWorld::CellCoordi limitedAlteredHeightYAxis = new float(downHeight + limitHeightChange - (thisHeight - thisAlteredHeight)); // Limit altered height value based on x or y, whichever is the smallest - if (limitedAlteredHeightXAxis) - { - if (limitedAlteredHeightYAxis) - { - if(std::abs(*limitedAlteredHeightXAxis) >= std::abs(*limitedAlteredHeightYAxis)) - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightXAxis, false); - else - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightYAxis, false); - } - else - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightXAxis, false); - } - else if (limitedAlteredHeightYAxis) - { - alterHeight(cellCoords, inCellX, inCellY, *limitedAlteredHeightYAxis, false); - } + compareAndLimit(cellCoords, inCellX, inCellY, limitedAlteredHeightXAxis, limitedAlteredHeightYAxis, &steepnessIsWithinLimits); delete limitedAlteredHeightXAxis; delete limitedAlteredHeightYAxis; } } } } + return steepnessIsWithinLimits; } void CSVRender::TerrainShapeMode::selectTerrainShapes(const std::pair& vertexCoords, unsigned char selectMode, bool dragOperation) diff --git a/apps/opencs/view/render/terrainshapemode.hpp b/apps/opencs/view/render/terrainshapemode.hpp index 43978eea3..b5b642b46 100644 --- a/apps/opencs/view/render/terrainshapemode.hpp +++ b/apps/opencs/view/render/terrainshapemode.hpp @@ -101,8 +101,12 @@ namespace CSVRender /// Bind edge vertices to next cells void fixEdges(const CSMWorld::CellCoordinates& cellCoords); - /// Check that the edit doesn't break save format limits, fix if necessary - void limitAlteredHeights(const CSMWorld::CellCoordinates& cellCoords, bool reverseMode = false); + ///Limit steepness based on either X or Y and return false if steepness is limited + void compareAndLimit(const CSMWorld::CellCoordinates& cellCoords, int inCellX, int inCellY, float* limitedAlteredHeightXAxis, + float* limitedAlteredHeightYAxis, bool* steepnessIsWithinLimits); + + /// Check that the edit doesn't break save format limits, fix if necessary, return true if slope steepness is within limits + bool limitAlteredHeights(const CSMWorld::CellCoordinates& cellCoords, bool reverseMode = false); /// Handle brush mechanics for terrain shape selection void selectTerrainShapes (const std::pair& vertexCoords, unsigned char selectMode, bool dragOperation);