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
174 lines (150 loc) · 5.13 KB
/
graphics.rs
File metadata and controls
174 lines (150 loc) · 5.13 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
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(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) -> 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}")))?;
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 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 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}")))
}
}
// 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}")))
}