diff --git a/trlevel/Level.cpp b/trlevel/Level.cpp index c63d35017..555c02172 100644 --- a/trlevel/Level.cpp +++ b/trlevel/Level.cpp @@ -394,8 +394,7 @@ namespace trlevel _log->clear(); // Load the level from the file. - auto last_index = std::min(filename.find_last_of('\\'), filename.find_last_of('/')); - _name = last_index == filename.npos ? filename : filename.substr(std::min(last_index + 1, filename.size())); + _name = trview::filename_without_path(filename); trview::Activity activity(log, "IO", "Load Level " + _name); diff --git a/trview.app.tests/Windows/ViewerTests.cpp b/trview.app.tests/Windows/ViewerTests.cpp index b7b8e78b9..14df5777e 100644 --- a/trview.app.tests/Windows/ViewerTests.cpp +++ b/trview.app.tests/Windows/ViewerTests.cpp @@ -801,6 +801,19 @@ TEST(Viewer, ReloadLevelSyncProperties) viewer->open(reloaded, ILevel::OpenMode::Reload); } +TEST(Viewer, ReloadSetsLevelOnUi) +{ + auto original = mock_shared(); + auto reloaded = mock_shared(); + auto [ui_ptr, ui] = create_mock(); + EXPECT_CALL(ui, set_level).Times(2); + + auto viewer = register_test_module().with_ui(std::move(ui_ptr)).build(); + + viewer->open(original, ILevel::OpenMode::Full); + viewer->open(reloaded, ILevel::OpenMode::Reload); +} + TEST(Viewer, CopyPosition) { auto [ui_ptr, ui] = create_mock(); diff --git a/trview.app/Elements/ILevel.h b/trview.app/Elements/ILevel.h index 10bfaeadd..d4cdd3138 100644 --- a/trview.app/Elements/ILevel.h +++ b/trview.app/Elements/ILevel.h @@ -57,6 +57,7 @@ namespace trview virtual std::weak_ptr light(uint32_t index) const = 0; virtual std::vector> lights() const = 0; virtual MapColours map_colours() const = 0; + virtual std::string name() const = 0; virtual uint32_t neighbour_depth() const = 0; /// Get the number of rooms in the level. virtual uint32_t number_of_rooms() const = 0; diff --git a/trview.app/Elements/Level.cpp b/trview.app/Elements/Level.cpp index a6689ba83..1799a8b4d 100644 --- a/trview.app/Elements/Level.cpp +++ b/trview.app/Elements/Level.cpp @@ -117,6 +117,11 @@ namespace trview return entities; } + std::string Level::name() const + { + return _name; + } + uint32_t Level::neighbour_depth() const { return _neighbour_depth; @@ -1279,6 +1284,7 @@ namespace trview { _version = level->get_version(); _floor_data = level->get_floor_data_all(); + _name = level->name(); record_models(*level); generate_rooms(*level, room_source, *mesh_storage); diff --git a/trview.app/Elements/Level.h b/trview.app/Elements/Level.h index 817217115..898608c81 100644 --- a/trview.app/Elements/Level.h +++ b/trview.app/Elements/Level.h @@ -42,6 +42,7 @@ namespace trview std::weak_ptr selected_room() const override; virtual std::weak_ptr item(uint32_t index) const override; virtual std::vector> items() const override; + std::string name() const override; virtual uint32_t neighbour_depth() const override; virtual uint32_t number_of_rooms() const override; virtual std::vector> rooms() const override; @@ -200,6 +201,7 @@ namespace trview std::vector _floor_data; std::set _models; TokenStore _token_store; + std::string _name; }; /// Find the first item with the type id specified. diff --git a/trview.app/Mocks/Elements/ILevel.h b/trview.app/Mocks/Elements/ILevel.h index b4020af0a..5aa150f00 100644 --- a/trview.app/Mocks/Elements/ILevel.h +++ b/trview.app/Mocks/Elements/ILevel.h @@ -25,6 +25,7 @@ namespace trview MOCK_METHOD(std::vector, level_textures, (), (const, override)); MOCK_METHOD(std::weak_ptr, light, (uint32_t), (const, override)); MOCK_METHOD(std::vector>, lights, (), (const, override)); + MOCK_METHOD(std::string, name, (), (const, override)); MOCK_METHOD(uint32_t, neighbour_depth, (), (const, override)); MOCK_METHOD(uint32_t, number_of_rooms, (), (const, override)); MOCK_METHOD(void, on_camera_moved, (), (override)); diff --git a/trview.app/Mocks/UI/IViewerUI.h b/trview.app/Mocks/UI/IViewerUI.h index 4bfe06fd5..b9252b049 100644 --- a/trview.app/Mocks/UI/IViewerUI.h +++ b/trview.app/Mocks/UI/IViewerUI.h @@ -24,7 +24,7 @@ namespace trview MOCK_METHOD(void, set_flip_enabled, (bool), (override)); MOCK_METHOD(void, set_hide_enabled, (bool), (override)); MOCK_METHOD(void, set_host_size, (const Size&), (override)); - MOCK_METHOD(void, set_level, (const std::string&, const std::weak_ptr&), (override)); + MOCK_METHOD(void, set_level, (const std::weak_ptr&), (override)); MOCK_METHOD(void, set_max_rooms, (uint32_t), (override)); MOCK_METHOD(void, set_measure_distance, (float), (override)); MOCK_METHOD(void, set_measure_position, (const Point&), (override)); diff --git a/trview.app/UI/IViewerUI.h b/trview.app/UI/IViewerUI.h index 265c84091..521ad15d9 100644 --- a/trview.app/UI/IViewerUI.h +++ b/trview.app/UI/IViewerUI.h @@ -149,9 +149,8 @@ namespace trview virtual void set_host_size(const Size& size) = 0; /// Set the level name and version. - /// @param name The file. /// @param version The version of the level. - virtual void set_level(const std::string& name, const std::weak_ptr& level) = 0; + virtual void set_level(const std::weak_ptr& level) = 0; /// Set the maximum number of rooms in the level. /// @param rooms The number of rooms that are in the level. diff --git a/trview.app/UI/ViewerUI.cpp b/trview.app/UI/ViewerUI.cpp index b4f22876c..95f7d7812 100644 --- a/trview.app/UI/ViewerUI.cpp +++ b/trview.app/UI/ViewerUI.cpp @@ -311,15 +311,19 @@ namespace trview _map_renderer->set_window_size(size); } - void ViewerUI::set_level(const std::string& name, const std::weak_ptr& level) + void ViewerUI::set_level(const std::weak_ptr& level) { _level = level; - _level_info->set_level(name); _map_renderer->load({}); if (auto new_level = _level.lock()) { + _level_info->set_level(new_level->name()); _level_info->set_level_version(new_level->version()); } + else + { + _level_info->set_level(""); + } } void ViewerUI::set_max_rooms(uint32_t rooms) diff --git a/trview.app/UI/ViewerUI.h b/trview.app/UI/ViewerUI.h index b1b0c06b7..b3d61dfbe 100644 --- a/trview.app/UI/ViewerUI.h +++ b/trview.app/UI/ViewerUI.h @@ -46,7 +46,7 @@ namespace trview /// Set the size of the host window. void set_host_size(const Size& size) override; - void set_level(const std::string& name, const std::weak_ptr& level) override; + void set_level(const std::weak_ptr& level) override; virtual void set_max_rooms(uint32_t rooms) override; virtual void set_measure_distance(float distance) override; virtual void set_measure_position(const Point& position) override; diff --git a/trview.app/Windows/Viewer.cpp b/trview.app/Windows/Viewer.cpp index dd3f86cd6..c1f8c9edc 100644 --- a/trview.app/Windows/Viewer.cpp +++ b/trview.app/Windows/Viewer.cpp @@ -635,15 +635,11 @@ namespace trview // Set up the views. auto rooms = new_level->rooms(); + _ui->set_level(new_level); + window().set_title("trview - " + new_level->name()); + if (open_mode == ILevel::OpenMode::Full || !old_level) { - // Strip the last part of the path away. - const auto filename = new_level->filename(); - auto last_index = std::min(filename.find_last_of('\\'), filename.find_last_of('/')); - auto name = last_index == filename.npos ? filename : filename.substr(std::min(last_index + 1, filename.size())); - _ui->set_level(name, new_level); - window().set_title("trview - " + name); - _camera.reset(); _ui->set_toggle(Options::highlight, false); _ui->set_toggle(Options::flip, false); diff --git a/trview.common/Strings.cpp b/trview.common/Strings.cpp index 7b9ce78e9..966e01b7c 100644 --- a/trview.common/Strings.cpp +++ b/trview.common/Strings.cpp @@ -42,4 +42,10 @@ namespace trview [](unsigned char c) { return static_cast(std::tolower(c)); }); return result; } + + std::string filename_without_path(const std::string& filename) + { + auto last_index = std::min(filename.find_last_of('\\'), filename.find_last_of('/')); + return last_index == filename.npos ? filename : filename.substr(std::min(last_index + 1, filename.size())); + } } diff --git a/trview.common/Strings.h b/trview.common/Strings.h index 3533166b0..3b23113b5 100644 --- a/trview.common/Strings.h +++ b/trview.common/Strings.h @@ -38,6 +38,8 @@ namespace trview constexpr std::string get_string(T&& value); std::string join(std::ranges::input_range auto&& range); + + std::string filename_without_path(const std::string& filename); } #include "Strings.inl" diff --git a/trview.common/stdafx.h b/trview.common/stdafx.h index 552884748..d0cf416bf 100644 Binary files a/trview.common/stdafx.h and b/trview.common/stdafx.h differ