From 111d8bc8dc616cfec4c57b38c33ecd57ff2042d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?charlotte=20=F0=9F=8C=B8?= Date: Thu, 12 Nov 2026 14:25:21 -0800 Subject: [PATCH 1/2] Python input events and sketch folder asset root. --- crates/processing_core/src/constants.rs | 45 +++- crates/processing_ffi/src/lib.rs | 240 ++---------------- crates/processing_input/src/keys.rs | 145 +++++++++++ crates/processing_input/src/lib.rs | 2 + .../processing_pyo3/processing_py/__init__.py | 20 +- crates/processing_pyo3/src/constants.rs | 35 ++- crates/processing_pyo3/src/graphics.rs | 16 +- crates/processing_pyo3/src/input.rs | 169 ++++-------- crates/processing_pyo3/src/lib.rs | 153 ++++++++--- crates/processing_pyo3/src/surface.rs | 1 - crates/processing_wasm/src/lib.rs | 239 +---------------- 11 files changed, 438 insertions(+), 627 deletions(-) create mode 100644 crates/processing_input/src/keys.rs diff --git a/crates/processing_core/src/constants.rs b/crates/processing_core/src/constants.rs index 3fb7ba0c..f6771497 100644 --- a/crates/processing_core/src/constants.rs +++ b/crates/processing_core/src/constants.rs @@ -138,7 +138,7 @@ pub const TAU: f32 = std::f32::consts::TAU; pub const DEG_TO_RAD: f32 = std::f32::consts::PI / 180.0; pub const RAD_TO_DEG: f32 = 180.0 / std::f32::consts::PI; -// Key codes stay numeric (compared against `key_code`); values must match `key_code_to_u32`. +// Key codes are GLFW values, mapped to Bevy `KeyCode`s by `processing_input::keys`. pub const KEY_A: u32 = 65; pub const KEY_B: u32 = 66; @@ -223,3 +223,46 @@ pub const F9: u32 = 298; pub const F10: u32 = 299; pub const F11: u32 = 300; pub const F12: u32 = 301; +pub const F13: u32 = 302; +pub const F14: u32 = 303; +pub const F15: u32 = 304; +pub const F16: u32 = 305; +pub const F17: u32 = 306; +pub const F18: u32 = 307; +pub const F19: u32 = 308; +pub const F20: u32 = 309; +pub const F21: u32 = 310; +pub const F22: u32 = 311; +pub const F23: u32 = 312; +pub const F24: u32 = 313; +pub const F25: u32 = 314; + +pub const CAPS_LOCK: u32 = 280; +pub const SCROLL_LOCK: u32 = 281; +pub const NUM_LOCK: u32 = 282; +pub const PRINT_SCREEN: u32 = 283; +pub const PAUSE: u32 = 284; + +pub const NUMPAD_0: u32 = 320; +pub const NUMPAD_1: u32 = 321; +pub const NUMPAD_2: u32 = 322; +pub const NUMPAD_3: u32 = 323; +pub const NUMPAD_4: u32 = 324; +pub const NUMPAD_5: u32 = 325; +pub const NUMPAD_6: u32 = 326; +pub const NUMPAD_7: u32 = 327; +pub const NUMPAD_8: u32 = 328; +pub const NUMPAD_9: u32 = 329; +pub const NUMPAD_DECIMAL: u32 = 330; +pub const NUMPAD_DIVIDE: u32 = 331; +pub const NUMPAD_MULTIPLY: u32 = 332; +pub const NUMPAD_SUBTRACT: u32 = 333; +pub const NUMPAD_ADD: u32 = 334; +pub const NUMPAD_ENTER: u32 = 335; +pub const NUMPAD_EQUAL: u32 = 336; + +pub const SHIFT_RIGHT: u32 = 344; +pub const CONTROL_RIGHT: u32 = 345; +pub const ALT_RIGHT: u32 = 346; +pub const SUPER_RIGHT: u32 = 347; +pub const CONTEXT_MENU: u32 = 348; diff --git a/crates/processing_ffi/src/lib.rs b/crates/processing_ffi/src/lib.rs index 2b1e8c79..f6db5d22 100644 --- a/crates/processing_ffi/src/lib.rs +++ b/crates/processing_ffi/src/lib.rs @@ -4109,6 +4109,19 @@ pub const PROCESSING_KEY_F9: u32 = 298; pub const PROCESSING_KEY_F10: u32 = 299; pub const PROCESSING_KEY_F11: u32 = 300; pub const PROCESSING_KEY_F12: u32 = 301; +pub const PROCESSING_KEY_F13: u32 = 302; +pub const PROCESSING_KEY_F14: u32 = 303; +pub const PROCESSING_KEY_F15: u32 = 304; +pub const PROCESSING_KEY_F16: u32 = 305; +pub const PROCESSING_KEY_F17: u32 = 306; +pub const PROCESSING_KEY_F18: u32 = 307; +pub const PROCESSING_KEY_F19: u32 = 308; +pub const PROCESSING_KEY_F20: u32 = 309; +pub const PROCESSING_KEY_F21: u32 = 310; +pub const PROCESSING_KEY_F22: u32 = 311; +pub const PROCESSING_KEY_F23: u32 = 312; +pub const PROCESSING_KEY_F24: u32 = 313; +pub const PROCESSING_KEY_F25: u32 = 314; pub const PROCESSING_KEY_NUMPAD_0: u32 = 320; pub const PROCESSING_KEY_NUMPAD_1: u32 = 321; pub const PROCESSING_KEY_NUMPAD_2: u32 = 322; @@ -4327,7 +4340,8 @@ pub extern "C" fn processing_key() -> u32 { #[unsafe(no_mangle)] pub extern "C" fn processing_key_code() -> u32 { error::clear_error(); - error::check(|| input_key_code().map(|opt| opt.map(key_code_to_u32).unwrap_or(0))).unwrap_or(0) + error::check(|| input_key_code().map(|opt| opt.and_then(key_code_to_u32).unwrap_or(0))) + .unwrap_or(0) } #[unsafe(no_mangle)] @@ -4347,227 +4361,3 @@ pub extern "C" fn processing_mouse_wheel() -> f32 { error::clear_error(); error::check(input_mouse_wheel).unwrap_or(0.0) } - -fn key_code_from_u32(val: u32) -> processing::prelude::error::Result { - match val { - PROCESSING_KEY_SPACE => Ok(KeyCode::Space), - PROCESSING_KEY_QUOTE => Ok(KeyCode::Quote), - PROCESSING_KEY_COMMA => Ok(KeyCode::Comma), - PROCESSING_KEY_MINUS => Ok(KeyCode::Minus), - PROCESSING_KEY_PERIOD => Ok(KeyCode::Period), - PROCESSING_KEY_SLASH => Ok(KeyCode::Slash), - PROCESSING_KEY_0 => Ok(KeyCode::Digit0), - PROCESSING_KEY_1 => Ok(KeyCode::Digit1), - PROCESSING_KEY_2 => Ok(KeyCode::Digit2), - PROCESSING_KEY_3 => Ok(KeyCode::Digit3), - PROCESSING_KEY_4 => Ok(KeyCode::Digit4), - PROCESSING_KEY_5 => Ok(KeyCode::Digit5), - PROCESSING_KEY_6 => Ok(KeyCode::Digit6), - PROCESSING_KEY_7 => Ok(KeyCode::Digit7), - PROCESSING_KEY_8 => Ok(KeyCode::Digit8), - PROCESSING_KEY_9 => Ok(KeyCode::Digit9), - PROCESSING_KEY_SEMICOLON => Ok(KeyCode::Semicolon), - PROCESSING_KEY_EQUAL => Ok(KeyCode::Equal), - PROCESSING_KEY_A => Ok(KeyCode::KeyA), - PROCESSING_KEY_B => Ok(KeyCode::KeyB), - PROCESSING_KEY_C => Ok(KeyCode::KeyC), - PROCESSING_KEY_D => Ok(KeyCode::KeyD), - PROCESSING_KEY_E => Ok(KeyCode::KeyE), - PROCESSING_KEY_F => Ok(KeyCode::KeyF), - PROCESSING_KEY_G => Ok(KeyCode::KeyG), - PROCESSING_KEY_H => Ok(KeyCode::KeyH), - PROCESSING_KEY_I => Ok(KeyCode::KeyI), - PROCESSING_KEY_J => Ok(KeyCode::KeyJ), - PROCESSING_KEY_K => Ok(KeyCode::KeyK), - PROCESSING_KEY_L => Ok(KeyCode::KeyL), - PROCESSING_KEY_M => Ok(KeyCode::KeyM), - PROCESSING_KEY_N => Ok(KeyCode::KeyN), - PROCESSING_KEY_O => Ok(KeyCode::KeyO), - PROCESSING_KEY_P => Ok(KeyCode::KeyP), - PROCESSING_KEY_Q => Ok(KeyCode::KeyQ), - PROCESSING_KEY_R => Ok(KeyCode::KeyR), - PROCESSING_KEY_S => Ok(KeyCode::KeyS), - PROCESSING_KEY_T => Ok(KeyCode::KeyT), - PROCESSING_KEY_U => Ok(KeyCode::KeyU), - PROCESSING_KEY_V => Ok(KeyCode::KeyV), - PROCESSING_KEY_W => Ok(KeyCode::KeyW), - PROCESSING_KEY_X => Ok(KeyCode::KeyX), - PROCESSING_KEY_Y => Ok(KeyCode::KeyY), - PROCESSING_KEY_Z => Ok(KeyCode::KeyZ), - PROCESSING_KEY_BRACKET_LEFT => Ok(KeyCode::BracketLeft), - PROCESSING_KEY_BACKSLASH => Ok(KeyCode::Backslash), - PROCESSING_KEY_BRACKET_RIGHT => Ok(KeyCode::BracketRight), - PROCESSING_KEY_BACKQUOTE => Ok(KeyCode::Backquote), - PROCESSING_KEY_ESCAPE => Ok(KeyCode::Escape), - PROCESSING_KEY_ENTER => Ok(KeyCode::Enter), - PROCESSING_KEY_TAB => Ok(KeyCode::Tab), - PROCESSING_KEY_BACKSPACE => Ok(KeyCode::Backspace), - PROCESSING_KEY_INSERT => Ok(KeyCode::Insert), - PROCESSING_KEY_DELETE => Ok(KeyCode::Delete), - PROCESSING_KEY_RIGHT => Ok(KeyCode::ArrowRight), - PROCESSING_KEY_LEFT => Ok(KeyCode::ArrowLeft), - PROCESSING_KEY_DOWN => Ok(KeyCode::ArrowDown), - PROCESSING_KEY_UP => Ok(KeyCode::ArrowUp), - PROCESSING_KEY_PAGE_UP => Ok(KeyCode::PageUp), - PROCESSING_KEY_PAGE_DOWN => Ok(KeyCode::PageDown), - PROCESSING_KEY_HOME => Ok(KeyCode::Home), - PROCESSING_KEY_END => Ok(KeyCode::End), - PROCESSING_KEY_CAPS_LOCK => Ok(KeyCode::CapsLock), - PROCESSING_KEY_SCROLL_LOCK => Ok(KeyCode::ScrollLock), - PROCESSING_KEY_NUM_LOCK => Ok(KeyCode::NumLock), - PROCESSING_KEY_PRINT_SCREEN => Ok(KeyCode::PrintScreen), - PROCESSING_KEY_PAUSE => Ok(KeyCode::Pause), - PROCESSING_KEY_F1 => Ok(KeyCode::F1), - PROCESSING_KEY_F2 => Ok(KeyCode::F2), - PROCESSING_KEY_F3 => Ok(KeyCode::F3), - PROCESSING_KEY_F4 => Ok(KeyCode::F4), - PROCESSING_KEY_F5 => Ok(KeyCode::F5), - PROCESSING_KEY_F6 => Ok(KeyCode::F6), - PROCESSING_KEY_F7 => Ok(KeyCode::F7), - PROCESSING_KEY_F8 => Ok(KeyCode::F8), - PROCESSING_KEY_F9 => Ok(KeyCode::F9), - PROCESSING_KEY_F10 => Ok(KeyCode::F10), - PROCESSING_KEY_F11 => Ok(KeyCode::F11), - PROCESSING_KEY_F12 => Ok(KeyCode::F12), - PROCESSING_KEY_NUMPAD_0 => Ok(KeyCode::Numpad0), - PROCESSING_KEY_NUMPAD_1 => Ok(KeyCode::Numpad1), - PROCESSING_KEY_NUMPAD_2 => Ok(KeyCode::Numpad2), - PROCESSING_KEY_NUMPAD_3 => Ok(KeyCode::Numpad3), - PROCESSING_KEY_NUMPAD_4 => Ok(KeyCode::Numpad4), - PROCESSING_KEY_NUMPAD_5 => Ok(KeyCode::Numpad5), - PROCESSING_KEY_NUMPAD_6 => Ok(KeyCode::Numpad6), - PROCESSING_KEY_NUMPAD_7 => Ok(KeyCode::Numpad7), - PROCESSING_KEY_NUMPAD_8 => Ok(KeyCode::Numpad8), - PROCESSING_KEY_NUMPAD_9 => Ok(KeyCode::Numpad9), - PROCESSING_KEY_NUMPAD_DECIMAL => Ok(KeyCode::NumpadDecimal), - PROCESSING_KEY_NUMPAD_DIVIDE => Ok(KeyCode::NumpadDivide), - PROCESSING_KEY_NUMPAD_MULTIPLY => Ok(KeyCode::NumpadMultiply), - PROCESSING_KEY_NUMPAD_SUBTRACT => Ok(KeyCode::NumpadSubtract), - PROCESSING_KEY_NUMPAD_ADD => Ok(KeyCode::NumpadAdd), - PROCESSING_KEY_NUMPAD_ENTER => Ok(KeyCode::NumpadEnter), - PROCESSING_KEY_NUMPAD_EQUAL => Ok(KeyCode::NumpadEqual), - PROCESSING_KEY_SHIFT_LEFT => Ok(KeyCode::ShiftLeft), - PROCESSING_KEY_CONTROL_LEFT => Ok(KeyCode::ControlLeft), - PROCESSING_KEY_ALT_LEFT => Ok(KeyCode::AltLeft), - PROCESSING_KEY_SUPER_LEFT => Ok(KeyCode::SuperLeft), - PROCESSING_KEY_SHIFT_RIGHT => Ok(KeyCode::ShiftRight), - PROCESSING_KEY_CONTROL_RIGHT => Ok(KeyCode::ControlRight), - PROCESSING_KEY_ALT_RIGHT => Ok(KeyCode::AltRight), - PROCESSING_KEY_SUPER_RIGHT => Ok(KeyCode::SuperRight), - PROCESSING_KEY_CONTEXT_MENU => Ok(KeyCode::ContextMenu), - _ => Err(ProcessingError::InvalidArgument(format!( - "unknown key code: {val}" - ))), - } -} - -fn key_code_to_u32(kc: KeyCode) -> u32 { - match kc { - KeyCode::Space => PROCESSING_KEY_SPACE, - KeyCode::Quote => PROCESSING_KEY_QUOTE, - KeyCode::Comma => PROCESSING_KEY_COMMA, - KeyCode::Minus => PROCESSING_KEY_MINUS, - KeyCode::Period => PROCESSING_KEY_PERIOD, - KeyCode::Slash => PROCESSING_KEY_SLASH, - KeyCode::Digit0 => PROCESSING_KEY_0, - KeyCode::Digit1 => PROCESSING_KEY_1, - KeyCode::Digit2 => PROCESSING_KEY_2, - KeyCode::Digit3 => PROCESSING_KEY_3, - KeyCode::Digit4 => PROCESSING_KEY_4, - KeyCode::Digit5 => PROCESSING_KEY_5, - KeyCode::Digit6 => PROCESSING_KEY_6, - KeyCode::Digit7 => PROCESSING_KEY_7, - KeyCode::Digit8 => PROCESSING_KEY_8, - KeyCode::Digit9 => PROCESSING_KEY_9, - KeyCode::Semicolon => PROCESSING_KEY_SEMICOLON, - KeyCode::Equal => PROCESSING_KEY_EQUAL, - KeyCode::KeyA => PROCESSING_KEY_A, - KeyCode::KeyB => PROCESSING_KEY_B, - KeyCode::KeyC => PROCESSING_KEY_C, - KeyCode::KeyD => PROCESSING_KEY_D, - KeyCode::KeyE => PROCESSING_KEY_E, - KeyCode::KeyF => PROCESSING_KEY_F, - KeyCode::KeyG => PROCESSING_KEY_G, - KeyCode::KeyH => PROCESSING_KEY_H, - KeyCode::KeyI => PROCESSING_KEY_I, - KeyCode::KeyJ => PROCESSING_KEY_J, - KeyCode::KeyK => PROCESSING_KEY_K, - KeyCode::KeyL => PROCESSING_KEY_L, - KeyCode::KeyM => PROCESSING_KEY_M, - KeyCode::KeyN => PROCESSING_KEY_N, - KeyCode::KeyO => PROCESSING_KEY_O, - KeyCode::KeyP => PROCESSING_KEY_P, - KeyCode::KeyQ => PROCESSING_KEY_Q, - KeyCode::KeyR => PROCESSING_KEY_R, - KeyCode::KeyS => PROCESSING_KEY_S, - KeyCode::KeyT => PROCESSING_KEY_T, - KeyCode::KeyU => PROCESSING_KEY_U, - KeyCode::KeyV => PROCESSING_KEY_V, - KeyCode::KeyW => PROCESSING_KEY_W, - KeyCode::KeyX => PROCESSING_KEY_X, - KeyCode::KeyY => PROCESSING_KEY_Y, - KeyCode::KeyZ => PROCESSING_KEY_Z, - KeyCode::BracketLeft => PROCESSING_KEY_BRACKET_LEFT, - KeyCode::Backslash => PROCESSING_KEY_BACKSLASH, - KeyCode::BracketRight => PROCESSING_KEY_BRACKET_RIGHT, - KeyCode::Backquote => PROCESSING_KEY_BACKQUOTE, - KeyCode::Escape => PROCESSING_KEY_ESCAPE, - KeyCode::Enter => PROCESSING_KEY_ENTER, - KeyCode::Tab => PROCESSING_KEY_TAB, - KeyCode::Backspace => PROCESSING_KEY_BACKSPACE, - KeyCode::Insert => PROCESSING_KEY_INSERT, - KeyCode::Delete => PROCESSING_KEY_DELETE, - KeyCode::ArrowRight => PROCESSING_KEY_RIGHT, - KeyCode::ArrowLeft => PROCESSING_KEY_LEFT, - KeyCode::ArrowDown => PROCESSING_KEY_DOWN, - KeyCode::ArrowUp => PROCESSING_KEY_UP, - KeyCode::PageUp => PROCESSING_KEY_PAGE_UP, - KeyCode::PageDown => PROCESSING_KEY_PAGE_DOWN, - KeyCode::Home => PROCESSING_KEY_HOME, - KeyCode::End => PROCESSING_KEY_END, - KeyCode::CapsLock => PROCESSING_KEY_CAPS_LOCK, - KeyCode::ScrollLock => PROCESSING_KEY_SCROLL_LOCK, - KeyCode::NumLock => PROCESSING_KEY_NUM_LOCK, - KeyCode::PrintScreen => PROCESSING_KEY_PRINT_SCREEN, - KeyCode::Pause => PROCESSING_KEY_PAUSE, - KeyCode::F1 => PROCESSING_KEY_F1, - KeyCode::F2 => PROCESSING_KEY_F2, - KeyCode::F3 => PROCESSING_KEY_F3, - KeyCode::F4 => PROCESSING_KEY_F4, - KeyCode::F5 => PROCESSING_KEY_F5, - KeyCode::F6 => PROCESSING_KEY_F6, - KeyCode::F7 => PROCESSING_KEY_F7, - KeyCode::F8 => PROCESSING_KEY_F8, - KeyCode::F9 => PROCESSING_KEY_F9, - KeyCode::F10 => PROCESSING_KEY_F10, - KeyCode::F11 => PROCESSING_KEY_F11, - KeyCode::F12 => PROCESSING_KEY_F12, - KeyCode::Numpad0 => PROCESSING_KEY_NUMPAD_0, - KeyCode::Numpad1 => PROCESSING_KEY_NUMPAD_1, - KeyCode::Numpad2 => PROCESSING_KEY_NUMPAD_2, - KeyCode::Numpad3 => PROCESSING_KEY_NUMPAD_3, - KeyCode::Numpad4 => PROCESSING_KEY_NUMPAD_4, - KeyCode::Numpad5 => PROCESSING_KEY_NUMPAD_5, - KeyCode::Numpad6 => PROCESSING_KEY_NUMPAD_6, - KeyCode::Numpad7 => PROCESSING_KEY_NUMPAD_7, - KeyCode::Numpad8 => PROCESSING_KEY_NUMPAD_8, - KeyCode::Numpad9 => PROCESSING_KEY_NUMPAD_9, - KeyCode::NumpadDecimal => PROCESSING_KEY_NUMPAD_DECIMAL, - KeyCode::NumpadDivide => PROCESSING_KEY_NUMPAD_DIVIDE, - KeyCode::NumpadMultiply => PROCESSING_KEY_NUMPAD_MULTIPLY, - KeyCode::NumpadSubtract => PROCESSING_KEY_NUMPAD_SUBTRACT, - KeyCode::NumpadAdd => PROCESSING_KEY_NUMPAD_ADD, - KeyCode::NumpadEnter => PROCESSING_KEY_NUMPAD_ENTER, - KeyCode::NumpadEqual => PROCESSING_KEY_NUMPAD_EQUAL, - KeyCode::ShiftLeft => PROCESSING_KEY_SHIFT_LEFT, - KeyCode::ControlLeft => PROCESSING_KEY_CONTROL_LEFT, - KeyCode::AltLeft => PROCESSING_KEY_ALT_LEFT, - KeyCode::SuperLeft => PROCESSING_KEY_SUPER_LEFT, - KeyCode::ShiftRight => PROCESSING_KEY_SHIFT_RIGHT, - KeyCode::ControlRight => PROCESSING_KEY_CONTROL_RIGHT, - KeyCode::AltRight => PROCESSING_KEY_ALT_RIGHT, - KeyCode::SuperRight => PROCESSING_KEY_SUPER_RIGHT, - KeyCode::ContextMenu => PROCESSING_KEY_CONTEXT_MENU, - _ => 0, - } -} diff --git a/crates/processing_input/src/keys.rs b/crates/processing_input/src/keys.rs new file mode 100644 index 00000000..4f6c661d --- /dev/null +++ b/crates/processing_input/src/keys.rs @@ -0,0 +1,145 @@ +//! The numeric key codes front ends expose (GLFW values, see +//! `processing_core::constants`) and their Bevy `KeyCode`s. + +use bevy::input::keyboard::KeyCode; +use processing_core::constants as c; +use processing_core::error::{ProcessingError, Result}; + +macro_rules! key_table { + ($($code:ident => $key:ident),* $(,)?) => { + pub fn key_code_from_u32(code: u32) -> Result { + match code { + $(c::$code => Ok(KeyCode::$key),)* + _ => Err(ProcessingError::InvalidArgument(format!("unknown key code: {code}"))), + } + } + + pub fn key_code_to_u32(key: KeyCode) -> Option { + match key { + $(KeyCode::$key => Some(c::$code),)* + _ => None, + } + } + }; +} + +key_table! { + KEY_A => KeyA, + KEY_B => KeyB, + KEY_C => KeyC, + KEY_D => KeyD, + KEY_E => KeyE, + KEY_F => KeyF, + KEY_G => KeyG, + KEY_H => KeyH, + KEY_I => KeyI, + KEY_J => KeyJ, + KEY_K => KeyK, + KEY_L => KeyL, + KEY_M => KeyM, + KEY_N => KeyN, + KEY_O => KeyO, + KEY_P => KeyP, + KEY_Q => KeyQ, + KEY_R => KeyR, + KEY_S => KeyS, + KEY_T => KeyT, + KEY_U => KeyU, + KEY_V => KeyV, + KEY_W => KeyW, + KEY_X => KeyX, + KEY_Y => KeyY, + KEY_Z => KeyZ, + KEY_0 => Digit0, + KEY_1 => Digit1, + KEY_2 => Digit2, + KEY_3 => Digit3, + KEY_4 => Digit4, + KEY_5 => Digit5, + KEY_6 => Digit6, + KEY_7 => Digit7, + KEY_8 => Digit8, + KEY_9 => Digit9, + SPACE => Space, + QUOTE => Quote, + COMMA => Comma, + MINUS => Minus, + PERIOD => Period, + SLASH => Slash, + SEMICOLON => Semicolon, + EQUAL => Equal, + BRACKET_LEFT => BracketLeft, + BACKSLASH => Backslash, + BRACKET_RIGHT => BracketRight, + BACKQUOTE => Backquote, + ESCAPE => Escape, + ENTER => Enter, + TAB => Tab, + BACKSPACE => Backspace, + INSERT => Insert, + DELETE => Delete, + RIGHT_ARROW => ArrowRight, + LEFT_ARROW => ArrowLeft, + DOWN => ArrowDown, + UP => ArrowUp, + PAGE_UP => PageUp, + PAGE_DOWN => PageDown, + HOME => Home, + END => End, + CAPS_LOCK => CapsLock, + SCROLL_LOCK => ScrollLock, + NUM_LOCK => NumLock, + PRINT_SCREEN => PrintScreen, + PAUSE => Pause, + F1 => F1, + F2 => F2, + F3 => F3, + F4 => F4, + F5 => F5, + F6 => F6, + F7 => F7, + F8 => F8, + F9 => F9, + F10 => F10, + F11 => F11, + F12 => F12, + F13 => F13, + F14 => F14, + F15 => F15, + F16 => F16, + F17 => F17, + F18 => F18, + F19 => F19, + F20 => F20, + F21 => F21, + F22 => F22, + F23 => F23, + F24 => F24, + F25 => F25, + NUMPAD_0 => Numpad0, + NUMPAD_1 => Numpad1, + NUMPAD_2 => Numpad2, + NUMPAD_3 => Numpad3, + NUMPAD_4 => Numpad4, + NUMPAD_5 => Numpad5, + NUMPAD_6 => Numpad6, + NUMPAD_7 => Numpad7, + NUMPAD_8 => Numpad8, + NUMPAD_9 => Numpad9, + NUMPAD_DECIMAL => NumpadDecimal, + NUMPAD_DIVIDE => NumpadDivide, + NUMPAD_MULTIPLY => NumpadMultiply, + NUMPAD_SUBTRACT => NumpadSubtract, + NUMPAD_ADD => NumpadAdd, + NUMPAD_ENTER => NumpadEnter, + NUMPAD_EQUAL => NumpadEqual, + SHIFT => ShiftLeft, + CONTROL => ControlLeft, + ALT => AltLeft, + SUPER => SuperLeft, + SHIFT_RIGHT => ShiftRight, + CONTROL_RIGHT => ControlRight, + ALT_RIGHT => AltRight, + SUPER_RIGHT => SuperRight, + CONTEXT_MENU => ContextMenu, +} diff --git a/crates/processing_input/src/lib.rs b/crates/processing_input/src/lib.rs index 118a295f..5c758f33 100644 --- a/crates/processing_input/src/lib.rs +++ b/crates/processing_input/src/lib.rs @@ -1,3 +1,4 @@ +pub mod keys; pub mod state; use bevy::input::ButtonState; @@ -14,6 +15,7 @@ use bevy::window::{CursorMoved, WindowResized}; use processing_core::app_mut; use processing_core::error; +pub use keys::{key_code_from_u32, key_code_to_u32}; pub use state::{CursorPosition, LastKey, LastMouseButton}; pub struct InputPlugin; diff --git a/crates/processing_pyo3/processing_py/__init__.py b/crates/processing_pyo3/processing_py/__init__.py index e7735aec..aac23965 100644 --- a/crates/processing_pyo3/processing_py/__init__.py +++ b/crates/processing_pyo3/processing_py/__init__.py @@ -27,7 +27,6 @@ "width", "height", "focused", - "pixel_density", "pixel_width", "pixel_height", "mouse_x", @@ -36,12 +35,13 @@ "pmouse_y", "mouse_is_pressed", "mouse_button", - "mouse_wheel", "moved_x", "moved_y", "key", "key_code", "key_is_pressed", + "window_x", + "window_y", ) _DYNAMIC_TIME_ATTRS = ( @@ -54,7 +54,6 @@ "width": 100, "height": 100, "focused": False, - "pixel_density": 1.0, "pixel_width": 100, "pixel_height": 100, "mouse_x": 0.0, @@ -63,18 +62,19 @@ "pmouse_y": 0.0, "mouse_is_pressed": False, "mouse_button": None, - "mouse_wheel": 0.0, "moved_x": 0.0, "moved_y": 0.0, "key": None, "key_code": None, "key_is_pressed": False, + "window_x": 0, + "window_y": 0, } _DYNAMIC = ( _DYNAMIC_GRAPHICS_ATTRS + _DYNAMIC_TIME_ATTRS - + ("display_width", "display_height", "window_x", "window_y") + + ("display_width", "display_height") ) @@ -104,16 +104,6 @@ def __getattr__(name): if mon is None: return 0 return mon.width if name == "display_width" else mon.height - if name in ("window_x", "window_y"): - g = _get_graphics() - if g is None: - return 0 - # offscreen canvases (notebooks, embedding hosts) have no window position - try: - x, y = g.surface.position - except (AttributeError, RuntimeError): - return 0 - return x if name == "window_x" else y raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/crates/processing_pyo3/src/constants.rs b/crates/processing_pyo3/src/constants.rs index 99486f82..5a5673ce 100644 --- a/crates/processing_pyo3/src/constants.rs +++ b/crates/processing_pyo3/src/constants.rs @@ -135,8 +135,41 @@ pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { HOME, END ); + add!( + m, + CAPS_LOCK, + SCROLL_LOCK, + NUM_LOCK, + PRINT_SCREEN, + PAUSE, + CONTEXT_MENU + ); add!(m, SHIFT, CONTROL, ALT, SUPER); - add!(m, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12); + add!(m, SHIFT_RIGHT, CONTROL_RIGHT, ALT_RIGHT, SUPER_RIGHT); + add!( + m, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, F13, F14, F15, F16, F17, F18, F19, + F20, F21, F22, F23, F24, F25 + ); + add!( + m, + NUMPAD_0, + NUMPAD_1, + NUMPAD_2, + NUMPAD_3, + NUMPAD_4, + NUMPAD_5, + NUMPAD_6, + NUMPAD_7, + NUMPAD_8, + NUMPAD_9, + NUMPAD_DECIMAL, + NUMPAD_DIVIDE, + NUMPAD_MULTIPLY, + NUMPAD_SUBTRACT, + NUMPAD_ADD, + NUMPAD_ENTER, + NUMPAD_EQUAL + ); // Objects rather than strings, passed straight to blend_mode(). m.add("BLEND", PyBlendMode::from_preset(BlendMode::Blend))?; diff --git a/crates/processing_pyo3/src/graphics.rs b/crates/processing_pyo3/src/graphics.rs index d3b16919..ca1265c8 100644 --- a/crates/processing_pyo3/src/graphics.rs +++ b/crates/processing_pyo3/src/graphics.rs @@ -1005,6 +1005,17 @@ impl Graphics { self.surface.pixel_width() } + /// 0 for offscreen canvases, which have no window. + #[getter] + pub fn window_x(&self) -> i32 { + self.surface.position().map_or(0, |(x, _)| x) + } + + #[getter] + pub fn window_y(&self) -> i32 { + self.surface.position().map_or(0, |(_, y)| y) + } + #[getter] pub fn pixel_height(&self) -> PyResult { self.surface.pixel_height() @@ -2738,11 +2749,6 @@ impl Graphics { input::moved_y() } - #[getter] - fn mouse_wheel(&self) -> PyResult { - input::mouse_wheel() - } - #[getter] fn key(&self) -> PyResult> { input::key() diff --git a/crates/processing_pyo3/src/input.rs b/crates/processing_pyo3/src/input.rs index 1126d3fb..8c811a99 100644 --- a/crates/processing_pyo3/src/input.rs +++ b/crates/processing_pyo3/src/input.rs @@ -64,12 +64,12 @@ pub fn key_is_pressed() -> PyResult { } pub fn key_is_down(key_code: u32) -> PyResult { - let kc = u32_to_key_code(key_code)?; + let kc = key_code_from_u32(key_code).map_err(|e| PyValueError::new_err(format!("{e}")))?; processing::prelude::input_key_is_down(kc).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) } pub fn key_just_pressed(key_code: u32) -> PyResult { - let kc = u32_to_key_code(key_code)?; + let kc = key_code_from_u32(key_code).map_err(|e| PyValueError::new_err(format!("{e}")))?; processing::prelude::input_key_just_pressed(kc) .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) } @@ -82,7 +82,7 @@ pub fn key() -> PyResult> { pub fn key_code() -> PyResult> { processing::prelude::input_key_code() - .map(|opt| opt.map(key_code_to_u32)) + .map(|opt| opt.and_then(key_code_to_u32)) .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) } @@ -101,129 +101,62 @@ pub fn sync_globals( set_tracked(globals, "mouse_button", mouse_button()?)?; set_tracked(globals, "moved_x", moved_x()?)?; set_tracked(globals, "moved_y", moved_y()?)?; - set_tracked(globals, "mouse_wheel", mouse_wheel()?)?; set_tracked(globals, "key", key()?)?; set_tracked(globals, "key_code", key_code()?)?; set_tracked(globals, "key_is_pressed", key_is_pressed()?)?; Ok(()) } -fn u32_to_key_code(val: u32) -> PyResult { - use bevy::input::keyboard::KeyCode; - match val { - 32 => Ok(KeyCode::Space), - 256 => Ok(KeyCode::Escape), - 257 => Ok(KeyCode::Enter), - 258 => Ok(KeyCode::Tab), - 259 => Ok(KeyCode::Backspace), - 261 => Ok(KeyCode::Delete), - 262 => Ok(KeyCode::ArrowRight), - 263 => Ok(KeyCode::ArrowLeft), - 264 => Ok(KeyCode::ArrowDown), - 265 => Ok(KeyCode::ArrowUp), - 340 => Ok(KeyCode::ShiftLeft), - 341 => Ok(KeyCode::ControlLeft), - 342 => Ok(KeyCode::AltLeft), - 343 => Ok(KeyCode::SuperLeft), - 65..=90 => Ok(match val { - 65 => KeyCode::KeyA, - 66 => KeyCode::KeyB, - 67 => KeyCode::KeyC, - 68 => KeyCode::KeyD, - 69 => KeyCode::KeyE, - 70 => KeyCode::KeyF, - 71 => KeyCode::KeyG, - 72 => KeyCode::KeyH, - 73 => KeyCode::KeyI, - 74 => KeyCode::KeyJ, - 75 => KeyCode::KeyK, - 76 => KeyCode::KeyL, - 77 => KeyCode::KeyM, - 78 => KeyCode::KeyN, - 79 => KeyCode::KeyO, - 80 => KeyCode::KeyP, - 81 => KeyCode::KeyQ, - 82 => KeyCode::KeyR, - 83 => KeyCode::KeyS, - 84 => KeyCode::KeyT, - 85 => KeyCode::KeyU, - 86 => KeyCode::KeyV, - 87 => KeyCode::KeyW, - 88 => KeyCode::KeyX, - 89 => KeyCode::KeyY, - 90 => KeyCode::KeyZ, - _ => unreachable!(), - }), - 48..=57 => Ok(match val { - 48 => KeyCode::Digit0, - 49 => KeyCode::Digit1, - 50 => KeyCode::Digit2, - 51 => KeyCode::Digit3, - 52 => KeyCode::Digit4, - 53 => KeyCode::Digit5, - 54 => KeyCode::Digit6, - 55 => KeyCode::Digit7, - 56 => KeyCode::Digit8, - 57 => KeyCode::Digit9, - _ => unreachable!(), - }), - _ => Err(PyValueError::new_err(format!("unknown key code: {val}"))), +/// Passed to mouse callbacks that take a parameter, e.g. `def mouse_wheel(e)`. +#[pyclass(frozen, get_all)] +pub struct MouseEvent { + pub x: f32, + pub y: f32, + pub button: Option, + /// Vertical scroll since the last frame. + pub wheel: f32, +} + +impl MouseEvent { + pub fn current(surface: Entity, width: u32, height: u32) -> PyResult { + Ok(Self { + x: mouse_x(surface, width)?, + y: mouse_y(surface, height)?, + button: mouse_button()?, + wheel: mouse_wheel()?, + }) + } +} + +#[pymethods] +impl MouseEvent { + fn __repr__(&self) -> String { + format!( + "MouseEvent(x={}, y={}, button={:?}, wheel={})", + self.x, self.y, self.button, self.wheel + ) + } +} + +/// Passed to key callbacks that take a parameter, e.g. `def key_pressed(e)`. +#[pyclass(frozen, get_all)] +pub struct KeyEvent { + pub key: Option, + pub key_code: Option, +} + +impl KeyEvent { + pub fn current() -> PyResult { + Ok(Self { + key: key()?, + key_code: key_code()?, + }) } } -fn key_code_to_u32(kc: bevy::input::keyboard::KeyCode) -> u32 { - use bevy::input::keyboard::KeyCode; - match kc { - KeyCode::Space => 32, - KeyCode::Escape => 256, - KeyCode::Enter => 257, - KeyCode::Tab => 258, - KeyCode::Backspace => 259, - KeyCode::Delete => 261, - KeyCode::ArrowRight => 262, - KeyCode::ArrowLeft => 263, - KeyCode::ArrowDown => 264, - KeyCode::ArrowUp => 265, - KeyCode::ShiftLeft => 340, - KeyCode::ControlLeft => 341, - KeyCode::AltLeft => 342, - KeyCode::SuperLeft => 343, - KeyCode::KeyA => 65, - KeyCode::KeyB => 66, - KeyCode::KeyC => 67, - KeyCode::KeyD => 68, - KeyCode::KeyE => 69, - KeyCode::KeyF => 70, - KeyCode::KeyG => 71, - KeyCode::KeyH => 72, - KeyCode::KeyI => 73, - KeyCode::KeyJ => 74, - KeyCode::KeyK => 75, - KeyCode::KeyL => 76, - KeyCode::KeyM => 77, - KeyCode::KeyN => 78, - KeyCode::KeyO => 79, - KeyCode::KeyP => 80, - KeyCode::KeyQ => 81, - KeyCode::KeyR => 82, - KeyCode::KeyS => 83, - KeyCode::KeyT => 84, - KeyCode::KeyU => 85, - KeyCode::KeyV => 86, - KeyCode::KeyW => 87, - KeyCode::KeyX => 88, - KeyCode::KeyY => 89, - KeyCode::KeyZ => 90, - KeyCode::Digit0 => 48, - KeyCode::Digit1 => 49, - KeyCode::Digit2 => 50, - KeyCode::Digit3 => 51, - KeyCode::Digit4 => 52, - KeyCode::Digit5 => 53, - KeyCode::Digit6 => 54, - KeyCode::Digit7 => 55, - KeyCode::Digit8 => 56, - KeyCode::Digit9 => 57, - _ => 0, +#[pymethods] +impl KeyEvent { + fn __repr__(&self) -> String { + format!("KeyEvent(key={:?}, key_code={:?})", self.key, self.key_code) } } diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index f0235caa..15e0820b 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -128,9 +128,18 @@ impl Default for LoopState { } } +/// Input history the derived callbacks need across frames. +#[derive(Clone, Copy, Default)] +pub(crate) struct EventState { + /// Pressed and not dragged since, so the release is a click. + click_pending: bool, + canvas_size: Option<(u32, u32)>, +} + thread_local! { pub(crate) static LAST_GLOBALS: RefCell>> = RefCell::new(HashMap::new()); pub(crate) static LOOP_STATE: Cell = Cell::new(LoopState::default()); + pub(crate) static EVENT_STATE: Cell = Cell::new(EventState::default()); } fn update_loop_state(f: impl FnOnce(&mut LoopState)) { @@ -177,61 +186,132 @@ where Ok(()) } -pub(crate) fn reset_tracked_globals() { - LAST_GLOBALS.with(|cache| cache.borrow_mut().clear()); +fn canvas_size(graphics: &Graphics) -> PyResult<(u32, u32)> { + let err = + |e: processing::prelude::error::ProcessingError| PyRuntimeError::new_err(format!("{e}")); + let width = ::processing::prelude::surface_width(graphics.surface.entity).map_err(err)?; + let height = ::processing::prelude::surface_height(graphics.surface.entity).map_err(err)?; + Ok(( + if width == 0 { graphics.width } else { width }, + if height == 0 { graphics.height } else { height }, + )) } fn sync_globals(module: &Bound<'_, PyModule>, globals: &Bound<'_, PyAny>) -> PyResult<()> { let graphics = get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; - let width = ::processing::prelude::surface_width(graphics.surface.entity) - .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; - let width = if width == 0 { graphics.width } else { width }; - let height = ::processing::prelude::surface_height(graphics.surface.entity) - .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; - let height = if height == 0 { graphics.height } else { height }; + let (width, height) = canvas_size(&graphics)?; input::sync_globals(globals, graphics.surface.entity, width, height)?; surface::sync_globals(globals, &graphics.surface, width, height)?; time::sync_globals(globals)?; Ok(()) } -fn try_call(locals: &Bound<'_, PyAny>, name: &str) -> PyResult<()> { - if let Ok(cb) = locals.get_item(name) - && cb.is_callable() - { - cb.call0() - .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; +fn mouse_event(module: &Bound<'_, PyModule>) -> PyResult> { + // the graphics borrow has to end before the callback runs, which may draw + let event = { + let graphics = + get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; + let (width, height) = canvas_size(&graphics)?; + input::MouseEvent::current(graphics.surface.entity, width, height)? + }; + Ok(Py::new(module.py(), event)?.into_any()) +} + +fn key_event(module: &Bound<'_, PyModule>) -> PyResult> { + Ok(Py::new(module.py(), input::KeyEvent::current()?)?.into_any()) +} + +fn no_event(module: &Bound<'_, PyModule>) -> PyResult> { + Ok(module.py().None()) +} + +/// Calls the sketch's `name` callback, with an event if it takes a parameter +/// (`def mouse_pressed(e)`), as py5 and Processing's `mousePressed(MouseEvent)` do. +fn call_event_callback( + module: &Bound<'_, PyModule>, + locals: &Bound<'_, PyAny>, + name: &str, + event: fn(&Bound<'_, PyModule>) -> PyResult>, +) -> PyResult<()> { + let Ok(cb) = locals.get_item(name) else { + return Ok(()); + }; + if !cb.is_callable() { + return Ok(()); + } + let inspect = PyModule::import(module.py(), "inspect")?; + // builtins without a signature can't take our event either + let takes_event = inspect + .call_method1("signature", (&cb,)) + .and_then(|sig| sig.getattr("parameters")?.len()) + .is_ok_and(|n| n > 0); + if takes_event { + cb.call1((event(module)?,))?; + } else { + cb.call0()?; } Ok(()) } -fn dispatch_event_callbacks(locals: &Bound<'_, PyAny>) -> PyResult<()> { +fn dispatch_event_callbacks( + module: &Bound<'_, PyModule>, + locals: &Bound<'_, PyAny>, +) -> PyResult<()> { use processing::prelude::*; let err = |e: processing::prelude::error::ProcessingError| PyRuntimeError::new_err(format!("{e}")); - if input_mouse_any_just_pressed().map_err(err)? { - try_call(locals, "mouse_pressed")?; + let update_events = |f: &dyn Fn(&mut EventState)| { + EVENT_STATE.with(|s| { + let mut state = s.get(); + f(&mut state); + s.set(state); + }) + }; + + let size = { + let graphics = + get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; + canvas_size(&graphics)? + }; + let previous = EVENT_STATE.with(|s| s.get().canvas_size); + update_events(&|s| s.canvas_size = Some(size)); + if previous.is_some_and(|p| p != size) { + call_event_callback(module, locals, "window_resized", no_event)?; } - if input_mouse_any_just_released().map_err(err)? { - try_call(locals, "mouse_released")?; + + if input_mouse_any_just_pressed().map_err(err)? { + update_events(&|s| s.click_pending = true); + call_event_callback(module, locals, "mouse_pressed", mouse_event)?; } if input_mouse_moved().map_err(err)? { if input_mouse_is_pressed().map_err(err)? { - try_call(locals, "mouse_dragged")?; + update_events(&|s| s.click_pending = false); + call_event_callback(module, locals, "mouse_dragged", mouse_event)?; } else { - try_call(locals, "mouse_moved")?; + call_event_callback(module, locals, "mouse_moved", mouse_event)?; + } + } + if input_mouse_any_just_released().map_err(err)? { + call_event_callback(module, locals, "mouse_released", mouse_event)?; + if EVENT_STATE.with(|s| s.get().click_pending) { + update_events(&|s| s.click_pending = false); + call_event_callback(module, locals, "mouse_clicked", mouse_event)?; } } if input_mouse_scrolled().map_err(err)? { - try_call(locals, "mouse_wheel")?; + call_event_callback(module, locals, "mouse_wheel", mouse_event)?; } if input_key_any_just_pressed().map_err(err)? { - try_call(locals, "key_pressed")?; + call_event_callback(module, locals, "key_pressed", key_event)?; + // Processing's keyTyped: only keys that produce a character + if input_key().map_err(err)?.is_some_and(|c| !c.is_control()) { + call_event_callback(module, locals, "key_typed", key_event)?; + } } if input_key_any_just_released().map_err(err)? { - try_call(locals, "key_released")?; + call_event_callback(module, locals, "key_released", key_event)?; } Ok(()) } @@ -360,21 +440,18 @@ fn get_asset_root() -> PyResult { // in ipython/jupyter argv[0] is weird so we use cwd // todo: what is the correct way to get notebook path if filename.is_empty() || !path.getattr("isfile")?.call1((filename,))?.is_truthy()? { - let cwd = os.getattr("getcwd")?.call0()?.to_string(); - let asset_root = path.getattr("join")?.call1((cwd, "assets"))?.to_string(); - return Ok(asset_root); + return Ok(os.getattr("getcwd")?.call0()?.to_string()); } let dirname = path.getattr("dirname")?.call1((filename,))?; - let abspath = path.getattr("abspath")?.call1((dirname,))?; - let asset_root = path - .getattr("join")? - .call1((abspath, "assets"))? - .to_string(); - Ok(asset_root) + Ok(path.getattr("abspath")?.call1((dirname,))?.to_string()) }) } +pub(crate) fn reset_tracked_globals() { + LAST_GLOBALS.with(|cache| cache.borrow_mut().clear()); +} + fn get_sketch_info() -> PyResult<(String, String)> { Python::attach(|py| { let sys = PyModule::import(py, "sys")?; @@ -471,6 +548,10 @@ pub mod processing_py { #[pymodule_export] use super::cuda::CudaImage; #[pymodule_export] + use super::input::KeyEvent; + #[pymodule_export] + use super::input::MouseEvent; + #[pymodule_export] use super::math::PyQuat; #[pymodule_export] use super::math::PyVec2; @@ -693,7 +774,7 @@ pub mod processing_py { return Ok(()); } sync_globals(module, ns)?; - dispatch_event_callbacks(ns)?; + dispatch_event_callbacks(module, ns)?; Ok(()) } @@ -1106,7 +1187,7 @@ pub mod processing_py { break; } } - dispatch_event_callbacks(&locals)?; + dispatch_event_callbacks(module, &locals)?; std::thread::sleep(std::time::Duration::from_millis(16)); } @@ -1148,7 +1229,7 @@ pub mod processing_py { } } - dispatch_event_callbacks(&locals)?; + dispatch_event_callbacks(module, &locals)?; let should_draw = first_frame || LOOP_STATE.with(|s| { diff --git a/crates/processing_pyo3/src/surface.rs b/crates/processing_pyo3/src/surface.rs index 6aa70901..72828f58 100644 --- a/crates/processing_pyo3/src/surface.rs +++ b/crates/processing_pyo3/src/surface.rs @@ -187,7 +187,6 @@ pub fn sync_globals( set_tracked(globals, "width", canvas_width)?; set_tracked(globals, "height", canvas_height)?; set_tracked(globals, "focused", surface.focused()?)?; - set_tracked(globals, "pixel_density", surface.pixel_density()?)?; set_tracked(globals, "pixel_width", surface.pixel_width()?)?; set_tracked(globals, "pixel_height", surface.pixel_height()?)?; diff --git a/crates/processing_wasm/src/lib.rs b/crates/processing_wasm/src/lib.rs index 3153b783..260f005a 100644 --- a/crates/processing_wasm/src/lib.rs +++ b/crates/processing_wasm/src/lib.rs @@ -1885,6 +1885,19 @@ pub const PROCESSING_KEY_F9: u32 = 298; pub const PROCESSING_KEY_F10: u32 = 299; pub const PROCESSING_KEY_F11: u32 = 300; pub const PROCESSING_KEY_F12: u32 = 301; +pub const PROCESSING_KEY_F13: u32 = 302; +pub const PROCESSING_KEY_F14: u32 = 303; +pub const PROCESSING_KEY_F15: u32 = 304; +pub const PROCESSING_KEY_F16: u32 = 305; +pub const PROCESSING_KEY_F17: u32 = 306; +pub const PROCESSING_KEY_F18: u32 = 307; +pub const PROCESSING_KEY_F19: u32 = 308; +pub const PROCESSING_KEY_F20: u32 = 309; +pub const PROCESSING_KEY_F21: u32 = 310; +pub const PROCESSING_KEY_F22: u32 = 311; +pub const PROCESSING_KEY_F23: u32 = 312; +pub const PROCESSING_KEY_F24: u32 = 313; +pub const PROCESSING_KEY_F25: u32 = 314; pub const PROCESSING_KEY_NUMPAD_0: u32 = 320; pub const PROCESSING_KEY_NUMPAD_1: u32 = 321; pub const PROCESSING_KEY_NUMPAD_2: u32 = 322; @@ -2041,7 +2054,7 @@ pub fn js_key() -> Result { #[wasm_bindgen(js_name = "keyCode")] pub fn js_key_code() -> Result { - check(input_key_code().map(|opt| opt.map(key_code_to_u32).unwrap_or(0))) + check(input_key_code().map(|opt| opt.and_then(key_code_to_u32).unwrap_or(0))) } #[wasm_bindgen(js_name = "movedX")] @@ -2059,230 +2072,6 @@ pub fn js_mouse_wheel() -> Result { check(input_mouse_wheel()) } -fn key_code_from_u32(val: u32) -> processing::prelude::error::Result { - match val { - PROCESSING_KEY_SPACE => Ok(KeyCode::Space), - PROCESSING_KEY_QUOTE => Ok(KeyCode::Quote), - PROCESSING_KEY_COMMA => Ok(KeyCode::Comma), - PROCESSING_KEY_MINUS => Ok(KeyCode::Minus), - PROCESSING_KEY_PERIOD => Ok(KeyCode::Period), - PROCESSING_KEY_SLASH => Ok(KeyCode::Slash), - PROCESSING_KEY_0 => Ok(KeyCode::Digit0), - PROCESSING_KEY_1 => Ok(KeyCode::Digit1), - PROCESSING_KEY_2 => Ok(KeyCode::Digit2), - PROCESSING_KEY_3 => Ok(KeyCode::Digit3), - PROCESSING_KEY_4 => Ok(KeyCode::Digit4), - PROCESSING_KEY_5 => Ok(KeyCode::Digit5), - PROCESSING_KEY_6 => Ok(KeyCode::Digit6), - PROCESSING_KEY_7 => Ok(KeyCode::Digit7), - PROCESSING_KEY_8 => Ok(KeyCode::Digit8), - PROCESSING_KEY_9 => Ok(KeyCode::Digit9), - PROCESSING_KEY_SEMICOLON => Ok(KeyCode::Semicolon), - PROCESSING_KEY_EQUAL => Ok(KeyCode::Equal), - PROCESSING_KEY_A => Ok(KeyCode::KeyA), - PROCESSING_KEY_B => Ok(KeyCode::KeyB), - PROCESSING_KEY_C => Ok(KeyCode::KeyC), - PROCESSING_KEY_D => Ok(KeyCode::KeyD), - PROCESSING_KEY_E => Ok(KeyCode::KeyE), - PROCESSING_KEY_F => Ok(KeyCode::KeyF), - PROCESSING_KEY_G => Ok(KeyCode::KeyG), - PROCESSING_KEY_H => Ok(KeyCode::KeyH), - PROCESSING_KEY_I => Ok(KeyCode::KeyI), - PROCESSING_KEY_J => Ok(KeyCode::KeyJ), - PROCESSING_KEY_K => Ok(KeyCode::KeyK), - PROCESSING_KEY_L => Ok(KeyCode::KeyL), - PROCESSING_KEY_M => Ok(KeyCode::KeyM), - PROCESSING_KEY_N => Ok(KeyCode::KeyN), - PROCESSING_KEY_O => Ok(KeyCode::KeyO), - PROCESSING_KEY_P => Ok(KeyCode::KeyP), - PROCESSING_KEY_Q => Ok(KeyCode::KeyQ), - PROCESSING_KEY_R => Ok(KeyCode::KeyR), - PROCESSING_KEY_S => Ok(KeyCode::KeyS), - PROCESSING_KEY_T => Ok(KeyCode::KeyT), - PROCESSING_KEY_U => Ok(KeyCode::KeyU), - PROCESSING_KEY_V => Ok(KeyCode::KeyV), - PROCESSING_KEY_W => Ok(KeyCode::KeyW), - PROCESSING_KEY_X => Ok(KeyCode::KeyX), - PROCESSING_KEY_Y => Ok(KeyCode::KeyY), - PROCESSING_KEY_Z => Ok(KeyCode::KeyZ), - PROCESSING_KEY_BRACKET_LEFT => Ok(KeyCode::BracketLeft), - PROCESSING_KEY_BACKSLASH => Ok(KeyCode::Backslash), - PROCESSING_KEY_BRACKET_RIGHT => Ok(KeyCode::BracketRight), - PROCESSING_KEY_BACKQUOTE => Ok(KeyCode::Backquote), - PROCESSING_KEY_ESCAPE => Ok(KeyCode::Escape), - PROCESSING_KEY_ENTER => Ok(KeyCode::Enter), - PROCESSING_KEY_TAB => Ok(KeyCode::Tab), - PROCESSING_KEY_BACKSPACE => Ok(KeyCode::Backspace), - PROCESSING_KEY_INSERT => Ok(KeyCode::Insert), - PROCESSING_KEY_DELETE => Ok(KeyCode::Delete), - PROCESSING_KEY_RIGHT => Ok(KeyCode::ArrowRight), - PROCESSING_KEY_LEFT => Ok(KeyCode::ArrowLeft), - PROCESSING_KEY_DOWN => Ok(KeyCode::ArrowDown), - PROCESSING_KEY_UP => Ok(KeyCode::ArrowUp), - PROCESSING_KEY_PAGE_UP => Ok(KeyCode::PageUp), - PROCESSING_KEY_PAGE_DOWN => Ok(KeyCode::PageDown), - PROCESSING_KEY_HOME => Ok(KeyCode::Home), - PROCESSING_KEY_END => Ok(KeyCode::End), - PROCESSING_KEY_CAPS_LOCK => Ok(KeyCode::CapsLock), - PROCESSING_KEY_SCROLL_LOCK => Ok(KeyCode::ScrollLock), - PROCESSING_KEY_NUM_LOCK => Ok(KeyCode::NumLock), - PROCESSING_KEY_PRINT_SCREEN => Ok(KeyCode::PrintScreen), - PROCESSING_KEY_PAUSE => Ok(KeyCode::Pause), - PROCESSING_KEY_F1 => Ok(KeyCode::F1), - PROCESSING_KEY_F2 => Ok(KeyCode::F2), - PROCESSING_KEY_F3 => Ok(KeyCode::F3), - PROCESSING_KEY_F4 => Ok(KeyCode::F4), - PROCESSING_KEY_F5 => Ok(KeyCode::F5), - PROCESSING_KEY_F6 => Ok(KeyCode::F6), - PROCESSING_KEY_F7 => Ok(KeyCode::F7), - PROCESSING_KEY_F8 => Ok(KeyCode::F8), - PROCESSING_KEY_F9 => Ok(KeyCode::F9), - PROCESSING_KEY_F10 => Ok(KeyCode::F10), - PROCESSING_KEY_F11 => Ok(KeyCode::F11), - PROCESSING_KEY_F12 => Ok(KeyCode::F12), - PROCESSING_KEY_NUMPAD_0 => Ok(KeyCode::Numpad0), - PROCESSING_KEY_NUMPAD_1 => Ok(KeyCode::Numpad1), - PROCESSING_KEY_NUMPAD_2 => Ok(KeyCode::Numpad2), - PROCESSING_KEY_NUMPAD_3 => Ok(KeyCode::Numpad3), - PROCESSING_KEY_NUMPAD_4 => Ok(KeyCode::Numpad4), - PROCESSING_KEY_NUMPAD_5 => Ok(KeyCode::Numpad5), - PROCESSING_KEY_NUMPAD_6 => Ok(KeyCode::Numpad6), - PROCESSING_KEY_NUMPAD_7 => Ok(KeyCode::Numpad7), - PROCESSING_KEY_NUMPAD_8 => Ok(KeyCode::Numpad8), - PROCESSING_KEY_NUMPAD_9 => Ok(KeyCode::Numpad9), - PROCESSING_KEY_NUMPAD_DECIMAL => Ok(KeyCode::NumpadDecimal), - PROCESSING_KEY_NUMPAD_DIVIDE => Ok(KeyCode::NumpadDivide), - PROCESSING_KEY_NUMPAD_MULTIPLY => Ok(KeyCode::NumpadMultiply), - PROCESSING_KEY_NUMPAD_SUBTRACT => Ok(KeyCode::NumpadSubtract), - PROCESSING_KEY_NUMPAD_ADD => Ok(KeyCode::NumpadAdd), - PROCESSING_KEY_NUMPAD_ENTER => Ok(KeyCode::NumpadEnter), - PROCESSING_KEY_NUMPAD_EQUAL => Ok(KeyCode::NumpadEqual), - PROCESSING_KEY_SHIFT_LEFT => Ok(KeyCode::ShiftLeft), - PROCESSING_KEY_CONTROL_LEFT => Ok(KeyCode::ControlLeft), - PROCESSING_KEY_ALT_LEFT => Ok(KeyCode::AltLeft), - PROCESSING_KEY_SUPER_LEFT => Ok(KeyCode::SuperLeft), - PROCESSING_KEY_SHIFT_RIGHT => Ok(KeyCode::ShiftRight), - PROCESSING_KEY_CONTROL_RIGHT => Ok(KeyCode::ControlRight), - PROCESSING_KEY_ALT_RIGHT => Ok(KeyCode::AltRight), - PROCESSING_KEY_SUPER_RIGHT => Ok(KeyCode::SuperRight), - PROCESSING_KEY_CONTEXT_MENU => Ok(KeyCode::ContextMenu), - _ => Err(ProcessingError::InvalidArgument(format!( - "unknown key code: {val}" - ))), - } -} - -fn key_code_to_u32(kc: KeyCode) -> u32 { - match kc { - KeyCode::Space => PROCESSING_KEY_SPACE, - KeyCode::Quote => PROCESSING_KEY_QUOTE, - KeyCode::Comma => PROCESSING_KEY_COMMA, - KeyCode::Minus => PROCESSING_KEY_MINUS, - KeyCode::Period => PROCESSING_KEY_PERIOD, - KeyCode::Slash => PROCESSING_KEY_SLASH, - KeyCode::Digit0 => PROCESSING_KEY_0, - KeyCode::Digit1 => PROCESSING_KEY_1, - KeyCode::Digit2 => PROCESSING_KEY_2, - KeyCode::Digit3 => PROCESSING_KEY_3, - KeyCode::Digit4 => PROCESSING_KEY_4, - KeyCode::Digit5 => PROCESSING_KEY_5, - KeyCode::Digit6 => PROCESSING_KEY_6, - KeyCode::Digit7 => PROCESSING_KEY_7, - KeyCode::Digit8 => PROCESSING_KEY_8, - KeyCode::Digit9 => PROCESSING_KEY_9, - KeyCode::Semicolon => PROCESSING_KEY_SEMICOLON, - KeyCode::Equal => PROCESSING_KEY_EQUAL, - KeyCode::KeyA => PROCESSING_KEY_A, - KeyCode::KeyB => PROCESSING_KEY_B, - KeyCode::KeyC => PROCESSING_KEY_C, - KeyCode::KeyD => PROCESSING_KEY_D, - KeyCode::KeyE => PROCESSING_KEY_E, - KeyCode::KeyF => PROCESSING_KEY_F, - KeyCode::KeyG => PROCESSING_KEY_G, - KeyCode::KeyH => PROCESSING_KEY_H, - KeyCode::KeyI => PROCESSING_KEY_I, - KeyCode::KeyJ => PROCESSING_KEY_J, - KeyCode::KeyK => PROCESSING_KEY_K, - KeyCode::KeyL => PROCESSING_KEY_L, - KeyCode::KeyM => PROCESSING_KEY_M, - KeyCode::KeyN => PROCESSING_KEY_N, - KeyCode::KeyO => PROCESSING_KEY_O, - KeyCode::KeyP => PROCESSING_KEY_P, - KeyCode::KeyQ => PROCESSING_KEY_Q, - KeyCode::KeyR => PROCESSING_KEY_R, - KeyCode::KeyS => PROCESSING_KEY_S, - KeyCode::KeyT => PROCESSING_KEY_T, - KeyCode::KeyU => PROCESSING_KEY_U, - KeyCode::KeyV => PROCESSING_KEY_V, - KeyCode::KeyW => PROCESSING_KEY_W, - KeyCode::KeyX => PROCESSING_KEY_X, - KeyCode::KeyY => PROCESSING_KEY_Y, - KeyCode::KeyZ => PROCESSING_KEY_Z, - KeyCode::BracketLeft => PROCESSING_KEY_BRACKET_LEFT, - KeyCode::Backslash => PROCESSING_KEY_BACKSLASH, - KeyCode::BracketRight => PROCESSING_KEY_BRACKET_RIGHT, - KeyCode::Backquote => PROCESSING_KEY_BACKQUOTE, - KeyCode::Escape => PROCESSING_KEY_ESCAPE, - KeyCode::Enter => PROCESSING_KEY_ENTER, - KeyCode::Tab => PROCESSING_KEY_TAB, - KeyCode::Backspace => PROCESSING_KEY_BACKSPACE, - KeyCode::Insert => PROCESSING_KEY_INSERT, - KeyCode::Delete => PROCESSING_KEY_DELETE, - KeyCode::ArrowRight => PROCESSING_KEY_RIGHT, - KeyCode::ArrowLeft => PROCESSING_KEY_LEFT, - KeyCode::ArrowDown => PROCESSING_KEY_DOWN, - KeyCode::ArrowUp => PROCESSING_KEY_UP, - KeyCode::PageUp => PROCESSING_KEY_PAGE_UP, - KeyCode::PageDown => PROCESSING_KEY_PAGE_DOWN, - KeyCode::Home => PROCESSING_KEY_HOME, - KeyCode::End => PROCESSING_KEY_END, - KeyCode::CapsLock => PROCESSING_KEY_CAPS_LOCK, - KeyCode::ScrollLock => PROCESSING_KEY_SCROLL_LOCK, - KeyCode::NumLock => PROCESSING_KEY_NUM_LOCK, - KeyCode::PrintScreen => PROCESSING_KEY_PRINT_SCREEN, - KeyCode::Pause => PROCESSING_KEY_PAUSE, - KeyCode::F1 => PROCESSING_KEY_F1, - KeyCode::F2 => PROCESSING_KEY_F2, - KeyCode::F3 => PROCESSING_KEY_F3, - KeyCode::F4 => PROCESSING_KEY_F4, - KeyCode::F5 => PROCESSING_KEY_F5, - KeyCode::F6 => PROCESSING_KEY_F6, - KeyCode::F7 => PROCESSING_KEY_F7, - KeyCode::F8 => PROCESSING_KEY_F8, - KeyCode::F9 => PROCESSING_KEY_F9, - KeyCode::F10 => PROCESSING_KEY_F10, - KeyCode::F11 => PROCESSING_KEY_F11, - KeyCode::F12 => PROCESSING_KEY_F12, - KeyCode::Numpad0 => PROCESSING_KEY_NUMPAD_0, - KeyCode::Numpad1 => PROCESSING_KEY_NUMPAD_1, - KeyCode::Numpad2 => PROCESSING_KEY_NUMPAD_2, - KeyCode::Numpad3 => PROCESSING_KEY_NUMPAD_3, - KeyCode::Numpad4 => PROCESSING_KEY_NUMPAD_4, - KeyCode::Numpad5 => PROCESSING_KEY_NUMPAD_5, - KeyCode::Numpad6 => PROCESSING_KEY_NUMPAD_6, - KeyCode::Numpad7 => PROCESSING_KEY_NUMPAD_7, - KeyCode::Numpad8 => PROCESSING_KEY_NUMPAD_8, - KeyCode::Numpad9 => PROCESSING_KEY_NUMPAD_9, - KeyCode::NumpadDecimal => PROCESSING_KEY_NUMPAD_DECIMAL, - KeyCode::NumpadDivide => PROCESSING_KEY_NUMPAD_DIVIDE, - KeyCode::NumpadMultiply => PROCESSING_KEY_NUMPAD_MULTIPLY, - KeyCode::NumpadSubtract => PROCESSING_KEY_NUMPAD_SUBTRACT, - KeyCode::NumpadAdd => PROCESSING_KEY_NUMPAD_ADD, - KeyCode::NumpadEnter => PROCESSING_KEY_NUMPAD_ENTER, - KeyCode::NumpadEqual => PROCESSING_KEY_NUMPAD_EQUAL, - KeyCode::ShiftLeft => PROCESSING_KEY_SHIFT_LEFT, - KeyCode::ControlLeft => PROCESSING_KEY_CONTROL_LEFT, - KeyCode::AltLeft => PROCESSING_KEY_ALT_LEFT, - KeyCode::SuperLeft => PROCESSING_KEY_SUPER_LEFT, - KeyCode::ShiftRight => PROCESSING_KEY_SHIFT_RIGHT, - KeyCode::ControlRight => PROCESSING_KEY_CONTROL_RIGHT, - KeyCode::AltRight => PROCESSING_KEY_ALT_RIGHT, - KeyCode::SuperRight => PROCESSING_KEY_SUPER_RIGHT, - KeyCode::ContextMenu => PROCESSING_KEY_CONTEXT_MENU, - _ => 0, - } -} - #[wasm_bindgen(js_name = "geometryAttributeRotation")] pub fn js_geometry_attribute_rotation() -> u64 { geometry_attribute_rotation().to_bits() From abd4cf257b392f0b43a11a077933b9dd68a0c144 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?charlotte=20=F0=9F=8C=B8?= Date: Thu, 12 Nov 2026 14:25:21 -0800 Subject: [PATCH 2/2] Processing core functions for Python. --- Cargo.lock | 1 + crates/processing_core/Cargo.toml | 1 + crates/processing_core/src/constants.rs | 10 + crates/processing_core/src/lib.rs | 1 + crates/processing_core/src/noise.rs | 101 ++++++++++ crates/processing_pyo3/processing_py/math.py | 39 ++++ crates/processing_pyo3/src/color.rs | 56 ++++++ crates/processing_pyo3/src/constants.rs | 2 + crates/processing_pyo3/src/graphics.rs | 91 +++++++++ crates/processing_pyo3/src/host.rs | 19 +- crates/processing_pyo3/src/lib.rs | 186 +++++++++++++++++++ crates/processing_render/src/lib.rs | 26 +++ crates/processing_render/src/time.rs | 67 ++++++- 13 files changed, 594 insertions(+), 6 deletions(-) create mode 100644 crates/processing_core/src/noise.rs diff --git a/Cargo.lock b/Cargo.lock index 805c9193..32a93615 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6255,6 +6255,7 @@ name = "processing_core" version = "0.4.0" dependencies = [ "bevy", + "rand 0.10.2", "raw-window-handle", "thiserror 2.0.20", "tracing", diff --git a/crates/processing_core/Cargo.toml b/crates/processing_core/Cargo.toml index bb388668..52761fec 100644 --- a/crates/processing_core/Cargo.toml +++ b/crates/processing_core/Cargo.toml @@ -9,6 +9,7 @@ workspace = true [dependencies] bevy = { workspace = true } +rand = { workspace = true } raw-window-handle = "0.6" thiserror = "2" tracing = "0.1" diff --git a/crates/processing_core/src/constants.rs b/crates/processing_core/src/constants.rs index f6771497..0cd5866a 100644 --- a/crates/processing_core/src/constants.rs +++ b/crates/processing_core/src/constants.rs @@ -54,6 +54,16 @@ pub const OKLCH: &str = "oklch"; pub const LAB: &str = "lab"; pub const LCH: &str = "lch"; pub const XYZ: &str = "xyz"; +// Processing's names for the same spaces +pub const RGB: &str = SRGB; +pub const HSB: &str = HSV; + +pub const ARROW: &str = "arrow"; +pub const CROSS: &str = "cross"; +pub const HAND: &str = "hand"; +pub const MOVE: &str = "move"; +pub const TEXT: &str = "text"; +pub const WAIT: &str = "wait"; pub const PBR_NEUTRAL: &str = "pbr_neutral"; // tonemapping pub const AGX: &str = "agx"; diff --git a/crates/processing_core/src/lib.rs b/crates/processing_core/src/lib.rs index 489754e7..29638048 100644 --- a/crates/processing_core/src/lib.rs +++ b/crates/processing_core/src/lib.rs @@ -1,6 +1,7 @@ pub mod config; pub mod constants; pub mod error; +pub mod noise; use std::cell::RefCell; use std::sync::OnceLock; diff --git a/crates/processing_core/src/noise.rs b/crates/processing_core/src/noise.rs new file mode 100644 index 00000000..1dc52b62 --- /dev/null +++ b/crates/processing_core/src/noise.rs @@ -0,0 +1,101 @@ +//! Processing's `noise()`: value noise over a table of 4096 random values with +//! cosine smoothing, summed over octaves. A port of `PApplet.noise`, including +//! its half-degree cosine table, so sketches look the way they do in Processing. + +use rand::rngs::StdRng; +use rand::{RngExt, SeedableRng}; + +const YWRAPB: u32 = 4; +const YWRAP: u32 = 1 << YWRAPB; +const ZWRAPB: u32 = 8; +const ZWRAP: u32 = 1 << ZWRAPB; +const SIZE: u32 = 4095; + +pub struct Noise { + table: Vec, + octaves: u32, + falloff: f32, +} + +impl Noise { + pub fn new(seed: u64) -> Self { + let mut rng = StdRng::seed_from_u64(seed); + Self { + table: (0..=SIZE).map(|_| rng.random::()).collect(), + octaves: 4, + falloff: 0.5, + } + } + + /// Reseeds the table, keeping the detail settings. + pub fn seed(&mut self, seed: u64) { + let mut rng = StdRng::seed_from_u64(seed); + self.table.iter_mut().for_each(|v| *v = rng.random()); + } + + /// Non-positive values leave the current setting unchanged, as in Processing. + pub fn detail(&mut self, octaves: i32, falloff: Option) { + if octaves > 0 { + self.octaves = octaves as u32; + } + if let Some(falloff) = falloff.filter(|f| *f > 0.0) { + self.falloff = falloff; + } + } + + pub fn get(&self, x: f32, y: f32, z: f32) -> f32 { + let (x, y, z) = (x.abs(), y.abs(), z.abs()); + let (mut xi, mut yi, mut zi) = (x as u32, y as u32, z as u32); + let (mut xf, mut yf, mut zf) = (x - xi as f32, y - yi as f32, z - zi as f32); + let at = |i: u32| self.table[(i & SIZE) as usize]; + + let mut r = 0.0; + let mut amplitude = 0.5; + for _ in 0..self.octaves { + let mut of = xi + .wrapping_add(yi.wrapping_shl(YWRAPB)) + .wrapping_add(zi.wrapping_shl(ZWRAPB)); + let rxf = smooth(xf); + let ryf = smooth(yf); + + let mut n1 = at(of); + n1 += rxf * (at(of.wrapping_add(1)) - n1); + let mut n2 = at(of.wrapping_add(YWRAP)); + n2 += rxf * (at(of.wrapping_add(YWRAP + 1)) - n2); + n1 += ryf * (n2 - n1); + + of = of.wrapping_add(ZWRAP); + n2 = at(of); + n2 += rxf * (at(of.wrapping_add(1)) - n2); + let mut n3 = at(of.wrapping_add(YWRAP)); + n3 += rxf * (at(of.wrapping_add(YWRAP + 1)) - n3); + n2 += ryf * (n3 - n2); + + n1 += smooth(zf) * (n2 - n1); + + r += n1 * amplitude; + amplitude *= self.falloff; + (xi, yi, zi) = (xi.wrapping_shl(1), yi.wrapping_shl(1), zi.wrapping_shl(1)); + (xf, yf, zf) = (xf * 2.0, yf * 2.0, zf * 2.0); + if xf >= 1.0 { + xi = xi.wrapping_add(1); + xf -= 1.0; + } + if yf >= 1.0 { + yi = yi.wrapping_add(1); + yf -= 1.0; + } + if zf >= 1.0 { + zi = zi.wrapping_add(1); + zf -= 1.0; + } + } + r + } +} + +/// `0.5 * (1 - cos(t * PI))`, quantized to half a degree like Processing's `cosLUT`. +fn smooth(t: f32) -> f32 { + let half_degrees = (t * 360.0) as u32 % 720; + 0.5 * (1.0 - (half_degrees as f32 * 0.5).to_radians().cos()) +} diff --git a/crates/processing_pyo3/processing_py/math.py b/crates/processing_pyo3/processing_py/math.py index 2ae3d903..de9bbf8a 100644 --- a/crates/processing_pyo3/processing_py/math.py +++ b/crates/processing_pyo3/processing_py/math.py @@ -1,5 +1,6 @@ """Processing math methods and vector/quaternion types.""" import math as _math +import random as _random from .processing_py import math as _native_math # extend native math so glob imports work @@ -131,3 +132,41 @@ def dist(*args): dx, dy, dz = x2 - x1, y2 - y1, z2 - z1 return sqrt(dx * dx + dy * dy + dz * dz) raise TypeError(f"dist() takes 4 or 6 arguments ({len(args)} given)") + + +def random(low, high=None): + """`random(high)` is in [0, high), `random(low, high)` in [low, high).""" + if high is None: + low, high = 0.0, low + return low + _random.random() * (high - low) + + +def random_gaussian(): + return _random.gauss(0.0, 1.0) + + +def random_seed(seed): + _random.seed(seed) + + +def bezier_point(a, b, c, d, t): + u = 1.0 - t + return a * u * u * u + 3 * b * t * u * u + 3 * c * t * t * u + d * t * t * t + + +def bezier_tangent(a, b, c, d, t): + return 3 * t * t * (-a + 3 * b - 3 * c + d) + 6 * t * (a - 2 * b + c) + 3 * (-a + b) + + +# Catmull-Rom, matching `curve()` (Processing's curveTightness(0)) +def curve_point(a, b, c, d, t): + return 0.5 * ( + 2 * b + + (-a + c) * t + + (2 * a - 5 * b + 4 * c - d) * t * t + + (-a + 3 * b - 3 * c + d) * t * t * t + ) + + +def curve_tangent(a, b, c, d, t): + return 0.5 * ((-a + c) + 2 * (2 * a - 5 * b + 4 * c - d) * t + 3 * (-a + 3 * b - 3 * c + d) * t * t) diff --git a/crates/processing_pyo3/src/color.rs b/crates/processing_pyo3/src/color.rs index 6aef7072..a6ed2974 100644 --- a/crates/processing_pyo3/src/color.rs +++ b/crates/processing_pyo3/src/color.rs @@ -459,6 +459,62 @@ impl ColorLike { } } +/// A channel read back in the range `color_mode` set for its space, or the +/// space's native range when the mode is a different space. +fn channel_in_mode(mode: &ColorMode, space: ColorSpace, value: f32, ch: usize) -> f32 { + if mode.space == space { + value / space.default_maxes()[ch] * mode.max[ch] + } else { + value + } +} + +pub fn red(color: Color, mode: &ColorMode) -> f32 { + channel_in_mode(mode, ColorSpace::Srgb, Srgba::from(color).red, 0) +} + +pub fn green(color: Color, mode: &ColorMode) -> f32 { + channel_in_mode(mode, ColorSpace::Srgb, Srgba::from(color).green, 1) +} + +pub fn blue(color: Color, mode: &ColorMode) -> f32 { + channel_in_mode(mode, ColorSpace::Srgb, Srgba::from(color).blue, 2) +} + +pub fn alpha(color: Color, mode: &ColorMode) -> f32 { + color.alpha() * mode.max[3] +} + +pub fn hue(color: Color, mode: &ColorMode) -> f32 { + channel_in_mode(mode, ColorSpace::Hsv, Hsva::from(color).hue, 0) +} + +pub fn saturation(color: Color, mode: &ColorMode) -> f32 { + channel_in_mode(mode, ColorSpace::Hsv, Hsva::from(color).saturation, 1) +} + +pub fn brightness(color: Color, mode: &ColorMode) -> f32 { + channel_in_mode(mode, ColorSpace::Hsv, Hsva::from(color).value, 2) +} + +/// Interpolates in the `color_mode` space, as Processing's `lerpColor` does +/// for RGB and HSB. +pub fn lerp_color(from: Color, to: Color, amount: f32, mode: &ColorMode) -> Color { + let in_space = |c: Color| match mode.space { + ColorSpace::Srgb => Color::Srgba(c.into()), + ColorSpace::Linear => Color::LinearRgba(c.into()), + ColorSpace::Hsl => Color::Hsla(c.into()), + ColorSpace::Hsv => Color::Hsva(c.into()), + ColorSpace::Hwb => Color::Hwba(c.into()), + ColorSpace::Oklab => Color::Oklaba(c.into()), + ColorSpace::Oklch => Color::Oklcha(c.into()), + ColorSpace::Lab => Color::Laba(c.into()), + ColorSpace::Lch => Color::Lcha(c.into()), + ColorSpace::Xyz => Color::Xyza(c.into()), + }; + in_space(from).mix(&in_space(to), amount.clamp(0.0, 1.0)) +} + fn parse_hex(s: &str) -> PyResult { Srgba::hex(s) .map(Color::Srgba) diff --git a/crates/processing_pyo3/src/constants.rs b/crates/processing_pyo3/src/constants.rs index 5a5673ce..a29bbff6 100644 --- a/crates/processing_pyo3/src/constants.rs +++ b/crates/processing_pyo3/src/constants.rs @@ -44,6 +44,8 @@ pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { add!(m, WORD, CHAR); add!(m, NEAREST, CLAMP, REPEAT, MIRROR); add!(m, SRGB, LINEAR, HSL, HSV, HWB, OKLAB, OKLCH, LAB, LCH, XYZ); + add!(m, RGB, HSB); + add!(m, ARROW, CROSS, HAND, MOVE, TEXT, WAIT); add!( m, PBR_NEUTRAL, diff --git a/crates/processing_pyo3/src/graphics.rs b/crates/processing_pyo3/src/graphics.rs index ca1265c8..d8052205 100644 --- a/crates/processing_pyo3/src/graphics.rs +++ b/crates/processing_pyo3/src/graphics.rs @@ -864,6 +864,10 @@ impl Drop for Graphics { } impl Graphics { + pub(crate) fn current_color_mode(&self) -> PyResult { + graphics_get_color_mode(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) + } + /// Create an offscreen graphics buffer in the already-running app (no window, /// no `init`). Backs `create_graphics()` / `new_offscreen()`. The caller must /// ensure the app exists (i.e. `size()` was called first). @@ -1162,6 +1166,93 @@ impl Graphics { .map(crate::color::PyColor::from) } + pub fn red(&self, color: crate::color::ColorLike) -> PyResult { + Ok(crate::color::red( + color.into_color()?, + &self.current_color_mode()?, + )) + } + + pub fn green(&self, color: crate::color::ColorLike) -> PyResult { + Ok(crate::color::green( + color.into_color()?, + &self.current_color_mode()?, + )) + } + + pub fn blue(&self, color: crate::color::ColorLike) -> PyResult { + Ok(crate::color::blue( + color.into_color()?, + &self.current_color_mode()?, + )) + } + + pub fn alpha(&self, color: crate::color::ColorLike) -> PyResult { + Ok(crate::color::alpha( + color.into_color()?, + &self.current_color_mode()?, + )) + } + + pub fn hue(&self, color: crate::color::ColorLike) -> PyResult { + Ok(crate::color::hue( + color.into_color()?, + &self.current_color_mode()?, + )) + } + + pub fn saturation(&self, color: crate::color::ColorLike) -> PyResult { + Ok(crate::color::saturation( + color.into_color()?, + &self.current_color_mode()?, + )) + } + + pub fn brightness(&self, color: crate::color::ColorLike) -> PyResult { + Ok(crate::color::brightness( + color.into_color()?, + &self.current_color_mode()?, + )) + } + + pub fn lerp_color( + &self, + from: crate::color::ColorLike, + to: crate::color::ColorLike, + amount: f32, + ) -> PyResult { + let color = crate::color::lerp_color( + from.into_color()?, + to.into_color()?, + amount, + &self.current_color_mode()?, + ); + Ok(color.into()) + } + + /// `ARROW`, `CROSS`, `HAND`, `MOVE`, `TEXT` or `WAIT`. Also shows a hidden cursor. + #[pyo3(signature = (kind=constants::ARROW))] + pub fn cursor(&self, kind: &str) -> PyResult<()> { + use bevy::window::SystemCursorIcon; + let icon = match kind { + constants::ARROW => SystemCursorIcon::Default, + constants::CROSS => SystemCursorIcon::Crosshair, + constants::HAND => SystemCursorIcon::Pointer, + constants::MOVE => SystemCursorIcon::Move, + constants::TEXT => SystemCursorIcon::Text, + constants::WAIT => SystemCursorIcon::Wait, + _ => return Err(PyValueError::new_err(format!("unknown cursor: {kind:?}"))), + }; + input_set_cursor_visible(self.surface.entity, true) + .and_then(|_| input_set_cursor_icon(self.surface.entity, icon)) + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) + } + + pub fn no_cursor(&self) -> PyResult<()> { + input_set_cursor_visible(self.surface.entity, false) + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) + } + #[pyo3(signature = (*args))] pub fn background(&self, args: &Bound<'_, PyTuple>) -> PyResult<()> { let color = extract_color_with_mode( diff --git a/crates/processing_pyo3/src/host.rs b/crates/processing_pyo3/src/host.rs index fb7071f7..7d172c7e 100644 --- a/crates/processing_pyo3/src/host.rs +++ b/crates/processing_pyo3/src/host.rs @@ -9,8 +9,9 @@ //! it directly but call these functions on the registered module instead. //! Entities cross as raw bits. //! 2. Each sketch has a [`SketchContext`]. The canvas graphics, extra -//! windows, `loop()`/`no_loop()` state, the tracked-globals cache and the -//! frame counter are otherwise process-global. The host enters a sketch's +//! windows, `loop()`/`no_loop()` state, the tracked-globals cache, event +//! history, noise field, frame counter and frame timing are otherwise +//! process-global. The host enters a sketch's //! context before touching it and exits after. use std::collections::HashMap; @@ -23,7 +24,7 @@ use pyo3::exceptions::{PyRuntimeError, PyValueError}; use pyo3::prelude::*; use pyo3::types::PyBytes; -use crate::{LAST_GLOBALS, LOOP_STATE, LoopState}; +use crate::{EVENT_STATE, EventState, LAST_GLOBALS, LOOP_STATE, LoopState, NOISE, new_noise}; fn err(e: impl std::fmt::Display) -> PyErr { PyRuntimeError::new_err(format!("{e}")) @@ -41,7 +42,10 @@ pub struct SketchContext { windows: Option>, loop_state: LoopState, last_globals: HashMap<&'static str, Py>, + event_state: EventState, + noise: processing_core::noise::Noise, frame_count: u32, + frame_timing: processing_render::time::FrameTiming, entered: bool, } @@ -51,7 +55,10 @@ pub(crate) fn context_new() -> SketchContext { windows: None, loop_state: LoopState::default(), last_globals: HashMap::new(), + event_state: EventState::default(), + noise: new_noise(), frame_count: 0, + frame_timing: Default::default(), entered: false, } } @@ -77,8 +84,11 @@ pub(crate) fn context_enter(module: &Bound<'_, PyModule>, ctx: &mut SketchContex module.setattr("_windows", ctx.windows.take().unwrap_or_else(|| py.None()))?; LOOP_STATE.with(|s| s.set(ctx.loop_state)); LAST_GLOBALS.with(|c| *c.borrow_mut() = std::mem::take(&mut ctx.last_globals)); + EVENT_STATE.with(|s| s.set(ctx.event_state)); + NOISE.with(|n| std::mem::swap(&mut *n.borrow_mut(), &mut ctx.noise)); // The app may not exist yet (first sketch, before `size()`). let _ = set_frame_count(ctx.frame_count); + let _ = processing_render::set_frame_timing(ctx.frame_timing); ctx.entered = true; Ok(()) } @@ -91,7 +101,10 @@ pub(crate) fn context_exit(module: &Bound<'_, PyModule>, ctx: &mut SketchContext ctx.windows = take_attr(module, "_windows")?; ctx.loop_state = LOOP_STATE.with(|s| s.replace(LoopState::default())); ctx.last_globals = LAST_GLOBALS.with(|c| std::mem::take(&mut *c.borrow_mut())); + ctx.event_state = EVENT_STATE.with(|s| s.replace(EventState::default())); + NOISE.with(|n| std::mem::swap(&mut *n.borrow_mut(), &mut ctx.noise)); ctx.frame_count = frame_count().unwrap_or(ctx.frame_count); + ctx.frame_timing = processing_render::frame_timing().unwrap_or(ctx.frame_timing); ctx.entered = false; Ok(()) } diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index 15e0820b..ee56d520 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -117,6 +117,9 @@ use std::env; pub(crate) struct LoopState { looping: bool, redraw_requested: bool, + exit_requested: bool, + /// Set by `frame_rate(fps)`. `run()` sleeps out the rest of each frame. + frame_interval: Option, } impl Default for LoopState { @@ -124,6 +127,8 @@ impl Default for LoopState { Self { looping: true, redraw_requested: false, + exit_requested: false, + frame_interval: None, } } } @@ -140,6 +145,16 @@ thread_local! { pub(crate) static LAST_GLOBALS: RefCell>> = RefCell::new(HashMap::new()); pub(crate) static LOOP_STATE: Cell = Cell::new(LoopState::default()); pub(crate) static EVENT_STATE: Cell = Cell::new(EventState::default()); + pub(crate) static NOISE: RefCell = RefCell::new(new_noise()); +} + +/// A sketch's noise field: randomly seeded, fixed under CI so captures are stable. +pub(crate) fn new_noise() -> processing_core::noise::Noise { + processing_core::noise::Noise::new(if processing_render::ci::enabled() { + 0 + } else { + rand::random() + }) } fn update_loop_state(f: impl FnOnce(&mut LoopState)) { @@ -207,6 +222,14 @@ fn sync_globals(module: &Bound<'_, PyModule>, globals: &Bound<'_, PyAny>) -> PyR Ok(()) } +/// The main canvas's color mode, or the default before `size()`. +fn current_color_mode(module: &Bound<'_, PyModule>) -> PyResult { + match get_graphics(module)? { + Some(graphics) => graphics.current_color_mode(), + None => Ok(color::ColorMode::default()), + } +} + fn mouse_event(module: &Bound<'_, PyModule>) -> PyResult> { // the graphics borrow has to end before the callback runs, which may draw let event = { @@ -976,6 +999,75 @@ pub mod processing_py { Ok(packages) } + /// Ends `run()` once the current `draw()` returns. Processing's `exit()`, + /// renamed so it doesn't shadow Python's builtin. + #[pyfunction] + fn exit_sketch() -> PyResult<()> { + update_loop_state(|s| s.exit_requested = true); + Ok(()) + } + + /// `frame_rate()` returns the measured frames per second. `frame_rate(fps)` + /// caps how fast `run()` draws (the display's vsync still applies above it). + #[pyfunction] + #[pyo3(signature = (fps=None))] + fn frame_rate(py: Python<'_>, fps: Option) -> PyResult> { + match fps { + Some(fps) if fps > 0.0 => { + let interval = std::time::Duration::from_secs_f32(1.0 / fps); + update_loop_state(|s| s.frame_interval = Some(interval)); + Ok(py.None()) + } + Some(fps) => Err(pyo3::exceptions::PyValueError::new_err(format!( + "frame_rate must be positive, got {fps}" + ))), + None => { + let rate = processing::prelude::frame_rate() + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; + Ok(rate.into_pyobject(py)?.into_any().unbind()) + } + } + } + + /// Milliseconds since the sketch started. + #[pyfunction] + fn millis() -> PyResult { + let elapsed = processing::prelude::elapsed_time() + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; + Ok((elapsed as f64 * 1000.0) as u64) + } + + /// Processing's Perlin noise, in [0, 1). + #[pyfunction] + #[pyo3(signature = (x, y=0.0, z=0.0))] + fn noise(x: f32, y: f32, z: f32) -> f32 { + NOISE.with(|n| n.borrow().get(x, y, z)) + } + + /// `octaves` (default 4) and `falloff` (default 0.5) per octave. + #[pyfunction] + #[pyo3(signature = (octaves, falloff=None))] + fn noise_detail(octaves: i32, falloff: Option) { + NOISE.with(|n| n.borrow_mut().detail(octaves, falloff)); + } + + #[pyfunction] + fn noise_seed(seed: u64) { + NOISE.with(|n| n.borrow_mut().seed(seed)); + } + + #[pyfunction] + #[pyo3(pass_module, signature = (kind=processing::prelude::constants::ARROW))] + fn cursor(module: &Bound<'_, PyModule>, kind: &str) -> PyResult<()> { + graphics!(module).cursor(kind) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn no_cursor(module: &Bound<'_, PyModule>) -> PyResult<()> { + graphics!(module).no_cursor() + } + #[pyfunction] #[pyo3(pass_module)] fn window_title(module: &Bound<'_, PyModule>, title: &str) -> PyResult<()> { @@ -1150,9 +1242,15 @@ pub mod processing_py { let setup_fn = locals.get_item("setup").ok(); let mut draw_fn = locals.get_item("draw").ok(); + update_loop_state(|s| s.exit_requested = false); + let exit_requested = || LOOP_STATE.with(|s| s.get().exit_requested); + if let Some(ref setup) = setup_fn { setup.call0()?; } + if exit_requested() { + return Ok(()); + } ensure_graphics(module)?; @@ -1188,6 +1286,9 @@ pub mod processing_py { } } dispatch_event_callbacks(module, &locals)?; + if exit_requested() { + break; + } std::thread::sleep(std::time::Duration::from_millis(16)); } @@ -1230,6 +1331,9 @@ pub mod processing_py { } dispatch_event_callbacks(module, &locals)?; + if exit_requested() { + break; + } let should_draw = first_frame || LOOP_STATE.with(|s| { @@ -1242,6 +1346,7 @@ pub mod processing_py { continue; } first_frame = false; + let frame_start = std::time::Instant::now(); processing::prelude::advance_frame_count() .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; @@ -1263,6 +1368,12 @@ pub mod processing_py { } update_loop_state(|s| s.redraw_requested = false); + if exit_requested() { + break; + } + if let Some(interval) = LOOP_STATE.with(|s| s.get().frame_interval) { + std::thread::sleep(interval.saturating_sub(frame_start.elapsed())); + } } Ok(()) @@ -1483,6 +1594,81 @@ pub mod processing_py { ) } + #[pyfunction] + #[pyo3(pass_module)] + fn red(module: &Bound<'_, PyModule>, color: super::color::ColorLike) -> PyResult { + Ok(super::color::red( + color.into_color()?, + ¤t_color_mode(module)?, + )) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn green(module: &Bound<'_, PyModule>, color: super::color::ColorLike) -> PyResult { + Ok(super::color::green( + color.into_color()?, + ¤t_color_mode(module)?, + )) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn blue(module: &Bound<'_, PyModule>, color: super::color::ColorLike) -> PyResult { + Ok(super::color::blue( + color.into_color()?, + ¤t_color_mode(module)?, + )) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn alpha(module: &Bound<'_, PyModule>, color: super::color::ColorLike) -> PyResult { + Ok(super::color::alpha( + color.into_color()?, + ¤t_color_mode(module)?, + )) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn hue(module: &Bound<'_, PyModule>, color: super::color::ColorLike) -> PyResult { + Ok(super::color::hue( + color.into_color()?, + ¤t_color_mode(module)?, + )) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn saturation(module: &Bound<'_, PyModule>, color: super::color::ColorLike) -> PyResult { + Ok(super::color::saturation( + color.into_color()?, + ¤t_color_mode(module)?, + )) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn brightness(module: &Bound<'_, PyModule>, color: super::color::ColorLike) -> PyResult { + Ok(super::color::brightness( + color.into_color()?, + ¤t_color_mode(module)?, + )) + } + + #[pyfunction] + #[pyo3(pass_module)] + fn lerp_color( + module: &Bound<'_, PyModule>, + from: super::color::ColorLike, + to: super::color::ColorLike, + amount: f32, + ) -> PyResult { + let mode = current_color_mode(module)?; + Ok(super::color::lerp_color(from.into_color()?, to.into_color()?, amount, &mode).into()) + } + #[pyfunction(name = "color")] #[pyo3(pass_module, signature = (*args))] fn create_color( diff --git a/crates/processing_render/src/lib.rs b/crates/processing_render/src/lib.rs index c01747b3..f9fc08b3 100644 --- a/crates/processing_render/src/lib.rs +++ b/crates/processing_render/src/lib.rs @@ -80,6 +80,10 @@ impl Plugin for ProcessingRenderPlugin { let config = app.world().resource::().clone(); app.init_resource::(); + app.init_resource::(); + app.init_resource::(); + // sketch time is wall time: no 250ms cap on a slow or paused frame + app.insert_resource(Time::::from_max_delta(std::time::Duration::MAX)); let has_sketch_file = config .get(ConfigKey::SketchFileName) @@ -2245,6 +2249,24 @@ pub fn set_frame_count(n: u32) -> error::Result<()> { }) } +pub fn frame_timing() -> error::Result { + app_mut(|app| { + Ok(app + .world_mut() + .run_system_cached(time::frame_timing) + .unwrap()) + }) +} + +pub fn set_frame_timing(timing: time::FrameTiming) -> error::Result<()> { + app_mut(|app| { + app.world_mut() + .run_system_cached_with(time::set_frame_timing, timing) + .unwrap(); + Ok(()) + }) +} + pub fn advance_frame_count() -> error::Result<()> { app_mut(|app| { app.world_mut() @@ -2254,6 +2276,10 @@ pub fn advance_frame_count() -> error::Result<()> { }) } +pub fn frame_rate() -> error::Result { + app_mut(|app| Ok(app.world_mut().run_system_cached(time::frame_rate).unwrap())) +} + pub fn delta_time() -> error::Result { app_mut(|app| Ok(app.world_mut().run_system_cached(time::delta_secs).unwrap())) } diff --git a/crates/processing_render/src/time.rs b/crates/processing_render/src/time.rs index 4c7d156e..b31b98dd 100644 --- a/crates/processing_render/src/time.rs +++ b/crates/processing_render/src/time.rs @@ -12,12 +12,73 @@ pub fn set_frame_count(In(n): In, mut frame: ResMut) { frame.0 = n; } -pub fn advance_frame_count(mut frame: ResMut) { +/// Frames per second, smoothed like Processing's `frameRate`: starts at 60 and +/// moves 10% toward the instantaneous rate each frame. +#[derive(Resource, Debug, Clone, Copy)] +pub struct ProcessingFrameRate(pub f32); + +impl Default for ProcessingFrameRate { + fn default() -> Self { + Self(60.0) + } +} + +/// Time between Processing frames; `Time::delta` only spans the last of a frame's `app.update()`s. +#[derive(Resource, Default, Debug, Clone, Copy)] +pub struct ProcessingFrameTime { + last_elapsed: Option, + delta: f32, +} + +pub fn advance_frame_count( + mut frame: ResMut, + mut rate: ResMut, + mut frame_time: ResMut, + time: Option>, +) { frame.0 = frame.0.wrapping_add(1); + let Some(elapsed) = time.map(|t| t.elapsed_secs_f64()) else { + return; + }; + let dt = frame_time + .last_elapsed + .map_or(0.0, |last| (elapsed - last) as f32); + frame_time.last_elapsed = Some(elapsed); + frame_time.delta = dt; + if dt > 0.0 { + rate.0 = rate.0 * 0.9 + 0.1 / dt; + } +} + +/// The frame rate and frame time of one sketch, for hosts that drive several over one app. +#[derive(Default, Debug, Clone, Copy)] +pub struct FrameTiming { + rate: ProcessingFrameRate, + time: ProcessingFrameTime, +} + +pub fn frame_timing(rate: Res, time: Res) -> FrameTiming { + FrameTiming { + rate: *rate, + time: *time, + } +} + +pub fn set_frame_timing( + In(timing): In, + mut rate: ResMut, + mut time: ResMut, +) { + *rate = timing.rate; + *time = timing.time; +} + +pub fn frame_rate(rate: Res) -> f32 { + rate.0 } -pub fn delta_secs(time: Option>) -> f32 { - time.map(|t| t.delta_secs()).unwrap_or(0.0) +pub fn delta_secs(frame_time: Res) -> f32 { + frame_time.delta } pub fn elapsed_secs(time: Option>) -> f32 {