yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
panel_palette.cc
Go to the documentation of this file.
2#include "util/i18n/tr.h"
3
4#include <algorithm>
5#include <cctype>
6#include <sstream>
7#include <string>
8#include <vector>
9
13#include "imgui/imgui.h"
14#include "imgui/misc/cpp/imgui_stdlib.h"
15
16namespace yaze {
17namespace editor {
18namespace layout_designer {
19namespace panel_palette_internal {
20
21namespace {
22
23std::string ToLower(const std::string& s) {
24 std::string out;
25 out.reserve(s.size());
26 for (char c : s) {
27 out.push_back(
28 static_cast<char>(std::tolower(static_cast<unsigned char>(c))));
29 }
30 return out;
31}
32
33} // namespace
34
35bool MatchesQuery(const PanelPaletteEntry& entry, const std::string& query) {
36 if (query.empty())
37 return true;
38 const std::string haystack =
39 ToLower(entry.display_name + " " + entry.panel_id + " " + entry.category);
40 std::istringstream iss(ToLower(query));
41 std::string term;
42 while (iss >> term) {
43 if (haystack.find(term) == std::string::npos)
44 return false;
45 }
46 return true;
47}
48
49} // namespace panel_palette_internal
50
51std::vector<PanelPaletteEntry> CollectPaletteEntries(
52 const std::string& exclude_panel_id) {
53 std::vector<PanelPaletteEntry> out;
55 if (panel == nullptr)
56 continue;
58 entry.panel_id = panel->GetId();
59 if (!exclude_panel_id.empty() && entry.panel_id == exclude_panel_id) {
60 continue;
61 }
62 entry.display_name = panel->GetDisplayName();
63 entry.icon = panel->GetIcon();
64 entry.category = panel->GetEditorCategory();
65 out.push_back(std::move(entry));
66 }
67 std::sort(out.begin(), out.end(),
68 [](const PanelPaletteEntry& a, const PanelPaletteEntry& b) {
69 if (a.category != b.category)
70 return a.category < b.category;
71 return a.display_name < b.display_name;
72 });
73 return out;
74}
75
76void DrawPanelPalette(const std::vector<PanelPaletteEntry>& entries,
77 std::string* query) {
78 if (query != nullptr) {
79 ImGui::SetNextItemWidth(-FLT_MIN);
80 ImGui::InputTextWithHint("##layout_designer_palette_query", "Search panels",
81 query);
82 }
83 const std::string q = query != nullptr ? *query : std::string();
84
85 ImGui::TextDisabled(tr("Drag panels onto the canvas to place them."));
86 ImGui::Separator();
87
88 // Bucket filtered entries by category so collapsing headers group cleanly.
89 std::string current_category;
90 bool category_open = false;
91 int rendered_in_category = 0;
92
93 auto close_current_category = [&]() {
94 if (category_open && rendered_in_category == 0) {
95 ImGui::TextDisabled(tr(" (no matches)"));
96 }
97 category_open = false;
98 rendered_in_category = 0;
99 };
100
101 for (const PanelPaletteEntry& entry : entries) {
103 continue;
104
105 if (entry.category != current_category) {
106 close_current_category();
107 current_category = entry.category;
108 const std::string header =
109 entry.category.empty() ? "(uncategorized)" : entry.category;
110 category_open = ImGui::CollapsingHeader(header.c_str(),
111 ImGuiTreeNodeFlags_DefaultOpen);
112 }
113 if (!category_open)
114 continue;
115
116 ImGui::PushID(entry.panel_id.c_str());
117 const std::string label = entry.icon.empty()
118 ? entry.display_name
119 : entry.icon + " " + entry.display_name;
120 ImGui::TextUnformatted(label.c_str());
121 gui::BeginPanelDragSource(entry.panel_id.c_str(), label.c_str());
122 if (ImGui::IsItemHovered() && !entry.panel_id.empty()) {
123 ImGui::SetTooltip(tr("Drag onto the canvas to add this panel.\n%s"),
124 entry.panel_id.c_str());
125 }
126 ImGui::PopID();
127 ++rendered_in_category;
128 }
129 close_current_category();
130}
131
132} // namespace layout_designer
133} // namespace editor
134} // namespace yaze
Base interface for all logical window content components.
std::vector< WindowContent * > GetAll()
Get all registered panels.
bool MatchesQuery(const PanelPaletteEntry &entry, const std::string &query)
void DrawPanelPalette(const std::vector< PanelPaletteEntry > &entries, std::string *query)
std::vector< PanelPaletteEntry > CollectPaletteEntries(const std::string &exclude_panel_id)
bool BeginPanelDragSource(const char *panel_id, const char *preview_label)
Definition drag_drop.h:112