Skip to content

Commit 4ea1d28

Browse files
authored
Clamp mouse position. (#144)
1 parent 01efb5e commit 4ea1d28

3 files changed

Lines changed: 40 additions & 24 deletions

File tree

crates/processing_pyo3/src/graphics.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,22 +1181,22 @@ impl Graphics {
11811181

11821182
#[getter]
11831183
fn mouse_x(&self) -> PyResult<f32> {
1184-
input::mouse_x(self.surface.entity)
1184+
input::mouse_x(self.surface.entity, self.width)
11851185
}
11861186

11871187
#[getter]
11881188
fn mouse_y(&self) -> PyResult<f32> {
1189-
input::mouse_y(self.surface.entity)
1189+
input::mouse_y(self.surface.entity, self.height)
11901190
}
11911191

11921192
#[getter]
11931193
fn pmouse_x(&self) -> PyResult<f32> {
1194-
input::pmouse_x(self.surface.entity)
1194+
input::pmouse_x(self.surface.entity, self.width)
11951195
}
11961196

11971197
#[getter]
11981198
fn pmouse_y(&self) -> PyResult<f32> {
1199-
input::pmouse_y(self.surface.entity)
1199+
input::pmouse_y(self.surface.entity, self.height)
12001200
}
12011201

12021202
#[getter]

crates/processing_pyo3/src/input.rs

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,28 @@ use pyo3::{
55
prelude::*,
66
};
77

8-
pub fn mouse_x(surface: Entity) -> PyResult<f32> {
9-
processing::prelude::input_mouse_x(surface).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
8+
pub fn mouse_x(surface: Entity, width: u32) -> PyResult<f32> {
9+
let raw = processing::prelude::input_mouse_x(surface)
10+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
11+
Ok(raw.clamp(0.0, width as f32))
1012
}
1113

12-
pub fn mouse_y(surface: Entity) -> PyResult<f32> {
13-
processing::prelude::input_mouse_y(surface).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
14+
pub fn mouse_y(surface: Entity, height: u32) -> PyResult<f32> {
15+
let raw = processing::prelude::input_mouse_y(surface)
16+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
17+
Ok(raw.clamp(0.0, height as f32))
1418
}
1519

16-
pub fn pmouse_x(surface: Entity) -> PyResult<f32> {
17-
processing::prelude::input_pmouse_x(surface)
18-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
20+
pub fn pmouse_x(surface: Entity, width: u32) -> PyResult<f32> {
21+
let raw = processing::prelude::input_pmouse_x(surface)
22+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
23+
Ok(raw.clamp(0.0, width as f32))
1924
}
2025

21-
pub fn pmouse_y(surface: Entity) -> PyResult<f32> {
22-
processing::prelude::input_pmouse_y(surface)
23-
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
26+
pub fn pmouse_y(surface: Entity, height: u32) -> PyResult<f32> {
27+
let raw = processing::prelude::input_pmouse_y(surface)
28+
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
29+
Ok(raw.clamp(0.0, height as f32))
2430
}
2531

2632
pub fn mouse_is_pressed() -> PyResult<bool> {
@@ -80,12 +86,17 @@ pub fn key_code() -> PyResult<Option<u32>> {
8086
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
8187
}
8288

83-
pub fn sync_globals(globals: &Bound<'_, PyAny>, surface: Entity) -> PyResult<()> {
89+
pub fn sync_globals(
90+
globals: &Bound<'_, PyAny>,
91+
surface: Entity,
92+
canvas_width: u32,
93+
canvas_height: u32,
94+
) -> PyResult<()> {
8495
use crate::set_tracked;
85-
set_tracked(globals, "mouse_x", mouse_x(surface)?)?;
86-
set_tracked(globals, "mouse_y", mouse_y(surface)?)?;
87-
set_tracked(globals, "pmouse_x", pmouse_x(surface)?)?;
88-
set_tracked(globals, "pmouse_y", pmouse_y(surface)?)?;
96+
set_tracked(globals, "mouse_x", mouse_x(surface, canvas_width)?)?;
97+
set_tracked(globals, "mouse_y", mouse_y(surface, canvas_height)?)?;
98+
set_tracked(globals, "pmouse_x", pmouse_x(surface, canvas_width)?)?;
99+
set_tracked(globals, "pmouse_y", pmouse_y(surface, canvas_height)?)?;
89100
set_tracked(globals, "mouse_is_pressed", mouse_is_pressed()?)?;
90101
set_tracked(globals, "mouse_button", mouse_button()?)?;
91102
set_tracked(globals, "moved_x", moved_x()?)?;

crates/processing_pyo3/src/lib.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,12 @@ pub(crate) fn reset_tracked_globals() {
116116
fn sync_globals(module: &Bound<'_, PyModule>, globals: &Bound<'_, PyAny>) -> PyResult<()> {
117117
let graphics =
118118
get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?;
119-
input::sync_globals(globals, graphics.surface.entity)?;
119+
input::sync_globals(
120+
globals,
121+
graphics.surface.entity,
122+
graphics.width,
123+
graphics.height,
124+
)?;
120125
surface::sync_globals(globals, &graphics.surface, graphics.width, graphics.height)?;
121126
time::sync_globals(globals)?;
122127
Ok(())
@@ -1620,31 +1625,31 @@ mod mewnala {
16201625
fn mouse_x(module: &Bound<'_, PyModule>) -> PyResult<f32> {
16211626
let graphics =
16221627
get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?;
1623-
input::mouse_x(graphics.surface.entity)
1628+
input::mouse_x(graphics.surface.entity, graphics.width)
16241629
}
16251630

16261631
#[pyfunction]
16271632
#[pyo3(pass_module)]
16281633
fn mouse_y(module: &Bound<'_, PyModule>) -> PyResult<f32> {
16291634
let graphics =
16301635
get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?;
1631-
input::mouse_y(graphics.surface.entity)
1636+
input::mouse_y(graphics.surface.entity, graphics.height)
16321637
}
16331638

16341639
#[pyfunction]
16351640
#[pyo3(pass_module)]
16361641
fn pmouse_x(module: &Bound<'_, PyModule>) -> PyResult<f32> {
16371642
let graphics =
16381643
get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?;
1639-
input::pmouse_x(graphics.surface.entity)
1644+
input::pmouse_x(graphics.surface.entity, graphics.width)
16401645
}
16411646

16421647
#[pyfunction]
16431648
#[pyo3(pass_module)]
16441649
fn pmouse_y(module: &Bound<'_, PyModule>) -> PyResult<f32> {
16451650
let graphics =
16461651
get_graphics(module)?.ok_or_else(|| PyRuntimeError::new_err("call size() first"))?;
1647-
input::pmouse_y(graphics.surface.entity)
1652+
input::pmouse_y(graphics.surface.entity, graphics.height)
16481653
}
16491654

16501655
#[pyfunction]

0 commit comments

Comments
 (0)