Skip to content

Commit

Permalink
HDPI awareness
Browse files Browse the repository at this point in the history
  • Loading branch information
3dJan committed Oct 18, 2024
1 parent c16ec84 commit 7d1940d
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 69 deletions.
87 changes: 66 additions & 21 deletions gladius/src/ui/GLView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,25 @@
#include "../IconFontCppHeaders/IconsFontAwesome5.h"
#include "../gpgpu.h"


#include <imgui_impl_glfw.h>
#include <imgui_impl_opengl2.h>
#ifdef _WIN32
#include <imgui_impl_win32.h>
#endif
#include "imgui.h"
#include <GLFW/glfw3.h>

#ifdef _WIN32
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#endif
#include "Profiling.h"
#include <chrono>
#include <filesystem>
#include <fmt/format.h>
#include <iostream>
#include <sago/platform_folders.h>
#include <thread>
#include "Profiling.h"

namespace gladius
{
Expand Down Expand Up @@ -77,8 +79,6 @@ namespace gladius
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);



m_window = glfwCreateWindow(
1920, 1080, "Gladius - Advanced Cheese Grater Creator", nullptr, nullptr);

Expand All @@ -88,11 +88,8 @@ namespace gladius
return;
}


static auto staticDropCallback = [this](GLFWwindow *window, int count, const char ** paths)
{
handleDropCallback(window, count, paths);
};
static auto staticDropCallback = [this](GLFWwindow * window, int count, const char ** paths)
{ handleDropCallback(window, count, paths); };

glfwSetDropCallback(m_window,
[](GLFWwindow * window, int count, const char ** paths)
Expand All @@ -106,6 +103,14 @@ namespace gladius
}
glShadeModel(GL_FLAT);
initImgUI();

// determine hdpi scaling
determineUiScale();
static auto staticWindowSizeCallback = [this](GLFWwindow* window, int width, int height)
{ determineUiScale(); };
glfwSetWindowSizeCallback(m_window, [](GLFWwindow* window, int width, int height)
{ staticWindowSizeCallback(window, width, height); });

applyFullscreenMode();
}

Expand Down Expand Up @@ -162,7 +167,6 @@ namespace gladius
ImGui::CreateContext();
ImGuiIO & io = ImGui::GetIO();


std::filesystem::path gladiusConfigDir =
sago::getConfigHome() / std::filesystem::path{"gladius"};
m_gladiusImgUiFilename = gladiusConfigDir / "ui.config";
Expand Down Expand Up @@ -206,6 +210,9 @@ namespace gladius

io.FontGlobalScale /= font_scaling_factor;

// backup the style
m_originalStyle = ImGui::GetStyle();

// Setup Platform/Renderer bindings
ImGui_ImplGlfw_InitForOpenGL(m_window, true);
ImGui_ImplOpenGL2_Init();
Expand Down Expand Up @@ -246,8 +253,20 @@ namespace gladius
ImGui::GetIO().Framerate)
.c_str());
}

// zoom / dpi scaling
ImGui::Text("UI Scaling");
ImGui::SliderFloat("UI Scaling", &m_uiScale, 0.1f, 5.0f);

ImGui::End();
}

// Set all scales in style to the same value
ImGui::GetIO().FontGlobalScale = m_uiScale * 0.5f;
ImGui::GetStyle() = m_originalStyle;
ImGuiStyle & style = ImGui::GetStyle();
style.ScaleAllSizes(m_uiScale);

for (auto view : m_viewCallBacks)
{
view();
Expand All @@ -270,6 +289,29 @@ namespace gladius
#endif
}

void GLView::determineUiScale()
{
#ifdef _WIN32
// Windows DPI scaling
HWND hwnd = glfwGetWin32Window(m_window);
if (hwnd)
{
m_uiScale = ImGui_ImplWin32_GetDpiScaleForHwnd(hwnd);
return;
}

ImGui_ImplWin32_EnableDpiAwareness();
#endif

int width, height;
glfwGetWindowSize(m_window, &width, &height);
int fbWidth, fbHeight;
glfwGetFramebufferSize(m_window, &fbWidth, &fbHeight);
float hdpiScalingX = static_cast<float>(fbWidth) / static_cast<float>(width);
float hdpiScalingY = static_cast<float>(fbHeight) / static_cast<float>(height);
m_uiScale = (hdpiScalingX + hdpiScalingY) / 2.0f;
}

void GLView::handleDropCallback(GLFWwindow *, int count, const char ** paths)
{
for (int i = 0; i < count; ++i)
Expand Down Expand Up @@ -392,8 +434,10 @@ namespace gladius
{
auto lastAnimationTimePoint_ms = getTimeStamp_ms();
auto lastFrame_ms = getTimeStamp_ms();
auto constexpr minFrameDurationAnimation = std::chrono::milliseconds{static_cast<int>(1000. / 120.)};
auto constexpr minFrameDurationStatic = std::chrono::milliseconds{static_cast<int>(1000. / 30.)};
auto constexpr minFrameDurationAnimation =
std::chrono::milliseconds{static_cast<int>(1000. / 120.)};
auto constexpr minFrameDurationStatic =
std::chrono::milliseconds{static_cast<int>(1000. / 30.)};
while (!m_stateCloseRequested)
{
if (glfwWindowShouldClose(m_window))
Expand All @@ -404,7 +448,8 @@ namespace gladius
}

auto const durationSinceLastFrame_ms = getTimeStamp_ms() - lastFrame_ms;
auto const minFrameDuration = m_isAnimationRunning ? minFrameDurationAnimation : minFrameDurationStatic;
auto const minFrameDuration =
m_isAnimationRunning ? minFrameDurationAnimation : minFrameDurationStatic;

auto const delay_ms =
std::min(minFrameDuration, minFrameDuration - durationSinceLastFrame_ms);
Expand Down Expand Up @@ -468,13 +513,13 @@ namespace gladius
m_isAnimationRunning = false;
}

// HWND GLView::getNativeWindowHandle()
// {
// #ifdef _WIN32
// return m_window ? glfwGetWin32Window(m_window) : nullptr;
// #else
// return nullptr;
// #endif
// }
// HWND GLView::getNativeWindowHandle()
// {
// #ifdef _WIN32
// return m_window ? glfwGetWin32Window(m_window) : nullptr;
// #else
// return nullptr;
// #endif
// }

}
4 changes: 4 additions & 0 deletions gladius/src/ui/GLView.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace gladius
void initImgUI();

void displayUI();
void determineUiScale();

static void noOp()
{
Expand All @@ -91,5 +92,8 @@ namespace gladius
GLFWwindow* m_window{nullptr};
bool m_isAnimationRunning = false;
bool m_stateCloseRequested = false;

ImGuiStyle m_originalStyle;
float m_uiScale = 1.0f;
};
}
7 changes: 4 additions & 3 deletions gladius/src/ui/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ namespace gladius::ui
void MainWindow::render()
{
ProfileFunction;
m_uiScale = ImGui::GetIO().FontGlobalScale * 2.0f;
if (!m_core->getComputeContext().isValid())
{
m_logger->addEvent({"Reinitializing compute context", events::Severity::Info});
Expand Down Expand Up @@ -203,7 +204,7 @@ namespace gladius::ui
renderSettingsDialog();
}

ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {20.f, 12.f});
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, {20.f * m_uiScale, 12.f * m_uiScale});
if (ImGui::BeginMainMenuBar())
{
if (bigMenuItem(reinterpret_cast<const char *>(ICON_FA_BARS)))
Expand Down Expand Up @@ -449,15 +450,15 @@ namespace gladius::ui
const auto menuBarHeight = ImGui::GetWindowHeight();
ImGui::EndMainMenuBar();
auto & io = ImGui::GetIO();
const auto menuWidth = 400.f;
const auto menuWidth = 400.f * m_uiScale;
auto closeMenu = [&]()
{
m_showMainMenu = false;
m_mainMenuPosX = -menuWidth;
};

ImGui::SetNextWindowBgAlpha(0.9f);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20, 20});
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, {20 * m_uiScale, 20 * m_uiScale});

ImGui::Begin("Menu", &m_showMainMenu, window_flags);

Expand Down
2 changes: 2 additions & 0 deletions gladius/src/ui/MainWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,7 @@ namespace gladius::ui
std::chrono::time_point<std::chrono::steady_clock> m_lastUpateTime;

Outline m_outline;

float m_uiScale = 1.f;
};
}
5 changes: 3 additions & 2 deletions gladius/src/ui/ModelEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ namespace gladius::ui
ImGui::Begin("Tool Box");

ImGui::SetWindowSize(ImVec2(350, ImGui::GetWindowHeight()));
auto const frameHeight = ImGui::GetWindowHeight() - 50.f;
auto const height = std::max(frameHeight, 150.f);
auto const frameHeight = ImGui::GetWindowHeight() - 50.f * m_uiScale;
auto const height = std::max(frameHeight, 150.f * m_uiScale);

ImGui::BeginChildFrame(ImGui::GetID("Building blocks"),
ImVec2(300, height),
Expand Down Expand Up @@ -495,6 +495,7 @@ namespace gladius::ui

auto ModelEditor::showAndEdit() -> bool
{
m_uiScale = ImGui::GetIO().FontGlobalScale * 2.0f;
if (!m_currentModel || !m_assembly)
{
return false;
Expand Down
1 change: 1 addition & 0 deletions gladius/src/ui/ModelEditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ namespace gladius::ui
Outline m_outline;

NodeTypeToColor m_nodeTypeToColor;
float m_uiScale = 1.0f;
};

std::vector<ed::NodeId> selectedNodes(ed::EditorContext * editorContext);
Expand Down
Loading

0 comments on commit 7d1940d

Please sign in to comment.