Skip to content

Commit

Permalink
#5746: Add optional parameter to Grid interface methods retrieving th…
Browse files Browse the repository at this point in the history
…e spacing and the grid power.

Add getGridBase() method.
Add the first few unit tests.
  • Loading branch information
codereader committed Sep 25, 2021
1 parent 6658303 commit 6689955
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 11 deletions.
23 changes: 18 additions & 5 deletions include/igrid.h
Expand Up @@ -48,6 +48,14 @@ inline const char* getStringForSize(GridSize size)
};
}

// The space the grid is dividing. Regular map editing is using the
// World grid, while the Texture Tool is working in UV space.
enum class Space
{
World,
Texture,
};

}

// grid renderings
Expand All @@ -62,7 +70,7 @@ enum GridLook
GRIDLOOK_SQUARES,
};

const char* const MODULE_GRID("Grid");
constexpr const char* const MODULE_GRID("Grid");

class IGridManager :
public RegisterableModule
Expand All @@ -71,9 +79,15 @@ class IGridManager :
virtual ~IGridManager() {}

virtual void setGridSize(GridSize gridSize) = 0;
virtual float getGridSize() const = 0;

// Returns the grid spacing in units of the given space
virtual float getGridSize(grid::Space = grid::Space::World) const = 0;

virtual int getGridPower() const = 0;
// Returns the grid power of the currently active grid size
virtual int getGridPower(grid::Space = grid::Space::World) const = 0;

// Returns the base number the exponent is applied to (e.g. 2)
virtual int getGridBase(grid::Space = grid::Space::World) const = 0;

virtual void gridDown() = 0;
virtual void gridUp() = 0;
Expand All @@ -83,8 +97,7 @@ class IGridManager :

/// Signal emitted when the grid is changed
virtual sigc::signal<void> signal_gridChanged() const = 0;

}; // class IGridManager
};

// This is the accessor for the grid module
inline IGridManager& GlobalGrid()
Expand Down
25 changes: 21 additions & 4 deletions radiantcore/grid/GridManager.cpp
Expand Up @@ -225,14 +225,31 @@ void GridManager::setGridSize(GridSize gridSize)
gridChangeNotify();
}

float GridManager::getGridSize() const
float GridManager::getGridSize(grid::Space space) const
{
return pow(2.0f, static_cast<float>(_activeGridSize));
return pow(2.0f, static_cast<float>(getGridPower(space)));
}

int GridManager::getGridPower() const
int GridManager::getGridPower(grid::Space space) const
{
return static_cast<int>(_activeGridSize);
int power = static_cast<int>(_activeGridSize);

// Texture space is using smaller grid sizes, since it doesn't make much
// sense to have grid spacing greater than 1.0
if (space == grid::Space::Texture)
{
power -= 7;

if (power < -7) power = -7;
if (power > 0) power = 0;
}

return power;
}

int GridManager::getGridBase(grid::Space space) const
{
return 2;
}

GridLook GridManager::getLookFromNumber(int i)
Expand Down
5 changes: 3 additions & 2 deletions radiantcore/grid/GridManager.h
Expand Up @@ -31,9 +31,10 @@ class GridManager :
void gridDown() override;

void setGridSize(GridSize gridSize) override;
float getGridSize() const override;
float getGridSize(grid::Space space) const override;

int getGridPower() const override;
int getGridPower(grid::Space space) const override;
int getGridBase(grid::Space space) const override;

GridLook getMajorLook() const override;
GridLook getMinorLook() const override;
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Expand Up @@ -9,6 +9,7 @@ add_executable(drtest
Entity.cpp
Favourites.cpp
FileTypes.cpp
Grid.cpp
HeadlessOpenGLContext.cpp
ImageLoading.cpp
LayerManipulation.cpp
Expand Down
92 changes: 92 additions & 0 deletions test/Grid.cpp
@@ -0,0 +1,92 @@
#include "RadiantTest.h"

#include "igrid.h"

namespace test
{

using GridTest = RadiantTest;

inline void checkGridSize(GridSize size, grid::Space space, float expectedSize)
{
GlobalGrid().setGridSize(size);
EXPECT_EQ(GlobalGrid().getGridSize(space), expectedSize);
}

inline void checkGridPower(GridSize size, grid::Space space, int expectedPower)
{
GlobalGrid().setGridSize(size);
EXPECT_EQ(GlobalGrid().getGridPower(space), expectedPower);
}

TEST_F(GridTest, GridBase)
{
EXPECT_EQ(GlobalGrid().getGridBase(grid::Space::World), 2);
EXPECT_EQ(GlobalGrid().getGridBase(grid::Space::Texture), 2);
}

TEST_F(GridTest, WorldGridSize)
{
checkGridSize(GRID_0125, grid::Space::World, 0.125f);
checkGridSize(GRID_025, grid::Space::World, 0.25f);
checkGridSize(GRID_05, grid::Space::World, 0.5f);
checkGridSize(GRID_1, grid::Space::World, 1.0f);
checkGridSize(GRID_2, grid::Space::World, 2.0f);
checkGridSize(GRID_4, grid::Space::World, 4.0f);
checkGridSize(GRID_8, grid::Space::World, 8.0f);
checkGridSize(GRID_16, grid::Space::World, 16.0f);
checkGridSize(GRID_32, grid::Space::World, 32.0f);
checkGridSize(GRID_64, grid::Space::World, 64.0f);
checkGridSize(GRID_128, grid::Space::World, 128.0f);
checkGridSize(GRID_256, grid::Space::World, 256.0f);
}

TEST_F(GridTest, WorldGridPower)
{
checkGridPower(GRID_0125, grid::Space::World, -3);
checkGridPower(GRID_025, grid::Space::World, -2);
checkGridPower(GRID_05, grid::Space::World, -1);
checkGridPower(GRID_1, grid::Space::World, 0);
checkGridPower(GRID_2, grid::Space::World, 1);
checkGridPower(GRID_4, grid::Space::World, 2);
checkGridPower(GRID_8, grid::Space::World, 3);
checkGridPower(GRID_16, grid::Space::World, 4);
checkGridPower(GRID_32, grid::Space::World, 5);
checkGridPower(GRID_64, grid::Space::World, 6);
checkGridPower(GRID_128, grid::Space::World, 7);
checkGridPower(GRID_256, grid::Space::World, 8);
}

TEST_F(GridTest, TextureGridSize)
{
checkGridSize(GRID_0125, grid::Space::Texture, 0.0078125f);
checkGridSize(GRID_025, grid::Space::Texture, 0.0078125f);
checkGridSize(GRID_05, grid::Space::Texture, 0.0078125f);
checkGridSize(GRID_1, grid::Space::Texture, 0.0078125f); // lower bound
checkGridSize(GRID_2, grid::Space::Texture, 0.015625f);
checkGridSize(GRID_4, grid::Space::Texture, 0.03125f);
checkGridSize(GRID_8, grid::Space::Texture, 0.0625f);
checkGridSize(GRID_16, grid::Space::Texture, 0.125f);
checkGridSize(GRID_32, grid::Space::Texture, 0.25f);
checkGridSize(GRID_64, grid::Space::Texture, 0.5f);
checkGridSize(GRID_128, grid::Space::Texture, 1.0f); // upper bound
checkGridSize(GRID_256, grid::Space::Texture, 1.0f);
}

TEST_F(GridTest, TextureGridPower)
{
checkGridPower(GRID_0125, grid::Space::Texture, -7);
checkGridPower(GRID_025, grid::Space::Texture, -7);
checkGridPower(GRID_05, grid::Space::Texture, -7);
checkGridPower(GRID_1, grid::Space::Texture, -7); // lower bound
checkGridPower(GRID_2, grid::Space::Texture, -6);
checkGridPower(GRID_4, grid::Space::Texture, -5);
checkGridPower(GRID_8, grid::Space::Texture, -4);
checkGridPower(GRID_16, grid::Space::Texture, -3);
checkGridPower(GRID_32, grid::Space::Texture, -2);
checkGridPower(GRID_64, grid::Space::Texture, -1);
checkGridPower(GRID_128, grid::Space::Texture, 0); // upper bound
checkGridPower(GRID_256, grid::Space::Texture, 0);
}

}
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj
Expand Up @@ -80,6 +80,7 @@
<ClCompile Include="..\..\..\test\Entity.cpp" />
<ClCompile Include="..\..\..\test\Favourites.cpp" />
<ClCompile Include="..\..\..\test\FileTypes.cpp" />
<ClCompile Include="..\..\..\test\Grid.cpp" />
<ClCompile Include="..\..\..\test\HeadlessOpenGLContext.cpp" />
<ClCompile Include="..\..\..\test\ImageLoading.cpp" />
<ClCompile Include="..\..\..\test\LayerManipulation.cpp" />
Expand Down
1 change: 1 addition & 0 deletions tools/msvc/Tests/Tests.vcxproj.filters
Expand Up @@ -48,6 +48,7 @@
<Filter>math</Filter>
</ClCompile>
<ClCompile Include="..\..\..\test\TextureTool.cpp" />
<ClCompile Include="..\..\..\test\Grid.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\..\test\HeadlessOpenGLContext.h" />
Expand Down

0 comments on commit 6689955

Please sign in to comment.