From fff91d555bf057552d2daecb324f75daebcdf476 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Mon, 7 Jun 2021 18:14:40 +0200 Subject: [PATCH] ui/ux: Allow ImHex to redraw the screen while moving and resizing --- include/window.hpp | 2 +- source/window.cpp | 92 ++++++++++++++++++++++++++-------------------- 2 files changed, 53 insertions(+), 41 deletions(-) diff --git a/include/window.hpp b/include/window.hpp index 5a03b3ddc..391552441 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -28,6 +28,7 @@ namespace hex { bool setFont(const std::filesystem::path &font_path); private: void frameBegin(); + void frame(); void frameEnd(); void drawWelcomeScreen(); @@ -41,7 +42,6 @@ namespace hex { GLFWwindow* m_window = nullptr; float m_globalScale = 1.0f, m_fontScale = 1.0f; - bool m_fpsVisible = false; double m_targetFps = 60.0; bool m_demoWindowOpen = false; bool m_layoutConfigured = false; diff --git a/source/window.cpp b/source/window.cpp index fefb34219..1c5bfcf0d 100644 --- a/source/window.cpp +++ b/source/window.cpp @@ -179,45 +179,15 @@ namespace hex { } void Window::loop() { - bool pressedKeys[512] = { false }; - this->m_lastFrameTime = glfwGetTime(); while (!glfwWindowShouldClose(this->m_window)) { - std::copy_n(ImGui::GetIO().KeysDown, 512, this->m_prevKeysDown); + if (!glfwGetWindowAttrib(this->m_window, GLFW_VISIBLE) || glfwGetWindowAttrib(this->m_window, GLFW_ICONIFIED)) + glfwWaitEvents(); + + glfwPollEvents(); this->frameBegin(); - - for (u16 i = 0; i < 512; i++) - pressedKeys[i] = ImGui::GetIO().KeysDown[i] && !this->m_prevKeysDown[i]; - - for (const auto &call : View::getDeferedCalls()) - call(); - View::getDeferedCalls().clear(); - - for (auto &view : ContentRegistry::Views::getEntries()) { - view->drawAlwaysVisible(); - - if (!view->shouldProcess()) - continue; - - auto minSize = view->getMinSize(); - minSize.x *= this->m_globalScale; - minSize.y *= this->m_globalScale; - - ImGui::SetNextWindowSizeConstraints(minSize, view->getMaxSize()); - view->drawContent(); - view->handleShortcut(pressedKeys, ImGui::GetIO().KeyCtrl, ImGui::GetIO().KeyShift, ImGui::GetIO().KeyAlt); - } - - View::drawCommonInterfaces(); - - #ifdef DEBUG - if (this->m_demoWindowOpen) { - ImGui::ShowDemoWindow(&this->m_demoWindowOpen); - ImPlot::ShowDemoWindow(&this->m_demoWindowOpen); - } - #endif - + this->frame(); this->frameEnd(); } } @@ -278,11 +248,6 @@ namespace hex { void Window::frameBegin() { - if (!glfwGetWindowAttrib(this->m_window, GLFW_VISIBLE) || glfwGetWindowAttrib(this->m_window, GLFW_ICONIFIED)) - glfwWaitEvents(); - - glfwPollEvents(); - ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplGlfw_NewFrame(); ImGui::NewFrame(); @@ -385,6 +350,42 @@ namespace hex { } } + void Window::frame() { + bool pressedKeys[512] = { false }; + + std::copy_n(ImGui::GetIO().KeysDown, 512, this->m_prevKeysDown); + for (u16 i = 0; i < 512; i++) + pressedKeys[i] = ImGui::GetIO().KeysDown[i] && !this->m_prevKeysDown[i]; + + for (const auto &call : View::getDeferedCalls()) + call(); + View::getDeferedCalls().clear(); + + for (auto &view : ContentRegistry::Views::getEntries()) { + view->drawAlwaysVisible(); + + if (!view->shouldProcess()) + continue; + + auto minSize = view->getMinSize(); + minSize.x *= this->m_globalScale; + minSize.y *= this->m_globalScale; + + ImGui::SetNextWindowSizeConstraints(minSize, view->getMaxSize()); + view->drawContent(); + view->handleShortcut(pressedKeys, ImGui::GetIO().KeyCtrl, ImGui::GetIO().KeyShift, ImGui::GetIO().KeyAlt); + } + + View::drawCommonInterfaces(); + +#ifdef DEBUG + if (this->m_demoWindowOpen) { + ImGui::ShowDemoWindow(&this->m_demoWindowOpen); + ImPlot::ShowDemoWindow(&this->m_demoWindowOpen); + } +#endif + } + void Window::frameEnd() { ImGui::Render(); @@ -583,6 +584,7 @@ namespace hex { this->m_window = glfwCreateWindow(1280 * this->m_globalScale, 720 * this->m_globalScale, "ImHex", nullptr, nullptr); + glfwSetWindowUserPointer(this->m_window, this); if (this->m_window == nullptr) throw std::runtime_error("Failed to create window!"); @@ -604,10 +606,20 @@ namespace hex { glfwSetWindowPosCallback(this->m_window, [](GLFWwindow *window, int x, int y) { SharedData::windowPos = ImVec2(x, y); + + auto win = static_cast(glfwGetWindowUserPointer(window)); + win->frameBegin(); + win->frame(); + win->frameEnd(); }); glfwSetWindowSizeCallback(this->m_window, [](GLFWwindow *window, int width, int height) { SharedData::windowSize = ImVec2(width, height); + + auto win = static_cast(glfwGetWindowUserPointer(window)); + win->frameBegin(); + win->frame(); + win->frameEnd(); }); glfwSetKeyCallback(this->m_window, [](GLFWwindow *window, int key, int scancode, int action, int mods) {