ssruntime/eval/
z_order.rs1use alloc::vec::Vec;
2
3use crate::eval::framedata::CachedPartData;
4use crate::runtime::framedata::ss::runtime::PartState;
5
6pub struct ZOrderTable {
10 indices: Vec<usize>,
11 is_static: bool,
12 computed: bool,
13 animation: i32,
15}
16
17impl Default for ZOrderTable {
18 fn default() -> Self {
19 Self::new()
20 }
21}
22
23impl ZOrderTable {
24 pub fn new() -> Self {
25 Self { indices: Vec::new(), is_static: false, computed: false, animation: -1 }
26 }
27
28 pub fn setup(&mut self, animation: i32, cached_parts: &[CachedPartData]) {
32 let num_parts = cached_parts.len();
33 self.indices.clear();
34 self.indices.extend(0..num_parts);
35 self.animation = animation;
36
37 self.is_static = !cached_parts.iter().any(|p| p.run(crate::eval::framedata::IDX_ATTRIBUTEPRIO).is_some_and(|run| run.count() > 1));
39 if self.is_static && !cached_parts.iter().any(|p| p.run(crate::eval::framedata::IDX_ATTRIBUTEPRIO).is_some()) {
40 self.indices.sort_by_key(|&i| cached_parts[i].part_index);
42 self.computed = true;
43 } else {
44 self.computed = false;
45 }
46 }
47
48 pub fn fit(&mut self, animation: i32, cached_parts: &[CachedPartData]) {
51 if self.animation != animation {
52 self.setup(animation, cached_parts);
53 }
54 }
55
56 pub fn update(&mut self, parts: &[PartState]) {
57 if self.is_static && self.computed {
58 return;
59 }
60
61 let num_parts = parts.len();
62 if self.indices.len() != num_parts {
63 self.indices.clear();
64 self.indices.extend(0..num_parts);
65 } else {
66 for (i, v) in self.indices.iter_mut().enumerate() {
67 *v = i;
68 }
69 }
70
71 self.indices.sort_by(|&a, &b| {
72 let pa = &parts[a];
73 let pb = &parts[b];
74 pa.priority().cmp(&pb.priority()).then_with(|| pa.part_index().cmp(&pb.part_index()))
75 });
76
77 self.computed = true;
78 }
79
80 pub fn indices(&self) -> &[usize] {
83 &self.indices
84 }
85}
86
87#[cfg(test)]
88mod tests {
89 use super::*;
90 use crate::eval::framedata::cached_part_stub;
91 use crate::eval::keyframes::KeyRun;
92
93 fn parts(runs: &[Option<KeyRun>]) -> Vec<CachedPartData> {
94 runs.iter()
95 .enumerate()
96 .map(|(i, run)| {
97 let mut p = cached_part_stub(i as i16, -1, ssab::ssab::ss::format::PartType::PartTypeNormal, false);
98 if let Some(run) = run {
99 p.key_mask = 1 << crate::eval::framedata::IDX_ATTRIBUTEPRIO;
100 p.runs = alloc::boxed::Box::new([*run]);
101 }
102 p
103 })
104 .collect()
105 }
106
107 fn states(priorities: &[i32]) -> Vec<PartState> {
108 priorities
109 .iter()
110 .enumerate()
111 .map(|(i, &p)| {
112 let mut s = PartState::default();
113 s.set_part_index(i as i16);
114 s.set_priority(p);
115 s
116 })
117 .collect()
118 }
119
120 #[test]
121 fn a_constant_priority_is_sorted_once() {
122 let one = Some(KeyRun::new(0, 1));
123 let mut table = ZOrderTable::new();
124 table.setup(0, &parts(&[one, one, one]));
125 table.update(&states(&[2, 0, 1]));
126 assert_eq!(table.indices(), &[1, 2, 0]);
127 table.update(&states(&[0, 1, 2]));
129 assert_eq!(table.indices(), &[1, 2, 0]);
130 }
131
132 #[test]
133 fn an_animated_priority_is_sorted_every_frame() {
134 let one = Some(KeyRun::new(0, 1));
135 let mut table = ZOrderTable::new();
136 table.setup(0, &parts(&[one, Some(KeyRun::new(1, 2)), one]));
137 table.update(&states(&[2, 0, 1]));
138 assert_eq!(table.indices(), &[1, 2, 0]);
139 table.update(&states(&[0, 3, 1]));
140 assert_eq!(table.indices(), &[0, 2, 1]);
141 }
142
143 #[test]
144 fn another_animation_is_set_up_again() {
145 let one = Some(KeyRun::new(0, 1));
146 let p = parts(&[one, one, one]);
147 let mut table = ZOrderTable::new();
148 table.setup(0, &p);
149 table.update(&states(&[2, 0, 1]));
150 table.fit(0, &p);
151 table.update(&states(&[0, 1, 2]));
152 assert_eq!(table.indices(), &[1, 2, 0], "same animation: the order stands");
153 table.fit(1, &p);
154 table.update(&states(&[0, 1, 2]));
155 assert_eq!(table.indices(), &[0, 1, 2], "another animation: sorted again");
156 }
157}