Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/processing_pyo3/src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1181,22 +1181,22 @@ impl Graphics {

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

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

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

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

#[getter]
Expand Down
41 changes: 26 additions & 15 deletions crates/processing_pyo3/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ use pyo3::{
prelude::*,
};

pub fn mouse_x(surface: Entity) -> PyResult<f32> {
processing::prelude::input_mouse_x(surface).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
pub fn mouse_x(surface: Entity, width: u32) -> PyResult<f32> {
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<f32> {
processing::prelude::input_mouse_y(surface).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
pub fn mouse_y(surface: Entity, height: u32) -> PyResult<f32> {
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<f32> {
processing::prelude::input_pmouse_x(surface)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
pub fn pmouse_x(surface: Entity, width: u32) -> PyResult<f32> {
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<f32> {
processing::prelude::input_pmouse_y(surface)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
pub fn pmouse_y(surface: Entity, height: u32) -> PyResult<f32> {
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<bool> {
Expand Down Expand Up @@ -80,12 +86,17 @@ pub fn key_code() -> PyResult<Option<u32>> {
.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()?)?;
Expand Down
15 changes: 10 additions & 5 deletions crates/processing_pyo3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand Down Expand Up @@ -1620,31 +1625,31 @@ mod mewnala {
fn mouse_x(module: &Bound<'_, PyModule>) -> PyResult<f32> {
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]
#[pyo3(pass_module)]
fn mouse_y(module: &Bound<'_, PyModule>) -> PyResult<f32> {
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]
#[pyo3(pass_module)]
fn pmouse_x(module: &Bound<'_, PyModule>) -> PyResult<f32> {
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]
#[pyo3(pass_module)]
fn pmouse_y(module: &Bound<'_, PyModule>) -> PyResult<f32> {
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]
Expand Down
Loading