forked from processing/libprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphics.rs
More file actions
276 lines (234 loc) · 8.63 KB
/
graphics.rs
File metadata and controls
276 lines (234 loc) · 8.63 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use bevy::prelude::Entity;
use processing::prelude::*;
use pyo3::{exceptions::PyRuntimeError, prelude::*};
use crate::glfw::GlfwContext;
#[pyclass(unsendable)]
pub struct Surface {
entity: Entity,
glfw_ctx: GlfwContext,
}
#[pymethods]
impl Surface {
pub fn poll_events(&mut self) -> bool {
self.glfw_ctx.poll_events()
}
}
impl Drop for Surface {
fn drop(&mut self) {
let _ = surface_destroy(self.entity);
}
}
#[pyclass]
#[derive(Debug)]
pub struct Image {
entity: Entity,
}
impl Drop for Image {
fn drop(&mut self) {
let _ = image_destroy(self.entity);
}
}
#[pyclass(unsendable)]
pub struct Graphics {
entity: Entity,
pub surface: Surface,
}
impl Drop for Graphics {
fn drop(&mut self) {
let _ = graphics_destroy(self.entity);
}
}
#[pymethods]
impl Graphics {
#[new]
pub fn new(width: u32, height: u32, asset_path: &str) -> PyResult<Self> {
let glfw_ctx =
GlfwContext::new(width, height).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
let mut config = Config::new();
config.set(ConfigKey::AssetRootPath, asset_path.to_string());
init(config).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
let surface = glfw_ctx
.create_surface(width, height, 1.0)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
let surface = Surface {
entity: surface,
glfw_ctx,
};
let graphics = graphics_create(surface.entity, width, height)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Self {
entity: graphics,
surface,
})
}
pub fn background(&self, args: Vec<f32>) -> PyResult<()> {
let (r, g, b, a) = parse_color(&args)?;
let color = bevy::color::Color::srgba(r, g, b, a);
graphics_record_command(self.entity, DrawCommand::BackgroundColor(color))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn background_image(&self, image: &Image) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::BackgroundImage(image.entity))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn fill(&self, args: Vec<f32>) -> PyResult<()> {
let (r, g, b, a) = parse_color(&args)?;
let color = bevy::color::Color::srgba(r, g, b, a);
graphics_record_command(self.entity, DrawCommand::Fill(color))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn no_fill(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::NoFill)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn stroke(&self, args: Vec<f32>) -> PyResult<()> {
let (r, g, b, a) = parse_color(&args)?;
let color = bevy::color::Color::srgba(r, g, b, a);
graphics_record_command(self.entity, DrawCommand::StrokeColor(color))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn no_stroke(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::NoStroke)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn stroke_weight(&self, weight: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::StrokeWeight(weight))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn rect(
&self,
x: f32,
y: f32,
w: f32,
h: f32,
tl: f32,
tr: f32,
br: f32,
bl: f32,
) -> PyResult<()> {
graphics_record_command(
self.entity,
DrawCommand::Rect {
x,
y,
w,
h,
radii: [tl, tr, br, bl],
},
)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn image(&self, file: &str) -> PyResult<Image> {
match image_load(file) {
Ok(image) => Ok(Image { entity: image }),
Err(e) => Err(PyRuntimeError::new_err(format!("{e}"))),
}
}
pub fn push_matrix(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::PushMatrix)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn pop_matrix(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::PopMatrix)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn reset_matrix(&self) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::ResetMatrix)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn translate(&self, x: f32, y: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Translate { x, y })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn rotate(&self, angle: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Rotate { angle })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn scale(&self, x: f32, y: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Scale { x, y })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn shear_x(&self, angle: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::ShearX { angle })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn shear_y(&self, angle: f32) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::ShearY { angle })
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn begin_draw(&self) -> PyResult<()> {
graphics_begin_draw(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn end_draw(&self) -> PyResult<()> {
graphics_end_draw(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn mode_3d(&self) -> PyResult<()> {
graphics_mode_3d(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn mode_2d(&self) -> PyResult<()> {
graphics_mode_2d(self.entity).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn camera_position(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
graphics_camera_position(self.entity, x, y, z)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn camera_look_at(&self, target_x: f32, target_y: f32, target_z: f32) -> PyResult<()> {
graphics_camera_look_at(self.entity, target_x, target_y, target_z)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn perspective(&self, fov: f32, aspect: f32, near: f32, far: f32) -> PyResult<()> {
graphics_perspective(self.entity, fov, aspect, near, far)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
#[allow(clippy::too_many_arguments)]
pub fn ortho(
&self,
left: f32,
right: f32,
bottom: f32,
top: f32,
near: f32,
far: f32,
) -> PyResult<()> {
graphics_ortho(self.entity, left, right, bottom, top, near, far)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
}
// TODO: a real color type. or color parser? idk. color is confusing. let's think
// about how to expose different color spaces in an idiomatic pythonic way
fn parse_color(args: &[f32]) -> PyResult<(f32, f32, f32, f32)> {
match args.len() {
1 => {
let v = args[0] / 255.0;
Ok((v, v, v, 1.0))
}
2 => {
let v = args[0] / 255.0;
Ok((v, v, v, args[1] / 255.0))
}
3 => Ok((args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, 1.0)),
4 => Ok((
args[0] / 255.0,
args[1] / 255.0,
args[2] / 255.0,
args[3] / 255.0,
)),
_ => Err(PyRuntimeError::new_err("color requires 1-4 arguments")),
}
}
pub fn get_graphics<'py>(module: &Bound<'py, PyModule>) -> PyResult<PyRef<'py, Graphics>> {
module
.getattr("_graphics")?
.cast_into::<Graphics>()
.map_err(|_| PyRuntimeError::new_err("no graphics context"))?
.try_borrow()
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn get_graphics_mut<'py>(module: &Bound<'py, PyModule>) -> PyResult<PyRefMut<'py, Graphics>> {
module
.getattr("_graphics")?
.cast_into::<Graphics>()
.map_err(|_| PyRuntimeError::new_err("no graphics context"))?
.try_borrow_mut()
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}