wuple 0.4.0

Simple, Performant rendering on WGPU
Documentation
//! Structs for Drawing Shapes

use std::sync::Arc;

use crate::{
    builder::Shape,
    draw::{color, Color},
};

pub struct Painter {
    pub(crate) clear_color: Color,
    pub(crate) shapes: Vec<Arc<Shape>>,
}

impl Painter {
    pub fn new() -> Self {
        Self {
            clear_color: color::BLACK,
            shapes: Vec::new(),
        }
    }

    /// Change the Clear Color used
    pub fn clear_color(&mut self, color: Color) {
        self.clear_color = color;
    }

    pub fn draw(&mut self, shape: Shape) {
        self.shapes.push(Arc::new(shape));
    }

    pub fn draw_ref(&mut self, shape: Arc<Shape>) {
        self.shapes.push(shape);
    }

    /*/// Draw an ellipse
    pub fn ellipse(&mut self, pos: (f32, f32), radius: (f32, f32), res: u32, clr: Color) {
        if res < 3 {
            panic!("Failed to draw ellipse, Resolution can't be less then 3");
        }

        let clr = [clr.r, clr.g, clr.b];
        let mut vertices: Vec<Vertex> = vec![vertex([pos.0, pos.1, 0.0], clr)];
        let mut indices: Vec<u16> = Vec::new();

        for i in 0..res {
            let fi = (i as f32 / res as f32) * (PI * 2.0);
            let x = fi.cos() * radius.0 + pos.0;
            let y = fi.sin() * radius.1 + pos.1;
            vertices.push(vertex([x, y, 0.0], clr));
        }

        for i in 1..vertices.len() {
            indices.push(i as u16);
            indices.push(if i + 1 == vertices.len() {
                1
            } else {
                (i + 1) as u16
            });
            indices.push(0);
        }

        self.shapes.push(Shape::Mesh { vertices, indices });
    }

    /// Draw a rectangle
    pub fn rect(&mut self, pos0: (f32, f32), pos1: (f32, f32), clr: Color) {
        let clr = [clr.r, clr.g, clr.b];
        self.shapes.push(Shape::Mesh {
            vertices: vec![
                vertex([pos0.0, pos0.1, 0.0], clr),
                vertex([pos1.0, pos0.1, 0.0], clr),
                vertex([pos1.0, pos1.1, 0.0], clr),
                vertex([pos0.0, pos1.1, 0.0], clr),
            ],
            indices: vec![0, 1, 2, 2, 3, 0],
        });
    }

    /// Draw text on the screen
    ///
    /// 0. this function currently uses pixel coordinates
    /// 1. rendered text is in screen space
    pub fn text(&mut self, pos: (f32, f32), scale: f32, text: String, clr: Color) {
        self.shapes.push(Shape::Text {
            pos,
            text: vec![Text {
                string: text,
                scale,
                color: clr,
            }],
        });
    }

    /// Draw text on the screen
    ///
    /// 0. this function currently uses pixel coordinates
    /// 1. rendered text is in screen space
    pub fn text_raw(&mut self, pos: (f32, f32), text: Vec<Text>) {
        self.shapes.push(Shape::Text { pos, text });
    }

    // Draw a mesh from vertices and indices
    pub fn raw(&mut self, vertices: Vec<Vertex>, indices: Vec<u16>) {
        self.shapes.push(Shape::Mesh { vertices, indices })
    }*/
}