From ff7e6d6309d271557698582e35fa41048874da6f Mon Sep 17 00:00:00 2001 From: chreden <4263940+chreden@users.noreply.github.com> Date: Thu, 9 May 2024 22:54:04 +0100 Subject: [PATCH] Check whether alternate group exists before setting it If user presses an alternate group hotkey check whether the group actually exists in the level before setting it on the level and UI. Closes #1237 --- trview.app.tests/Windows/ViewerTests.cpp | 29 ++++++++++++++++++++++++ trview.app/Windows/Viewer.cpp | 9 +++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/trview.app.tests/Windows/ViewerTests.cpp b/trview.app.tests/Windows/ViewerTests.cpp index 14df5777e..7ff3ceb0f 100644 --- a/trview.app.tests/Windows/ViewerTests.cpp +++ b/trview.app.tests/Windows/ViewerTests.cpp @@ -1082,3 +1082,32 @@ TEST(Viewer, SectorHighlightForwarded) auto sector = mock_shared()->with_room(room); ui.on_sector_hover(sector); } + +TEST(Viewer, AlternateGroupForwarded) +{ + auto level = mock_shared(); + EXPECT_CALL(*level, set_alternate_group(6, true)).Times(1); + ON_CALL(*level, alternate_groups).WillByDefault(Return(std::set{ 6 })); + + auto [ui_ptr, ui] = create_mock(); + EXPECT_CALL(ui, set_alternate_group(6, true)).Times(1); + + auto viewer = register_test_module().with_ui(std::move(ui_ptr)).build(); + viewer->open(level, ILevel::OpenMode::Full); + + ui.on_alternate_group(6, true); +} + +TEST(Viewer, InvalidAlternateGroupNotForwarded) +{ + auto level = mock_shared(); + EXPECT_CALL(*level, set_alternate_group).Times(0); + + auto [ui_ptr, ui] = create_mock(); + EXPECT_CALL(ui, set_alternate_group).Times(0); + + auto viewer = register_test_module().with_ui(std::move(ui_ptr)).build(); + viewer->open(level, ILevel::OpenMode::Full); + + ui.on_alternate_group(6, true); +} diff --git a/trview.app/Windows/Viewer.cpp b/trview.app/Windows/Viewer.cpp index c1f8c9edc..357946388 100644 --- a/trview.app/Windows/Viewer.cpp +++ b/trview.app/Windows/Viewer.cpp @@ -960,9 +960,12 @@ namespace trview { if (auto level = _level.lock()) { - _was_alternate_select = true; - level->set_alternate_group(group, enabled); - _ui->set_alternate_group(group, enabled); + if (std::ranges::contains(level->alternate_groups(), group)) + { + _was_alternate_select = true; + level->set_alternate_group(group, enabled); + _ui->set_alternate_group(group, enabled); + } } }