spitfire_draw/
utils.rs

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
use crate::context::DrawContext;
use bytemuck::{Pod, Zeroable};
use fontdue::Font;
use spitfire_fontdue::TextVertex;
use spitfire_glow::{
    graphics::{Graphics, Shader, Texture},
    renderer::{GlowVertexAttrib, GlowVertexAttribs},
};
use std::borrow::Cow;
use vek::Rgba;

#[derive(Debug, Copy, Clone, Pod, Zeroable)]
#[repr(C)]
pub struct Vertex {
    pub position: [f32; 2],
    pub uv: [f32; 3],
    pub color: [f32; 4],
}

impl Default for Vertex {
    fn default() -> Self {
        Self {
            position: Default::default(),
            uv: Default::default(),
            color: [1.0, 1.0, 1.0, 1.0],
        }
    }
}

impl GlowVertexAttribs for Vertex {
    const ATTRIBS: &'static [(&'static str, GlowVertexAttrib)] = &[
        (
            "a_position",
            GlowVertexAttrib::Float {
                channels: 2,
                normalized: false,
            },
        ),
        (
            "a_uv",
            GlowVertexAttrib::Float {
                channels: 3,
                normalized: false,
            },
        ),
        (
            "a_color",
            GlowVertexAttrib::Float {
                channels: 4,
                normalized: false,
            },
        ),
    ];
}

impl TextVertex<Rgba<f32>> for Vertex {
    fn apply(&mut self, position: [f32; 2], tex_coord: [f32; 3], user_data: Rgba<f32>) {
        self.position = position;
        self.uv = tex_coord;
        self.color = user_data.into_array();
    }
}

pub trait Drawable {
    fn draw(&self, context: &mut DrawContext, graphics: &mut Graphics<Vertex>);
}

#[derive(Debug, Clone)]
pub enum ResourceRef<T> {
    Name(Cow<'static, str>),
    Object(T),
}

impl<T> ResourceRef<T> {
    pub fn name(value: impl Into<Cow<'static, str>>) -> Self {
        Self::Name(value.into())
    }

    pub fn object(value: T) -> Self {
        Self::Object(value)
    }
}

impl<T> From<&'static str> for ResourceRef<T> {
    fn from(value: &'static str) -> Self {
        Self::name(value)
    }
}

pub type ShaderRef = ResourceRef<Shader>;
pub type TextureRef = ResourceRef<Texture>;

#[derive(Debug, Default, Clone)]
pub struct FontMap {
    keys: Vec<Cow<'static, str>>,
    values: Vec<Font>,
}

impl FontMap {
    pub fn insert(&mut self, name: impl Into<Cow<'static, str>>, font: Font) {
        let name = name.into();
        if let Some(index) = self.index_of(&name) {
            self.values[index] = font;
        } else {
            self.keys.push(name);
            self.values.push(font);
        }
    }

    pub fn remove(&mut self, name: &str) -> Option<Font> {
        if let Some(index) = self.index_of(name) {
            self.keys.remove(index);
            Some(self.values.remove(index))
        } else {
            None
        }
    }

    pub fn index_of(&self, name: &str) -> Option<usize> {
        self.keys.iter().position(|key| key == name)
    }

    pub fn get(&self, name: &str) -> Option<&Font> {
        if let Some(index) = self.index_of(name) {
            self.values.get(index)
        } else {
            None
        }
    }

    pub fn keys(&self) -> &[Cow<'static, str>] {
        &self.keys
    }

    pub fn values(&self) -> &[Font] {
        &self.values
    }

    pub fn iter(&self) -> impl Iterator<Item = (&Cow<'static, str>, &Font)> {
        self.keys.iter().zip(self.values.iter())
    }
}