yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
font_picker.cc
Go to the documentation of this file.
2
3#include <array>
4#include <cstdio>
5
6#include "imgui/imgui.h"
7#include "imgui/imgui_internal.h"
8
9namespace yaze {
10namespace gui {
11namespace font_picker_internal {
12
13const char* FontNameAt(int index) {
14 static thread_local std::array<char, 64> fallback_name;
15 ImGuiIO& io = ImGui::GetIO();
16 if (io.Fonts == nullptr || index < 0 || index >= io.Fonts->Fonts.Size) {
17 std::snprintf(fallback_name.data(), fallback_name.size(), "Font #%d",
18 index);
19 return fallback_name.data();
20 }
21 const ImFont* font = io.Fonts->Fonts[index];
22 if (font == nullptr) {
23 std::snprintf(fallback_name.data(), fallback_name.size(), "Font #%d",
24 index);
25 return fallback_name.data();
26 }
27 const char* name = font->GetDebugName();
28 if (name != nullptr && name[0] != '\0' &&
29 // GetDebugName() returns "<unknown>" when no ImFontConfig::Name was set.
30 std::string_view(name) != "<unknown>") {
31 return name;
32 }
33 std::snprintf(fallback_name.data(), fallback_name.size(), "Font #%d", index);
34 return fallback_name.data();
35}
36
38 ImGuiContext* ctx = ImGui::GetCurrentContext();
39 if (ctx == nullptr)
40 return 0;
41 ImGuiIO& io = ImGui::GetIO();
42 if (io.Fonts == nullptr)
43 return 0;
44 return io.Fonts->Fonts.Size;
45}
46
47} // namespace font_picker_internal
48
49bool FontPicker(const char* label, int* index) {
50 if (index == nullptr)
51 return false;
52
54 if (count <= 0) {
55 ImGui::BeginDisabled();
56 int dummy = 0;
57 ImGui::Combo(label, &dummy, "(no fonts loaded)\0\0");
58 ImGui::EndDisabled();
59 return false;
60 }
61
62 const int current = (*index < 0 || *index >= count) ? 0 : *index;
63 const char* preview_name = font_picker_internal::FontNameAt(current);
64
65 bool changed = false;
66 if (ImGui::BeginCombo(label, preview_name)) {
67 ImGuiIO& io = ImGui::GetIO();
68 for (int i = 0; i < count; ++i) {
69 const bool is_selected = (i == current);
70 const char* name = font_picker_internal::FontNameAt(i);
71
72 ImFont* font = io.Fonts->Fonts[i];
73 if (font != nullptr) {
74 ImGui::PushFont(font);
75 }
76 const bool picked = ImGui::Selectable(name, is_selected);
77 if (font != nullptr) {
78 ImGui::PopFont();
79 }
80 if (picked && i != current) {
81 *index = i;
82 changed = true;
83 }
84 if (is_selected) {
85 ImGui::SetItemDefaultFocus();
86 }
87 }
88 ImGui::EndCombo();
89 }
90 return changed;
91}
92
93} // namespace gui
94} // namespace yaze
const char * FontNameAt(int index)
bool FontPicker(const char *label, int *index)