roast2d_internal 0.3.0-alpha.4

Roast2D internal crate
Documentation
use std::{fmt::Debug, rc::Rc};

use glam::Vec2;
use image::{DynamicImage, GenericImageView};

use crate::{
    color::Color,
    handle::Handle,
    prelude::Transform,
    renderer::traits::SpriteRenderer,
    sprite::Sprite,
    types::{DrawContent, Rect},
};

#[derive(Debug)]
pub enum Command {
    CreateTexture(CreateTexture),
    CreateRawTexture(CreateRawTexture),
    RemoveTexture(RemoveTexture),
    Draw(Draw),
    DrawUi(Draw),
}

pub struct CreateRawTexture {
    pub handle: Handle,
    pub texture: wgpu::Texture,
}

impl Debug for CreateRawTexture {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CreateRawTextureCommand")
            .field("data", &self.texture.size())
            .finish()
    }
}

pub struct CreateTexture {
    pub handle: Handle,
    pub data: DynamicImage,
}

impl Debug for CreateTexture {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("CreateTextureCommand")
            .field("data", &self.data.dimensions())
            .finish()
    }
}

#[derive(Debug)]
pub struct RemoveTexture(pub u64);

pub struct Draw {
    pub renderer: Rc<dyn SpriteRenderer>,
    pub renderer_data: Option<Vec<u8>>,
    pub content: DrawContent,
    pub color: Color,
    pub src: Option<Rect>,
    pub transform: Transform,
    pub flip_x: bool,
    pub flip_y: bool,
    pub anchor: Vec2,
}

impl Debug for Draw {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DrawCommand")
            .field("renderer", &self.renderer.key())
            .field("content", &self.content)
            .field("color", &self.color)
            .field("src", &self.src)
            .field("transform", &self.transform)
            .field("flip_x", &self.flip_x)
            .field("flip_y", &self.flip_y)
            .field("anchor", &self.anchor)
            .finish()
    }
}

impl Draw {
    pub fn new(
        sprite: &Sprite,
        transform: Transform,
        renderer: Rc<dyn SpriteRenderer>,
        renderer_data: Option<Vec<u8>>,
    ) -> Self {
        Draw {
            renderer,
            renderer_data,
            content: DrawContent::Texture(sprite.texture.clone()),
            color: sprite.color,
            src: sprite.src.clone(),
            flip_x: sprite.flip_x,
            flip_y: sprite.flip_y,
            anchor: sprite.anchor,
            transform,
        }
    }
}