diff --git a/src/definition/PaintedGrid.cpp b/src/definition/PaintedGrid.cpp index 4c8b154..2fece9e 100644 --- a/src/definition/PaintedGrid.cpp +++ b/src/definition/PaintedGrid.cpp @@ -140,7 +140,7 @@ void PaintedGrid::clearPainting() brush_data->clear(); } -void PaintedGrid::applyBrush(const PaintedGridBrush &brush, double x, double y, double force) +void PaintedGrid::applyBrush(const PaintedGridBrush &brush, double x, double y, double force, bool commit) { int xstart, xend, ystart, yend; @@ -165,6 +165,11 @@ void PaintedGrid::applyBrush(const PaintedGridBrush &brush, double x, double y, } } } + + if (commit) + { + endBrushStroke(); + } } void PaintedGrid::endBrushStroke() diff --git a/src/definition/PaintedGrid.h b/src/definition/PaintedGrid.h index a71a1a6..9773a7a 100644 --- a/src/definition/PaintedGrid.h +++ b/src/definition/PaintedGrid.h @@ -62,17 +62,17 @@ public: /** * Clear all painting and reset the initial value everywhere. */ - void clearPainting(); + virtual void clearPainting(); /** * Apply a brush stroke at a grid location (locating the brush center). */ - void applyBrush(const PaintedGridBrush &brush, double x, double y, double force); + virtual void applyBrush(const PaintedGridBrush &brush, double x, double y, double force, bool commit=false); /** * Commit previous brush strokes. */ - void endBrushStroke(); + virtual void endBrushStroke(); /** * Virtual method that can be reimplemented to provide the initial value at a grid location. diff --git a/src/definition/TerrainHeightMap.cpp b/src/definition/TerrainHeightMap.cpp index 8620b8a..b15cba1 100644 --- a/src/definition/TerrainHeightMap.cpp +++ b/src/definition/TerrainHeightMap.cpp @@ -22,32 +22,46 @@ double TerrainHeightMap::getInitialValue(double x, double y) const return terrain->getInterpolatedHeight(x, y, false, false); } -void TerrainHeightMap::brushElevation(const PaintedGridBrush &brush, double x, double y, double value) +void TerrainHeightMap::brushElevation(const PaintedGridBrush &brush, double x, double y, double value, bool commit) { PaintedGridBrushRaiseLower sbrush(brush); - applyBrush(sbrush, x, y, value / terrain->height); + applyBrush(sbrush, x, y, value / terrain->height, commit); } -void TerrainHeightMap::brushFlatten(const PaintedGridBrush &brush, double x, double y, double height, double force) +void TerrainHeightMap::brushFlatten(const PaintedGridBrush &brush, double x, double y, double height, double force, bool commit) { PaintedGridBrushFlatten sbrush(brush, height); - applyBrush(sbrush, x, y, force / terrain->height); + applyBrush(sbrush, x, y, force / terrain->height, commit); } -void TerrainHeightMap::brushSmooth(const PaintedGridBrush &brush, double x, double y, double value) +void TerrainHeightMap::brushSmooth(const PaintedGridBrush &brush, double x, double y, double value, bool commit) { PaintedGridBrushSmooth sbrush(brush); - applyBrush(sbrush, x, y, value / terrain->height); + applyBrush(sbrush, x, y, value / terrain->height, commit); } -void TerrainHeightMap::brushAddNoise(const PaintedGridBrush &brush, double x, double y, NoiseGenerator* generator, double value) +void TerrainHeightMap::brushAddNoise(const PaintedGridBrush &brush, double x, double y, NoiseGenerator* generator, double value, bool commit) { PaintedGridBrushAddNoise sbrush(brush, generator); - applyBrush(sbrush, x, y, value / terrain->height); + applyBrush(sbrush, x, y, value / terrain->height, commit); } -void TerrainHeightMap::brushReset(const PaintedGridBrush &brush, double x, double y, double value) +void TerrainHeightMap::brushReset(const PaintedGridBrush &brush, double x, double y, double value, bool commit) { PaintedGridBrushReset sbrush(brush); - applyBrush(sbrush, x, y, value / terrain->height); + applyBrush(sbrush, x, y, value / terrain->height, commit); +} + +void TerrainHeightMap::clearPainting() +{ + PaintedGrid::clearPainting(); + + terrain->validate(); +} + +void TerrainHeightMap::endBrushStroke() +{ + PaintedGrid::endBrushStroke(); + + terrain->validate(); } diff --git a/src/definition/TerrainHeightMap.h b/src/definition/TerrainHeightMap.h index 63d7c6c..cf459b1 100644 --- a/src/definition/TerrainHeightMap.h +++ b/src/definition/TerrainHeightMap.h @@ -19,11 +19,14 @@ public: virtual double getInitialValue(double x, double y) const override; - void brushElevation(const PaintedGridBrush &brush, double x, double y, double value); - void brushSmooth(const PaintedGridBrush &brush, double x, double y, double value); - void brushAddNoise(const PaintedGridBrush &brush, double x, double y, NoiseGenerator* generator, double value); - void brushReset(const PaintedGridBrush &brush, double x, double y, double value); - void brushFlatten(const PaintedGridBrush &brush, double x, double y, double height, double force); + void brushElevation(const PaintedGridBrush &brush, double x, double y, double value, bool commit=false); + void brushSmooth(const PaintedGridBrush &brush, double x, double y, double value, bool commit=false); + void brushAddNoise(const PaintedGridBrush &brush, double x, double y, NoiseGenerator* generator, double value, bool commit=false); + void brushReset(const PaintedGridBrush &brush, double x, double y, double value, bool commit=false); + void brushFlatten(const PaintedGridBrush &brush, double x, double y, double height, double force, bool commit=false); + + virtual void clearPainting() override; + virtual void endBrushStroke() override; private: TerrainDefinition* terrain; diff --git a/src/tests/TerrainPainting_Test.cpp b/src/tests/TerrainPainting_Test.cpp index 6cf298d..c3a6af5 100644 --- a/src/tests/TerrainPainting_Test.cpp +++ b/src/tests/TerrainPainting_Test.cpp @@ -113,24 +113,24 @@ TEST_F(TerrainPainting_Test, brush_flatten) /* Test flattening center at 0.5 */ _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); - terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 1.0); + terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 1.0, true); _checkBrushResult(terrain, &brush, 0.5, 0.5, 0.5, 0.25, 0.0, 0.0, 0); /* Test brush strength */ terrain->height_map->clearPainting(); _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); - terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 0.01); + terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 0.01, true); _checkBrushResult(terrain, &brush, 0.005, 0.005, 0.005, 0.0025, 0.0, 0.0, 0); /* Test cumulative effect */ - terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 0.01); + terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 0.01, true); _checkBrushResult(terrain, &brush, 0.00995, 0.00995, 0.00995, 0.0049875, 0.0, 0.0, 0); /* Test with height modifier */ terrain->height = 10.0; terrain->height_map->clearPainting(); _checkBrushResult(terrain, &brush, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0); - terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 1.0); + terrain->height_map->brushFlatten(brush, 0.0, 0.0, 0.5, 1.0, true); _checkBrushResult(terrain, &brush, 0.05, 0.05, 0.05, 0.025, 0.0, 0.0, 0); } @@ -144,29 +144,29 @@ TEST_F(TerrainPainting_Test, brush_reset) /* Test resetting at center */ _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0); - terrain->height_map->brushFlatten(brush_full, 0.0, 0.0, 2.0, 1.0); + terrain->height_map->brushFlatten(brush_full, 0.0, 0.0, 2.0, 1.0, true); _checkBrushResult(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 0); - terrain->height_map->brushReset(brush, 0.0, 0.0, 1.0); + terrain->height_map->brushReset(brush, 0.0, 0.0, 1.0, true); _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.5, 2.0, 1.0, 0); /* Test brush strength */ terrain->height_map->clearPainting(); _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0); - terrain->height_map->brushFlatten(brush_full, 0.0, 0.0, 2.0, 1.0); + terrain->height_map->brushFlatten(brush_full, 0.0, 0.0, 2.0, 1.0, true); _checkBrushResult(terrain, &brush, 2.0, 2.0, 2.0, 2.0, 2.0, 1.0, 0); - terrain->height_map->brushReset(brush, 0.0, 0.0, 0.1); + terrain->height_map->brushReset(brush, 0.0, 0.0, 0.1, true); _checkBrushResult(terrain, &brush, 1.9, 1.9, 1.9, 1.95, 2.0, 1.0, 0); /* Test cumulative effect */ - terrain->height_map->brushReset(brush, 0.0, 0.0, 0.1); + terrain->height_map->brushReset(brush, 0.0, 0.0, 0.1, true); _checkBrushResult(terrain, &brush, 1.81, 1.81, 1.81, 1.9025, 2.0, 1.0, 0); /* Test with height modifier */ terrain->height = 10.0; terrain->height_map->clearPainting(); _checkBrushResult(terrain, &brush, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0); - terrain->height_map->brushFlatten(brush_full, 0.0, 0.0, 2.0, 1.0); + terrain->height_map->brushFlatten(brush_full, 0.0, 0.0, 2.0, 1.0, true); _checkBrushResult(terrain, &brush, 1.1, 1.1, 1.1, 1.1, 1.1, 1.0, 0); - terrain->height_map->brushReset(brush, 0.0, 0.0, 0.1); + terrain->height_map->brushReset(brush, 0.0, 0.0, 0.1, true); _checkBrushResult(terrain, &brush, 1.099, 1.099, 1.099, 1.0995, 1.1, 1.0, 0); }