Skip to content

Commit

Permalink
Always set level on UI on open or reload
Browse files Browse the repository at this point in the history
Make sure to always pass through the new level to the UI. Previously it was only being passed on a full load, not a reload.
Move filename without path function to a common place, call it from level and remove from viewer (gets it from level now).
Closes #1234
  • Loading branch information
chreden committed Apr 23, 2024
1 parent 540300f commit 33d4d8a
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 15 deletions.
3 changes: 1 addition & 2 deletions trlevel/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 13 additions & 0 deletions trview.app.tests/Windows/ViewerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,19 @@ TEST(Viewer, ReloadLevelSyncProperties)
viewer->open(reloaded, ILevel::OpenMode::Reload);
}

TEST(Viewer, ReloadSetsLevelOnUi)
{
auto original = mock_shared<MockLevel>();
auto reloaded = mock_shared<MockLevel>();
auto [ui_ptr, ui] = create_mock<MockViewerUI>();
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<MockViewerUI>();
Expand Down
1 change: 1 addition & 0 deletions trview.app/Elements/ILevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ namespace trview
virtual std::weak_ptr<ILight> light(uint32_t index) const = 0;
virtual std::vector<std::weak_ptr<ILight>> 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;
Expand Down
6 changes: 6 additions & 0 deletions trview.app/Elements/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ namespace trview
return entities;
}

std::string Level::name() const
{
return _name;
}

uint32_t Level::neighbour_depth() const
{
return _neighbour_depth;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions trview.app/Elements/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace trview
std::weak_ptr<IRoom> selected_room() const override;
virtual std::weak_ptr<IItem> item(uint32_t index) const override;
virtual std::vector<std::weak_ptr<IItem>> 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<std::weak_ptr<IRoom>> rooms() const override;
Expand Down Expand Up @@ -200,6 +201,7 @@ namespace trview
std::vector<uint16_t> _floor_data;
std::set<uint32_t> _models;
TokenStore _token_store;
std::string _name;
};

/// Find the first item with the type id specified.
Expand Down
1 change: 1 addition & 0 deletions trview.app/Mocks/Elements/ILevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace trview
MOCK_METHOD(std::vector<graphics::Texture>, level_textures, (), (const, override));
MOCK_METHOD(std::weak_ptr<ILight>, light, (uint32_t), (const, override));
MOCK_METHOD(std::vector<std::weak_ptr<ILight>>, 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));
Expand Down
2 changes: 1 addition & 1 deletion trview.app/Mocks/UI/IViewerUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILevel>&), (override));
MOCK_METHOD(void, set_level, (const std::weak_ptr<ILevel>&), (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));
Expand Down
3 changes: 1 addition & 2 deletions trview.app/UI/IViewerUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILevel>& level) = 0;
virtual void set_level(const std::weak_ptr<ILevel>& level) = 0;

/// Set the maximum number of rooms in the level.
/// @param rooms The number of rooms that are in the level.
Expand Down
8 changes: 6 additions & 2 deletions trview.app/UI/ViewerUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,15 +311,19 @@ namespace trview
_map_renderer->set_window_size(size);
}

void ViewerUI::set_level(const std::string& name, const std::weak_ptr<ILevel>& level)
void ViewerUI::set_level(const std::weak_ptr<ILevel>& 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)
Expand Down
2 changes: 1 addition & 1 deletion trview.app/UI/ViewerUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ILevel>& level) override;
void set_level(const std::weak_ptr<ILevel>& 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;
Expand Down
10 changes: 3 additions & 7 deletions trview.app/Windows/Viewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions trview.common/Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ namespace trview
[](unsigned char c) { return static_cast<char>(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()));
}
}
2 changes: 2 additions & 0 deletions trview.common/Strings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Binary file modified trview.common/stdafx.h
Binary file not shown.

0 comments on commit 33d4d8a

Please sign in to comment.