yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dungeon_room_nav_widget.cc
Go to the documentation of this file.
2
3#include <string>
4
5#include "imgui/imgui.h"
8
9namespace yaze::editor {
10
11namespace {
12
13constexpr int kRoomMatrixCols = 16;
14constexpr int kRoomMatrixRows = 19;
15std::optional<int> RoomIfValid(int candidate) {
16 if (candidate < 0 || candidate >= zelda3::kNumberOfRooms) {
17 return std::nullopt;
18 }
19 return candidate;
20}
21
22std::string MakeTooltip(const std::optional<int>& target,
23 const char* direction) {
24 if (!target.has_value()) {
25 return "";
26 }
27 const auto label = zelda3::GetRoomLabel(*target);
28 char buf[192];
29 snprintf(buf, sizeof(buf), "%s: [%03X] %s", direction, *target,
30 label.c_str());
31 return buf;
32}
33
34bool ArrowButtonWithTooltip(const char* id, ImGuiDir dir,
35 const std::optional<int>& target,
36 const std::string& tooltip,
37 const std::function<void(int)>& on_navigate) {
38 const bool enabled = target.has_value();
39 if (!enabled) {
40 ImGui::BeginDisabled();
41 }
42 const bool pressed = ImGui::ArrowButton(id, dir);
43 if (!enabled) {
44 ImGui::EndDisabled();
45 }
46 if (enabled && ImGui::IsItemHovered() && !tooltip.empty()) {
47 ImGui::SetTooltip("%s", tooltip.c_str());
48 }
49 if (pressed && enabled && on_navigate) {
50 on_navigate(*target);
51 return true;
52 }
53 return false;
54}
55
56} // namespace
57
59 int room_id) {
60 if (room_id < 0 || room_id >= zelda3::kNumberOfRooms) {
61 return {};
62 }
63
64 const int col = room_id % kRoomMatrixCols;
65 const int row = room_id / kRoomMatrixCols;
66
67 // Note: the matrix has empty slots in the final row; RoomIfValid handles it.
68 Neighbors out;
69 out.west = RoomIfValid(col > 0 ? room_id - 1 : -1);
70 out.east = RoomIfValid(col < (kRoomMatrixCols - 1) ? room_id + 1 : -1);
71 out.north = RoomIfValid(row > 0 ? room_id - kRoomMatrixCols : -1);
72 out.south =
73 RoomIfValid(row < (kRoomMatrixRows - 1) ? room_id + kRoomMatrixCols : -1);
74 return out;
75}
76
77bool DungeonRoomNavWidget::Draw(const char* id, int room_id,
78 const std::function<void(int)>& on_navigate) {
79 ImGui::PushID(id);
80
81 const Neighbors n = GetNeighbors(room_id);
82 const std::string tip_w = MakeTooltip(n.west, "West");
83 const std::string tip_n = MakeTooltip(n.north, "North");
84 const std::string tip_s = MakeTooltip(n.south, "South");
85 const std::string tip_e = MakeTooltip(n.east, "East");
86
87 bool navigated = false;
88 navigated |=
89 ArrowButtonWithTooltip("West", ImGuiDir_Left, n.west, tip_w, on_navigate);
90 ImGui::SameLine();
91 navigated |=
92 ArrowButtonWithTooltip("North", ImGuiDir_Up, n.north, tip_n, on_navigate);
93 ImGui::SameLine();
94 navigated |= ArrowButtonWithTooltip("South", ImGuiDir_Down, n.south, tip_s,
95 on_navigate);
96 ImGui::SameLine();
97 navigated |= ArrowButtonWithTooltip("East", ImGuiDir_Right, n.east, tip_e,
98 on_navigate);
99
100 ImGui::PopID();
101 return navigated;
102}
103
104} // namespace yaze::editor
static bool Draw(const char *id, int room_id, const std::function< void(int)> &on_navigate)
static Neighbors GetNeighbors(int room_id)
std::string MakeTooltip(const std::optional< int > &target, const char *direction)
bool ArrowButtonWithTooltip(const char *id, ImGuiDir dir, const std::optional< int > &target, const std::string &tooltip, const std::function< void(int)> &on_navigate)
Editors are the view controllers for the application.
std::string GetRoomLabel(int id)
Convenience function to get a room label.
constexpr int kNumberOfRooms