1use psx_gpu::display::DisplayConfig;
12
13use crate::label::Label;
14
15#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, Hash)]
26pub struct ScreenOffset<const RANGE: i8 = 16> {
27 x: i8,
28 y: i8,
29}
30
31impl<const RANGE: i8> ScreenOffset<RANGE> {
32 pub const CENTRE: Self = Self { x: 0, y: 0 };
34
35 pub const fn new(x: i8, y: i8) -> Self {
37 Self {
38 x: clamp(x, RANGE),
39 y: clamp(y, RANGE),
40 }
41 }
42
43 pub const fn is_centre(self) -> bool {
45 self.x == 0 && self.y == 0
46 }
47
48 pub const fn x(self) -> i8 {
50 self.x
51 }
52
53 pub const fn y(self) -> i8 {
55 self.y
56 }
57
58 pub const fn stepped_x(self, delta: i8) -> Self {
60 Self::new(add(self.x, delta), self.y)
61 }
62
63 pub const fn stepped_y(self, delta: i8) -> Self {
65 Self::new(self.x, add(self.y, delta))
66 }
67
68 pub const fn label_x(self) -> Label {
70 if self.x == 0 {
71 Label::word(b"CENTRE")
72 } else if self.x < 0 {
73 Label::toward(b"LEFT", self.x.unsigned_abs())
74 } else {
75 Label::toward(b"RIGHT", self.x.unsigned_abs())
76 }
77 }
78
79 pub const fn label_y(self) -> Label {
81 if self.y == 0 {
82 Label::word(b"CENTRE")
83 } else if self.y < 0 {
84 Label::toward(b"UP", self.y.unsigned_abs())
85 } else {
86 Label::toward(b"DOWN", self.y.unsigned_abs())
87 }
88 }
89
90 pub const fn to_bytes(self) -> [u8; 2] {
93 [self.x as u8, self.y as u8]
94 }
95
96 pub const fn from_bytes(bytes: [u8; 2]) -> Self {
99 Self::new(bytes[0] as i8, bytes[1] as i8)
100 }
101
102 pub const fn pixels(self) -> (i16, i16) {
105 (self.x as i16, self.y as i16)
106 }
107
108 pub const fn apply_to(self, display: DisplayConfig) -> DisplayConfig {
112 display.with_offset(self.pixels())
113 }
114}
115
116const fn clamp(value: i8, range: i8) -> i8 {
117 let range = if range < 0 { 0 } else { range };
118 if value < -range {
119 -range
120 } else if value > range {
121 range
122 } else {
123 value
124 }
125}
126
127const fn add(value: i8, delta: i8) -> i8 {
128 let sum = value as i16 + delta as i16;
129 if sum < i8::MIN as i16 {
130 i8::MIN
131 } else if sum > i8::MAX as i16 {
132 i8::MAX
133 } else {
134 sum as i8
135 }
136}
137
138#[cfg(test)]
139mod tests {
140 use super::ScreenOffset;
141 use psx_gpu::display::{DisplayConfig, Resolution, VideoMode};
142
143 #[test]
144 fn labels_read_centre_at_zero() {
145 let offset = ScreenOffset::<16>::CENTRE;
146 assert_eq!(offset.label_x().as_str(), "CENTRE");
147 assert_eq!(offset.label_y().as_str(), "CENTRE");
148 assert!(offset.is_centre());
149 let offset = ScreenOffset::<16>::new(16, -16);
150 assert_eq!(offset.label_x().as_str(), "RIGHT 16");
151 assert_eq!(offset.label_y().as_str(), "UP 16");
152 let offset = ScreenOffset::<16>::new(-1, 1);
153 assert_eq!(offset.label_x().as_str(), "LEFT 1");
154 assert_eq!(offset.label_y().as_str(), "DOWN 1");
155 }
156
157 #[test]
158 fn stepping_and_new_clamp_to_the_range() {
159 let mut offset = ScreenOffset::<16>::CENTRE;
160 for _ in 0..40 {
161 offset = offset.stepped_x(1).stepped_y(-1);
162 }
163 assert_eq!((offset.x(), offset.y()), (16, -16));
164 assert_eq!(
165 ScreenOffset::<16>::new(i8::MAX, i8::MIN).pixels(),
166 (16, -16)
167 );
168 assert_eq!(ScreenOffset::<16>::new(0, 0).stepped_x(i8::MAX).x(), 16);
169 assert_eq!(ScreenOffset::<24>::new(100, -100).pixels(), (24, -24));
170 assert_eq!(ScreenOffset::<0>::new(5, 5), ScreenOffset::<0>::CENTRE);
171 }
172
173 #[test]
174 fn the_save_bytes_match_wipeouts_and_survive_any_byte() {
175 for x in -16..=16i8 {
176 for y in [-16, -1, 0, 1, 16] {
177 let offset = ScreenOffset::<16>::new(x, y);
178 assert_eq!(offset.to_bytes(), [x as u8, y as u8]);
180 assert_eq!(ScreenOffset::<16>::from_bytes(offset.to_bytes()), offset);
181 }
182 }
183 for byte in 0..=u8::MAX {
184 let want = (byte as i8).clamp(-16, 16);
185 assert_eq!(
186 ScreenOffset::<16>::from_bytes([byte, byte]).pixels(),
187 (want as i16, want as i16)
188 );
189 }
190 }
191
192 #[test]
193 fn the_offset_moves_the_display_config() {
194 let display = DisplayConfig::new(VideoMode::Ntsc, Resolution::R320X240);
195 let moved = ScreenOffset::<16>::new(-4, 3).apply_to(display);
196 assert_eq!(moved.offset, (-4, 3));
197 assert_eq!(moved.resolution, display.resolution);
198 assert_eq!(ScreenOffset::<16>::CENTRE.apply_to(display), display);
199 }
200}