Archived
1
Fork 0

Allow better DPI handling under SDL, handle external resize events in imgui

This commit is contained in:
redstrate 2021-10-12 11:06:59 -04:00
parent 8205f65a8e
commit a2f81d7ca7
7 changed files with 24 additions and 15 deletions

View file

@ -16,6 +16,7 @@ namespace prism {
void render(); void render();
void process_move(platform::window_ptr identifier); void process_move(platform::window_ptr identifier);
void process_resize(platform::window_ptr identifier);
void process_mouse_down(int button); void process_mouse_down(int button);

View file

@ -425,6 +425,8 @@ void engine::resize(const platform::window_ptr identifier, const prism::Extent e
gfx->recreate_view(identifier, drawable_extent.width, drawable_extent.height); gfx->recreate_view(identifier, drawable_extent.width, drawable_extent.height);
current_renderer->resize_render_target(*window->render_target, drawable_extent); current_renderer->resize_render_target(*window->render_target, drawable_extent);
imgui->process_resize(identifier);
} }
void engine::move(const platform::window_ptr identifier) { void engine::move(const platform::window_ptr identifier) {

View file

@ -159,6 +159,12 @@ void imgui_backend::process_move(platform::window_ptr identifier) {
viewport->PlatformRequestMove = true; viewport->PlatformRequestMove = true;
} }
void imgui_backend::process_resize(platform::window_ptr identifier) {
auto viewport = ImGui::FindViewportByPlatformHandle(identifier);
if(viewport != nullptr)
viewport->PlatformRequestResize = true;
}
void imgui_backend::begin_frame(const float delta_time) { void imgui_backend::begin_frame(const float delta_time) {
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();

View file

@ -101,9 +101,6 @@ namespace platform {
/// Forces the platform to quit the application. This is not related to Engine::quit(). /// Forces the platform to quit the application. This is not related to Engine::quit().
void force_quit(); void force_quit();
/// Gets the content scale for the window. 1.0 would be 1x scale, 2.0 would be 2x scale, etc.
float get_window_dpi(window_ptr window);
/// Gets the content scale for the monitor. 1.0 would be 1x scale, 2.0 would be 2x scale, etc. /// Gets the content scale for the monitor. 1.0 would be 1x scale, 2.0 would be 2x scale, etc.
float get_monitor_dpi(); float get_monitor_dpi();

View file

@ -154,8 +154,8 @@ void ImGuiPass::load_font(const std::string_view filename) {
font_file->read_all(); font_file->read_all();
io.Fonts->AddFontFromMemoryTTF(font_file->cast_data<unsigned char>(), font_file->size(), 15.0f * platform::get_window_dpi(engine->get_main_window())); io.Fonts->AddFontFromMemoryTTF(font_file->cast_data<unsigned char>(), font_file->size(), 15.0f * platform::get_monitor_dpi());
ImGui::GetIO().FontGlobalScale = 1.0f / platform::get_window_dpi(engine->get_main_window()); ImGui::GetIO().FontGlobalScale = 1.0f / platform::get_monitor_dpi();
} else { } else {
prism::log("Failed to load font file for imgui!"); prism::log("Failed to load font file for imgui!");
return; return;

View file

@ -135,23 +135,26 @@ void platform::force_quit() {
SDL_Quit(); SDL_Quit();
} }
float platform::get_window_dpi(const platform::window_ptr index) {
return 1.0;
}
float platform::get_monitor_dpi() { float platform::get_monitor_dpi() {
return 1.0; float dpi = 1.0f;
if (!SDL_GetDisplayDPI(0, &dpi, nullptr, nullptr))
dpi = dpi / 96.0f;
return dpi;
} }
prism::Rectangle platform::get_monitor_resolution() { prism::Rectangle platform::get_monitor_resolution() {
SDL_DisplayMode DM; SDL_Rect r;
SDL_GetCurrentDisplayMode(0, &DM); SDL_GetDisplayBounds(0, &r);
return {0, 0, (uint32_t)DM.w, (uint32_t)DM.h}; return {r.x, r.y, r.w, r.h};
} }
prism::Rectangle platform::get_monitor_work_area() { prism::Rectangle platform::get_monitor_work_area() {
return platform::get_monitor_resolution(); SDL_Rect r = {};
SDL_GetDisplayUsableBounds(0, &r);
return {r.x, r.y, r.w, r.h};
} }
prism::Offset platform::get_window_position(const platform::window_ptr index) { prism::Offset platform::get_window_position(const platform::window_ptr index) {

View file

@ -662,7 +662,7 @@ void CommonEditor::createDockArea() {
void CommonEditor::drawViewport(Scene* scene) { void CommonEditor::drawViewport(Scene* scene) {
const auto size = ImGui::GetContentRegionAvail(); const auto size = ImGui::GetContentRegionAvail();
const auto real_size = ImVec2(size.x * platform::get_window_dpi(engine->get_main_window()), size.y * platform::get_window_dpi(0)); const auto real_size = ImVec2(size.x * platform::get_monitor_dpi(), size.y * platform::get_monitor_dpi());
if(real_size.x <= 0 || real_size.y <= 0) if(real_size.x <= 0 || real_size.y <= 0)
return; return;