forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBef5832fTahinli3 months ago597 commits

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