From 8291aa1cd262ec500fe00456f2e9747d0812d15c Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Fri, 29 Sep 2023 16:21:35 +0200 Subject: [PATCH] improve dpi scaling --- yarge-frontend-sdl/Cargo.toml | 5 +-- yarge-frontend-sdl/src/config.rs | 2 +- yarge-frontend-sdl/src/main.rs | 53 +++++++++++++++++++------------- yarge-frontend-sdl/src/menu.rs | 22 ++++++------- 4 files changed, 46 insertions(+), 36 deletions(-) diff --git a/yarge-frontend-sdl/Cargo.toml b/yarge-frontend-sdl/Cargo.toml index 84233d4..38dda4c 100644 --- a/yarge-frontend-sdl/Cargo.toml +++ b/yarge-frontend-sdl/Cargo.toml @@ -16,9 +16,10 @@ anyhow = "1.0" dirs = "5.0" [target.'cfg(windows)'.dependencies] -windows = { version = "0.51", features = ["Win32_UI_HiDpi"] } +windows = { version = "0.51", features = ["Win32_UI_HiDpi"], optional = true } [features] -default = ["global_config"] +default = ["global_config", "hidpi"] production = [] global_config = [] +hidpi = ["dep:windows"] diff --git a/yarge-frontend-sdl/src/config.rs b/yarge-frontend-sdl/src/config.rs index 0a270fa..0736eec 100644 --- a/yarge-frontend-sdl/src/config.rs +++ b/yarge-frontend-sdl/src/config.rs @@ -80,7 +80,7 @@ impl Default for Configuration { speed: 1, save_slot: 0, dpi_scaling: true, - dpi_scaling_frac: false, + dpi_scaling_frac: true, } } } diff --git a/yarge-frontend-sdl/src/main.rs b/yarge-frontend-sdl/src/main.rs index a6915c8..f5cfce9 100644 --- a/yarge-frontend-sdl/src/main.rs +++ b/yarge-frontend-sdl/src/main.rs @@ -53,7 +53,7 @@ struct Args { fn main() { //Set dpi aware flag on windows - #[cfg(windows)] { + #[cfg(all(windows, feature = "hidpi"))] { use windows::Win32::UI::HiDpi::{SetProcessDpiAwareness, PROCESS_PER_MONITOR_DPI_AWARE}; if unsafe { SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE) }.is_err() { println!("[ERR] Failed to set DPI awareness"); @@ -186,30 +186,39 @@ fn main() { println!("[INIT/INFO] Initialization done"); - //Main loop + #[cfg(feature = "hidpi")] let mut dpi_prev = 1.; + + //Main loop 'run: loop { - //Update dpi scale - let mut display_dpi_scale = if config.dpi_scaling { - video_subsystem.display_dpi(canvas.window().display_index().unwrap()).unwrap().0 / 96. - } else { - 1. + //Figure out dpi stuff + let display_dpi_scale = { + #[cfg(feature = "hidpi")] { + let mut display_dpi_scale = if config.dpi_scaling { + video_subsystem.display_dpi(canvas.window().display_index().unwrap()).unwrap().0 / 96. + } else { + 1. + }; + if !config.dpi_scaling_frac { + display_dpi_scale = display_dpi_scale.ceil(); + } + if dpi_prev != display_dpi_scale { + println!("[INFO/DPI] dpi scale changed from {} to {}", dpi_prev, display_dpi_scale); + dpi_prev = display_dpi_scale; + let s = ( + GB_WIDTH as u32 * config.scale.scale_or_default(), + GB_HEIGHT as u32 * config.scale.scale_or_default() + ); + canvas.window_mut().set_size( + (display_dpi_scale * s.0 as f32) as u32, + (display_dpi_scale * s.1 as f32) as u32 + ).unwrap(); + } + text_renderer.set_render_dpi_scale(display_dpi_scale); + display_dpi_scale + } + #[cfg(not(feature = "hidpi"))] { 1. } }; - if !config.dpi_scaling_frac { - display_dpi_scale = display_dpi_scale.ceil(); - } - if dpi_prev != display_dpi_scale { - dpi_prev = display_dpi_scale; - let s = ( - GB_WIDTH as u32 * config.scale.scale_or_default(), - GB_HEIGHT as u32 * config.scale.scale_or_default() - ); - canvas.window_mut().set_size( - (display_dpi_scale * s.0 as f32) as u32, - (display_dpi_scale * s.1 as f32) as u32 - ).unwrap(); - } - text_renderer.set_render_dpi_scale(display_dpi_scale); //Process SDL2 events for event in event_pump.poll_iter() { diff --git a/yarge-frontend-sdl/src/menu.rs b/yarge-frontend-sdl/src/menu.rs index eb90024..8fad266 100644 --- a/yarge-frontend-sdl/src/menu.rs +++ b/yarge-frontend-sdl/src/menu.rs @@ -667,14 +667,14 @@ impl Menu { // Update scroll //TODO rewrite!! if let Some(cursor_y) = x_cursor_y { - let cursor_underscroll = cursor_y - (res.1 as i32 - (text.char_size(1.).1 as i32 + 2) - 2 * MENU_ITEM_HEIGHT as i32); + let cursor_underscroll = cursor_y - ((res.1 as f32 / dpi_scale) as i32 - (text.char_size(1.).1 as i32 + 2) - 2 * MENU_ITEM_HEIGHT as i32); let cursor_overscroll = cursor_y - list_start_y_noscroll - MENU_ITEM_HEIGHT as i32; self.scroll += cursor_underscroll.max(0) + cursor_overscroll.min(0); } self.scroll = self.scroll.max(0); // Draw scroll bar - let viewport_height = res.1 as f32 - list_start_y_noscroll as f32 - (text.char_size(1.).1 as f32 + 2.); + let viewport_height = (res.1 as f32 / dpi_scale) - list_start_y_noscroll as f32 - (text.char_size(1.).1 as f32 + 2.); let scrollbar_visible = (x_position.1 - list_start_y) as f32 > viewport_height; if scrollbar_visible { //Compute stuff @@ -684,16 +684,16 @@ impl Menu { let scrollbar_h = viewport_height_ratio * viewport_height; let y_correction: f32 = - (scrollbar_h * progress); let scrollbar_rect = Rect::from(( - res.0 as i32 - SCROLLBAR_WIDTH as i32, - list_start_y_noscroll + ((progress * viewport_height) + y_correction) as i32, - SCROLLBAR_WIDTH, - scrollbar_h as u32 + res.0 as i32 - m(SCROLLBAR_WIDTH as i32, dpi_scale), + m(list_start_y_noscroll + ((progress * viewport_height) + y_correction) as i32, dpi_scale), + mu(SCROLLBAR_WIDTH, dpi_scale), + mu(scrollbar_h as u32, dpi_scale) )); let scrollbar_bg_rect = Rect::from(( - res.0 as i32 - SCROLLBAR_WIDTH as i32, - list_start_y_noscroll, - SCROLLBAR_WIDTH, - viewport_height as u32 + res.0 as i32 - m(SCROLLBAR_WIDTH as i32, dpi_scale), + m(list_start_y_noscroll, dpi_scale), + mu(SCROLLBAR_WIDTH, dpi_scale), + mu(viewport_height as u32, dpi_scale) )); canvas.set_draw_color(Color::RGBA(0, 0, 0, 96)); canvas.fill_rects(&[scrollbar_rect, scrollbar_rect, scrollbar_bg_rect]).unwrap(); @@ -713,7 +713,7 @@ impl Menu { let h = text.char_size(1.).1 + 2; //animation let opa = (self.activation_anim_state.value * 255.) as u32 as u8; - let offst = h as i32 - (self.activation_anim_state.value * h as f32) as i32; + let offst = -(h as i32 - (self.activation_anim_state.value * h as f32) as i32); //box canvas.set_draw_color(Color::RGBA(0, 0, 0, opa / 4)); canvas.fill_rects(&[