Guest User

modal

a guest
Apr 19th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <imgui.h>
  2. #include <imgui_impl_glfw.h>
  3. #include <imgui_impl_opengl3.h>
  4.  
  5. #include <GLFW/glfw3.h>
  6.  
  7. #include <cstdio>
  8.  
  9. int main()
  10. {
  11.     const GLFWerrorfun glfwErrorFun = [](int error, const char* description)
  12.         {
  13.             fprintf(stderr, "Glfw Error %d: %s\n", error, description);
  14.         };
  15.     ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  16.     // Setup
  17.     glfwSetErrorCallback(glfwErrorFun);
  18.     if (!glfwInit()) {
  19.         return 1;
  20.     }
  21.  
  22.     const char* glsl_version = "#version 130";
  23.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  24.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
  25.  
  26.     GLFWwindow* window = glfwCreateWindow(640, 480, "Dear ImGui test", nullptr, nullptr);
  27.     if (window == nullptr) {
  28.         puts("Cannot create window. Exiting.");
  29.         return 1;
  30.     }
  31.  
  32.     glfwMakeContextCurrent(window);
  33.     glfwSwapInterval(1); // Enable Vsync
  34.  
  35.     // Setup Dear ImGui context
  36.     IMGUI_CHECKVERSION();
  37.     ImGui::CreateContext();
  38.     auto& io = ImGui::GetIO();
  39.  
  40.     ImGui::StyleColorsDark();
  41.  
  42.     ImGui_ImplGlfw_InitForOpenGL(window, true);
  43.     ImGui_ImplOpenGL3_Init(glsl_version);
  44.  
  45.     while(!glfwWindowShouldClose(window)) {
  46.         glfwPollEvents();
  47.  
  48.         // Start new DearImGui frame
  49.         ImGui_ImplOpenGL3_NewFrame();
  50.         ImGui_ImplGlfw_NewFrame();
  51.         ImGui::NewFrame();
  52.  
  53.         ImGui::Begin("Modal test"); {
  54.             ImGui::TextUnformatted("Click button to open modal");
  55.             if (ImGui::Button("Here")) {
  56.                 ImGui::OpenPopup("Open modal");
  57.             }
  58.         }
  59.  
  60.         static bool theBooleans = false;
  61.         if (ImGui::BeginPopupModal("Open modal", nullptr, ImGuiWindowFlags_AlwaysAutoResize)) {
  62.             ImGui::TextUnformatted("wow");
  63.             ImGui::MenuItem("outside BeginMenu", nullptr, false);
  64.             if (ImGui::BeginMenu("options...")) {
  65.                 ImGui::MenuItem("(options)", nullptr, false, false);
  66.                 if (ImGui::MenuItem("inside BeginMenu", nullptr, theBooleans)) {
  67.                     theBooleans = !theBooleans;
  68.                 }
  69.                 ImGui::MenuItem("Inside BeginMenu 1", nullptr, false);
  70.                 ImGui::EndMenu();
  71.             }
  72.             ImGui::EndPopup();
  73.         }
  74.  
  75.         ImGui::End();
  76.         ImGui::Render();
  77.  
  78.         int display_w, display_h;
  79.         glfwGetFramebufferSize(window, &display_w, &display_h);
  80.         glViewport(0, 0, display_w, display_h);
  81.         glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w,
  82.                      clear_color.w
  83.                     );
  84.         glClear(GL_COLOR_BUFFER_BIT);
  85.         ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
  86.  
  87.         glfwSwapBuffers(window);
  88.     }
  89.  
  90.     // Cleanup
  91.     {
  92.         ImGui_ImplOpenGL3_Shutdown();
  93.         ImGui_ImplGlfw_Shutdown();
  94.         ImGui::DestroyContext();
  95.  
  96.         glfwDestroyWindow(window);
  97.         glfwTerminate();
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment