Add Paragraph and ClickableText functions; implement MapKeyToImGuiKey for key mapping

This commit is contained in:
scawful
2025-03-09 18:14:48 -04:00
parent 412e617ce7
commit c38932fd9c
2 changed files with 86 additions and 47 deletions

View File

@@ -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);

View File

@@ -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<std::string> &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::function<void()>, std::string>;
struct Table {
@@ -77,58 +83,12 @@ struct MenuItem {
std::function<bool()> enabled_condition = kDefaultEnabledCondition;
std::vector<MenuItem> subitems;
};
using Menu = std::array<MenuItem, 5>;
using Menu = std::vector<MenuItem>;
void DrawMenu(Menu &params);
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<void()> callback) {
kMainMenu[MenuType::kFile].subitems.emplace_back(label, shortcut, callback);
}
inline void AddToFileMenu(const std::string &label, const std::string &shortcut,
std::function<void()> callback,
std::function<bool()> enabled_condition,
std::vector<MenuItem> 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<void()> callback) {
kMainMenu[MenuType::kEdit].subitems.emplace_back(label, shortcut, callback);
}
inline void AddToViewMenu(const std::string &label, const std::string &shortcut,
std::function<void()> callback) {
kMainMenu[MenuType::kView].subitems.emplace_back(label, shortcut, callback);
}
inline void AddToHelpMenu(const std::string &label, const std::string &shortcut,
std::function<void()> callback) {
kMainMenu[MenuType::kHelp].subitems.emplace_back(label, shortcut, callback);
}
} // namespace gui
} // namespace yaze