|
| 1 | +use bevy::prelude::Entity; |
| 2 | +use processing::prelude::*; |
| 3 | +use pyo3::exceptions::PyRuntimeError; |
| 4 | +use pyo3::prelude::*; |
| 5 | +use pyo3::types::PyAny; |
| 6 | + |
| 7 | +use crate::glfw::GlfwContext; |
| 8 | + |
| 9 | +#[pyclass(unsendable)] |
| 10 | +pub struct Graphics { |
| 11 | + glfw_ctx: GlfwContext, |
| 12 | + surface: Entity, |
| 13 | +} |
| 14 | + |
| 15 | +#[pymethods] |
| 16 | +impl Graphics { |
| 17 | + #[new] |
| 18 | + pub fn new(width: u32, height: u32) -> PyResult<Self> { |
| 19 | + let glfw_ctx = GlfwContext::new(width, height) |
| 20 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 21 | + |
| 22 | + init().map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 23 | + |
| 24 | + let window_handle = glfw_ctx.get_window(); |
| 25 | + let display_handle = glfw_ctx.get_display(); |
| 26 | + let surface = surface_create(window_handle, display_handle, width, height, 1.0) |
| 27 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 28 | + |
| 29 | + Ok(Self { glfw_ctx, surface }) |
| 30 | + } |
| 31 | + |
| 32 | + pub fn background(&self, args: Vec<f32>) -> PyResult<()> { |
| 33 | + let (r, g, b, a) = parse_color(&args)?; |
| 34 | + let color = bevy::color::Color::srgba(r, g, b, a); |
| 35 | + graphics_record_command(self.surface, DrawCommand::BackgroundColor(color)) |
| 36 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn fill(&self, args: Vec<f32>) -> PyResult<()> { |
| 40 | + let (r, g, b, a) = parse_color(&args)?; |
| 41 | + let color = bevy::color::Color::srgba(r, g, b, a); |
| 42 | + graphics_record_command(self.surface, DrawCommand::Fill(color)) |
| 43 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 44 | + } |
| 45 | + |
| 46 | + pub fn no_fill(&self) -> PyResult<()> { |
| 47 | + graphics_record_command(self.surface, DrawCommand::NoFill) |
| 48 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 49 | + } |
| 50 | + |
| 51 | + pub fn stroke(&self, args: Vec<f32>) -> PyResult<()> { |
| 52 | + let (r, g, b, a) = parse_color(&args)?; |
| 53 | + let color = bevy::color::Color::srgba(r, g, b, a); |
| 54 | + graphics_record_command(self.surface, DrawCommand::StrokeColor(color)) |
| 55 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 56 | + } |
| 57 | + |
| 58 | + pub fn no_stroke(&self) -> PyResult<()> { |
| 59 | + graphics_record_command(self.surface, DrawCommand::NoStroke) |
| 60 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 61 | + } |
| 62 | + |
| 63 | + pub fn stroke_weight(&self, weight: f32) -> PyResult<()> { |
| 64 | + graphics_record_command(self.surface, DrawCommand::StrokeWeight(weight)) |
| 65 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 66 | + } |
| 67 | + |
| 68 | + pub fn rect(&self, x: f32, y: f32, w: f32, h: f32, tl: f32, tr: f32, br: f32, bl: f32) -> PyResult<()> { |
| 69 | + graphics_record_command( |
| 70 | + self.surface, |
| 71 | + DrawCommand::Rect { x, y, w, h, radii: [tl, tr, br, bl] }, |
| 72 | + ) |
| 73 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 74 | + } |
| 75 | + |
| 76 | + pub fn run(&mut self, draw_fn: Option<Py<PyAny>>) -> PyResult<()> { |
| 77 | + loop { |
| 78 | + if !self.glfw_ctx.poll_events() { |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + graphics_begin_draw(self.surface) |
| 83 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 84 | + |
| 85 | + if let Some(ref draw) = draw_fn { |
| 86 | + Python::attach(|py| { |
| 87 | + draw.call0(py).map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 88 | + })?; |
| 89 | + } |
| 90 | + |
| 91 | + graphics_end_draw(self.surface) |
| 92 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}")))?; |
| 93 | + } |
| 94 | + Ok(()) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +// TODO: a real color type. or color parser? idk. color is confusing. let's think |
| 99 | +// about how to expose different color spaces in an idiomatic pythonic way |
| 100 | +fn parse_color(args: &[f32]) -> PyResult<(f32, f32, f32, f32)> { |
| 101 | + match args.len() { |
| 102 | + 1 => { |
| 103 | + let v = args[0] / 255.0; |
| 104 | + Ok((v, v, v, 1.0)) |
| 105 | + } |
| 106 | + 2 => { |
| 107 | + let v = args[0] / 255.0; |
| 108 | + Ok((v, v, v, args[1] / 255.0)) |
| 109 | + } |
| 110 | + 3 => Ok((args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, 1.0)), |
| 111 | + 4 => Ok((args[0] / 255.0, args[1] / 255.0, args[2] / 255.0, args[3] / 255.0)), |
| 112 | + _ => Err(PyRuntimeError::new_err("color requires 1-4 arguments")), |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +pub fn get_graphics<'py>(module: &Bound<'py, PyModule>) -> PyResult<PyRef<'py, Graphics>> { |
| 117 | + module |
| 118 | + .getattr("_graphics")? |
| 119 | + .cast_into::<Graphics>() |
| 120 | + .map_err(|_| PyRuntimeError::new_err("no graphics context"))? |
| 121 | + .try_borrow() |
| 122 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 123 | +} |
| 124 | + |
| 125 | +pub fn get_graphics_mut<'py>(module: &Bound<'py, PyModule>) -> PyResult<PyRefMut<'py, Graphics>> { |
| 126 | + module |
| 127 | + .getattr("_graphics")? |
| 128 | + .cast_into::<Graphics>() |
| 129 | + .map_err(|_| PyRuntimeError::new_err("no graphics context"))? |
| 130 | + .try_borrow_mut() |
| 131 | + .map_err(|e| PyRuntimeError::new_err(format!("{e}"))) |
| 132 | +} |
0 commit comments