forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

transform_ui.rs137 lines, 5.4 KiBHistoryRawDownload
1//! The transform card's numbers -- [`crate::color_ui`]'s twin for where a
2//! clip sits instead of what it looks like.
3
4use crate::*;
5
6/// The transform card's eight controls, in the order it lists them: what each
7/// is called and the range it moves in. The order is `TransformParams`' own,
8/// which is what [`transform_band_mut`] indexes. Rotation's range is one turn
9/// short (`0..270`) because 360 is the same picture as 0 and a slider with two
10/// ends that read the same is not a range, it is a cycle
11/// ([`ROTATE_BAND`]/[`rotate_key`]).
12pub(crate) const TRANSFORM_BANDS: [(&str, f32, f32); 8] = [
13 ("Position X", -1., 1.),
14 ("Position Y", -1., 1.),
15 ("Scale", 0.1, 4.),
16 ("Rotation", 0., 270.),
17 ("Crop left", 0., 0.45),
18 ("Crop right", 0., 0.45),
19 ("Crop top", 0., 0.45),
20 ("Crop bottom", 0., 0.45),
21];
22
23/// Which band is rotation: the one control on the card that wraps at its ends
24/// instead of clamping, and steps by [`ROTATE_STEP`] rather than
25/// [`TRANSFORM_STEP`] -- the engine only ever renders a 90-degree turn
26/// ([`crate::transform::TransformParams::rotate`]), so a finer grid on this
27/// one row would be numbers nothing on screen answers to.
28pub(crate) const ROTATE_BAND: usize = 3;
29
30/// Which band is scale: the one control on the card whose unit is a
31/// multiplier (`×`) rather than a percent of frame or a degree.
32pub(crate) const SCALE_BAND: usize = 2;
33
34/// A press of a nudge key on every band but rotation: a fortieth-ish of a
35/// band's range, [`crate::color_ui::COLOR_STEP`]'s own reason. A drag lands on
36/// the same grid ([`Player::drag_transform`]).
37pub(crate) const TRANSFORM_STEP: f32 = 0.05;
38
39/// Rotation's own step: one 90-degree turn, [`ROTATE_BAND`]'s reason.
40pub(crate) const ROTATE_STEP: f32 = 90.;
41
42/// The card's width, [`crate::color_ui::COLOR_W`]'s own measure against this
43/// card's longest label ("Position X" is short, but the value column is wider
44/// here for the crop rows' extra digit).
45pub(crate) const TRANSFORM_W: f32 = 460.;
46
47/// The slider's own range for a band: rotation reads as a signed turn,
48/// -180..180, so 0 degrees sits mid-track (stored 0..270 -- `-90` is 270).
49pub(crate) fn slider_range(band: usize) -> (f32, f32) {
50 match band {
51 ROTATE_BAND => (-180., 180.),
52 _ => {
53 let (_, low, high) = TRANSFORM_BANDS[band];
54 (low, high)
55 }
56 }
57}
58
59/// Where a band's knob sits for `value` (0..1), through [`slider_range`] --
60/// the one mapping the row draws and a drag reads back.
61pub(crate) fn knob_fraction(band: usize, value: f32) -> f32 {
62 let (low, high) = slider_range(band);
63 let value = match band {
64 ROTATE_BAND if value > 180. => value - 360.,
65 _ => value,
66 };
67 crate::ui::widgets::slider_fraction(value, low, high)
68}
69
70/// A slider value on the band's own grid: [`ROTATE_BAND`] snaps to
71/// [`ROTATE_STEP`] and wraps into its range, every other band snaps to
72/// [`TRANSFORM_STEP`] and clamps into its own -- what a drag rounds to, so the
73/// pointer stops where the arrow keys do.
74pub(crate) fn transform_snap(band: usize, value: f32) -> f32 {
75 let (_, low, high) = TRANSFORM_BANDS[band];
76 if band == ROTATE_BAND {
77 let span = high - low + ROTATE_STEP; // one turn: 0..360
78 let wrapped = (value - low).rem_euclid(span);
79 // The round can land exactly on `span` (315 rounds up to the 360 that
80 // never appears in the range) which needs one more wrap back to 0,
81 // not a clamp to a fourth stop past 270 -- `ROTATE_BAND`'s reason.
82 low + ((wrapped / ROTATE_STEP).round() * ROTATE_STEP).rem_euclid(span)
83 } else {
84 ((value / TRANSFORM_STEP).round() * TRANSFORM_STEP).clamp(low, high)
85 }
86}
87
88/// The band'th control of a placement, to read or to write. The order is
89/// [`TRANSFORM_BANDS`]', which is the order the card lists them in --
90/// [`crate::export_ui::band_mut`]'s own twin.
91pub(crate) fn transform_band_mut(params: &mut TransformParams, band: usize) -> &mut f32 {
92 match band {
93 0 => &mut params.pos_x,
94 1 => &mut params.pos_y,
95 2 => &mut params.scale,
96 3 => &mut params.rotate,
97 4 => &mut params.crop_l,
98 5 => &mut params.crop_r,
99 6 => &mut params.crop_t,
100 _ => &mut params.crop_b,
101 }
102}
103
104#[cfg(test)]
105mod tests {
106 use super::*;
107
108 #[test]
109 fn ordinary_bands_snap_to_the_step_and_clamp() {
110 assert_eq!(transform_snap(0, 0.024), 0.0);
111 assert_eq!(transform_snap(0, 0.026), 0.05);
112 assert_eq!(transform_snap(0, 5.0), 1.0); // clamped to Position X's high
113 assert_eq!(transform_snap(0, -5.0), -1.0);
114 }
115
116 #[test]
117 fn rotation_snaps_to_ninety_and_wraps() {
118 assert_eq!(transform_snap(ROTATE_BAND, 44.0), 0.0);
119 assert_eq!(transform_snap(ROTATE_BAND, 46.0), 90.0);
120 assert_eq!(transform_snap(ROTATE_BAND, 270.0), 270.0);
121 // Past the last stop wraps back to the first, not clamps to the last.
122 // -45 is the same angle as 315 mod 360, so it lands on the same stop.
123 assert_eq!(transform_snap(ROTATE_BAND, 315.0), 0.0);
124 assert_eq!(transform_snap(ROTATE_BAND, -45.0), 0.0);
125 }
126
127 #[test]
128 fn transform_band_mut_indexes_in_bands_order() {
129 let mut p = TransformParams::default();
130 *transform_band_mut(&mut p, 0) = 0.1;
131 *transform_band_mut(&mut p, 3) = 90.;
132 *transform_band_mut(&mut p, 7) = 0.2;
133 assert_eq!(p.pos_x, 0.1);
134 assert_eq!(p.rotate, 90.);
135 assert_eq!(p.crop_b, 0.2);
136 }
137}