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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! A GUI toolkit & 3D graphics renderer.
//!
//! ## Naming
//! The name is a combination of "bar" as in "foo bar baz qux" and "g" as in "graphics".  It is also fun to yell because "BARG!!!" sounds like "ARGH!!!!".
//!
//!

extern crate fonterator;

use fonterator::footile;

pub mod icons;
mod gui;
mod window;

pub use crate::gui::Gui;
pub use crate::window::*;

pub use fonterator::{
    FontGroup, PathOp,
    PathOp::*,
    PathOp::{Line, Move, Quad},
};

use footile::PixFmt;

/// Size of an image (width, height).
#[derive(Copy, Clone)]
pub struct Size(pub u16, pub u16);

/// Texture Coordinates (Mapped to a `PathOp`).
#[derive(Copy, Clone)]
pub struct TexCoord(pub f32, pub f32);

/// An Image
pub struct Image {
    plotter: footile::Plotter,
    raster: footile::RasterB<footile::Rgba8>,
}

impl Image {
    /// Create new Image.
    pub fn new(size: Size) -> Self {
        let (w, h) = (u32::from(size.0), u32::from(size.1));

        Image {
            plotter: footile::Plotter::new(w, h),
            raster: footile::RasterB::new(w, h),
        }
    }

    /// Get the size of the image.
    pub fn size(&self) -> Size {
        Size(self.raster.width() as u16, self.raster.height() as u16)
    }

    /// Clear the Image.
    pub unsafe fn clear_ptr(&mut self, pixels: *mut u8) {
        let len =
            self.raster.width() as usize * self.raster.height() as usize * 4;
        self.clear(std::slice::from_raw_parts_mut(pixels, len))
    }

    /// Clear the Image.
    pub fn clear(&mut self, pixels: &mut [u8]) {
        self.raster.clear(footile::Rgba8::as_slice_mut(pixels));
    }

    /// Draw a path a solid color (sRGBA).
    pub unsafe fn fill_ptr<'b, T>(&mut self, color: [u8; 4], path: T, pixels: *mut u8)
    where
        T: IntoIterator<Item = &'b PathOp>,
    {
        let len =
            self.raster.width() as usize * self.raster.height() as usize * 4;
        self.fill(color, path,
            std::slice::from_raw_parts_mut(pixels, len)
        )
    }

    /// Draw a path a solid color (sRGBA).
    pub unsafe fn stroke_ptr<'b, T>(
        &mut self,
        color: [u8; 4],
        path: T,
        pixels: *mut u8,
    ) where
        T: IntoIterator<Item = &'b PathOp>,
    {
        let len =
            self.raster.width() as usize * self.raster.height() as usize * 4;
        self.stroke(color, path,            std::slice::from_raw_parts_mut(pixels, len))
    }

    /// Draw a path a solid color (sRGBA).
    pub fn fill<'b, T>(&mut self, color: [u8; 4], path: T, pixels: &mut [u8])
    where
        T: IntoIterator<Item = &'b PathOp>,
    {
        let iter = path.into_iter();
        let color = footile::Rgba8::new(color[0], color[1], color[2], color[3]);

        self.raster.over(
            self.plotter.fill(iter, footile::FillRule::NonZero),
            color,
            footile::Rgba8::as_slice_mut(pixels),
        );
    }

    /// Draw a path a solid color (sRGBA).
    pub fn stroke<'b, T>(&mut self, color: [u8; 4], path: T, pixels: &mut [u8])
    where
        T: IntoIterator<Item = &'b PathOp>,
    {
        let iter = path.into_iter();
        let color = footile::Rgba8::new(color[0], color[1], color[2], color[3]);

        self.raster.over(
            self.plotter.stroke(iter),
            color,
            footile::Rgba8::as_slice_mut(pixels),
        );
    }

    /// Draw text.
    pub unsafe fn text_ptr(
        &mut self,
        color: [u8; 4],
        xysize: (f32, f32, f32),
        font: &FontGroup,
        text: &str,
        pixels: *mut u8,
    ) -> (f32, f32) {
        let len =
            self.raster.width() as usize * self.raster.height() as usize * 4;
        self.text(color, xysize, font, text,
            std::slice::from_raw_parts_mut(pixels, len)
        )
    }

    /// Draw text.
    pub fn text(
        &mut self,
        color: [u8; 4],
        xysize: (f32, f32, f32),
        font: &FontGroup,
        text: &str,
        pixels: &mut [u8],
    ) -> (f32, f32) {
        let color = footile::Rgba8::new(color[0], color[1], color[2], color[3]);

        // Render the text
        let mut path = font.render(
            text,                 /*text*/
            (xysize.0, xysize.1), /*position*/
            (xysize.2, xysize.2), /*size*/
        );

        self.raster.over(
            self.plotter.fill(&mut path, footile::FillRule::NonZero),
            color,
            footile::Rgba8::as_slice_mut(pixels),
        );

        let (cx, cy) = path.xy();

        (cx, cy)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn it_works() {
        assert_eq!(2 + 2, 4);
    }
}

// Initialize graphic shader.
pub fn init_toolbar(window: &mut Window) -> (Shader, Shape) {
    let mut gui = window.shader_new(shader!("gui"));

    // Define vertices.
    #[rustfmt::skip]
    let vertices = [
        -1.0, -1.0,  0.0, 1.0,
         1.0, -1.0,  1.0, 1.0,
         1.0,  1.0,  1.0, 0.0,

        -1.0,  1.0,  0.0, 0.0,
        -1.0, -1.0,  0.0, 1.0,
         1.0,  1.0,  1.0, 0.0,
    ];

    // Build cube Shape
    let mut rect = window.shape_new(ShapeBuilder::new(&mut gui).vert(&vertices).face(Transform::new()));
    window.instances(&mut rect, &[Transform::new()]);
    window.build(&mut gui);

    (gui, rect)
}