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

78 lines
2.5 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)) {
order.push_back(name);
}
2020-09-20 00:19:39 +02:00
}
void Menu::removeEntry(std::string name) {
items.erase(name);
}
void Menu::draw() {
MenuItem_t item;
2020-12-08 04:36:37 +01:00
float menuWidth = ImGui::GetContentRegionAvailWidth();
ImGuiWindow* window = ImGui::GetCurrentWindow();
2020-09-20 00:19:39 +02:00
for (std::string name : order) {
2020-09-24 19:36:57 +02:00
if (items.find(name) == items.end()) {
continue;
}
2020-09-20 00:19:39 +02:00
item = items[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));
}
2020-09-24 19:36:57 +02:00
if (ImGui::CollapsingHeader(name.c_str(), ImGuiTreeNodeFlags_DefaultOpen)) {
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();
if (ImGui::Checkbox(("##_menu_checkbox_" + name).c_str(), &enabled)) {
enabled ? item.inst->enable() : item.inst->disable();
}
ImGui::SetCursorPos(pos);
}
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();
if (ImGui::Checkbox(("##_menu_checkbox_" + name).c_str(), &enabled)) {
enabled ? item.inst->enable() : item.inst->disable();
}
ImGui::SetCursorPos(pos);
}
2020-09-24 19:36:57 +02:00
}
}
bool Menu::isInOrderList(std::string name) {
for (std::string _name : order) {
if (_name == name) {
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
}