Files
ab_glyph_rasterizer
adler
adler32
andrew
bitflags
bytemuck
byteorder
calloop
cfg_if
color_quant
crc32fast
crossbeam_channel
crossbeam_deque
crossbeam_epoch
crossbeam_utils
deflate
dlib
downcast_rs
draw_state
either
event_loop
float
fnv
gfx
gfx_core
gfx_device_gl
gfx_gl
gfx_graphics
gfx_texture
gif
gl
glutin
glutin_egl_sys
glutin_glx_sys
glutin_window
graphics
graphics_api_version
image
input
instant
interpolation
iovec
jpeg_decoder
lazy_static
lazycell
libc
libloading
lock_api
log
maybe_uninit
memchr
memmap2
memoffset
miniz_oxide
mio
mio_extras
net2
nix
nom
num_cpus
num_integer
num_iter
num_rational
num_traits
once_cell
osmesa_sys
owned_ttf_parser
parking_lot
parking_lot_core
percent_encoding
piston
piston_window
png
proc_macro2
quote
raw_window_handle
rayon
rayon_core
read_color
rusttype
same_file
scoped_threadpool
scoped_tls
scopeguard
serde
serde_derive
shader_version
shaders_graphics2d
colored
textured
textured_color
shared_library
slab
smallvec
smithay_client_toolkit
spin_sleep
syn
texture
tiff
ttf_parser
unicode_xid
vecmath
viewport
walkdir
wayland_client
wayland_commons
wayland_cursor
wayland_egl
wayland_protocols
wayland_sys
weezl
window
winit
x11_dl
xcursor
xdg
xml
  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
use std::cmp::min;

use Canvas;
use Drawable;

bitflags! {
    /// The Sides bitflag presents the sides of a rectangle
    pub struct Sides: u32 {
        /// The top side of the rectangle
        const TOP = 0b0001;
        /// The bottom side of the rectangle
        const BOTTOM = 0b0010;
        /// The left side of the rectangle
        const LEFT = 0b0100;
        /// The right side of the rectangle
        const RIGHT = 0b1000;
        /// All sides of the rectangle
        const ALL = Self::TOP.bits | Self::BOTTOM.bits | Self::LEFT.bits | Self::RIGHT.bits;
    }
}

/// A drawable object that represents a rectangle
pub struct Rectangle {
    /// Position of the top-left corner of rectangle
    pub pos: (usize, usize),
    /// The size of the rectangle to be drawn, the border will be contained within this size
    pub size: (usize, usize),
    /// The border that is drawn around the perimeter of the rectangle. It's arguments are
    /// thickness of border, color of border, sides that the border is drawn around, rounding size
    /// of the corners
    pub border: Option<(usize, [u8; 4], Sides, Option<usize>)>,
    /// The color of the fill (area) of the rectangle
    pub fill: Option<[u8; 4]>,
}

impl Rectangle {
    /// Creates a new Rectangle object
    pub fn new(
        pos: (usize, usize),
        size: (usize, usize),
        border: Option<(usize, [u8; 4], Sides, Option<usize>)>,
        fill: Option<[u8; 4]>,
    ) -> Rectangle {
        Rectangle {
            pos,
            size,
            border,
            fill,
        }
    }

    fn draw_borders(&self, canvas: &mut Canvas) {
        if let Some(border) = self.border {
            for i in 0..=border.0 {
                let rounding_space = if let Some(round_size) = border.3 {
                    if i < round_size {
                        round_size
                            - ((round_size as f32).powi(2) - ((round_size - i - 1) as f32).powi(2))
                                .sqrt()
                                .round() as usize
                    } else {
                        0
                    }
                } else {
                    0
                };

                // Top line
                if border.2.contains(Sides::TOP)
                    && canvas.width > rounding_space * 2
                    && self.pos.1 + i <= canvas.height - 1
                {
                    for x in self.pos.0 + rounding_space
                        ..=min(
                            self.pos.0 + self.size.0 - rounding_space - 1,
                            canvas.width - 1,
                        )
                    {
                        canvas.draw_point(x, self.pos.1 + i, border.1)
                    }
                }
                // Bottom line
                if border.2.contains(Sides::BOTTOM)
                    && canvas.width > rounding_space * 2
                    && self.pos.1 + self.size.1 - i - 1 <= canvas.height - 1
                {
                    for x in self.pos.0 + rounding_space
                        ..=min(
                            self.pos.0 + self.size.0 - rounding_space - 1,
                            canvas.width - 1,
                        )
                    {
                        canvas.draw_point(x, self.pos.1 + self.size.1 - i - 1, border.1)
                    }
                }
                // Left line
                if border.2.contains(Sides::LEFT)
                    && canvas.height > rounding_space * 2
                    && self.pos.0 + i <= canvas.width - 1
                {
                    for y in self.pos.1 + rounding_space
                        ..=min(
                            self.pos.1 + self.size.1 - rounding_space - 1,
                            canvas.height - 1,
                        )
                    {
                        canvas.draw_point(self.pos.0 + i, y, border.1)
                    }
                }
                // Right line
                if border.2.contains(Sides::RIGHT)
                    && canvas.height > rounding_space * 2
                    && self.pos.0 + self.size.0 - i - 1 <= canvas.width - 1
                {
                    for y in self.pos.1 + rounding_space
                        ..=min(
                            self.pos.1 + self.size.1 - rounding_space - 1,
                            canvas.height - 1,
                        )
                    {
                        canvas.draw_point(self.pos.0 + self.size.0 - i - 1, y, border.1)
                    }
                }
            }
        }
    }

    fn draw_area(&self, canvas: &mut Canvas) {
        if let Some(fill) = self.fill {
            let (area_pos, area_size) = self.measure_area();
            for y in area_pos.1..=min(area_pos.1 + area_size.1 - 1, canvas.height - 1) {
                for x in area_pos.0..=min(area_pos.0 + area_size.0 - 1, canvas.width - 1) {
                    canvas.draw_point(x, y, fill)
                }
            }
        }
    }

    fn measure_area(&self) -> ((usize, usize), (usize, usize)) {
        let (mut area_pos, mut area_size) = (self.pos, self.size);
        if let Some(border) = self.border {
            if border.2.contains(Sides::TOP) {
                area_pos.1 += border.0;
                area_size.1 -= border.0;
            }
            if border.2.contains(Sides::BOTTOM) {
                area_size.1 -= border.0;
            }
            if border.2.contains(Sides::LEFT) {
                area_pos.0 += border.0;
                area_size.0 -= border.0;
            }
            if border.2.contains(Sides::RIGHT) {
                area_size.0 -= border.0;
            }
        }
        (area_pos, area_size)
    }
}

impl Drawable for Rectangle {
    fn draw(&self, canvas: &mut Canvas) {
        self.draw_borders(canvas);
        self.draw_area(canvas);
    }
}