yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dungeon_browser.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_DUNGEON_UI_WORKBENCH_DUNGEON_BROWSER_H_
2#define YAZE_APP_EDITOR_DUNGEON_UI_WORKBENCH_DUNGEON_BROWSER_H_
3
4#include <algorithm>
5#include <cctype>
6#include <cstdio>
7#include <span>
8#include <string>
9#include <string_view>
10
12#include "imgui/imgui.h"
13
15
17
18// Presentation state only. Each browser keeps its domain's existing placement
19// callback, categories and asset identity.
26
28 DungeonBrowserState& state, std::span<const char* const> categories) {
29 ImGui::SetNextItemWidth(-1);
30 ImGui::InputTextWithHint("##Search", "Search name or ID", state.search,
31 sizeof(state.search));
32 gui::AutoRegisterLastItem("input_text", "search");
33 const float sort_width = ImGui::GetFontSize() * 5.0f;
34 const float width = std::max(1.0f, ImGui::GetContentRegionAvail().x);
35 ImGui::SetNextItemWidth(
36 std::max(1.0f, width - sort_width - ImGui::GetStyle().ItemSpacing.x));
37 if (!categories.empty())
38 ImGui::Combo("##Category", &state.category, categories.data(),
39 static_cast<int>(categories.size()));
40 gui::AutoRegisterLastItem("combo", "category");
41 ImGui::SameLine();
42 ImGui::SetNextItemWidth(sort_width);
43 constexpr const char* sorts[] = {"ID", "Name"};
44 ImGui::Combo("##Sort", &state.sort, sorts, IM_ARRAYSIZE(sorts));
45 gui::AutoRegisterLastItem("combo", "sort");
46 if (ImGui::IsItemHovered())
47 ImGui::SetTooltip("Sort by ID or name.");
48}
49
51 if (ImGui::BeginTable(
52 "##BrowserView", 2,
53 ImGuiTableFlags_SizingStretchSame | ImGuiTableFlags_NoPadOuterX)) {
54 ImGui::TableNextColumn();
55 if (ImGui::Selectable("List", state.view == DungeonBrowserView::kList, 0,
56 ImVec2(ImGui::GetContentRegionAvail().x, 0)))
58 gui::AutoRegisterLastItem("selectable", "view_list");
59 ImGui::TableNextColumn();
60 if (ImGui::Selectable("Icons", state.view == DungeonBrowserView::kIcons, 0,
61 ImVec2(ImGui::GetContentRegionAvail().x, 0)))
63 gui::AutoRegisterLastItem("selectable", "view_icons");
64 ImGui::EndTable();
65 }
66}
67
68inline std::string DungeonBrowserFold(std::string_view value) {
69 std::string folded(value);
70 std::transform(
71 folded.begin(), folded.end(), folded.begin(),
72 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
73 return folded;
74}
75
76inline bool DungeonBrowserMatches(std::string_view query, int id,
77 std::string_view name) {
78 if (query.empty())
79 return true;
80 const std::string folded = DungeonBrowserFold(query);
81 if (DungeonBrowserFold(name).find(folded) != std::string::npos)
82 return true;
83 char hex[16];
84 std::snprintf(hex, sizeof(hex), "%03X", id);
85 std::string_view number = folded;
86 if (number.starts_with("0x"))
87 number.remove_prefix(2);
88 if (number.starts_with("$"))
89 number.remove_prefix(1);
90 return !number.empty() && DungeonBrowserFold(hex).ends_with(number);
91}
92
93template <typename DrawPreview>
94bool DrawDungeonBrowserRow(int id, const std::string& name, bool selected,
95 DrawPreview preview, std::string_view badge = {}) {
96 const float height = std::max(28.0f, ImGui::GetTextLineHeight() + 10.0f);
97 const ImVec2 pos = ImGui::GetCursorScreenPos();
98 const bool pressed =
99 ImGui::Selectable("##row", selected, 0, ImVec2(0, height));
100 const ImVec2 max = ImGui::GetItemRectMax();
101 if (ImGui::IsItemVisible()) {
102 const bool rendered =
103 preview(ImVec2(pos.x + 3, pos.y + 2), ImVec2(24, height - 4));
104 char hex[16];
105 std::snprintf(hex, sizeof(hex), "%03X", id);
106 const std::string id_text =
107 badge.empty() ? std::string(hex) : std::string(badge);
108 const float id_width = ImGui::CalcTextSize(id_text.c_str()).x;
109 const float name_x = pos.x + (rendered ? 34.0f : 6.0f);
110 auto* draw = ImGui::GetWindowDrawList();
111 draw->PushClipRect(ImVec2(name_x, pos.y),
112 ImVec2(std::max(name_x, max.x - id_width - 12), max.y),
113 true);
114 draw->AddText(
115 ImVec2(name_x, pos.y + (height - ImGui::GetTextLineHeight()) * .5f),
116 ImGui::GetColorU32(ImGuiCol_Text), name.c_str());
117 draw->PopClipRect();
118 draw->AddText(ImVec2(max.x - id_width - 4,
119 pos.y + (height - ImGui::GetTextLineHeight()) * .5f),
120 ImGui::GetColorU32(ImGuiCol_TextDisabled), id_text.c_str());
121 }
122 if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
123 ImGui::SetTooltip("%s ($%03X)", name.c_str(), id);
124 ImGui::LogText("%03X %s\n", id, name.c_str());
125 return pressed;
126}
127
128template <typename DrawPreview>
129bool DrawDungeonBrowserIcon(int id, const std::string& name, bool selected,
130 float width, DrawPreview preview,
131 std::string_view badge = {}) {
132 width = std::max(width, 1.0f);
133 const float line_height = ImGui::GetTextLineHeight();
134 const float padding = std::min(3.0f, width * 0.08f);
135 const ImVec2 pos = ImGui::GetCursorScreenPos();
136 const ImVec2 size(width, width + line_height * 2.0f + padding);
137 // Keep the entry ID stable when the browser changes presentation.
138 const bool pressed = ImGui::Selectable("##row", selected, 0, size);
139 if (ImGui::IsItemVisible()) {
140 char hex[16];
141 std::snprintf(hex, sizeof(hex), "%03X", id);
142 const std::string id_text =
143 badge.empty() ? std::string(hex) : std::string(badge);
144 const ImVec2 preview_size(std::max(1.0f, width - padding * 2.0f),
145 std::max(1.0f, width - padding * 2.0f));
146 auto* draw = ImGui::GetWindowDrawList();
147 draw->PushClipRect(pos, ImVec2(pos.x + size.x, pos.y + size.y), true);
148 if (!preview(ImVec2(pos.x + padding, pos.y + padding), preview_size)) {
149 const ImVec2 label_size = ImGui::CalcTextSize(id_text.c_str());
150 draw->AddText(ImVec2(pos.x + (width - label_size.x) * 0.5f,
151 pos.y + (width - line_height) * 0.5f),
152 ImGui::GetColorU32(ImGuiCol_TextDisabled), id_text.c_str());
153 }
154 draw->AddText(ImVec2(pos.x + padding, pos.y + width),
155 ImGui::GetColorU32(ImGuiCol_Text), name.c_str());
156 draw->AddText(ImVec2(pos.x + padding, pos.y + width + line_height),
157 ImGui::GetColorU32(ImGuiCol_TextDisabled), id_text.c_str());
158 draw->PopClipRect();
159 }
160 if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
161 ImGui::SetTooltip("%s ($%03X)%s%s", name.c_str(), id,
162 badge.empty() ? "" : " - ",
163 badge.empty() ? "" : std::string(badge).c_str());
164 ImGui::LogText("%03X %s\n", id, name.c_str());
165 return pressed;
166}
167
168} // namespace yaze::editor::workbench
169
170#endif // YAZE_APP_EDITOR_DUNGEON_UI_WORKBENCH_DUNGEON_BROWSER_H_
bool DungeonBrowserMatches(std::string_view query, int id, std::string_view name)
void DrawDungeonBrowserControls(DungeonBrowserState &state, std::span< const char *const > categories)
void DrawDungeonBrowserViewControls(DungeonBrowserState &state)
std::string DungeonBrowserFold(std::string_view value)
bool DrawDungeonBrowserIcon(int id, const std::string &name, bool selected, float width, DrawPreview preview, std::string_view badge={})
bool DrawDungeonBrowserRow(int id, const std::string &name, bool selected, DrawPreview preview, std::string_view badge={})
void AutoRegisterLastItem(const std::string &widget_type, const std::string &explicit_label, const std::string &description)
Automatically register the last ImGui item.
Automatic widget registration helpers for ImGui Test Engine integration.