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
132 lines (112 loc) · 4.53 KB
/
graphics.rs
File metadata and controls
132 lines (112 loc) · 4.53 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
use bevy::prelude::Entity;
use processing::prelude::*;
use pyo3::exceptions::PyRuntimeError;
use pyo3::prelude::*;
use pyo3::types::PyAny;
use crate::glfw::GlfwContext;
#[pyclass(unsendable)]
pub struct Graphics {
glfw_ctx: GlfwContext,
surface: Entity,
}
#[pymethods]
impl Graphics {
#[new]
pub fn new(width: u32, height: u32) -> PyResult<Self> {
let glfw_ctx = GlfwContext::new(width, height)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
init().map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
let window_handle = glfw_ctx.get_window();
let display_handle = glfw_ctx.get_display();
let surface = surface_create(window_handle, display_handle, width, height, 1.0)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
Ok(Self { glfw_ctx, 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);
record_command(self.surface, DrawCommand::BackgroundColor(color))
.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);
record_command(self.surface, DrawCommand::Fill(color))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn no_fill(&self) -> PyResult<()> {
record_command(self.surface, 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);
record_command(self.surface, DrawCommand::StrokeColor(color))
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn no_stroke(&self) -> PyResult<()> {
record_command(self.surface, DrawCommand::NoStroke)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn stroke_weight(&self, weight: f32) -> PyResult<()> {
record_command(self.surface, 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<()> {
record_command(
self.surface,
DrawCommand::Rect { x, y, w, h, radii: [tl, tr, br, bl] },
)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))
}
pub fn run(&mut self, draw_fn: Option<Py<PyAny>>) -> PyResult<()> {
loop {
if !self.glfw_ctx.poll_events() {
break;
}
begin_draw(self.surface)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
if let Some(ref draw) = draw_fn {
Python::attach(|py| {
draw.call0(py).map_err(|e| PyRuntimeError::new_err(format!("{e}")))
})?;
}
end_draw(self.surface)
.map_err(|e| PyRuntimeError::new_err(format!("{e}")))?;
}
Ok(())
}
}
// 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}")))
}