ImGuiMessageBox.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <Utils/ImGuiMessageBox.h>
  9. #include <Automation/ScriptableImGui.h>
  10. namespace AtomSampleViewer
  11. {
  12. void ImGuiMessageBox::OpenPopupMessage(AZStd::string title, AZStd::string message)
  13. {
  14. AZ_Assert(m_state == State::Closed, "Popup is already open");
  15. m_type = Type::Ok;
  16. m_title = AZStd::move(title);
  17. m_message = AZStd::move(message);
  18. m_okButtonLabel = "OK";
  19. m_cancelButtonLabel = "";
  20. m_okAction = nullptr;
  21. m_state = State::Opening;
  22. }
  23. void ImGuiMessageBox::OpenPopupConfirmation(AZStd::string title, AZStd::string message, AZStd::function<void()> okAction, AZStd::string okButton, AZStd::string cancelButton)
  24. {
  25. AZ_Assert(m_state == State::Closed, "Popup is already open");
  26. m_type = Type::OkCancel;
  27. m_title = AZStd::move(title);
  28. m_message = AZStd::move(message);
  29. m_okButtonLabel = AZStd::move(okButton);
  30. m_cancelButtonLabel = AZStd::move(cancelButton);
  31. m_okAction = okAction;
  32. m_state = State::Opening;
  33. }
  34. void ImGuiMessageBox::TickPopup()
  35. {
  36. // We delay showing the popup until TickPopup() so that a single ImGuiMessageBox can be used to service multiple controls in an ImGui update function.
  37. // This is because the ImGui "ID" context needs to match between OpenPopup and IsPopupOpen.
  38. if (m_state == State::Opening)
  39. {
  40. ImGui::OpenPopup(m_title.c_str());
  41. m_state = State::Open;
  42. }
  43. if (ImGui::IsPopupOpen(m_title.c_str()))
  44. {
  45. const ImGuiWindowFlags windowFlags =
  46. ImGuiWindowFlags_NoCollapse |
  47. ImGuiWindowFlags_NoResize |
  48. ImGuiWindowFlags_NoMove |
  49. ImGuiWindowFlags_AlwaysAutoResize;
  50. if (ImGui::BeginPopupModal(m_title.c_str(), nullptr, windowFlags))
  51. {
  52. ImGui::Text("%s", m_message.c_str());
  53. ScriptableImGui::PushNameContext(m_title.c_str());
  54. if (ScriptableImGui::Button(m_okButtonLabel.c_str()))
  55. {
  56. ImGui::CloseCurrentPopup();
  57. m_state = State::Closed;
  58. if (m_okAction)
  59. {
  60. m_okAction();
  61. }
  62. }
  63. else if (m_type == Type::OkCancel && ScriptableImGui::Button("Cancel"))
  64. {
  65. ImGui::CloseCurrentPopup();
  66. m_state = State::Closed;
  67. }
  68. ScriptableImGui::PopNameContext();
  69. ImGui::EndPopup();
  70. }
  71. }
  72. else if(m_state == State::Open)
  73. {
  74. // If another ImGui window is opened while a modal popup is open, it kills the modal popup. So open it again.
  75. m_state = State::Opening;
  76. }
  77. }
  78. } // namespace AtomSampleViewer