forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBfbc29d1Tahinli2 months ago972 commits

transform_ui.rs116 lines, 4.8 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/// How much of a slider row the bar itself gets, [`crate::color_ui::COLOR_BAR_W`].
47pub(crate) const TRANSFORM_BAR_W: f32 = 240.;
48
49/// A slider value on the band's own grid: [`ROTATE_BAND`] snaps to
50/// [`ROTATE_STEP`] and wraps into its range, every other band snaps to
51/// [`TRANSFORM_STEP`] and clamps into its own -- what a drag rounds to, so the
52/// pointer stops where the arrow keys do.
53pub(crate) fn transform_snap(band: usize, value: f32) -> f32 {
54 let (_, low, high) = TRANSFORM_BANDS[band];
55 if band == ROTATE_BAND {
56 let span = high - low + ROTATE_STEP; // one turn: 0..360
57 let wrapped = (value - low).rem_euclid(span);
58 // The round can land exactly on `span` (315 rounds up to the 360 that
59 // never appears in the range) which needs one more wrap back to 0,
60 // not a clamp to a fourth stop past 270 -- `ROTATE_BAND`'s reason.
61 low + ((wrapped / ROTATE_STEP).round() * ROTATE_STEP).rem_euclid(span)
62 } else {
63 ((value / TRANSFORM_STEP).round() * TRANSFORM_STEP).clamp(low, high)
64 }
65}
66
67/// The band'th control of a placement, to read or to write. The order is
68/// [`TRANSFORM_BANDS`]', which is the order the card lists them in --
69/// [`crate::export_ui::band_mut`]'s own twin.
70pub(crate) fn transform_band_mut(params: &mut TransformParams, band: usize) -> &mut f32 {
71 match band {
72 0 => &mut params.pos_x,
73 1 => &mut params.pos_y,
74 2 => &mut params.scale,
75 3 => &mut params.rotate,
76 4 => &mut params.crop_l,
77 5 => &mut params.crop_r,
78 6 => &mut params.crop_t,
79 _ => &mut params.crop_b,
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use super::*;
86
87 #[test]
88 fn ordinary_bands_snap_to_the_step_and_clamp() {
89 assert_eq!(transform_snap(0, 0.024), 0.0);
90 assert_eq!(transform_snap(0, 0.026), 0.05);
91 assert_eq!(transform_snap(0, 5.0), 1.0); // clamped to Position X's high
92 assert_eq!(transform_snap(0, -5.0), -1.0);
93 }
94
95 #[test]
96 fn rotation_snaps_to_ninety_and_wraps() {
97 assert_eq!(transform_snap(ROTATE_BAND, 44.0), 0.0);
98 assert_eq!(transform_snap(ROTATE_BAND, 46.0), 90.0);
99 assert_eq!(transform_snap(ROTATE_BAND, 270.0), 270.0);
100 // Past the last stop wraps back to the first, not clamps to the last.
101 // -45 is the same angle as 315 mod 360, so it lands on the same stop.
102 assert_eq!(transform_snap(ROTATE_BAND, 315.0), 0.0);
103 assert_eq!(transform_snap(ROTATE_BAND, -45.0), 0.0);
104 }
105
106 #[test]
107 fn transform_band_mut_indexes_in_bands_order() {
108 let mut p = TransformParams::default();
109 *transform_band_mut(&mut p, 0) = 0.1;
110 *transform_band_mut(&mut p, 3) = 90.;
111 *transform_band_mut(&mut p, 7) = 0.2;
112 assert_eq!(p.pos_x, 0.1);
113 assert_eq!(p.rotate, 90.);
114 assert_eq!(p.crop_b, 0.2);
115 }
116}