79 lines
2.3 KiB
C++
79 lines
2.3 KiB
C++
#pragma once
|
|
#include <imgui.h>
|
|
#include <string>
|
|
#include <ranges>
|
|
#include <functional>
|
|
|
|
namespace gui {
|
|
struct MenuItem {
|
|
MenuItem(const std::string& label, std::function<void()>&& func = nullptr, bool enabled = true) : label(label), exec(std::move(func)), enabled(enabled) {}
|
|
bool render() {
|
|
bool ret = false;
|
|
ImGui::BeginDisabled(!enabled);
|
|
if(ImGui::MenuItem(label.c_str())) {
|
|
if(exec)
|
|
exec();
|
|
ret = true;
|
|
}
|
|
ImGui::EndDisabled();
|
|
return ret;
|
|
}
|
|
|
|
void setLabel(const std::string& label) { this->label = label; }
|
|
const std::string& getLabel() { return label; }
|
|
|
|
void setFunc(std::function<void()>&& func) { exec = func; }
|
|
|
|
void setEnabled(bool v) { enabled = v; }
|
|
private:
|
|
bool enabled = true;
|
|
std::string label;
|
|
std::function<void()> exec;
|
|
};
|
|
|
|
struct Menu {
|
|
Menu(const std::string& label, const std::vector<MenuItem>& items = {}, bool enabled = true) : label(label), items(items), enabled(enabled) {}
|
|
void addMenuItem(const MenuItem& item) { items.push_back(item); }
|
|
bool render() {
|
|
bool ret = false;
|
|
ImGui::BeginDisabled(!enabled);
|
|
if(ImGui::BeginMenu(label.c_str())) {
|
|
for(auto& item : items) {
|
|
ret |= item.render();
|
|
}
|
|
ImGui::EndMenu();
|
|
}
|
|
ImGui::EndDisabled();
|
|
return ret;
|
|
}
|
|
void setEnabled(bool v) { enabled = v; }
|
|
private:
|
|
std::vector<MenuItem> items{};
|
|
std::string label{};
|
|
bool enabled = true;
|
|
};
|
|
|
|
template <bool main = false>
|
|
struct MenuBar {
|
|
MenuBar(bool enabled = true) : enabled(enabled) {}
|
|
void addMenu(const Menu& menu) { menus.push_back(menu); }
|
|
bool render() {
|
|
bool ret = false;
|
|
ImGui::BeginDisabled(!enabled);
|
|
auto beginMenuBar = main ? &ImGui::BeginMainMenuBar : &ImGui::BeginMenuBar;
|
|
auto endMenuBar = main ? &ImGui::EndMainMenuBar : &ImGui::EndMenuBar;
|
|
if(beginMenuBar()) {
|
|
for(auto& menu : menus) {
|
|
ret |= menu.render();
|
|
}
|
|
endMenuBar();
|
|
}
|
|
ImGui::EndDisabled();
|
|
return ret;
|
|
}
|
|
void setEnabled(bool v) { enabled = v; }
|
|
private:
|
|
bool enabled = true;
|
|
std::vector<Menu> menus{};
|
|
};
|
|
} |