Skip to content

Commit

Permalink
Make a single search window (#1193)
Browse files Browse the repository at this point in the history
Replaces the `Item` and `Room` versions of the 'go to' windows with a single 'Find' window which also includes `Trigger` searching.
Shortcut is changed to Ctrl + F, docs are updated.
Closes #1189
  • Loading branch information
chreden authored Oct 15, 2023
1 parent 5f92bab commit 766085c
Show file tree
Hide file tree
Showing 12 changed files with 87 additions and 137 deletions.
18 changes: 5 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ a level file using the File menu or drag and drop a level file onto the window.
Key|Action
---|------
CTRL + O | Open file
CTRL + G | Show 'go to room' box
CTRL + E | Show 'go to item' box
CTRL + F | Show find window
CTRL + R | Open Route window
CTRL + T | New Triggers window
CTRL + I | New Items window
Expand Down Expand Up @@ -177,19 +176,12 @@ Orthographic mode can be useful when paired with the compass selector to choose
### Reset
Reset the orbit camera to default rotation.

## Go To Room
_Shortcut: Ctrl+G_
## Find
_Shortcut: Ctrl+F_

Enter a room number and press enter to go to that room.
Enter the number or name to search through all available items, triggers or rooms. The results will be presented below and selecting them with the arrow keys or clicking them will select them in the viewer.

![Go To Room](doc/go_to_room.png)

## Go To Item
_Shortcut: Ctrl+E_

Enter an item number and press enter to go to that item.

![Go To Item](doc/go_to_item.png)
![Find](doc/find.png)

## Minimap

Expand Down
Binary file added doc/find.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed doc/go_to_item.png
Binary file not shown.
Binary file removed doc/go_to_room.png
Binary file not shown.
47 changes: 5 additions & 42 deletions trview.app.tests/UI/GoToTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,12 @@
using namespace trview;
using namespace trview::tests;

TEST(GoTo, Name)
{
GoTo window;
window.toggle_visible();

ASSERT_EQ(window.name(), "");
window.set_name("Item");
ASSERT_EQ(window.name(), "Item");

TestImgui imgui([&]() { window.render(); });
ASSERT_NE(imgui.find_window(imgui.popup_id("Go To Item").name()), nullptr);
}

TEST(GoTo, OnSelectedNotRaisedWhenMinusPressedAtZero)
{
GoTo window;
window.toggle_visible();
window.set_name("Item");

std::optional<uint32_t> raised;
auto token = window.on_selected += [&](auto value)
{
raised = value;
};

TestImgui imgui([&]() { window.render(); });
imgui.click_element(
imgui.popup_id("Go To Item").push("##gotoentry").id("-"),
false,
imgui.popup_id("Go To Item").id("##gotoentry"));

ASSERT_FALSE(raised.has_value());
}

TEST(GoTo, OnSelectedRaisedNumber)
{
GoTo window;
window.toggle_visible();
window.set_name("Item");
window.set_items({ { .number = 10, .name = "Room Ten" } });
std::optional<uint32_t> raised;
std::optional<GoTo::GoToItem> raised;
auto token = window.on_selected += [&](auto value)
{
raised = value;
Expand All @@ -59,17 +24,16 @@ TEST(GoTo, OnSelectedRaisedNumber)
imgui.render();

ASSERT_TRUE(raised.has_value());
ASSERT_EQ(raised.value(), 10u);
ASSERT_EQ(raised.value().number, 10u);
ASSERT_FALSE(window.visible());
}

TEST(GoTo, OnSelectedRaisedText)
{
GoTo window;
window.toggle_visible();
window.set_name("Item");
window.set_items({ {.number = 10, .name = "Room Ten" } });
std::optional<uint32_t> raised;
std::optional<GoTo::GoToItem> raised;
auto token = window.on_selected += [&](auto value)
{
raised = value;
Expand All @@ -84,17 +48,16 @@ TEST(GoTo, OnSelectedRaisedText)
imgui.render();

ASSERT_TRUE(raised.has_value());
ASSERT_EQ(raised.value(), 10u);
ASSERT_EQ(raised.value().number, 10u);
ASSERT_FALSE(window.visible());
}

TEST(GoTo, OnSelectedNotRaisedWhenCancelled)
{
GoTo window;
window.toggle_visible();
window.set_name("Item");

std::optional<uint32_t> raised;
std::optional<GoTo::GoToItem> raised;
auto token = window.on_selected += [&](auto value)
{
raised = value;
Expand Down
22 changes: 4 additions & 18 deletions trview.app.tests/Windows/ViewerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,34 +119,19 @@ TEST(Viewer, SelectItemRaisedForValidItem)

auto item = mock_shared<MockItem>();
auto level = mock_shared<MockLevel>();
EXPECT_CALL(*level, item(123)).WillRepeatedly(Return(item));

auto viewer = register_test_module().with_ui(std::move(ui_ptr)).build();
viewer->open(level, ILevel::OpenMode::Full);

std::shared_ptr<IItem> raised_item;
auto token = viewer->on_item_selected += [&raised_item](const auto& item) { raised_item = item.lock(); };

ui.on_select_item(123);
ui.on_select_item(item);

ASSERT_TRUE(raised_item);
ASSERT_EQ(raised_item, item);
}

/// Tests that the on_hide event from the UI is observed but not forwarded when the item is invalid.
TEST(Viewer, SelectItemNotRaisedForInvalidItem)
{
auto [ui_ptr, ui] = create_mock<MockViewerUI>();
auto viewer = register_test_module().with_ui(std::move(ui_ptr)).build();

std::shared_ptr<IItem> raised_item;
auto token = viewer->on_item_selected += [&raised_item](const auto& item) { raised_item = item.lock(); };

ui.on_select_item(0);

ASSERT_FALSE(raised_item);
}

/// Tests that the on_hide event from the UI is observed and forwarded when the item is valid.
TEST(Viewer, ItemVisibilityRaisedForValidItem)
{
Expand Down Expand Up @@ -198,13 +183,14 @@ TEST(Viewer, SelectRoomRaised)
auto [ui_ptr, ui] = create_mock<MockViewerUI>();
auto viewer = register_test_module().with_ui(std::move(ui_ptr)).build();

auto room = mock_shared<MockRoom>()->with_number(100);
std::optional<uint32_t> raised_room;
auto token = viewer->on_room_selected += [&raised_room](const auto& room) { raised_room = room; };

ui.on_select_room(0);
ui.on_select_room(room);

ASSERT_TRUE(raised_room.has_value());
ASSERT_EQ(raised_room.value(), 0u);
ASSERT_EQ(raised_room.value(), 100u);
}

/// Tests that the trigger selected event is raised when the user clicks on a trigger.
Expand Down
33 changes: 20 additions & 13 deletions trview.app/UI/GoTo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@

namespace trview
{
std::string GoTo::GoToItem::type() const
{
if (std::holds_alternative<std::weak_ptr<IItem>>(item))
{
return "Item";
}
else if (std::holds_alternative<std::weak_ptr<ITrigger>>(item))
{
return "Trigger";
}
else if (std::holds_alternative<std::weak_ptr<IRoom>>(item))
{
return "Room";
}
return "?";
}

bool GoTo::visible() const
{
return _visible;
Expand All @@ -15,16 +32,6 @@ namespace trview
_current_input.clear();
}

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

void GoTo::set_name(const std::string& name)
{
_name = name;
}

void GoTo::set_items(const std::vector<GoToItem>& items)
{
_items = items;
Expand All @@ -37,7 +44,7 @@ namespace trview
const auto viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->Pos + ImVec2(viewport->Size.x * 0.5f, viewport->Size.y * 0.25f), 0, ImVec2(0.5f, 0.5f));

const std::string id = std::format("Go To {}", _name);
const std::string id = "Find";
if (!_shown)
{
ImGui::OpenPopup(id.c_str());
Expand Down Expand Up @@ -107,7 +114,7 @@ namespace trview
list_focused = true;
}

const auto item_id = std::format("{} - {}", item.number, item.name);
const auto item_id = std::format("{} {} - {}", item.type(), item.number, item.name);
if (first_item &&
ImGui::GetCurrentContext()->NavId == ImGui::GetCurrentWindow()->GetID(item_id.c_str()) &&
ImGui::IsKeyPressed(ImGuiKey_UpArrow))
Expand All @@ -118,7 +125,7 @@ namespace trview

if (ImGui::Selectable(item_id.c_str(), false, ImGuiSelectableFlags_DontClosePopups | static_cast<int>(ImGuiSelectableFlags_SelectOnNav)))
{
on_selected(item.number);
on_selected(item);
}

any_selected |= ImGui::GetCurrentContext()->NavId == ImGui::GetCurrentWindow()->GetID(item_id.c_str());
Expand Down
17 changes: 9 additions & 8 deletions trview.app/UI/GoTo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,16 @@
#pragma once

#include <cstdint>
#include <variant>
#include <string>
#include <trview.common/Event.h>

namespace trview
{
struct IItem;
struct IRoom;
struct ITrigger;

/// This window presents the user with a box where they can enter the number of the thing
/// that they want to go to. Then when they press enter, that will be the selected.
class GoTo final
Expand All @@ -21,6 +26,9 @@ namespace trview
{
uint32_t number;
std::string name;
std::variant<std::weak_ptr<IItem>, std::weak_ptr<IRoom>, std::weak_ptr<ITrigger>> item;

std::string type() const;
};

/// Gets whether the window is currently visible.
Expand All @@ -32,19 +40,12 @@ namespace trview

/// Event raised when the user selects a new room. The newly selected room is passed as
/// a parameter when the event is raised.
Event<uint32_t> on_selected;

/// Get the name of the type of thing being selected.
std::string name() const;
Event<GoToItem> on_selected;

void render();

/// Set the name of the type of thing that is being gone to.
void set_name(const std::string& name);

void set_items(const std::vector<GoToItem>& items);
private:
std::string _name;
bool _visible{ false };
bool _shown{ false };
std::vector<GoToItem> _items;
Expand Down
8 changes: 5 additions & 3 deletions trview.app/UI/IViewerUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <trview.app/Camera/CameraMode.h>
#include <trview.app/Elements/ISector.h>
#include <trview.app/Elements/IRoom.h>
#include <trview.app/Geometry/PickInfo.h>
#include <trview.app/Settings/UserSettings.h>
#include "IContextMenu.h"
Expand All @@ -14,6 +13,9 @@

namespace trview
{
struct IRoom;
struct IItem;

enum class Tool
{
None,
Expand Down Expand Up @@ -73,10 +75,10 @@ namespace trview
Event<std::shared_ptr<ISector>> on_sector_hover;

/// Event raised when an item is selected.
Event<uint32_t> on_select_item;
Event<std::weak_ptr<IItem>> on_select_item;

/// Event raised when a room is selected.
Event<int32_t> on_select_room;
Event<std::weak_ptr<IRoom>> on_select_room;

/// Event raised when the user settings are changed.
Event<UserSettings> on_settings;
Expand Down
Loading

0 comments on commit 766085c

Please sign in to comment.