Repository navigation
Expand file tree
/
Copy pathinput.rs
More file actions
162 lines (141 loc) · 5.17 KB
/
Copy pathinput.rs
File metadata and controls
162 lines (141 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use bevy::prelude::Entity;
use processing::prelude::*;
use pyo3::{
exceptions::{PyRuntimeError, PyValueError},
prelude::*,
};
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, 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, 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, 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> {
processing::prelude::input_mouse_is_pressed()
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn mouse_button() -> PyResult<Option<String>> {
processing::prelude::input_mouse_button()
.map(|opt| {
opt.map(|b| match b {
MouseButton::Left => constants::LEFT.to_string(),
MouseButton::Right => constants::RIGHT.to_string(),
MouseButton::Middle => constants::CENTER.to_string(),
_ => format!("{b:?}").to_lowercase(),
})
})
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn moved_x() -> PyResult<f32> {
processing::prelude::input_moved_x().map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn moved_y() -> PyResult<f32> {
processing::prelude::input_moved_y().map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn mouse_wheel() -> PyResult<f32> {
processing::prelude::input_mouse_wheel().map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn key_is_pressed() -> PyResult<bool> {
processing::prelude::input_key_is_pressed().map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn key_is_down(key_code: u32) -> PyResult<bool> {
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<bool> {
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}")))
}
pub fn key() -> PyResult<Option<String>> {
processing::prelude::input_key()
.map(|opt| opt.map(String::from))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn key_code() -> PyResult<Option<u32>> {
processing::prelude::input_key_code()
.map(|opt| opt.and_then(key_code_to_u32))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
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, 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()?)?;
set_tracked(globals, "moved_y", moved_y()?)?;
set_tracked(globals, "key", key()?)?;
set_tracked(globals, "key_code", key_code()?)?;
set_tracked(globals, "key_is_pressed", key_is_pressed()?)?;
Ok(())
}
/// 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<String>,
/// Vertical scroll since the last frame.
pub wheel: f32,
}
impl MouseEvent {
pub fn current(surface: Entity, width: u32, height: u32) -> PyResult<Self> {
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<String>,
pub key_code: Option<u32>,
}
impl KeyEvent {
pub fn current() -> PyResult<Self> {
Ok(Self {
key: key()?,
key_code: key_code()?,
})
}
}
#[pymethods]
impl KeyEvent {
fn __repr__(&self) -> String {
format!("KeyEvent(key={:?}, key_code={:?})", self.key, self.key_code)
}
}