diff --git a/crates/processing_pyo3/src/graphics.rs b/crates/processing_pyo3/src/graphics.rs index bac0137..cafd305 100644 --- a/crates/processing_pyo3/src/graphics.rs +++ b/crates/processing_pyo3/src/graphics.rs @@ -1181,22 +1181,22 @@ impl Graphics { #[getter] fn mouse_x(&self) -> PyResult { - input::mouse_x(self.surface.entity) + input::mouse_x(self.surface.entity, self.width) } #[getter] fn mouse_y(&self) -> PyResult { - input::mouse_y(self.surface.entity) + input::mouse_y(self.surface.entity, self.height) } #[getter] fn pmouse_x(&self) -> PyResult { - input::pmouse_x(self.surface.entity) + input::pmouse_x(self.surface.entity, self.width) } #[getter] fn pmouse_y(&self) -> PyResult { - input::pmouse_y(self.surface.entity) + input::pmouse_y(self.surface.entity, self.height) } #[getter] diff --git a/crates/processing_pyo3/src/input.rs b/crates/processing_pyo3/src/input.rs index 5096d5c..de40a2e 100644 --- a/crates/processing_pyo3/src/input.rs +++ b/crates/processing_pyo3/src/input.rs @@ -5,22 +5,28 @@ use pyo3::{ prelude::*, }; -pub fn mouse_x(surface: Entity) -> PyResult { - processing::prelude::input_mouse_x(surface).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) +pub fn mouse_x(surface: Entity, width: u32) -> PyResult { + let raw = processing::prelude::input_mouse_x(surface) + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; + Ok(raw.clamp(0.0, width as f32)) } -pub fn mouse_y(surface: Entity) -> PyResult { - processing::prelude::input_mouse_y(surface).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) +pub fn mouse_y(surface: Entity, height: u32) -> PyResult { + let raw = processing::prelude::input_mouse_y(surface) + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; + Ok(raw.clamp(0.0, height as f32)) } -pub fn pmouse_x(surface: Entity) -> PyResult { - processing::prelude::input_pmouse_x(surface) - .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) +pub fn pmouse_x(surface: Entity, width: u32) -> PyResult { + let raw = processing::prelude::input_pmouse_x(surface) + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; + Ok(raw.clamp(0.0, width as f32)) } -pub fn pmouse_y(surface: Entity) -> PyResult { - processing::prelude::input_pmouse_y(surface) - .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) +pub fn pmouse_y(surface: Entity, height: u32) -> PyResult { + let raw = processing::prelude::input_pmouse_y(surface) + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; + Ok(raw.clamp(0.0, height as f32)) } pub fn mouse_is_pressed() -> PyResult { @@ -80,12 +86,17 @@ pub fn key_code() -> PyResult> { .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) } -pub fn sync_globals(globals: &Bound<'_, PyAny>, surface: Entity) -> PyResult<()> { +pub fn sync_globals( + globals: &Bound<'_, PyAny>, + surface: Entity, + canvas_width: u32, + canvas_height: u32, +) -> PyResult<()> { use crate::set_tracked; - set_tracked(globals, "mouse_x", mouse_x(surface)?)?; - set_tracked(globals, "mouse_y", mouse_y(surface)?)?; - set_tracked(globals, "pmouse_x", pmouse_x(surface)?)?; - set_tracked(globals, "pmouse_y", pmouse_y(surface)?)?; + set_tracked(globals, "mouse_x", mouse_x(surface, canvas_width)?)?; + set_tracked(globals, "mouse_y", mouse_y(surface, canvas_height)?)?; + set_tracked(globals, "pmouse_x", pmouse_x(surface, canvas_width)?)?; + set_tracked(globals, "pmouse_y", pmouse_y(surface, canvas_height)?)?; set_tracked(globals, "mouse_is_pressed", mouse_is_pressed()?)?; set_tracked(globals, "mouse_button", mouse_button()?)?; set_tracked(globals, "moved_x", moved_x()?)?; diff --git a/crates/processing_pyo3/src/lib.rs b/crates/processing_pyo3/src/lib.rs index af5324e..a248054 100644 --- a/crates/processing_pyo3/src/lib.rs +++ b/crates/processing_pyo3/src/lib.rs @@ -116,7 +116,12 @@ pub(crate) fn reset_tracked_globals() { fn sync_globals(module: &Bound<'_, PyModule>, globals: &Bound<'_, PyAny>) -> PyResult<()> { let graphics = get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; - input::sync_globals(globals, graphics.surface.entity)?; + input::sync_globals( + globals, + graphics.surface.entity, + graphics.width, + graphics.height, + )?; surface::sync_globals(globals, &graphics.surface, graphics.width, graphics.height)?; time::sync_globals(globals)?; Ok(()) @@ -1620,7 +1625,7 @@ mod mewnala { fn mouse_x(module: &Bound<'_, PyModule>) -> PyResult { let graphics = get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; - input::mouse_x(graphics.surface.entity) + input::mouse_x(graphics.surface.entity, graphics.width) } #[pyfunction] @@ -1628,7 +1633,7 @@ mod mewnala { fn mouse_y(module: &Bound<'_, PyModule>) -> PyResult { let graphics = get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; - input::mouse_y(graphics.surface.entity) + input::mouse_y(graphics.surface.entity, graphics.height) } #[pyfunction] @@ -1636,7 +1641,7 @@ mod mewnala { fn pmouse_x(module: &Bound<'_, PyModule>) -> PyResult { let graphics = get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; - input::pmouse_x(graphics.surface.entity) + input::pmouse_x(graphics.surface.entity, graphics.width) } #[pyfunction] @@ -1644,7 +1649,7 @@ mod mewnala { fn pmouse_y(module: &Bound<'_, PyModule>) -> PyResult { let graphics = get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?; - input::pmouse_y(graphics.surface.entity) + input::pmouse_y(graphics.surface.entity, graphics.height) } #[pyfunction]