Compare commits

...

2 Commits

Author SHA1 Message Date
21eaeadf44
更新点 2024-11-08 00:36:46 +08:00
1164fe18c9
yeeeeeeeee 2024-11-07 01:26:39 +08:00
5 changed files with 72 additions and 11 deletions

View File

@ -16,7 +16,7 @@ basic = {
"psutil >= 6.0.0" "psutil >= 6.0.0"
], ],
"file read": [ "file read": [
"tomli >= 2.0.1", "tomli >= 2.0.2",
"tomli-w >= 1.0.0", "tomli-w >= 1.0.0",
"defusedxml >= 0.7.1" "defusedxml >= 0.7.1"
], ],
@ -27,7 +27,7 @@ build = {
"nuitka >= 2.4", "nuitka >= 2.4",
"imageio >= 2.34.2", "imageio >= 2.34.2",
"setuptools >= 69", "setuptools >= 69",
"setuptools-rust >= 1.10.0", "setuptools-rust >= 1.10.2",
"wheel >= 0.43.0", "wheel >= 0.43.0",
] ]
} }

View File

@ -1 +0,0 @@
nightly

View File

@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"

View File

@ -7,6 +7,7 @@
*/ */
mod dr_physics; mod dr_physics;
/// Python 交互
mod python; mod python;
/// 也许是一些渲染的东西 /// 也许是一些渲染的东西
mod renders; mod renders;

View File

@ -1,5 +1,8 @@
use std::num::NonZeroIsize; use std::num::NonZeroIsize;
use winit::event::WindowEvent;
use winit::platform::run_on_demand::EventLoopExtRunOnDemand;
use winit::platform::windows::EventLoopBuilderExtWindows;
use winit::window::WindowAttributes; use winit::window::WindowAttributes;
use winit::{application::ApplicationHandler, event_loop::EventLoop, window::Window}; use winit::{application::ApplicationHandler, event_loop::EventLoop, window::Window};
@ -7,7 +10,7 @@ use windows_sys::Win32::Foundation::HWND;
use windows_sys::Win32::System::Threading::GetCurrentProcessId; use windows_sys::Win32::System::Threading::GetCurrentProcessId;
use windows_sys::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowThreadProcessId}; use windows_sys::Win32::UI::WindowsAndMessaging::{EnumWindows, GetWindowThreadProcessId};
use raw_window_handle::{RawWindowHandle, Win32WindowHandle, WinRtWindowHandle}; use raw_window_handle::{RawWindowHandle, Win32WindowHandle};
struct App { struct App {
window: Option<Window>, window: Option<Window>,
@ -17,16 +20,42 @@ struct App {
impl ApplicationHandler for App { impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) { fn resumed(&mut self, event_loop: &winit::event_loop::ActiveEventLoop) {
// 这里需要获取现有的窗口, 毕竟是运行在一个已有窗口的 Python 程序里 // 这里需要获取现有的窗口, 毕竟是运行在一个已有窗口的 Python 程序里
// self.window = Some(event_loop.ge) if self.window.is_none() {
let mut window_attribute = unsafe { WindowAttributes::default().with_parent_window(Some(self.parent.clone())) }; println!("Create window");
let window_attribute = unsafe { WindowAttributes::default().with_parent_window(Some(self.parent.clone())) };
let window = event_loop.create_window(window_attribute).unwrap();
self.window = Some(window);
}
} }
fn window_event( fn window_event(
&mut self, &mut self,
event_loop: &winit::event_loop::ActiveEventLoop, event_loop: &winit::event_loop::ActiveEventLoop,
window_id: winit::window::WindowId, _window_id: winit::window::WindowId,
event: winit::event::WindowEvent, event: winit::event::WindowEvent,
) { ) {
match event {
WindowEvent::CloseRequested => {
println!("Close window");
event_loop.exit();
}
WindowEvent::Destroyed => {
println!("Window destroyed");
event_loop.exit();
}
WindowEvent::RedrawRequested => {
println!("Redraw window");
// self.ref_window().
event_loop.exit();
}
WindowEvent::MouseInput { button, .. } => {
println!("Mouse input: {:?}", button);
}
WindowEvent::Moved(pos) => {
println!("Window moved: {:?}", pos);
}
_ => {}
}
} }
} }
@ -37,6 +66,10 @@ impl App {
parent: handle, parent: handle,
} }
} }
pub fn ref_window(&self) -> &Window {
self.window.as_ref().unwrap()
}
} }
unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 { unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
@ -50,19 +83,45 @@ unsafe extern "system" fn enum_windows_proc(hwnd: HWND, lparam: isize) -> i32 {
1 1
} }
pub fn render_main() -> anyhow::Result<()> { fn render_thread(handler: isize) -> anyhow::Result<()> {
let window = handler as HWND;
let win32_handle = Win32WindowHandle::new(NonZeroIsize::new(window as isize).unwrap());
let raw_handle = RawWindowHandle::Win32(win32_handle);
let mut app = App::new(raw_handle);
let event_loop = EventLoop::builder().with_any_thread(true).build()?;
// let event_loop = EventLoop::new()?;
event_loop.run_app(&mut app)?;
Ok(())
}
pub fn render_main() {
let mut window: HWND = std::ptr::null_mut(); let mut window: HWND = std::ptr::null_mut();
let result = unsafe { EnumWindows(Some(enum_windows_proc), &mut window as *mut _ as isize) }; let result = unsafe { EnumWindows(Some(enum_windows_proc), &mut window as *mut _ as isize) };
if result != 0 { if result != 0 {
println!("Find window failed"); println!("Find window failed");
return Err(anyhow::anyhow!("Find window failed")); return;
} }
println!("找到 pyglet 的窗口: {:?}", window); println!("找到 pyglet 的窗口: {:?}", window);
// let window_ptr = window as isize;
let win32_handle = Win32WindowHandle::new(NonZeroIsize::new(window as isize).unwrap()); let win32_handle = Win32WindowHandle::new(NonZeroIsize::new(window as isize).unwrap());
let raw_handle = RawWindowHandle::Win32(win32_handle); let raw_handle = RawWindowHandle::Win32(win32_handle);
let app = App::new(raw_handle);
Ok(()) let mut app = App::new(raw_handle);
let mut event_loop = EventLoop::new().unwrap();
// let event_loop = EventLoop::new()?;
// event_loop.run_app(&mut app).unwrap();
let threaded_event_loop = event_loop.run_app_on_demand(&mut app).unwrap();
std::thread::spawn(move || {
// render_thread(window as isize).unwrap();
});
} }