mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 01:44:25 +00:00
TransformBox: the whole outside is four parts, the outside nearest each corner, under the band parts' keys, so a band press at any corner keeps rotating after the Move controls hand the drag to Free Transform
This commit is contained in:
@@ -29,8 +29,9 @@ use crate::ui::corner::{CornerStyle, paint_widget};
|
||||
/// The part of the box a handle, the inside or the reference point is: `BOX.at(part.index())`.
|
||||
pub const BOX: PartKey = PartKey::new("box");
|
||||
|
||||
/// The parts that stand for the outside of the box: `OUTSIDE.at(0)` in [`RotateZone::Outside`], and `OUTSIDE.at(i)`
|
||||
/// for the band at corner `i` in [`RotateZone::Band`].
|
||||
/// The parts that stand for the outside of the box near corner `i`: `OUTSIDE.at(i)` is the outside nearest that corner
|
||||
/// in [`RotateZone::Outside`] and the band at that corner in [`RotateZone::Band`]. One key scheme in both zones, so a
|
||||
/// press that began on a band keeps its part when the box it hands off to takes the whole outside.
|
||||
const OUTSIDE: PartKey = PartKey::new("outside");
|
||||
|
||||
/// The pointer-only part of an edge: the whole side, beside the midpoint handle `BOX.at(Edge(k))` that the keyboard
|
||||
@@ -903,10 +904,12 @@ impl Component for TransformBox<'_> {
|
||||
}
|
||||
match self.rotate_zone {
|
||||
RotateZone::Outside => {
|
||||
cx.part(OUTSIDE.at(0), HitShape::outside_polygon(rect, quad.to_vec()))
|
||||
.sense(Sense::DRAG | Sense::CLICK | slop_sense(BoxPart::Outside))
|
||||
.priority(HitPriority::new(HitLevel::Background, 0))
|
||||
.pointer_only(ROTATE_BY_KEY);
|
||||
for (corner, region) in outside_regions(&quad, rect).into_iter().enumerate() {
|
||||
cx.part(OUTSIDE.at(corner as u64), HitShape::Polygon(region))
|
||||
.sense(Sense::DRAG | Sense::CLICK | slop_sense(BoxPart::Outside))
|
||||
.priority(HitPriority::new(HitLevel::Background, 0))
|
||||
.pointer_only(ROTATE_BY_KEY);
|
||||
}
|
||||
}
|
||||
RotateZone::Band if self.rules.allows(BoxPart::Outside) => {
|
||||
for (corner, ring) in band_rings(&screen, style).into_iter().enumerate() {
|
||||
@@ -1062,6 +1065,53 @@ fn nearest_corner(screen: &BoxState, p: Pos2) -> (usize, f32) {
|
||||
(0..4).map(|i| (i, screen.quad[i].distance(p) as f32)).min_by(|a, b| a.1.total_cmp(&b.1)).unwrap_or((0, f32::INFINITY))
|
||||
}
|
||||
|
||||
/// The outside of the box `quad` within `within`, split into one region per corner: region `i` lies between the rays
|
||||
/// from the box's centre through the midpoints of the two sides that meet at corner `i`. For a convex box the regions
|
||||
/// tile the outside; where a folded box makes one reach inside, the inside part ranks above it.
|
||||
fn outside_regions(quad: &[Pos2; 4], within: Rect) -> [Vec<Pos2>; 4] {
|
||||
let centre = quad.iter().fold(Vec2::ZERO, |sum, corner| sum + corner.to_vec2()) / 4.0;
|
||||
let centre = centre.to_pos2();
|
||||
let far = (within.size().length() + centre.distance(within.center())) * 2.0 + 1.0;
|
||||
// Side `k` runs from corner `k` to corner `k + 1`; a box with no extent there points the ray along an axis.
|
||||
let mid = |k: usize| quad[k % 4].lerp(quad[(k + 1) % 4], 0.5);
|
||||
let ray = |k: usize| {
|
||||
let along = mid(k) - centre;
|
||||
if along.length() > 1e-3 { along.normalized() } else { [Vec2::new(0.0, -1.0), Vec2::new(1.0, 0.0), Vec2::new(0.0, 1.0), Vec2::new(-1.0, 0.0)][k % 4] }
|
||||
};
|
||||
std::array::from_fn(|i| {
|
||||
let (previous, next) = (i + 3, i);
|
||||
let between = ray(previous) + ray(next);
|
||||
// Far enough along the bisector to lie past the canvas however wide the angle between the rays is.
|
||||
let out = if between.length() > 0.1 { between * (2.0 * far / between.length_sq()) } else { (quad[i] - centre).normalized() * far };
|
||||
let region = vec![quad[i], mid(next), mid(next) + ray(next) * far, centre + out, mid(previous) + ray(previous) * far, mid(previous)];
|
||||
clipped_to(®ion, within)
|
||||
})
|
||||
}
|
||||
|
||||
/// `polygon` clipped to `rect` (Sutherland-Hodgman, one rect side at a time).
|
||||
fn clipped_to(polygon: &[Pos2], rect: Rect) -> Vec<Pos2> {
|
||||
// How far a point lies inside each side of the rect.
|
||||
let sides: [fn(Pos2, Rect) -> f32; 4] = [|p, r| p.x - r.min.x, |p, r| r.max.x - p.x, |p, r| p.y - r.min.y, |p, r| r.max.y - p.y];
|
||||
let mut out = polygon.to_vec();
|
||||
for inside in sides {
|
||||
let input = std::mem::take(&mut out);
|
||||
for (k, &here) in input.iter().enumerate() {
|
||||
let before = input[(k + input.len() - 1) % input.len()];
|
||||
let (a, b) = (inside(before, rect), inside(here, rect));
|
||||
if (a >= 0.0) != (b >= 0.0) {
|
||||
out.push(before + (here - before) * (a / (a - b)));
|
||||
}
|
||||
if b >= 0.0 {
|
||||
out.push(here);
|
||||
}
|
||||
}
|
||||
if out.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
/// The rotate band at each corner of `screen`: the L-shaped ring outside the box between the band's inner and outer
|
||||
/// reach, as long as the outer reach along both edges.
|
||||
fn band_rings(screen: &BoxState, style: &TransformBoxStyle) -> [Vec<Pos2>; 4] {
|
||||
|
||||
@@ -681,6 +681,40 @@ fn a_live_line_takes_a_press_only_at_its_ends_and_an_end_drag_scales_from_the_ot
|
||||
}
|
||||
}
|
||||
|
||||
/// A press on the rotate band at any corner keeps rotating when the box it began on hands the drag to a box that
|
||||
/// takes the whole outside (the Move tool's controls opening Free Transform): both declare the outside near corner
|
||||
/// `i` under one part.
|
||||
#[trial]
|
||||
fn a_band_press_at_any_corner_rotates_after_the_hand_off_to_the_whole_outside() {
|
||||
for corner in 0..4usize {
|
||||
let mut h = harness(BoxState::from_rect(RECT), |config| {
|
||||
config.zone = RotateZone::Band;
|
||||
config.inside_moves = false;
|
||||
config.reference_point = false;
|
||||
});
|
||||
let quad = h.state().quad;
|
||||
let centre = quad[0].midpoint(quad[2]);
|
||||
let out = (quad[corner] - centre).normalize() * 14.0;
|
||||
let start = h.screen(quad[corner] + out);
|
||||
frame(&mut h, vec![pointer::moved(start)]);
|
||||
frame(&mut h, vec![pointer::press(start)]);
|
||||
{
|
||||
let mut config = h.config.borrow_mut();
|
||||
config.zone = RotateZone::Outside;
|
||||
config.inside_moves = true;
|
||||
config.reference_point = true;
|
||||
}
|
||||
let turn = by(-(start.y - h.screen(centre).y), start.x - h.screen(centre).x).normalized() * 30.0;
|
||||
frame(&mut h, vec![pointer::moved(start + turn * 0.5)]);
|
||||
frame(&mut h, vec![pointer::moved(start + turn)]);
|
||||
frame(&mut h, vec![pointer::release(start + turn)]);
|
||||
frame(&mut h, Vec::new());
|
||||
let turned = h.state().quad;
|
||||
assert!((turned[1].y - turned[0].y).abs() > 1.0, "corner {corner}: the box turns after the hand-off: {turned:?}");
|
||||
assert_eq!(h.host.borrow().events.last(), Some(&Seen::Commit), "corner {corner}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A level line's ends stand for the side handles they point to.
|
||||
#[trial]
|
||||
fn a_level_live_lines_ends_stand_for_the_sides() {
|
||||
|
||||
Reference in New Issue
Block a user