dlxjj/imradv3
0262
1#include "ui_message_box.h"2#include "IconsFontAwesome6.h"3 4MessageBox messageBox;5 6void MessageBox::OpenPopup(std::function<void(ImRad::ModalResult)> f)7{8 callback = std::move(f);9 ImGui::OpenPopup(ID);10}11 12void MessageBox::Draw()13{14 const float BWIDTH = 90;15 const float BHEIGHT = 30;16 17 ID = ImGui::GetID("###MessageBox");18 ImVec2 center = ImGui::GetMainViewport()->GetCenter();19 //todo: ImGuiCond_Appearing won't center20 ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, { 0.5f, 0.5f });21 bool open = true;22 ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, { 10, 10 });23 if (ImGui::BeginPopupModal((title + "###MessageBox").c_str(), &open, ImGuiWindowFlags_AlwaysAutoResize))24 {25 wasOpen = ID;26 /*bool resizing = ImGui::IsWindowAppearing() ||27 std::abs(ImGui::GetWindowSize().x - lastSize.x) >= 1 ||28 std::abs(ImGui::GetWindowSize().y - lastSize.y) >= 1;29 lastSize = ImGui::GetWindowSize();*/30 31 ImGui::BeginChild("ch", { 300, 50 });32 ImGui::TextWrapped("%s", message.c_str());33 ImGui::EndChild();34 ImGui::Spacing();35 ImGui::Spacing();36 37 int n = (bool)(buttons & ImRad::Ok) + (bool)(buttons & ImRad::Cancel) + (bool)(buttons & ImRad::Yes) + (bool)(buttons & ImRad::No);38 float w = (n * BWIDTH) + (n - 1) * ImGui::GetStyle().ItemSpacing.x;39 float x = (ImGui::GetContentRegionAvail().x + ImGui::GetStyle().FramePadding.x - w) / 2.f;40 x += ImGui::GetStyle().WindowPadding.x;41 float y = ImGui::GetCursorPosY();42 if (buttons & ImRad::Ok)43 {44 if (ImGui::IsWindowAppearing())45 ImGui::SetKeyboardFocusHere();46 ImGui::SetCursorPos({ x, y });47 if (ImGui::Button("OK", { BWIDTH, BHEIGHT }) ||48 (buttons == ImRad::Ok && ImGui::IsKeyPressed(ImGuiKey_Escape)))49 {50 ImGui::CloseCurrentPopup();51 callback(ImRad::Ok);52 }53 x += BWIDTH + ImGui::GetStyle().ItemSpacing.x;54 }55 if (buttons & ImRad::Yes)56 {57 ImGui::SetCursorPos({ x, y });58 if (ImGui::Button("Yes", { BWIDTH, BHEIGHT })) {59 ImGui::CloseCurrentPopup();60 callback(ImRad::Yes);61 }62 x += BWIDTH + ImGui::GetStyle().ItemSpacing.x;63 }64 if (buttons & ImRad::No)65 {66 ImGui::SetCursorPos({ x, y });67 if (ImGui::Button("No", { BWIDTH, BHEIGHT })) {68 ImGui::CloseCurrentPopup();69 callback(ImRad::No);70 }71 x += BWIDTH + ImGui::GetStyle().ItemSpacing.x;72 }73 if (buttons & ImRad::Cancel)74 {75 ImGui::SetCursorPos({ x, y });76 if (ImGui::Button("Cancel", { BWIDTH, BHEIGHT }) || ImGui::IsKeyPressed(ImGuiKey_Escape)) {77 ImGui::CloseCurrentPopup();78 callback(ImRad::Cancel); //currently used in Save File Confirmation upon Exit79 }80 x += BWIDTH + ImGui::GetStyle().ItemSpacing.x;81 }82 83 ImGui::EndPopup();84 }85 else if (wasOpen == ID)86 {87 //hack - clear fields for next invocation88 wasOpen = 0;89 title = "title";90 message = "";91 buttons = ImRad::Ok;92 }93 ImGui::PopStyleVar();94}