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;
15
16std::optional<int> RoomIfValid(int candidate) {
17 if (candidate < 0 || candidate >= zelda3::kNumberOfRooms) {
18 return std::nullopt;
19 }
20 return candidate;
21}
22
23std::string MakeTooltip(const std::optional<int>& target,
24 const char* direction) {
25 if (!target.has_value()) {
26 return "";
27 }
28 const auto label = zelda3::GetRoomLabel(*target);
29 char buf[192];
30 snprintf(buf, sizeof(buf), "%s: [%03X] %s", direction, *target, 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 if (room_id < 0 || room_id >= zelda3::kNumberOfRooms) {
60 return {};
61 }
62
63 const int col = room_id % kRoomMatrixCols;
64 const int row = room_id / kRoomMatrixCols;
65
66 // Note: the matrix has empty slots in the final row; RoomIfValid handles it.
67 Neighbors out;
68 out.west = RoomIfValid(col > 0 ? room_id - 1 : -1);
69 out.east = RoomIfValid(col < (kRoomMatrixCols - 1) ? room_id + 1 : -1);
70 out.north = RoomIfValid(row > 0 ? room_id - kRoomMatrixCols : -1);
71 out.south = RoomIfValid(row < (kRoomMatrixRows - 1) ? room_id + kRoomMatrixCols
72 : -1);
73 return out;
74}
75
76bool DungeonRoomNavWidget::Draw(const char* id, int room_id,
77 const std::function<void(int)>& on_navigate) {
78 ImGui::PushID(id);
79
80 const Neighbors n = GetNeighbors(room_id);
81 const std::string tip_w = MakeTooltip(n.west, "West");
82 const std::string tip_n = MakeTooltip(n.north, "North");
83 const std::string tip_s = MakeTooltip(n.south, "South");
84 const std::string tip_e = MakeTooltip(n.east, "East");
85
86 bool navigated = false;
87 navigated |= ArrowButtonWithTooltip("West", ImGuiDir_Left, n.west, tip_w,
88 on_navigate);
89 ImGui::SameLine();
90 navigated |= ArrowButtonWithTooltip("North", ImGuiDir_Up, n.north, tip_n,
91 on_navigate);
92 ImGui::SameLine();
93 navigated |= ArrowButtonWithTooltip("South", ImGuiDir_Down, n.south, tip_s,
94 on_navigate);
95 ImGui::SameLine();
96 navigated |= ArrowButtonWithTooltip("East", ImGuiDir_Right, n.east, tip_e,
97 on_navigate);
98
99 ImGui::PopID();
100 return navigated;
101}
102
103} // namespace yaze::editor
104
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