From 734eda2d5752e2f5c152ae9c61708aa961dab7c2 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 13 Jun 2016 14:31:49 +1000 Subject: [PATCH 1/6] Add hidpi support --- examples/support/mod.rs | 16 ++++++--- src/glium_renderer.rs | 79 ++++++++++++++++++++++++----------------- src/lib.rs | 22 +++++++++--- 3 files changed, 75 insertions(+), 42 deletions(-) diff --git a/examples/support/mod.rs b/examples/support/mod.rs index 8b52987..ab9afca 100644 --- a/examples/support/mod.rs +++ b/examples/support/mod.rs @@ -56,10 +56,10 @@ impl Support { } } - pub fn update_mouse(&mut self) { - self.imgui.set_mouse_pos(self.mouse_pos.0 as f32, self.mouse_pos.1 as f32); + pub fn update_mouse(&mut self, hidpi_factor: f32) { + self.imgui.set_mouse_pos(self.mouse_pos.0 as f32 / hidpi_factor, self.mouse_pos.1 as f32 / hidpi_factor); self.imgui.set_mouse_down(&[self.mouse_pressed.0, self.mouse_pressed.1, self.mouse_pressed.2, false, false]); - self.imgui.set_mouse_wheel(self.mouse_wheel); + self.imgui.set_mouse_wheel(self.mouse_wheel / hidpi_factor); } pub fn render<'ui, 'a: 'ui , F: FnMut(&Ui<'ui>)>( @@ -69,7 +69,12 @@ impl Support { let delta_f = delta.num_nanoseconds().unwrap() as f32 / 1_000_000_000.0; self.last_frame = now; - self.update_mouse(); + let hidpi_factor = + self.display.get_window() + .expect("Failed to get window") + .hidpi_factor(); + + self.update_mouse(hidpi_factor); self.mouse_wheel = 0.0; let mut target = self.display.draw(); @@ -77,8 +82,9 @@ impl Support { clear_color.2, clear_color.3); let (width, height) = target.get_dimensions(); - let ui = self.imgui.frame(width, height, delta_f); + let ui = self.imgui.frame(width, height, hidpi_factor, delta_f); f(&ui); + self.renderer.render(&mut target, ui).unwrap(); target.finish().unwrap(); diff --git a/src/glium_renderer.rs b/src/glium_renderer.rs index d1a802d..0f11c6a 100644 --- a/src/glium_renderer.rs +++ b/src/glium_renderer.rs @@ -1,8 +1,9 @@ -use glium::{Blend, DrawError, DrawParameters, GlObject, IndexBuffer, Program, Rect, Surface, - Texture2d, VertexBuffer, index, program, texture, vertex}; +use glium::{DrawError, GlObject, IndexBuffer, Program, Surface, Texture2d, VertexBuffer}; use glium::backend::{Context, Facade}; -use glium::index::PrimitiveType; -use glium::texture::{ClientFormat, RawImage2d}; +use glium::program; +use glium::index::{self, PrimitiveType}; +use glium::texture; +use glium::vertex; use libc::uintptr_t; use std::borrow::Cow; use std::fmt; @@ -67,22 +68,31 @@ impl Renderer { device_objects: device_objects, }) } + pub fn render<'a, S: Surface>(&mut self, surface: &mut S, ui: Ui<'a>) -> RendererResult<()> { let _ = self.ctx.insert_debug_marker("imgui-rs: starting rendering"); - let result = ui.render(|draw_list| self.render_draw_list(surface, draw_list)); + let result = ui.render(|draw_list, hidpi_factor| { + self.render_draw_list(surface, draw_list, hidpi_factor) + }); let _ = self.ctx.insert_debug_marker("imgui-rs: rendering finished"); result } + fn render_draw_list<'a, S: Surface>(&mut self, surface: &mut S, - draw_list: DrawList<'a>) + draw_list: DrawList<'a>, + hidpi_factor: f32) -> RendererResult<()> { + use glium::{Blend, DrawParameters, Rect}; + use glium::uniforms::MagnifySamplerFilter; + try!(self.device_objects.upload_vertex_buffer(&self.ctx, draw_list.vtx_buffer)); try!(self.device_objects.upload_index_buffer(&self.ctx, draw_list.idx_buffer)); let (width, height) = surface.get_dimensions(); - let matrix = [[2.0 / (width as f32), 0.0, 0.0, 0.0], - [0.0, 2.0 / -(height as f32), 0.0, 0.0], + + let matrix = [[2.0 / (width as f32 / hidpi_factor), 0.0, 0.0, 0.0], + [0.0, 2.0 / -(height as f32 / hidpi_factor), 0.0, 0.0], [0.0, 0.0, -1.0, 0.0], [-1.0, 1.0, 0.0, 1.0]]; let font_texture_id = self.device_objects.texture.get_id() as uintptr_t; @@ -91,31 +101,34 @@ impl Renderer { for cmd in draw_list.cmd_buffer { // We don't support custom textures...yet! assert!(cmd.texture_id as uintptr_t == font_texture_id); - let uniforms = uniform! { - matrix: matrix, - tex: &self.device_objects.texture - }; - let draw_params = DrawParameters { - blend: Blend::alpha_blending(), - scissor: Some(Rect { - left: cmd.clip_rect.x as u32, - bottom: (height as f32 - cmd.clip_rect.w) as u32, - width: (cmd.clip_rect.z - cmd.clip_rect.x) as u32, - height: (cmd.clip_rect.w - cmd.clip_rect.y) as u32, - }), - ..Default::default() - }; + let idx_end = idx_start + cmd.elem_count as usize; + try!(surface.draw(&self.device_objects.vertex_buffer, - &self.device_objects - .index_buffer - .slice(idx_start..idx_end) - .expect("Invalid index buffer range"), - &self.device_objects.program, - &uniforms, - &draw_params)); + &self.device_objects + .index_buffer + .slice(idx_start..idx_end) + .expect("Invalid index buffer range"), + &self.device_objects.program, + &uniform! { + matrix: matrix, + tex: self.device_objects.texture.sampled() + .magnify_filter(MagnifySamplerFilter::Nearest), + }, + &DrawParameters { + blend: Blend::alpha_blending(), + scissor: Some(Rect { + left: (cmd.clip_rect.x * hidpi_factor) as u32, + bottom: (height as f32 - (cmd.clip_rect.w * hidpi_factor)) as u32, + width: ((cmd.clip_rect.z - cmd.clip_rect.x) * hidpi_factor) as u32, + height: ((cmd.clip_rect.w - cmd.clip_rect.y) * hidpi_factor) as u32, + }), + ..DrawParameters::default() + })); + idx_start = idx_end; } + Ok(()) } } @@ -134,18 +147,20 @@ fn compile_default_program(ctx: &F) 140 => { vertex: include_str!("shader/vert_140.glsl"), fragment: include_str!("shader/frag_140.glsl"), - outputs_srgb: true + outputs_srgb: true, }, 110 => { vertex: include_str!("shader/vert_110.glsl"), fragment: include_str!("shader/frag_110.glsl"), - outputs_srgb: true - } + outputs_srgb: true, + }, ) } impl DeviceObjects { pub fn init(im_gui: &mut ImGui, ctx: &F) -> RendererResult { + use glium::texture::{ClientFormat, RawImage2d}; + let vertex_buffer = try!(VertexBuffer::empty_dynamic(ctx, 0)); let index_buffer = try!(IndexBuffer::empty_dynamic(ctx, PrimitiveType::TrianglesList, 0)); diff --git a/src/lib.rs b/src/lib.rs index 3c4ebc5..99d2629 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -265,7 +265,12 @@ impl ImGui { pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } } pub fn get_frame_count(&self) -> i32 { unsafe { imgui_sys::igGetFrameCount() } } pub fn get_frame_rate(&self) -> f32 { self.io().framerate } - pub fn frame<'ui, 'a: 'ui>(&'a mut self, width: u32, height: u32, delta_time: f32) -> Ui<'ui> { + pub fn frame<'ui, 'a: 'ui>(&'a mut self, + width: u32, + height: u32, + hidpi_factor: f32, + delta_time: f32) + -> Ui<'ui> { { let io = self.io_mut(); io.display_size.x = width as c_float; @@ -274,9 +279,15 @@ impl ImGui { } unsafe { imgui_sys::igNewFrame(); - CURRENT_UI = Some(Ui { imgui: mem::transmute(self as &'a ImGui) }); + CURRENT_UI = Some(Ui { + imgui: mem::transmute(self as &'a ImGui), + hidpi_factor: hidpi_factor, + }); + } + Ui { + imgui: self, + hidpi_factor: hidpi_factor, } - Ui { imgui: self } } } @@ -299,6 +310,7 @@ pub struct DrawList<'a> { pub struct Ui<'ui> { imgui: &'ui ImGui, + hidpi_factor: f32, } static FMT: &'static [u8] = b"%s\0"; @@ -336,7 +348,7 @@ impl<'ui> Ui<'ui> { io.metrics_active_windows } pub fn render(self, mut f: F) -> Result<(), E> - where F: FnMut(DrawList<'ui>) -> Result<(), E> + where F: FnMut(DrawList<'ui>, f32) -> Result<(), E> { unsafe { imgui_sys::igRender(); @@ -348,7 +360,7 @@ impl<'ui> Ui<'ui> { idx_buffer: (*cmd_list).idx_buffer.as_slice(), vtx_buffer: (*cmd_list).vtx_buffer.as_slice(), }; - try!(f(draw_list)); + try!(f(draw_list, self.hidpi_factor)); } CURRENT_UI = None; } From f075dde395294a74c39666a992020ddecfd56014 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 13 Jun 2016 14:43:51 +1000 Subject: [PATCH 2/6] Move hidpi_factor into ImGui struct --- examples/support/mod.rs | 21 +++++++++++---------- src/lib.rs | 33 ++++++++++++++------------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/examples/support/mod.rs b/examples/support/mod.rs index ab9afca..29cd5fe 100644 --- a/examples/support/mod.rs +++ b/examples/support/mod.rs @@ -56,10 +56,15 @@ impl Support { } } - pub fn update_mouse(&mut self, hidpi_factor: f32) { - self.imgui.set_mouse_pos(self.mouse_pos.0 as f32 / hidpi_factor, self.mouse_pos.1 as f32 / hidpi_factor); + pub fn update_hidpi_factor(&mut self) { + let hidpi_factor = self.display.get_window().expect("Failed to get window").hidpi_factor(); + self.imgui.set_hidpi_factor(hidpi_factor); + } + + pub fn update_mouse(&mut self) { + self.imgui.set_mouse_pos(self.mouse_pos.0 as f32, self.mouse_pos.1 as f32); self.imgui.set_mouse_down(&[self.mouse_pressed.0, self.mouse_pressed.1, self.mouse_pressed.2, false, false]); - self.imgui.set_mouse_wheel(self.mouse_wheel / hidpi_factor); + self.imgui.set_mouse_wheel(self.mouse_wheel); } pub fn render<'ui, 'a: 'ui , F: FnMut(&Ui<'ui>)>( @@ -69,12 +74,8 @@ impl Support { let delta_f = delta.num_nanoseconds().unwrap() as f32 / 1_000_000_000.0; self.last_frame = now; - let hidpi_factor = - self.display.get_window() - .expect("Failed to get window") - .hidpi_factor(); - - self.update_mouse(hidpi_factor); + self.update_hidpi_factor(); + self.update_mouse(); self.mouse_wheel = 0.0; let mut target = self.display.draw(); @@ -82,7 +83,7 @@ impl Support { clear_color.2, clear_color.3); let (width, height) = target.get_dimensions(); - let ui = self.imgui.frame(width, height, hidpi_factor, delta_f); + let ui = self.imgui.frame(width, height, delta_f); f(&ui); self.renderer.render(&mut target, ui).unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 99d2629..986b979 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,6 +68,8 @@ pub struct ImGui { // lives long enough in case the ImStr contains a Cow::Owned ini_filename: Option>, log_filename: Option>, + // Ideally this would be handled by imgui, but for now we have to keep track of it ourselves + hidpi_factor: f32, } #[macro_export] @@ -126,6 +128,7 @@ impl ImGui { ImGui { ini_filename: None, log_filename: None, + hidpi_factor: 1.0, } } fn io(&self) -> &imgui_sys::ImGuiIO { unsafe { mem::transmute(imgui_sys::igGetIO()) } } @@ -206,14 +209,18 @@ impl ImGui { let io = self.io_mut(); io.key_repeat_rate = value; } + pub fn hidpi_factor(&self) -> f32 { self.hidpi_factor } + pub fn set_hidpi_factor(&mut self, hidpi_factor: f32) { self.hidpi_factor = hidpi_factor; } pub fn mouse_pos(&self) -> (f32, f32) { + let hidpi_factor = self.hidpi_factor; let io = self.io(); - (io.mouse_pos.x, io.mouse_pos.y) + (io.mouse_pos.x * hidpi_factor, io.mouse_pos.y * hidpi_factor) } pub fn set_mouse_pos(&mut self, x: f32, y: f32) { + let hidpi_factor = self.hidpi_factor; let io = self.io_mut(); - io.mouse_pos.x = x; - io.mouse_pos.y = y; + io.mouse_pos.x = x / hidpi_factor; + io.mouse_pos.y = y / hidpi_factor; } pub fn set_mouse_down(&mut self, states: &[bool; 5]) { let io = self.io_mut(); @@ -265,12 +272,7 @@ impl ImGui { pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } } pub fn get_frame_count(&self) -> i32 { unsafe { imgui_sys::igGetFrameCount() } } pub fn get_frame_rate(&self) -> f32 { self.io().framerate } - pub fn frame<'ui, 'a: 'ui>(&'a mut self, - width: u32, - height: u32, - hidpi_factor: f32, - delta_time: f32) - -> Ui<'ui> { + pub fn frame<'ui, 'a: 'ui>(&'a mut self, width: u32, height: u32, delta_time: f32) -> Ui<'ui> { { let io = self.io_mut(); io.display_size.x = width as c_float; @@ -279,15 +281,9 @@ impl ImGui { } unsafe { imgui_sys::igNewFrame(); - CURRENT_UI = Some(Ui { - imgui: mem::transmute(self as &'a ImGui), - hidpi_factor: hidpi_factor, - }); - } - Ui { - imgui: self, - hidpi_factor: hidpi_factor, + CURRENT_UI = Some(Ui { imgui: mem::transmute(self as &'a ImGui) }); } + Ui { imgui: self } } } @@ -310,7 +306,6 @@ pub struct DrawList<'a> { pub struct Ui<'ui> { imgui: &'ui ImGui, - hidpi_factor: f32, } static FMT: &'static [u8] = b"%s\0"; @@ -360,7 +355,7 @@ impl<'ui> Ui<'ui> { idx_buffer: (*cmd_list).idx_buffer.as_slice(), vtx_buffer: (*cmd_list).vtx_buffer.as_slice(), }; - try!(f(draw_list, self.hidpi_factor)); + try!(f(draw_list, self.imgui.hidpi_factor)); } CURRENT_UI = None; } From 88878896fa0235d47987c40f9599a1c78966df86 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 13 Jun 2016 16:07:18 +1000 Subject: [PATCH 3/6] Simplify type signature --- examples/support/mod.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/support/mod.rs b/examples/support/mod.rs index 29cd5fe..269020a 100644 --- a/examples/support/mod.rs +++ b/examples/support/mod.rs @@ -67,8 +67,7 @@ impl Support { self.imgui.set_mouse_wheel(self.mouse_wheel); } - pub fn render<'ui, 'a: 'ui , F: FnMut(&Ui<'ui>)>( - &'a mut self, clear_color: (f32, f32, f32, f32), mut f: F) { + pub fn render(&mut self, clear_color: (f32, f32, f32, f32), mut run_ui: F) { let now = SteadyTime::now(); let delta = now - self.last_frame; let delta_f = delta.num_nanoseconds().unwrap() as f32 / 1_000_000_000.0; @@ -84,7 +83,8 @@ impl Support { let (width, height) = target.get_dimensions(); let ui = self.imgui.frame(width, height, delta_f); - f(&ui); + + run_ui(&ui); self.renderer.render(&mut target, ui).unwrap(); From ce45b81e8e04d2adf6f7ac7224b51d9d09ce30a3 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 13 Jun 2016 20:20:14 +1000 Subject: [PATCH 4/6] Pass Ui struct to render closure instead of raw hidpi factor --- src/glium_renderer.rs | 9 ++++----- src/lib.rs | 4 ++-- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/glium_renderer.rs b/src/glium_renderer.rs index 0f11c6a..a93a5c2 100644 --- a/src/glium_renderer.rs +++ b/src/glium_renderer.rs @@ -71,17 +71,15 @@ impl Renderer { pub fn render<'a, S: Surface>(&mut self, surface: &mut S, ui: Ui<'a>) -> RendererResult<()> { let _ = self.ctx.insert_debug_marker("imgui-rs: starting rendering"); - let result = ui.render(|draw_list, hidpi_factor| { - self.render_draw_list(surface, draw_list, hidpi_factor) - }); + let result = ui.render(|ui, draw_list| self.render_draw_list(surface, ui, draw_list)); let _ = self.ctx.insert_debug_marker("imgui-rs: rendering finished"); result } fn render_draw_list<'a, S: Surface>(&mut self, surface: &mut S, - draw_list: DrawList<'a>, - hidpi_factor: f32) + ui: &'a Ui<'a>, + draw_list: DrawList<'a>) -> RendererResult<()> { use glium::{Blend, DrawParameters, Rect}; use glium::uniforms::MagnifySamplerFilter; @@ -89,6 +87,7 @@ impl Renderer { try!(self.device_objects.upload_vertex_buffer(&self.ctx, draw_list.vtx_buffer)); try!(self.device_objects.upload_index_buffer(&self.ctx, draw_list.idx_buffer)); + let hidpi_factor = ui.imgui().hidpi_factor(); let (width, height) = surface.get_dimensions(); let matrix = [[2.0 / (width as f32 / hidpi_factor), 0.0, 0.0, 0.0], diff --git a/src/lib.rs b/src/lib.rs index 986b979..bd928e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -343,7 +343,7 @@ impl<'ui> Ui<'ui> { io.metrics_active_windows } pub fn render(self, mut f: F) -> Result<(), E> - where F: FnMut(DrawList<'ui>, f32) -> Result<(), E> + where F: FnMut(&Ui, DrawList) -> Result<(), E> { unsafe { imgui_sys::igRender(); @@ -355,7 +355,7 @@ impl<'ui> Ui<'ui> { idx_buffer: (*cmd_list).idx_buffer.as_slice(), vtx_buffer: (*cmd_list).vtx_buffer.as_slice(), }; - try!(f(draw_list, self.imgui.hidpi_factor)); + try!(f(&self, draw_list)); } CURRENT_UI = None; } From 3548542ca5bef727df32c6022bf8ce374da76658 Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Mon, 13 Jun 2016 22:22:47 +1000 Subject: [PATCH 5/6] Make hidpi implementation consistent with upstream --- examples/support/mod.rs | 21 ++++++++++---------- src/glium_renderer.rs | 20 +++++++++++-------- src/lib.rs | 43 +++++++++++++++++++++++++++-------------- 3 files changed, 51 insertions(+), 33 deletions(-) diff --git a/examples/support/mod.rs b/examples/support/mod.rs index 269020a..55c272d 100644 --- a/examples/support/mod.rs +++ b/examples/support/mod.rs @@ -56,15 +56,12 @@ impl Support { } } - pub fn update_hidpi_factor(&mut self) { - let hidpi_factor = self.display.get_window().expect("Failed to get window").hidpi_factor(); - self.imgui.set_hidpi_factor(hidpi_factor); - } - pub fn update_mouse(&mut self) { - self.imgui.set_mouse_pos(self.mouse_pos.0 as f32, self.mouse_pos.1 as f32); + let scale = self.imgui.display_framebuffer_scale(); + self.imgui.set_mouse_pos(self.mouse_pos.0 as f32 / scale.0, self.mouse_pos.1 as f32 / scale.1); self.imgui.set_mouse_down(&[self.mouse_pressed.0, self.mouse_pressed.1, self.mouse_pressed.2, false, false]); - self.imgui.set_mouse_wheel(self.mouse_wheel); + self.imgui.set_mouse_wheel(self.mouse_wheel / scale.1); + self.mouse_wheel = 0.0; } pub fn render(&mut self, clear_color: (f32, f32, f32, f32), mut run_ui: F) { @@ -73,16 +70,18 @@ impl Support { let delta_f = delta.num_nanoseconds().unwrap() as f32 / 1_000_000_000.0; self.last_frame = now; - self.update_hidpi_factor(); self.update_mouse(); - self.mouse_wheel = 0.0; let mut target = self.display.draw(); target.clear_color(clear_color.0, clear_color.1, clear_color.2, clear_color.3); - let (width, height) = target.get_dimensions(); - let ui = self.imgui.frame(width, height, delta_f); + // Early return if the window closes before we get the sizes + let window = match self.display.get_window() { Some(x) => x, None => return }; + let size_points = match window.get_inner_size_points() { Some(x) => x, None => return }; + let size_pixels = match window.get_inner_size_pixels() { Some(x) => x, None => return }; + + let ui = self.imgui.frame(size_points, size_pixels, delta_f); run_ui(&ui); diff --git a/src/glium_renderer.rs b/src/glium_renderer.rs index a93a5c2..76b718b 100644 --- a/src/glium_renderer.rs +++ b/src/glium_renderer.rs @@ -87,11 +87,15 @@ impl Renderer { try!(self.device_objects.upload_vertex_buffer(&self.ctx, draw_list.vtx_buffer)); try!(self.device_objects.upload_index_buffer(&self.ctx, draw_list.idx_buffer)); - let hidpi_factor = ui.imgui().hidpi_factor(); - let (width, height) = surface.get_dimensions(); + let (width, height) = ui.imgui().display_size(); + let (scale_width, scale_height) = ui.imgui().display_framebuffer_scale(); - let matrix = [[2.0 / (width as f32 / hidpi_factor), 0.0, 0.0, 0.0], - [0.0, 2.0 / -(height as f32 / hidpi_factor), 0.0, 0.0], + if width == 0.0 || height == 0.0 { + return Ok(()); + } + + let matrix = [[2.0 / width as f32, 0.0, 0.0, 0.0], + [0.0, 2.0 / -(height as f32), 0.0, 0.0], [0.0, 0.0, -1.0, 0.0], [-1.0, 1.0, 0.0, 1.0]]; let font_texture_id = self.device_objects.texture.get_id() as uintptr_t; @@ -117,10 +121,10 @@ impl Renderer { &DrawParameters { blend: Blend::alpha_blending(), scissor: Some(Rect { - left: (cmd.clip_rect.x * hidpi_factor) as u32, - bottom: (height as f32 - (cmd.clip_rect.w * hidpi_factor)) as u32, - width: ((cmd.clip_rect.z - cmd.clip_rect.x) * hidpi_factor) as u32, - height: ((cmd.clip_rect.w - cmd.clip_rect.y) * hidpi_factor) as u32, + left: (cmd.clip_rect.x * scale_width) as u32, + bottom: ((height - cmd.clip_rect.w) * scale_height) as u32, + width: ((cmd.clip_rect.z - cmd.clip_rect.x) * scale_width) as u32, + height: ((cmd.clip_rect.w - cmd.clip_rect.y) * scale_height) as u32, }), ..DrawParameters::default() })); diff --git a/src/lib.rs b/src/lib.rs index bd928e3..2863c11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -68,8 +68,6 @@ pub struct ImGui { // lives long enough in case the ImStr contains a Cow::Owned ini_filename: Option>, log_filename: Option>, - // Ideally this would be handled by imgui, but for now we have to keep track of it ourselves - hidpi_factor: f32, } #[macro_export] @@ -128,7 +126,6 @@ impl ImGui { ImGui { ini_filename: None, log_filename: None, - hidpi_factor: 1.0, } } fn io(&self) -> &imgui_sys::ImGuiIO { unsafe { mem::transmute(imgui_sys::igGetIO()) } } @@ -209,18 +206,22 @@ impl ImGui { let io = self.io_mut(); io.key_repeat_rate = value; } - pub fn hidpi_factor(&self) -> f32 { self.hidpi_factor } - pub fn set_hidpi_factor(&mut self, hidpi_factor: f32) { self.hidpi_factor = hidpi_factor; } - pub fn mouse_pos(&self) -> (f32, f32) { - let hidpi_factor = self.hidpi_factor; + pub fn display_size(&self) -> (f32, f32) { let io = self.io(); - (io.mouse_pos.x * hidpi_factor, io.mouse_pos.y * hidpi_factor) + (io.display_size.x, io.display_size.y) + } + pub fn display_framebuffer_scale(&self) -> (f32, f32) { + let io = self.io(); + (io.display_framebuffer_scale.x, io.display_framebuffer_scale.y) + } + pub fn mouse_pos(&self) -> (f32, f32) { + let io = self.io(); + (io.mouse_pos.x, io.mouse_pos.y) } pub fn set_mouse_pos(&mut self, x: f32, y: f32) { - let hidpi_factor = self.hidpi_factor; let io = self.io_mut(); - io.mouse_pos.x = x / hidpi_factor; - io.mouse_pos.y = y / hidpi_factor; + io.mouse_pos.x = x; + io.mouse_pos.y = y; } pub fn set_mouse_down(&mut self, states: &[bool; 5]) { let io = self.io_mut(); @@ -272,11 +273,25 @@ impl ImGui { pub fn get_time(&self) -> f32 { unsafe { imgui_sys::igGetTime() } } pub fn get_frame_count(&self) -> i32 { unsafe { imgui_sys::igGetFrameCount() } } pub fn get_frame_rate(&self) -> f32 { self.io().framerate } - pub fn frame<'ui, 'a: 'ui>(&'a mut self, width: u32, height: u32, delta_time: f32) -> Ui<'ui> { + pub fn frame<'ui, 'a: 'ui>(&'a mut self, + size_points: (u32, u32), + size_pixels: (u32, u32), + delta_time: f32) + -> Ui<'ui> { { let io = self.io_mut(); - io.display_size.x = width as c_float; - io.display_size.y = height as c_float; + io.display_size.x = size_points.0 as c_float; + io.display_size.y = size_points.1 as c_float; + io.display_framebuffer_scale.x = if size_points.0 > 0 { + size_pixels.0 as c_float / size_points.0 as c_float + } else { + 0.0 + }; + io.display_framebuffer_scale.y = if size_points.1 > 0 { + size_pixels.1 as c_float / size_points.1 as c_float + } else { + 0.0 + }; io.delta_time = delta_time; } unsafe { From e57df7173a449fdcf71aefe8733b0c86ab44b15c Mon Sep 17 00:00:00 2001 From: Brendan Zabarauskas Date: Fri, 17 Jun 2016 23:02:04 +1000 Subject: [PATCH 6/6] Simplify window size accessing --- examples/support/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/examples/support/mod.rs b/examples/support/mod.rs index 55c272d..34e2e93 100644 --- a/examples/support/mod.rs +++ b/examples/support/mod.rs @@ -76,10 +76,9 @@ impl Support { target.clear_color(clear_color.0, clear_color.1, clear_color.2, clear_color.3); - // Early return if the window closes before we get the sizes - let window = match self.display.get_window() { Some(x) => x, None => return }; - let size_points = match window.get_inner_size_points() { Some(x) => x, None => return }; - let size_pixels = match window.get_inner_size_pixels() { Some(x) => x, None => return }; + let window = self.display.get_window().unwrap(); + let size_points = window.get_inner_size_points().unwrap(); + let size_pixels = window.get_inner_size_pixels().unwrap(); let ui = self.imgui.frame(size_points, size_pixels, delta_f);