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
16// Presentation state only. Each browser keeps its domain's existing placement
17// callback, categories and asset identity.
19 char search[64] = {};
20 int category = 0;
21 int sort = 0;
22};
23
25 DungeonBrowserState& state, std::span<const char* const> categories) {
26 ImGui::SetNextItemWidth(-1);
27 ImGui::InputTextWithHint("##Search", "Search name or ID", state.search,
28 sizeof(state.search));
29 gui::AutoRegisterLastItem("input_text", "search");
30 const float sort_width = ImGui::GetFontSize() * 5.0f;
31 const float width = std::max(1.0f, ImGui::GetContentRegionAvail().x);
32 ImGui::SetNextItemWidth(
33 std::max(1.0f, width - sort_width - ImGui::GetStyle().ItemSpacing.x));
34 if (!categories.empty())
35 ImGui::Combo("##Category", &state.category, categories.data(),
36 static_cast<int>(categories.size()));
37 gui::AutoRegisterLastItem("combo", "category");
38 ImGui::SameLine();
39 ImGui::SetNextItemWidth(sort_width);
40 constexpr const char* sorts[] = {"ID", "Name"};
41 ImGui::Combo("##Sort", &state.sort, sorts, IM_ARRAYSIZE(sorts));
42 gui::AutoRegisterLastItem("combo", "sort");
43 if (ImGui::IsItemHovered())
44 ImGui::SetTooltip("Sort by ID or name.");
45}
46
47inline std::string DungeonBrowserFold(std::string_view value) {
48 std::string folded(value);
49 std::transform(
50 folded.begin(), folded.end(), folded.begin(),
51 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
52 return folded;
53}
54
55inline bool DungeonBrowserMatches(std::string_view query, int id,
56 std::string_view name) {
57 if (query.empty())
58 return true;
59 const std::string folded = DungeonBrowserFold(query);
60 if (DungeonBrowserFold(name).find(folded) != std::string::npos)
61 return true;
62 char hex[16];
63 std::snprintf(hex, sizeof(hex), "%03X", id);
64 std::string_view number = folded;
65 if (number.starts_with("0x"))
66 number.remove_prefix(2);
67 if (number.starts_with("$"))
68 number.remove_prefix(1);
69 return !number.empty() && DungeonBrowserFold(hex).ends_with(number);
70}
71
72template <typename DrawPreview>
73bool DrawDungeonBrowserRow(int id, const std::string& name, bool selected,
74 DrawPreview preview, std::string_view badge = {}) {
75 const float height = std::max(28.0f, ImGui::GetTextLineHeight() + 10.0f);
76 const ImVec2 pos = ImGui::GetCursorScreenPos();
77 const bool pressed =
78 ImGui::Selectable("##row", selected, 0, ImVec2(0, height));
79 const ImVec2 max = ImGui::GetItemRectMax();
80 if (ImGui::IsItemVisible()) {
81 const bool rendered =
82 preview(ImVec2(pos.x + 3, pos.y + 2), ImVec2(24, height - 4));
83 char hex[16];
84 std::snprintf(hex, sizeof(hex), "%03X", id);
85 const std::string id_text =
86 badge.empty() ? std::string(hex) : std::string(badge);
87 const float id_width = ImGui::CalcTextSize(id_text.c_str()).x;
88 const float name_x = pos.x + (rendered ? 34.0f : 6.0f);
89 auto* draw = ImGui::GetWindowDrawList();
90 draw->PushClipRect(ImVec2(name_x, pos.y),
91 ImVec2(std::max(name_x, max.x - id_width - 12), max.y),
92 true);
93 draw->AddText(
94 ImVec2(name_x, pos.y + (height - ImGui::GetTextLineHeight()) * .5f),
95 ImGui::GetColorU32(ImGuiCol_Text), name.c_str());
96 draw->PopClipRect();
97 draw->AddText(ImVec2(max.x - id_width - 4,
98 pos.y + (height - ImGui::GetTextLineHeight()) * .5f),
99 ImGui::GetColorU32(ImGuiCol_TextDisabled), id_text.c_str());
100 }
101 if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled))
102 ImGui::SetTooltip("%s ($%03X)", name.c_str(), id);
103 ImGui::LogText("%03X %s\n", id, name.c_str());
104 return pressed;
105}
106
107} // namespace yaze::editor::workbench
108
109#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)
std::string DungeonBrowserFold(std::string_view value)
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.