1
0
Fork 0

fix windows build

This commit is contained in:
Billy Messenger 2020-10-17 15:19:06 -05:00
parent dc5d3b9622
commit 11ee384908
2 changed files with 56 additions and 22 deletions

View file

@ -24,7 +24,7 @@ use raw_window_handle::{
use crate::{ use crate::{
Event, KeyboardEvent, MouseButton, MouseEvent, Parent::WithParent, ScrollDelta, WindowEvent, Event, KeyboardEvent, MouseButton, MouseEvent, Parent::WithParent, ScrollDelta, WindowEvent,
WindowHandler, WindowInfo, WindowOpenOptions, WindowHandler, WindowInfo, WindowOpenOptions, WindowOpenResult, WindowScalePolicy, Size, Point,
}; };
unsafe fn message_box(title: &str, msg: &str) { unsafe fn message_box(title: &str, msg: &str) {
@ -76,13 +76,21 @@ unsafe extern "system" fn wnd_proc<H: WindowHandler>(
match msg { match msg {
WM_MOUSEMOVE => { WM_MOUSEMOVE => {
let x = (lparam & 0xFFFF) as i32; let x = (lparam & 0xFFFF) as f64;
let y = ((lparam >> 16) & 0xFFFF) as i32; let y = ((lparam >> 16) & 0xFFFF) as f64;
window_state.borrow_mut().handler.on_event( let physical_pos = Point::new(x, y);
let mut window_state = window_state.borrow_mut();
// FIXME: For some reason, the data in window_info is corrupted.
// let logical_pos = window_state.window_info.physical_to_logical(physical_pos);
let logical_pos = physical_pos;
window_state.handler.on_event(
&mut window, &mut window,
Event::Mouse(MouseEvent::CursorMoved { Event::Mouse(MouseEvent::CursorMoved {
x: x as i32, logical_pos: logical_pos.into(),
y: y as i32, physical_pos: physical_pos.into(),
}), }),
); );
return 0; return 0;
@ -134,7 +142,7 @@ unsafe fn unregister_wnd_class(wnd_class: ATOM) {
struct WindowState<H> { struct WindowState<H> {
window_class: ATOM, window_class: ATOM,
scaling: Option<f64>, // DPI scale, 96.0 is "default". window_info: WindowInfo,
handler: H, handler: H,
} }
@ -166,7 +174,7 @@ impl WindowHandle {
} }
impl Window { impl Window {
pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> WindowHandle { pub fn open<H: WindowHandler>(options: WindowOpenOptions) -> Result<(WindowHandle, WindowInfo), ()> {
unsafe { unsafe {
let title = (options.title.to_owned() + "\0").as_ptr() as *const i8; let title = (options.title.to_owned() + "\0").as_ptr() as *const i8;
@ -181,12 +189,22 @@ impl Window {
| WS_MAXIMIZEBOX | WS_MAXIMIZEBOX
| WS_CLIPSIBLINGS; | WS_CLIPSIBLINGS;
let scaling = match options.scale {
// TODO: Find system scale factor
WindowScalePolicy::TrySystemScaleFactor => 1.0,
WindowScalePolicy::TrySystemScaleFactorTimes(user_scale) => 1.0 * user_scale,
WindowScalePolicy::UseScaleFactor(user_scale) => user_scale,
WindowScalePolicy::NoScaling => 1.0,
};
let window_info = options.window_info_from_scale(scaling);
let mut rect = RECT { let mut rect = RECT {
left: 0, left: 0,
top: 0, top: 0,
// todo: check if usize fits into i32 // todo: check if usize fits into i32
right: options.width as i32, right: window_info.physical_size().width as i32,
bottom: options.height as i32, bottom: window_info.physical_size().height as i32,
}; };
// todo: add check flags https://github.com/wrl/rutabaga/blob/f30ff67e157375cafdbafe5fb549f1790443a3a8/src/platform/win/window.c#L351 // todo: add check flags https://github.com/wrl/rutabaga/blob/f30ff67e157375cafdbafe5fb549f1790443a3a8/src/platform/win/window.c#L351
@ -226,7 +244,7 @@ impl Window {
let window_state = Rc::new(RefCell::new(WindowState { let window_state = Rc::new(RefCell::new(WindowState {
window_class, window_class,
scaling: None, window_info,
handler, handler,
})); }));
@ -235,7 +253,7 @@ impl Window {
SetWindowLongPtrA(hwnd, GWLP_USERDATA, Rc::into_raw(win) as *const _ as _); SetWindowLongPtrA(hwnd, GWLP_USERDATA, Rc::into_raw(win) as *const _ as _);
SetTimer(hwnd, 4242, 13, None); SetTimer(hwnd, 4242, 13, None);
WindowHandle { hwnd } Ok((WindowHandle { hwnd }, window_info))
} }
} }
} }

View file

@ -1,6 +1,7 @@
use crate::{Size, Point}; use crate::{Size, Point};
/// The info about the window /// The info about the window
#[repr(C)]
#[derive(Debug, Copy, Clone)] #[derive(Debug, Copy, Clone)]
pub struct WindowInfo { pub struct WindowInfo {
logical_size: Size, logical_size: Size,
@ -11,27 +12,42 @@ pub struct WindowInfo {
impl WindowInfo { impl WindowInfo {
pub fn from_logical_size(logical_size: Size, scale: f64) -> Self { pub fn from_logical_size(logical_size: Size, scale: f64) -> Self {
let scale_recip = 1.0 / scale; let (scale_recip, physical_size) = if scale == 1.0 {
(1.0, logical_size)
} else {
(
1.0 / scale,
Size {
width: (logical_size.width as f64 * scale).round() as u32,
height: (logical_size.height as f64 * scale).round() as u32,
}
)
};
Self { Self {
logical_size, logical_size,
physical_size: Size { physical_size,
width: (logical_size.width as f64 * scale).round() as u32,
height: (logical_size.height as f64 * scale).round() as u32,
},
scale, scale,
scale_recip, scale_recip,
} }
} }
pub fn from_physical_size(physical_size: Size, scale: f64) -> Self { pub fn from_physical_size(physical_size: Size, scale: f64) -> Self {
let scale_recip = 1.0 / scale; let (scale_recip, logical_size) = if scale == 1.0 {
(1.0, physical_size)
} else {
let scale_recip = 1.0 / scale;
(
scale_recip,
Size {
width: (physical_size.width as f64 * scale_recip).round() as u32,
height: (physical_size.height as f64 * scale_recip).round() as u32,
}
)
};
Self { Self {
logical_size: Size { logical_size,
width: (physical_size.width as f64 * scale_recip).round() as u32,
height: (physical_size.height as f64 * scale_recip).round() as u32,
},
physical_size, physical_size,
scale, scale,
scale_recip, scale_recip,