SDRPlusPlus/core/src/gui/widgets/menu.cpp

96 lines
3.0 KiB
C++
Raw Normal View History

#include <gui/widgets/menu.h>
#include <imgui/imgui.h>
2020-12-08 04:36:37 +01:00
#include <imgui/imgui_internal.h>
2020-09-20 00:19:39 +02:00
Menu::Menu() {
}
2020-12-08 04:36:37 +01:00
void Menu::registerEntry(std::string name, void (*drawHandler)(void* ctx), void* ctx, ModuleManager::Instance* inst) {
2020-09-20 00:19:39 +02:00
MenuItem_t item;
item.drawHandler = drawHandler;
item.ctx = ctx;
2020-12-08 04:36:37 +01:00
item.inst = inst;
2020-09-20 00:19:39 +02:00
items[name] = item;
2020-09-24 19:36:57 +02:00
if (!isInOrderList(name)) {
2021-04-22 23:49:35 +02:00
MenuOption_t opt;
opt.name = name;
opt.open = true;
order.push_back(opt);
2020-09-24 19:36:57 +02:00
}
2020-09-20 00:19:39 +02:00
}
void Menu::removeEntry(std::string name) {
items.erase(name);
}
2021-04-22 23:49:35 +02:00
bool Menu::draw() {
bool changed = false;
2020-12-08 04:36:37 +01:00
float menuWidth = ImGui::GetContentRegionAvailWidth();
ImGuiWindow* window = ImGui::GetCurrentWindow();
2021-04-22 23:49:35 +02:00
for (MenuOption_t& opt : order) {
if (items.find(opt.name) == items.end()) {
2020-09-24 19:36:57 +02:00
continue;
}
2021-04-22 23:49:35 +02:00
MenuItem_t& item = items[opt.name];
2020-12-08 04:36:37 +01:00
ImRect orginalRect = window->WorkRect;
if (item.inst != NULL) {
window->WorkRect = ImRect(orginalRect.Min, ImVec2(orginalRect.Max.x - ImGui::GetTextLineHeight() - 6, orginalRect.Max.y));
}
2021-04-22 23:49:35 +02:00
if (ImGui::CollapsingHeader(opt.name.c_str(), opt.open ? ImGuiTreeNodeFlags_DefaultOpen : 0)) {
2020-12-08 04:36:37 +01:00
if (item.inst != NULL) {
window->WorkRect = orginalRect;
ImVec2 pos = ImGui::GetCursorPos();
ImGui::SetCursorPosX(pos.x + menuWidth - ImGui::GetTextLineHeight() - 6);
ImGui::SetCursorPosY(pos.y - 10 - ImGui::GetTextLineHeight());
bool enabled = item.inst->isEnabled();
2021-04-22 23:49:35 +02:00
if (ImGui::Checkbox(("##_menu_checkbox_" + opt.name).c_str(), &enabled)) {
2020-12-08 04:36:37 +01:00
enabled ? item.inst->enable() : item.inst->disable();
}
ImGui::SetCursorPos(pos);
}
2021-04-22 23:49:35 +02:00
// Check if the state changed
if (!opt.open) {
opt.open = true;
changed = true;
}
2020-09-24 19:36:57 +02:00
item.drawHandler(item.ctx);
ImGui::Spacing();
2020-09-24 19:36:57 +02:00
}
2020-12-08 04:36:37 +01:00
else if (item.inst != NULL) {
window->WorkRect = orginalRect;
ImVec2 pos = ImGui::GetCursorPos();
ImGui::SetCursorPosX(pos.x + menuWidth - ImGui::GetTextLineHeight() - 6);
ImGui::SetCursorPosY(pos.y - 10 - ImGui::GetTextLineHeight());
bool enabled = item.inst->isEnabled();
2021-04-22 23:49:35 +02:00
if (ImGui::Checkbox(("##_menu_checkbox_" + opt.name).c_str(), &enabled)) {
2020-12-08 04:36:37 +01:00
enabled ? item.inst->enable() : item.inst->disable();
}
ImGui::SetCursorPos(pos);
2021-04-22 23:49:35 +02:00
if (opt.open) {
opt.open = false;
changed = true;
}
}
else if (opt.open) {
opt.open = false;
changed = true;
2020-12-08 04:36:37 +01:00
}
2020-09-24 19:36:57 +02:00
}
2021-04-22 23:49:35 +02:00
return changed;
2020-09-24 19:36:57 +02:00
}
bool Menu::isInOrderList(std::string name) {
2021-04-22 23:49:35 +02:00
for (MenuOption_t opt : order) {
if (opt.name == name) {
2020-09-24 19:36:57 +02:00
return true;
}
2020-09-20 00:19:39 +02:00
}
2020-09-24 19:36:57 +02:00
return false;
2020-09-20 00:19:39 +02:00
}