rsgt 0.3.0

Rust simple GUI Toolkit
//========================================================================
// RSGT | wgpu/mod.rs - https://overtimecoder.github.io
//------------------------------------------------------------------------
// wgpu utility for RSGT
//------------------------------------------------------------------------
//
// Author LatteS
//
// File was created in 2023/01/07
//
//========================================================================

use crate::theme::ComponentColor;

pub fn parse_color(color:ComponentColor) -> wgpu::Color {
    match color {
        ComponentColor::White => {wgpu::Color::WHITE}
        ComponentColor::WhiteGray => {wgpu::Color{r:0.85,g:0.85,b:0.85,a:1.0}}
        ComponentColor::Gray => {wgpu::Color{r:0.5,g:0.5,b:0.5,a:1.0}}
        ComponentColor::Black => {wgpu::Color::BLACK}
        ComponentColor::Red => {wgpu::Color::RED}
        ComponentColor::Green => {wgpu::Color::GREEN}
        ComponentColor::Blue => {wgpu::Color::BLUE}
        ComponentColor::CustomColor(r, g, b, a) => {wgpu::Color {r,g,b,a}}

    }
}

pub struct Vec4<T>(pub T,pub T,pub T,pub T);

pub struct Fragment {
    color: Vec4<f32>
}

pub struct Vertex {
    vertex: Vec4<f32>
}

pub struct WGSL {
    vertex:Vertex,
    fragment:Fragment
}

impl WGSL {
    pub fn new() -> Self {
        Default::default()
    }
    
    pub fn vertex(self,vertex:Vertex) -> Self {
        Self {
            vertex,
            fragment: self.fragment,
        }
    }

    pub fn fragment(self,fragment:Fragment) -> Self {
        Self {
            vertex:self.vertex,
            fragment
        }
    }

    pub fn build(&self) -> &str {
       stringify!(
           @vertex
            fn vs_main(@builtin(vertex_index) in_vertex_index: u32) -> @builtin(position) vec4<f32> {
                let x = f32(i32(in_vertex_index) - 1);
                let y = f32(i32(in_vertex_index & 1u) * 2 - 1);

                return vec4<f32>(x,y, 1.0, 1.0);
            }

            @fragment
            fn fs_main() -> @location(0) vec4<f32> {
                return vec4<f32>(1.0, 0.0, 1.0, 1.0);
            }
       )
    }
}

impl Default for WGSL {
    fn default() -> Self {
        Self {
            vertex: Vertex {
                vertex: Vec4(0.0,0.0,0.0,0.0),
            },
            fragment: Fragment {
                color: Vec4(0.0,0.0,0.0,0.0)
            },
        }
    }
}