Event management refactoring
This commit is contained in:
parent
0bd1ae52c5
commit
46dacdfc37
|
@ -3,21 +3,40 @@ use std::sync::{Arc, Mutex};
|
||||||
use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM};
|
use winapi::shared::minwindef::{LPARAM, LRESULT, UINT, WPARAM};
|
||||||
|
|
||||||
use crate::Window;
|
use crate::Window;
|
||||||
use winapi::um::winuser::WM_MOUSEMOVE;
|
use winapi::um::winuser::{DefWindowProcA, WM_MOUSEMOVE, WM_TIMER};
|
||||||
|
|
||||||
|
const WIN_FRAME_TIMER: usize = 4242;
|
||||||
|
|
||||||
|
unsafe fn handle_timer(win: Arc<Mutex<Window>>, timer_id: usize) {
|
||||||
|
match timer_id {
|
||||||
|
WIN_FRAME_TIMER => {
|
||||||
|
win.lock().unwrap().draw_frame();
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) unsafe fn handle_message(
|
pub(crate) unsafe fn handle_message(
|
||||||
win: Arc<Mutex<Window>>,
|
win: Arc<Mutex<Window>>,
|
||||||
msg: UINT,
|
message: UINT,
|
||||||
wparam: WPARAM,
|
wparam: WPARAM,
|
||||||
lparam: LPARAM,
|
lparam: LPARAM,
|
||||||
) -> LRESULT {
|
) -> LRESULT {
|
||||||
match msg {
|
let hwnd;
|
||||||
|
{
|
||||||
|
hwnd = win.lock().unwrap().hwnd;
|
||||||
|
}
|
||||||
|
match message {
|
||||||
WM_MOUSEMOVE => {
|
WM_MOUSEMOVE => {
|
||||||
let x = (lparam & 0xFFFF) as i32;
|
let x = (lparam & 0xFFFF) as i32;
|
||||||
let y = ((lparam >> 16) & 0xFFFF) as i32;
|
let y = ((lparam >> 16) & 0xFFFF) as i32;
|
||||||
win.lock().unwrap().handle_mouse_motion(x, y);
|
win.lock().unwrap().handle_mouse_motion(x, y);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
_ => 0,
|
WM_TIMER => {
|
||||||
|
handle_timer(win, wparam);
|
||||||
|
0
|
||||||
|
}
|
||||||
|
_ => DefWindowProcA(hwnd, message, wparam, lparam),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,22 +58,23 @@ unsafe extern "system" fn wnd_proc(
|
||||||
wparam: WPARAM,
|
wparam: WPARAM,
|
||||||
lparam: LPARAM,
|
lparam: LPARAM,
|
||||||
) -> LRESULT {
|
) -> LRESULT {
|
||||||
let win = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void;
|
let win_ptr = GetWindowLongPtrA(hwnd, GWLP_USERDATA) as *const c_void;
|
||||||
match msg {
|
match msg {
|
||||||
WM_CREATE => {
|
WM_CREATE => {
|
||||||
PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0);
|
PostMessageA(hwnd, WM_SHOWWINDOW, 0, 0);
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
if !win.is_null() {
|
if !win_ptr.is_null() {
|
||||||
let win: &Arc<Mutex<Window>> = std::mem::transmute(win);
|
let win_ref: &Arc<Mutex<Window>> = std::mem::transmute(win_ptr);
|
||||||
let win = Arc::clone(win);
|
let win = Arc::clone(win_ref);
|
||||||
let ret = handle_message(win, msg, wparam, lparam);
|
let ret = handle_message(win, msg, wparam, lparam);
|
||||||
|
|
||||||
// todo: need_reconfigure thing?
|
// todo: need_reconfigure thing?
|
||||||
|
|
||||||
// return ret
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
return DefWindowProcA(hwnd, msg, wparam, lparam);
|
return DefWindowProcA(hwnd, msg, wparam, lparam);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -106,7 +107,7 @@ unsafe fn unregister_wnd_class(wnd_class: ATOM) {
|
||||||
unsafe fn init_gl_context() {}
|
unsafe fn init_gl_context() {}
|
||||||
|
|
||||||
pub struct Window {
|
pub struct Window {
|
||||||
hwnd: HWND,
|
pub(crate) hwnd: HWND,
|
||||||
hdc: HDC,
|
hdc: HDC,
|
||||||
gl_context: HGLRC,
|
gl_context: HGLRC,
|
||||||
window_class: ATOM,
|
window_class: ATOM,
|
||||||
|
@ -234,18 +235,7 @@ impl Window {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
TranslateMessage(&mut msg);
|
TranslateMessage(&mut msg);
|
||||||
|
handle_message(Arc::clone(&win), msg.message, msg.wParam, msg.lParam);
|
||||||
match msg.message {
|
|
||||||
WM_TIMER => {
|
|
||||||
// todo: pass callback rendering function instead
|
|
||||||
gl::ClearColor(0.3, 0.8, 0.3, 1.0);
|
|
||||||
gl::Clear(gl::COLOR_BUFFER_BIT);
|
|
||||||
SwapBuffers(hdc);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
DefWindowProcA(hwnd, msg.message, msg.wParam, msg.lParam);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -264,6 +254,13 @@ impl Window {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) unsafe fn draw_frame(&self) {
|
||||||
|
// todo: pass callback rendering function instead
|
||||||
|
gl::ClearColor(0.3, 0.8, 0.3, 1.0);
|
||||||
|
gl::Clear(gl::COLOR_BUFFER_BIT);
|
||||||
|
SwapBuffers(self.hdc);
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn handle_mouse_motion(&self, x: i32, y: i32) {
|
pub(crate) fn handle_mouse_motion(&self, x: i32, y: i32) {
|
||||||
println!("{}, {}", x, y);
|
println!("{}, {}", x, y);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue