1use super::{Resettable, Settings};
2
3#[derive(Clone)]
4pub struct ObjectData<I, A, S> {
5 pub images: Vec<I>,
6 pub anchors: Vec<A>,
7 pub scale: S,
8}
9
10impl<I: Default, A: Default, S> ObjectData<I, A, S> {
11 fn new() -> Self
12 where
13 S: From<f32>,
14 {
15 Self {
16 images: Vec::new(),
17 anchors: Vec::new(),
18 scale: 1.0.into(),
19 }
20 }
21
22 fn image(&mut self, index: usize) -> &mut I {
23 if index >= self.images.len() {
24 self.images.resize_with(index + 1, Default::default);
25 }
26 &mut self.images[index]
27 }
28
29 fn anchor(&mut self, index: usize) -> &mut A {
30 if index >= self.anchors.len() {
31 self.anchors.resize_with(index + 1, Default::default);
32 }
33 &mut self.anchors[index]
34 }
35}
36
37#[derive(Clone)]
38pub struct ObjectSettings<I, A, S> {
39 pub objects: Vec<ObjectData<I, A, S>>,
40}
41
42impl<I: Default, A: Default, S: From<f32>> ObjectSettings<I, A, S> {
43 pub fn common() -> Self {
44 Self {
45 objects: Vec::new(),
46 }
47 }
48
49 fn at(&mut self, index: usize) -> &mut ObjectData<I, A, S> {
50 if index >= self.objects.len() {
51 self.objects.resize_with(index + 1, ObjectData::new);
52 }
53 &mut self.objects[index]
54 }
55}
56
57#[derive(Clone, Debug, PartialEq, Eq, Hash)]
58pub struct ObjectImageParameter(pub usize, pub usize);
59
60#[derive(Clone, Debug, PartialEq, Eq, Hash)]
61pub struct ObjectAnchorParameter(pub usize, pub usize);
62
63#[derive(Clone, Debug, PartialEq, Eq, Hash)]
64pub struct ObjectScaleParameter(pub usize);
65
66impl<I: Default, A: Default, S: From<f32>> Settings<ObjectImageParameter>
67 for ObjectSettings<I, A, S>
68{
69 type Value = I;
70
71 fn get_mut(&mut self, parameter: &ObjectImageParameter) -> &mut I {
72 let &ObjectImageParameter(o, i) = parameter;
73 self.at(o).image(i)
74 }
75}
76
77impl<I: Default, A: Default, S: From<f32>> Settings<ObjectAnchorParameter>
78 for ObjectSettings<I, A, S>
79{
80 type Value = A;
81
82 fn get_mut(&mut self, parameter: &ObjectAnchorParameter) -> &mut A {
83 let &ObjectAnchorParameter(o, a) = parameter;
84 self.at(o).anchor(a)
85 }
86}
87
88impl<I: Default, A: Default, S: From<f32>> Settings<ObjectScaleParameter>
89 for ObjectSettings<I, A, S>
90{
91 type Value = S;
92
93 fn get_mut(&mut self, parameter: &ObjectScaleParameter) -> &mut S {
94 let &ObjectScaleParameter(o) = parameter;
95 &mut self.at(o).scale
96 }
97}
98
99impl<I: Resettable, A: Resettable, S: Resettable> Resettable for ObjectSettings<I, A, S> {
100 fn reset(&mut self) {
101 for object in &mut self.objects {
102 for image in &mut object.images {
103 image.reset();
104 }
105 for anchor in &mut object.anchors {
106 anchor.reset();
107 }
108 object.scale.reset();
109 }
110 }
111}