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
pub mod mutation;
pub mod shape_box;

use crate::drawing::Renderable;
use crate::prelude::collection::shape_box::FromDrawable;
use crate::prelude::*;
use crate::shapes::collection::shape_box::{FromShape, ShapeBox};
use crate::Graphics;
use graphics_shapes::coord::Coord;
use graphics_shapes::rect::Rect;
use graphics_shapes::Shape;
use std::fmt::Debug;
use std::slice::Iter;

pub mod prelude {
    pub use crate::shapes::collection::shape_box::*;
    pub use crate::shapes::collection::*;
}

#[derive(Debug, Clone)]
pub struct ShapeCollection {
    shapes: Vec<ShapeBox>,
    bounds: Rect,
}

pub fn calc_bounds(list: &[ShapeBox]) -> Rect {
    if list.is_empty() {
        return Rect::new((0, 0), (0, 0));
    }
    let mut bounds = Rect::new(
        (list[0].left(), list[0].top()),
        (list[0].right(), list[0].bottom()),
    );
    for shape in list.iter().skip(1) {
        bounds = Rect::new(
            (
                bounds.left().min(shape.left()),
                bounds.top().min(shape.top()),
            ),
            (
                bounds.right().max(shape.right()),
                bounds.bottom().max(shape.bottom()),
            ),
        );
    }
    bounds
}

impl ShapeCollection {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        ShapeCollection {
            shapes: vec![],
            bounds: Rect::new((0, 0), (0, 0)),
        }
    }
}

impl ShapeCollection {
    pub fn iter(&self) -> Iter<'_, ShapeBox> {
        self.shapes.iter()
    }

    pub fn len(&self) -> usize {
        self.shapes.len()
    }

    pub fn is_empty(&self) -> bool {
        self.shapes.is_empty()
    }

    fn update_bounds(&mut self) {
        self.bounds = calc_bounds(&self.shapes);
    }

    pub fn remove(&mut self, idx: usize) -> ShapeBox {
        self.shapes.remove(idx)
    }

    pub fn left(&self) -> isize {
        self.bounds.left()
    }

    pub fn top(&self) -> isize {
        self.bounds.top()
    }

    pub fn bottom(&self) -> isize {
        self.bounds.bottom()
    }

    pub fn right(&self) -> isize {
        self.bounds.right()
    }

    pub fn center(&self) -> Coord {
        self.bounds.center()
    }
}

impl Renderable<ShapeCollection> for ShapeCollection {
    fn render(&self, graphics: &mut Graphics) {
        for shape in &self.shapes {
            shape.render(graphics);
        }
    }
}

impl<S: Clone> InsertDrawable<S> for ShapeCollection
where
    ShapeBox: FromDrawable<S>,
{
    fn insert(&mut self, index: usize, drawable: Drawable<S>) {
        InsertShapeBox::insert(self, index, ShapeBox::from_drawable(drawable));
    }

    fn insert_above(&mut self, drawable: Drawable<S>) {
        InsertShapeBox::insert_above(self, ShapeBox::from_drawable(drawable));
    }

    fn insert_under(&mut self, drawable: Drawable<S>) {
        InsertShapeBox::insert_under(self, ShapeBox::from_drawable(drawable));
    }
}

impl<S> InsertShape<S> for ShapeCollection
where
    ShapeBox: FromShape<S>,
{
    fn insert(&mut self, index: usize, shape: S, draw_type: DrawType) {
        InsertShapeBox::insert(self, index, ShapeBox::from_shape(shape, draw_type));
    }

    fn insert_above(&mut self, shape: S, draw_type: DrawType) {
        InsertShapeBox::insert_above(self, ShapeBox::from_shape(shape, draw_type));
    }

    fn insert_under(&mut self, shape: S, draw_type: DrawType) {
        InsertShapeBox::insert_under(self, ShapeBox::from_shape(shape, draw_type));
    }
}

impl InsertShapeBox for ShapeCollection {
    fn insert(&mut self, index: usize, shape_box: ShapeBox) {
        self.shapes.insert(index, shape_box);
        self.update_bounds();
    }

    fn insert_above(&mut self, shape_box: ShapeBox) {
        self.shapes.push(shape_box);
        self.update_bounds();
    }

    fn insert_under(&mut self, shape_box: ShapeBox) {
        InsertShapeBox::insert(self, 0, shape_box);
        self.update_bounds();
    }
}

pub trait InsertDrawable<S: Clone>: InsertShapeBox {
    fn insert(&mut self, index: usize, drawable: Drawable<S>);
    fn insert_above(&mut self, drawable: Drawable<S>);
    fn insert_under(&mut self, drawable: Drawable<S>);
}

pub trait InsertShape<S> {
    fn insert(&mut self, index: usize, shape: S, draw_type: DrawType);
    fn insert_above(&mut self, shape: S, draw_type: DrawType);
    fn insert_under(&mut self, shape: S, draw_type: DrawType);
}

pub trait InsertShapeBox {
    fn insert(&mut self, index: usize, shape_box: ShapeBox);
    fn insert_above(&mut self, shape_box: ShapeBox);
    fn insert_under(&mut self, shape_box: ShapeBox);
}