Make hidpi implementation consistent with upstream

This commit is contained in:
Brendan Zabarauskas 2016-06-13 22:22:47 +10:00
parent ce45b81e8e
commit 3548542ca5
3 changed files with 51 additions and 33 deletions

View File

@ -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<F: FnMut(&Ui)>(&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);

View File

@ -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()
}));

View File

@ -68,8 +68,6 @@ pub struct ImGui {
// lives long enough in case the ImStr contains a Cow::Owned
ini_filename: Option<ImStr<'static>>,
log_filename: Option<ImStr<'static>>,
// 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 {