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
376 lines (320 loc) · 11.7 KB
/
graphics.rs
File metadata and controls
376 lines (320 loc) · 11.7 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use bevy::prelude::Entity;
use processing::prelude::*;
use pyo3::{exceptions::PyRuntimeError, prelude::*, types::PyDict};
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 Geometry {
entity: Entity,
}
#[pyclass]
pub enum Topology {
PointList = 0,
LineList = 1,
LineStrip = 2,
TriangleList = 3,
TriangleStrip = 4,
}
impl Topology {
pub fn as_u8(&self) -> u8 {
match self {
Self::PointList => 0,
Self::LineList => 1,
Self::LineStrip => 2,
Self::TriangleList => 3,
Self::TriangleStrip => 4,
}
}
}
#[pyclass]
pub struct Sketch {
pub source: String,
}
#[pymethods]
impl Geometry {
#[new]
#[pyo3(signature = (**kwargs))]
pub fn new(kwargs: Option<&Bound<'_, PyDict>>) -> PyResult<Self> {
let topology = kwargs
.and_then(|k| k.get_item("topology").ok().flatten())
.and_then(|t| t.cast_into::<Topology>().ok())
.and_then(|t| geometry::Topology::from_u8(t.borrow().as_u8()))
.unwrap_or(geometry::Topology::TriangleList);
let geometry =
geometry_create(topology).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Self { entity: geometry })
}
pub fn color(&self, r: f32, g: f32, b: f32, a: f32) -> PyResult<()> {
geometry_color(self.entity, r, g, b, a).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn normal(&self, nx: f32, ny: f32, nz: f32) -> PyResult<()> {
geometry_normal(self.entity, nx, ny, nz)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn vertex(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
geometry_vertex(self.entity, x, y, z).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn index(&self, i: u32) -> PyResult<()> {
geometry_index(self.entity, i).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn set_vertex(&self, i: u32, x: f32, y: f32, z: f32) -> PyResult<()> {
geometry_set_vertex(self.entity, i, x, y, z)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
}
#[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,
sketch_root_path: &str,
sketch_file_name: &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());
config.set(ConfigKey::SketchRootPath, sketch_root_path.to_string());
config.set(ConfigKey::SketchFileName, sketch_file_name.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 poll_for_sketch_update(&self) -> PyResult<Sketch> {
match poll_for_sketch_updates().map_err(|_| PyRuntimeError::new_err("SKETCH UPDATE ERR"))? {
Some(sketch) => Ok(Sketch {
source: sketch.source,
}),
None => Ok(Sketch {
source: "".to_string(),
}),
}
}
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 draw_box(&self, x: f32, y: f32, z: f32) -> PyResult<()> {
let box_geo = geometry_box(x, y, z).map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
graphics_record_command(self.entity, DrawCommand::Geometry(box_geo))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn draw_geometry(&self, geometry: &Geometry) -> PyResult<()> {
graphics_record_command(self.entity, DrawCommand::Geometry(geometry.entity))
.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<()> {
transform_set_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<()> {
transform_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}")))
}