Skip to content

Commit

Permalink
fix: prefer extracted function
Browse files Browse the repository at this point in the history
EventManager.{c.h}xx:
* Added namespace: `detail`.
* Added function: updatePlacementMode
* Added test cases for: EventManager: update_placement_mode.
  • Loading branch information
tcoyvwac committed Sep 26, 2022
1 parent c340376 commit 07ed006
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 18 deletions.
44 changes: 26 additions & 18 deletions src/engine/EventManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@
#include "microprofile/microprofile.h"
#endif

namespace detail
{
std::optional<PlacementMode> updatePlacementMode(Layer topMostActiveLayer)
{
switch (topMostActiveLayer)
{
case Layer::BUILDINGS:
return PlacementMode::SINGLE;
case Layer::ROAD:
case Layer::POWERLINES:
case Layer::UNDERGROUND:
return PlacementMode::LINE;
case Layer::GROUND_DECORATION:
case Layer::WATER:
case Layer::ZONE:
return PlacementMode::RECTANGLE;
default:
return {};
}
}
} // namespace detail

using namespace detail;

void EventManager::unHighlightNodes()
{
if (!m_isPuttingTile)
Expand Down Expand Up @@ -53,24 +77,8 @@ void EventManager::pickTileUnderCursor(Point mouseIsoCoords)
if (topMostActiveLayer == Layer::TERRAIN || topMostActiveLayer == Layer::NONE)
return;
// update placement mode
switch (topMostActiveLayer)
{
case Layer::BUILDINGS:
GameStates::instance().placementMode = PlacementMode::SINGLE;
break;
case Layer::ROAD:
case Layer::POWERLINES:
case Layer::UNDERGROUND:
GameStates::instance().placementMode = PlacementMode::LINE;
break;
case Layer::GROUND_DECORATION:
case Layer::WATER:
case Layer::ZONE:
GameStates::instance().placementMode = PlacementMode::RECTANGLE;
break;
default:
break;
}
if (const auto newPlacementMode = updatePlacementMode(topMostActiveLayer))
GameStates::instance().placementMode = *newPlacementMode;
mapNodeData = node.getMapNodeData();
tileToPlace = mapNodeData[topMostActiveLayer].tileID;
highlightSelection = true;
Expand Down
7 changes: 7 additions & 0 deletions src/engine/EventManager.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,16 @@

#include <SDL.h>
#include <set>
#include <optional>

#include <Point.hxx>
#include "../util/Singleton.hxx"
#include "common/enums.hxx"

namespace detail
{
std::optional<PlacementMode> updatePlacementMode(Layer topMostActiveLayer);
}

class UIElement;

Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ LIST(APPEND TEST_SOURCES
engine/basics/Point.cxx
engine/basics/Settings.cxx
engine/ResourcesManager.cxx
engine/EventManager.cxx
engine/MapFunctions.cxx
engine/UIManager.cxx
engine/WindowManager.cxx
Expand Down
32 changes: 32 additions & 0 deletions tests/engine/EventManager.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <catch.hpp>
#include "../../src/engine/EventManager.hxx"

using string = std::string;

TEST_CASE("I selected POWERLINES, update placement method to the LINE tool", "[engine][eventmanager]")
{
const auto topMostActiveLayer = POWERLINES;
const auto new_placement_mode = detail::updatePlacementMode(topMostActiveLayer);
CHECK(new_placement_mode == PlacementMode::LINE);
}

TEST_CASE("I selected BUILDINGS, update placement method to the SINGLE tool", "[engine][eventmanager]")
{
const auto topMostActiveLayer = BUILDINGS;
const auto new_placement_mode = detail::updatePlacementMode(topMostActiveLayer);
CHECK(new_placement_mode == PlacementMode::SINGLE);
}

TEST_CASE("I selected ZONE, update placement method to the RECTANGLE tool", "[engine][eventmanager]")
{
const auto topMostActiveLayer = ZONE;
const auto new_placement_mode = detail::updatePlacementMode(topMostActiveLayer);
CHECK(new_placement_mode == PlacementMode::RECTANGLE);
}

TEST_CASE("I selected TERRAIN, do not update placement tool, this is an invalid entry", "[engine][eventmanager]")
{
const auto topMostActiveLayer = TERRAIN;
const auto new_placement_mode = detail::updatePlacementMode(topMostActiveLayer);
CHECK(new_placement_mode == std::nullopt);
}

0 comments on commit 07ed006

Please sign in to comment.