Implement menu system: add DrawMenu function and MenuItem structure for enhanced menu handling

This commit is contained in:
scawful
2025-02-10 11:50:37 -05:00
parent 169bd09495
commit 4c3e9e4286
3 changed files with 103 additions and 4 deletions

View File

@@ -255,13 +255,15 @@ bool InputTileInfo(const char* label, gfx::TileInfo* tile_info) {
ImGuiID GetID(const std::string& id) { return ImGui::GetID(id.c_str()); }
void AddTableColumn(Table &table, const std::string &label, GuiElement element) {
void AddTableColumn(Table& table, const std::string& label,
GuiElement element) {
table.column_labels.push_back(label);
table.column_contents.push_back(element);
}
void DrawTable(Table& params) {
if (ImGui::BeginTable(params.id, params.num_columns, params.flags, params.size)) {
if (ImGui::BeginTable(params.id, params.num_columns, params.flags,
params.size)) {
for (int i = 0; i < params.num_columns; ++i)
ImGui::TableSetupColumn(params.column_labels[i].c_str());
@@ -280,5 +282,34 @@ void DrawTable(Table& params) {
}
}
void DrawMenu(Menu& menu) {
for (const auto& each_menu : menu) {
if (ImGui::BeginMenu(each_menu.name.c_str())) {
for (const auto& each_item : each_menu.subitems) {
if (!each_item.subitems.empty()) {
if (ImGui::BeginMenu(each_item.name.c_str())) {
for (const auto& each_subitem : each_item.subitems) {
if (ImGui::MenuItem(each_subitem.name.c_str(),
each_subitem.shortcut.c_str())) {
if (each_subitem.callback) each_subitem.callback();
}
}
ImGui::EndMenu();
}
} else if (each_item.name == "-") {
ImGui::Separator();
} else {
if (ImGui::MenuItem(each_item.name.c_str(),
each_item.shortcut.c_str(),
each_item.enabled_condition())) {
if (each_item.callback) each_item.callback();
}
}
}
ImGui::EndMenu();
}
}
}
} // namespace gui
} // namespace yaze

View File

@@ -11,6 +11,7 @@
#include <variant>
#include <vector>
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "app/gfx/snes_tile.h"
#include "imgui/imgui.h"
@@ -67,7 +68,63 @@ void AddTableColumn(Table &table, const std::string &label, GuiElement element);
void DrawTable(Table &params);
} // namespace gui
} // namespace yaze
static std::function<bool()> kDefaultEnabledCondition = []() { return false; };
struct MenuItem {
std::string name;
std::string shortcut;
std::function<void()> callback;
std::function<bool()> enabled_condition = kDefaultEnabledCondition;
std::vector<MenuItem> subitems;
};
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);
}
} // namespace gui
} // namespace yaze
#endif

View File

@@ -1,6 +1,7 @@
#include "style.h"
#include "app/core/platform/file_dialog.h"
#include "core/platform/font_loader.h"
#include "gui/color.h"
#include "imgui/imgui.h"
#include "imgui/imgui_internal.h"
@@ -752,12 +753,21 @@ void TextWithSeparators(const absl::string_view &text) {
void DrawFontManager() {
ImGuiIO& io = ImGui::GetIO();
ImFontAtlas* atlas = io.Fonts;
static ImFont* current_font = atlas->Fonts[0];
static int current_font_index = 0;
static int font_size = 16;
static bool font_selected = false;
ImGui::Text("Loaded fonts");
for (const auto& loaded_font : core::global_font_state.fonts) {
ImGui::Text("%s", loaded_font.font_path);
}
ImGui::Separator();
ImGui::Text("Current Font: %s", current_font->GetDebugName());
ImGui::Text("Font Size: %d", font_size);
if (ImGui::BeginCombo("Fonts", current_font->GetDebugName())) {
for (int i = 0; i < atlas->Fonts.Size; i++) {
bool is_selected = (current_font == atlas->Fonts[i]);
@@ -772,6 +782,7 @@ void DrawFontManager() {
}
ImGui::EndCombo();
}
ImGui::Separator();
if (ImGui::SliderInt("Font Size", &font_size, 8, 32)) {
current_font->Scale = font_size / 16.0f;