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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use crate::drawable::Drawable;
use crate::shapes::collection::AutoShapeCollection;
use crate::shapes::collection::ShapeCollection;
use crate::shapes::CreateDrawable;
use graphics_shapes::coord::Coord;
use graphics_shapes::Shape;
use std::collections::HashMap;
use std::hash::Hash;

fn with_translation<K: Eq + PartialEq + Hash + Clone, P: Into<Coord>, T: Shape + Clone>(
    shapes: &HashMap<K, Drawable<T>>,
    delta: P,
) -> HashMap<K, Drawable<T>>
where
    Drawable<T>: CreateDrawable<T>,
{
    let delta = delta.into();
    shapes
        .iter()
        .map(|(key, shape)| (key.clone(), shape.with_translation(delta)))
        .collect()
}

fn with_move<K: Eq + PartialEq + Hash + Clone, P: Into<Coord>, T: Shape + Clone>(
    shapes: &HashMap<K, Drawable<T>>,
    delta: P,
) -> HashMap<K, Drawable<T>>
where
    Drawable<T>: CreateDrawable<T>,
{
    let delta = delta.into();
    shapes
        .iter()
        .map(|(key, shape)| (key.clone(), shape.with_move(delta)))
        .collect()
}

fn with_scale<K: Eq + PartialEq + Hash + Clone, T: Shape + Clone>(
    shapes: &HashMap<K, Drawable<T>>,
    delta: f32,
) -> HashMap<K, Drawable<T>>
where
    Drawable<T>: CreateDrawable<T>,
{
    shapes
        .iter()
        .map(|(key, shape)| (key.clone(), shape.with_scale(delta)))
        .collect()
}

fn with_rotation<K: Eq + PartialEq + Hash + Clone, T: Shape + Clone>(
    shapes: &HashMap<K, Drawable<T>>,
    degrees: isize,
) -> HashMap<K, Drawable<T>>
where
    Drawable<T>: CreateDrawable<T>,
{
    shapes
        .iter()
        .map(|(key, shape)| (key.clone(), shape.with_rotation(degrees)))
        .collect()
}

fn with_scale_around<K: Eq + PartialEq + Hash + Clone, T: Shape + Clone, P: Into<Coord>>(
    shapes: &HashMap<K, Drawable<T>>,
    delta: f32,
    center: P,
) -> HashMap<K, Drawable<T>>
where
    Drawable<T>: CreateDrawable<T>,
{
    let center = center.into();
    shapes
        .iter()
        .map(|(key, shape)| (key.clone(), shape.with_scale_around(delta, center)))
        .collect()
}

fn with_rotation_around<K: Eq + PartialEq + Hash + Clone, T: Shape + Clone, P: Into<Coord>>(
    shapes: &HashMap<K, Drawable<T>>,
    delta: isize,
    center: P,
) -> HashMap<K, Drawable<T>>
where
    Drawable<T>: CreateDrawable<T>,
{
    let center = center.into();
    shapes
        .iter()
        .map(|(key, shape)| (key.clone(), shape.with_rotation_around(delta, center)))
        .collect()
}

macro_rules! impl_coord_mutator {
    ($list_method: ident, $param_name: ident) => {
        impl<K: Eq + PartialEq + Hash + Clone> ShapeCollection<K> {
            pub fn $list_method<P: Into<Coord>>(&self, $param_name: P) -> Self {
                let $param_name = $param_name.into();
                Self {
                    rects: $list_method(&self.rects(), $param_name),
                    circles: $list_method(&self.circles(), $param_name),
                    triangles: $list_method(&self.triangles(), $param_name),
                    lines: $list_method(&self.lines(), $param_name),
                    polygons: $list_method(&self.polygons(), $param_name),
                    ellipses: $list_method(&self.ellipses(), $param_name),
                }
            }
        }
    };
}

macro_rules! impl_number_mutator {
    ($list_method: ident, $param_name: ident, $param_type: ty) => {
        impl<K: Eq + PartialEq + Hash + Clone> ShapeCollection<K> {
            pub fn $list_method(&self, $param_name: $param_type) -> Self {
                Self {
                    rects: $list_method(&self.rects(), $param_name),
                    circles: $list_method(&self.circles(), $param_name),
                    triangles: $list_method(&self.triangles(), $param_name),
                    lines: $list_method(&self.lines(), $param_name),
                    polygons: $list_method(&self.polygons(), $param_name),
                    ellipses: $list_method(&self.ellipses(), $param_name),
                }
            }
        }
    };
}

macro_rules! impl_number_coord_mutator {
    ($list_method: ident, $param_name: ident, $param_type: ty) => {
        impl<K: Eq + PartialEq + Hash + Clone> ShapeCollection<K> {
            pub fn $list_method<P: Into<Coord>>(
                &self,
                $param_name: $param_type,
                center: P,
            ) -> Self {
                let center = center.into();
                Self {
                    rects: $list_method(&self.rects(), $param_name, center),
                    circles: $list_method(&self.circles(), $param_name, center),
                    triangles: $list_method(&self.triangles(), $param_name, center),
                    lines: $list_method(&self.lines(), $param_name, center),
                    polygons: $list_method(&self.polygons(), $param_name, center),
                    ellipses: $list_method(&self.ellipses(), $param_name, center),
                }
            }
        }
    };
}

impl_coord_mutator!(with_translation, delta);
impl_coord_mutator!(with_move, xy);
impl_number_mutator!(with_scale, factor, f32);
impl_number_mutator!(with_rotation, degrees, isize);
impl_number_coord_mutator!(with_rotation_around, degrees, isize);
impl_number_coord_mutator!(with_scale_around, factor, f32);

macro_rules! impl_coord_mutator_auto {
    ($list_method: ident, $param_name: ident) => {
        impl AutoShapeCollection {
            pub fn $list_method<P: Into<Coord>>(&self, $param_name: P) -> Self {
                let $param_name = $param_name.into();
                Self {
                    next_id: self.next_id,
                    rects: $list_method(&self.rects(), $param_name),
                    circles: $list_method(&self.circles(), $param_name),
                    triangles: $list_method(&self.triangles(), $param_name),
                    lines: $list_method(&self.lines(), $param_name),
                    polygons: $list_method(&self.polygons(), $param_name),
                    ellipses: $list_method(&self.ellipses(), $param_name),
                }
            }
        }
    };
}

macro_rules! impl_number_mutator_auto {
    ($list_method: ident, $param_name: ident, $param_type: ty) => {
        impl AutoShapeCollection {
            pub fn $list_method(&self, $param_name: $param_type) -> Self {
                Self {
                    next_id: self.next_id,
                    rects: $list_method(&self.rects(), $param_name),
                    circles: $list_method(&self.circles(), $param_name),
                    triangles: $list_method(&self.triangles(), $param_name),
                    lines: $list_method(&self.lines(), $param_name),
                    polygons: $list_method(&self.polygons(), $param_name),
                    ellipses: $list_method(&self.ellipses(), $param_name),
                }
            }
        }
    };
}

macro_rules! impl_number_coord_mutator_auto {
    ($list_method: ident, $param_name: ident, $param_type: ty) => {
        impl AutoShapeCollection {
            pub fn $list_method<P: Into<Coord>>(
                &self,
                $param_name: $param_type,
                center: P,
            ) -> Self {
                let center = center.into();
                Self {
                    next_id: self.next_id,
                    rects: $list_method(&self.rects(), $param_name, center),
                    circles: $list_method(&self.circles(), $param_name, center),
                    triangles: $list_method(&self.triangles(), $param_name, center),
                    lines: $list_method(&self.lines(), $param_name, center),
                    polygons: $list_method(&self.polygons(), $param_name, center),
                    ellipses: $list_method(&self.ellipses(), $param_name, center),
                }
            }
        }
    };
}

impl_coord_mutator_auto!(with_translation, delta);
impl_coord_mutator_auto!(with_move, xy);
impl_number_mutator_auto!(with_scale, factor, f32);
impl_number_mutator_auto!(with_rotation, degrees, isize);
impl_number_coord_mutator_auto!(with_rotation_around, degrees, isize);
impl_number_coord_mutator_auto!(with_scale_around, factor, f32);