From c38932fd9cef8f4c1cc1c9d3a659b95163c91c53 Mon Sep 17 00:00:00 2001 From: scawful Date: Sun, 9 Mar 2025 18:14:48 -0400 Subject: [PATCH] Add Paragraph and ClickableText functions; implement MapKeyToImGuiKey for key mapping --- src/app/gui/input.cc | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/app/gui/input.h | 54 ++++-------------------------- 2 files changed, 86 insertions(+), 47 deletions(-) diff --git a/src/app/gui/input.cc b/src/app/gui/input.cc index bbab899f..a4039b10 100644 --- a/src/app/gui/input.cc +++ b/src/app/gui/input.cc @@ -184,6 +184,22 @@ bool InputHexByte(const char* label, uint8_t* data, uint8_t max_value, return false; } +void Paragraph(const std::string& text) { + ImGui::TextWrapped("%s", text.c_str()); +} + +bool ClickableText(const std::string& text) { + if (ImGui::IsItemHovered()) { + ImGui::SetMouseCursor(ImGuiMouseCursor_Hand); + auto color = ImGui::GetStyleColorVec4(ImGuiCol_TabHovered); + ImGui::TextColored(color, "%s", text.c_str()); + } else { + auto color = ImGui::GetStyleColorVec4(ImGuiCol_Tab); + ImGui::TextColored(color, "%s", text.c_str()); + } + return ImGui::IsItemClicked(); +} + void ItemLabel(absl::string_view title, ItemLabelFlags flags) { ImGuiWindow* window = ImGui::GetCurrentWindow(); const ImVec2 lineStart = ImGui::GetCursorScreenPos(); @@ -255,6 +271,69 @@ bool InputTileInfo(const char* label, gfx::TileInfo* tile_info) { ImGuiID GetID(const std::string& id) { return ImGui::GetID(id.c_str()); } +ImGuiKey MapKeyToImGuiKey(char key) { + switch (key) { + case 'A': + return ImGuiKey_A; + case 'B': + return ImGuiKey_B; + case 'C': + return ImGuiKey_C; + case 'D': + return ImGuiKey_D; + case 'E': + return ImGuiKey_E; + case 'F': + return ImGuiKey_F; + case 'G': + return ImGuiKey_G; + case 'H': + return ImGuiKey_H; + case 'I': + return ImGuiKey_I; + case 'J': + return ImGuiKey_J; + case 'K': + return ImGuiKey_K; + case 'L': + return ImGuiKey_L; + case 'M': + return ImGuiKey_M; + case 'N': + return ImGuiKey_N; + case 'O': + return ImGuiKey_O; + case 'P': + return ImGuiKey_P; + case 'Q': + return ImGuiKey_Q; + case 'R': + return ImGuiKey_R; + case 'S': + return ImGuiKey_S; + case 'T': + return ImGuiKey_T; + case 'U': + return ImGuiKey_U; + case 'V': + return ImGuiKey_V; + case 'W': + return ImGuiKey_W; + case 'X': + return ImGuiKey_X; + case 'Y': + return ImGuiKey_Y; + case 'Z': + return ImGuiKey_Z; + case '/': + return ImGuiKey_Slash; + case '-': + return ImGuiKey_Minus; + default: + return ImGuiKey_COUNT; + } +} + void AddTableColumn(Table& table, const std::string& label, GuiElement element) { table.column_labels.push_back(label); diff --git a/src/app/gui/input.h b/src/app/gui/input.h index b6a95043..4ade0b00 100644 --- a/src/app/gui/input.h +++ b/src/app/gui/input.h @@ -37,6 +37,10 @@ IMGUI_API bool InputHexByte(const char *label, uint8_t *data, IMGUI_API bool InputHexByte(const char *label, uint8_t *data, uint8_t max_value, float input_width = 50.f, bool no_step = false); +IMGUI_API void Paragraph(const std::string &text); + +IMGUI_API bool ClickableText(const std::string &text); + IMGUI_API bool ListBox(const char *label, int *current_item, const std::vector &items, int height_in_items = -1); @@ -53,6 +57,8 @@ IMGUI_API void ItemLabel(absl::string_view title, ItemLabelFlags flags); IMGUI_API ImGuiID GetID(const std::string &id); +ImGuiKey MapKeyToImGuiKey(char key); + using GuiElement = std::variant, std::string>; struct Table { @@ -77,58 +83,12 @@ struct MenuItem { std::function enabled_condition = kDefaultEnabledCondition; std::vector subitems; }; -using Menu = std::array; +using Menu = std::vector; void DrawMenu(Menu ¶ms); -enum MenuType { - kFile, - kEdit, - kView, - kTools, - kHelp, -}; - static Menu kMainMenu; -inline void AddToMenu(const std::string &label, const char *icon, - MenuType type) { - if (icon) { - kMainMenu[type].subitems.emplace_back(absl::StrCat(icon, " ", label), "", - nullptr); - } else { - kMainMenu[type].subitems.emplace_back(label, "", nullptr); - } -} - -inline void AddToFileMenu(const std::string &label, const std::string &shortcut, - std::function callback) { - kMainMenu[MenuType::kFile].subitems.emplace_back(label, shortcut, callback); -} - -inline void AddToFileMenu(const std::string &label, const std::string &shortcut, - std::function callback, - std::function enabled_condition, - std::vector subitems) { - kMainMenu[MenuType::kFile].subitems.emplace_back(label, shortcut, callback, - enabled_condition, subitems); -} - -inline void AddToEditMenu(const std::string &label, const std::string &shortcut, - std::function callback) { - kMainMenu[MenuType::kEdit].subitems.emplace_back(label, shortcut, callback); -} - -inline void AddToViewMenu(const std::string &label, const std::string &shortcut, - std::function callback) { - kMainMenu[MenuType::kView].subitems.emplace_back(label, shortcut, callback); -} - -inline void AddToHelpMenu(const std::string &label, const std::string &shortcut, - std::function callback) { - kMainMenu[MenuType::kHelp].subitems.emplace_back(label, shortcut, callback); -} - } // namespace gui } // namespace yaze