mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-25 18:27:02 -06:00
Merge pull request #1036 from mateususa/fix/1026-selection-transform
#1026: open filled paths hit by their fill, rotate cursor direction, flip icons, live Color Picker, Scale options in Properties
This commit is contained in:
+1
-1
@@ -123,7 +123,7 @@ Ranked; detail in [docs/roadmap.md](docs/roadmap.md) and [docs/gaps.md](docs/gap
|
||||
|
||||
| Date | What landed |
|
||||
|---|---|
|
||||
| 2026-10-11 | Effect › Brush Strokes: Accented Edges, Angled Strokes, Crosshatch, Dark Strokes, Ink Outlines, Spatter, Sprayed Strokes, Sumi-e (G2). Command parameters from agents and files checked before anything changes (paint, place, cuts, dashes, crop, asset ids, anchors, paths, distort; #984–#999); command dialogs keep whole numbers whole, so Create Object Mosaic takes the tiles typed (#1000); stepper arrows take Shift (×10) and Ctrl/Cmd (÷10) like the arrow keys (#991). Exports and Rasterize supersample Art Optimized edges, so shapes meeting edge to edge leave no seam (#983). Character and paragraph styles take leading, kerning, mojikumi and the other attributes left out at their defaults (#1001). Drawing, typing and pasting with no layer shown and unlocked is refused instead of adding art to a hidden or locked layer (#1002). MCP `add_text` gives type on or in a path the colour asked for (#1003). MCP `screenshot` of a connected app renders the artboard asked for (#1004). `vectorcraft-cli run` ends quietly when its reader closes early (#1005). Select › All on Active Artboard selects on the artboard made active, not always the first (#1006). Reverse Gradient keeps off-centre midpoints, mirrored (#1007). A polar grid with no radial dividers draws none (#1008). Use Precise Cursors also covers the Pen's join pointer (#1009). SVGs with one absolute and one unitless root side import at their size, unstretched (#1010). DXF US survey inches, yards and miles open at their size and name (#1011). A file placed into a document with no other links follows its first change even while the app sits idle (#1031). PDF import keeps the style a font name says when the file doesn't embed the font (#1023). Shift selects ruler guides and art together, and Delete removes both (#1022). |
|
||||
| 2026-10-11 | Effect › Brush Strokes: Accented Edges, Angled Strokes, Crosshatch, Dark Strokes, Ink Outlines, Spatter, Sprayed Strokes, Sumi-e (G2). Command parameters from agents and files checked before anything changes (paint, place, cuts, dashes, crop, asset ids, anchors, paths, distort; #984–#999); command dialogs keep whole numbers whole, so Create Object Mosaic takes the tiles typed (#1000); stepper arrows take Shift (×10) and Ctrl/Cmd (÷10) like the arrow keys (#991). Exports and Rasterize supersample Art Optimized edges, so shapes meeting edge to edge leave no seam (#983). Character and paragraph styles take leading, kerning, mojikumi and the other attributes left out at their defaults (#1001). Drawing, typing and pasting with no layer shown and unlocked is refused instead of adding art to a hidden or locked layer (#1002). MCP `add_text` gives type on or in a path the colour asked for (#1003). MCP `screenshot` of a connected app renders the artboard asked for (#1004). `vectorcraft-cli run` ends quietly when its reader closes early (#1005). Select › All on Active Artboard selects on the artboard made active, not always the first (#1006). Reverse Gradient keeps off-centre midpoints, mirrored (#1007). A polar grid with no radial dividers draws none (#1008). Use Precise Cursors also covers the Pen's join pointer (#1009). SVGs with one absolute and one unitless root side import at their size, unstretched (#1010). DXF US survey inches, yards and miles open at their size and name (#1011). A file placed into a document with no other links follows its first change even while the app sits idle (#1031). PDF import keeps the style a font name says when the file doesn't embed the font (#1023). Shift selects ruler guides and art together, and Delete removes both (#1022). Open filled paths are hit by their fill on every side, the rotate cursor bows around the corner it is at, the flip icons match their flips, the Color Picker previews on the selection, and Properties › Transform reaches Scale Corners and Scale Strokes & Effects (#1036). |
|
||||
| 2026-10-10 | Initial live Revolve (Effect › 3D and Materials, #846, #605), in its own `three-d` crate. Progress docs re-measured and restructured to the craftrules standard (this page, `docs/target-app-parity.md`, `gaps.md`, `ui-parity.md`, the format, hardware, localization, effects and type checklists). Same day: v0.8.0; Effect › Distort, Pixelate and Texture filters (13); German interface; Variables (data merge); Graph Type value axes and tick marks; restart when the first frame never reaches the screen (#964) |
|
||||
| 2026-10-09 | Radial Blur, Smart Blur, Unsharp Mask (gap 2 begins); 340 commits, the busiest day |
|
||||
| 2026-10-08 | v0.5.0–v0.7.0; community contributions: Pen and shape-tool modifiers, Layers, Artboards, Japanese composition, Hebrew/Arabic type, interface languages, saved selections |
|
||||
|
||||
@@ -1629,6 +1629,8 @@ mod tests_recolor;
|
||||
#[cfg(test)]
|
||||
mod tests_recovery;
|
||||
#[cfg(test)]
|
||||
mod tests_reflecthit;
|
||||
#[cfg(test)]
|
||||
mod tests_registration;
|
||||
#[cfg(test)]
|
||||
mod tests_save;
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
//! A reflected object is still clicked by its fill, not only by its outline.
|
||||
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_geom::Point;
|
||||
use vectorcraft_tools::{Mods, PointerEvent, PointerKind};
|
||||
|
||||
use super::*;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", &json!({"width": 800, "height": 600})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn id_of(v: &Value) -> NodeId {
|
||||
NodeId(v["id"].as_u64().unwrap())
|
||||
}
|
||||
|
||||
/// A click with the Selection tool at (x, y): what it selects.
|
||||
fn click(s: &mut Session, x: f64, y: f64) -> Vec<NodeId> {
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("selection", v).unwrap();
|
||||
for kind in [PointerKind::Down, PointerKind::Up] {
|
||||
s.pointer(&PointerEvent::new(kind, x, y).with_mods(Mods::default()), v).unwrap();
|
||||
}
|
||||
s.doc().unwrap().selection.objects.clone()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_reflected_shape_is_clicked_by_its_fill() {
|
||||
const HEART: &str = "M200 290C190 280 110 230 110 170C110 140 130 120 155 120C175 120 192 132 200 148C208 132 225 120 245 120C270 120 290 140 290 170C290 230 210 280 200 290Z";
|
||||
let makers: [(&str, Value); 5] = [
|
||||
("shape.rectangle", json!({"x": 150, "y": 150, "width": 100, "height": 100})),
|
||||
("shape.ellipse", json!({"x": 150, "y": 150, "width": 100, "height": 100})),
|
||||
("shape.polygon", json!({"cx": 200, "cy": 200, "radius": 60, "sides": 5})),
|
||||
("shape.star", json!({"cx": 200, "cy": 200, "radius1": 70, "radius2": 40, "points": 5})),
|
||||
("path.create", json!({"d": HEART})),
|
||||
];
|
||||
for (cmd, params) in makers {
|
||||
for axis in ["horizontal", "vertical"] {
|
||||
let mut s = session();
|
||||
let id = id_of(&s.execute(cmd, ¶ms).unwrap());
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.reflect", &json!({"axis": axis})).unwrap();
|
||||
let b = s.doc().unwrap().doc.bounds_of(&[id], false).unwrap();
|
||||
s.execute("select.set", &json!({"ids": []})).unwrap();
|
||||
let c = b.center();
|
||||
assert_eq!(click(&mut s, c.x, c.y), vec![id], "{cmd} reflected {axis}: a click in its fill at {c:?} (bounds {b:?})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Hearts as SVG files bring them: a plain path, a compound path, inside a group, under a
|
||||
/// transform. Reflected with the button and with a bounding-box handle dragged across.
|
||||
#[test]
|
||||
fn a_reflected_imported_heart_is_clicked_by_its_fill() {
|
||||
const D: &str = "M200 290C190 280 110 230 110 170C110 140 130 120 155 120C175 120 192 132 200 148C208 132 225 120 245 120C270 120 290 140 290 170C290 230 210 280 200 290Z";
|
||||
let files = [
|
||||
format!(r##"<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400"><path fill="#e2384d" d="{D}"/></svg>"##),
|
||||
format!(
|
||||
r##"<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400"><path fill="#e2384d" fill-rule="evenodd" d="{D} M190 200h20v20h-20z"/></svg>"##
|
||||
),
|
||||
format!(r##"<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400"><g><g fill="#e2384d"><path d="{D}"/></g></g></svg>"##),
|
||||
format!(
|
||||
r##"<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400"><g transform="translate(10 0) scale(1.2)"><path fill="#e2384d" transform="matrix(1 0 0 1 -20 -20)" d="{D}"/></g></svg>"##
|
||||
),
|
||||
format!(
|
||||
r##"<svg xmlns="http://www.w3.org/2000/svg" width="400" height="400" viewBox="0 0 200 200"><path style="fill:#e2384d;stroke:none" d="{D}" transform="scale(0.5)"/></svg>"##
|
||||
),
|
||||
];
|
||||
for (i, svg) in files.iter().enumerate() {
|
||||
for how in ["horizontal", "vertical", "handle"] {
|
||||
let mut s = Session::new();
|
||||
s.add_document(vectorcraft_svg::import(svg).unwrap(), None);
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
let ids = s.doc().unwrap().selection.objects.clone();
|
||||
assert!(!ids.is_empty(), "file {i}: nothing imported");
|
||||
let b = s.doc().unwrap().doc.bounds_of(&ids, false).unwrap();
|
||||
if how == "handle" {
|
||||
// Drag the top handle down past the bottom: a top-to-bottom flip.
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("selection", v).unwrap();
|
||||
let (from, to) = (Point::new(b.center().x, b.y0), Point::new(b.center().x, b.y1 + b.height()));
|
||||
for (kind, p) in
|
||||
[(PointerKind::Down, from), (PointerKind::Drag, Point::new(from.x, b.center().y)), (PointerKind::Drag, to), (PointerKind::Up, to)]
|
||||
{
|
||||
s.pointer(&PointerEvent::new(kind, p.x, p.y).with_mods(Mods::default()), v).unwrap();
|
||||
}
|
||||
} else {
|
||||
s.execute("object.reflect", &json!({"axis": how})).unwrap();
|
||||
}
|
||||
let b = s.doc().unwrap().doc.bounds_of(&ids, false).unwrap();
|
||||
s.execute("select.set", &json!({"ids": []})).unwrap();
|
||||
// Inside the heart away from the compound's square hole: a third of the way down.
|
||||
let c = Point::new(b.center().x, if how == "horizontal" || how == "handle" { b.y1 - b.height() * 0.3 } else { b.y0 + b.height() * 0.3 });
|
||||
let picked = click(&mut s, c.x, c.y);
|
||||
assert_eq!(picked, ids, "file {i} flipped by {how}: a click in its fill at {c:?} (bounds {b:?})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Still selected after the flip, a drag from inside the fill moves it.
|
||||
#[test]
|
||||
fn a_reflected_selected_shape_drags_by_its_fill() {
|
||||
const HEART: &str = "M200 290C190 280 110 230 110 170C110 140 130 120 155 120C175 120 192 132 200 148C208 132 225 120 245 120C270 120 290 140 290 170C290 230 210 280 200 290Z";
|
||||
let makers: [(&str, Value); 4] = [
|
||||
("shape.rectangle", json!({"x": 150, "y": 150, "width": 100, "height": 100})),
|
||||
("shape.polygon", json!({"cx": 200, "cy": 200, "radius": 60, "sides": 3})),
|
||||
("shape.star", json!({"cx": 200, "cy": 200, "radius1": 70, "radius2": 40, "points": 5})),
|
||||
("path.create", json!({"d": HEART})),
|
||||
];
|
||||
for (cmd, params) in makers {
|
||||
for axis in ["horizontal", "vertical"] {
|
||||
let mut s = session();
|
||||
let id = id_of(&s.execute(cmd, ¶ms).unwrap());
|
||||
s.execute("select.set", &json!({"ids": [id.0]})).unwrap();
|
||||
s.execute("object.reflect", &json!({"axis": axis})).unwrap();
|
||||
let b = s.doc().unwrap().doc.bounds_of(&[id], false).unwrap();
|
||||
let c = b.center();
|
||||
let v = ViewInfo::default();
|
||||
s.select_tool("selection", v).unwrap();
|
||||
for (kind, x) in
|
||||
[(PointerKind::Down, c.x), (PointerKind::Drag, c.x + 20.0), (PointerKind::Drag, c.x + 40.0), (PointerKind::Up, c.x + 40.0)]
|
||||
{
|
||||
s.pointer(&PointerEvent::new(kind, x, c.y).with_mods(Mods::default()), v).unwrap();
|
||||
}
|
||||
let after = s.doc().unwrap().doc.bounds_of(&[id], false).unwrap();
|
||||
assert!((after.x0 - b.x0 - 40.0).abs() < 1e-6, "{cmd} reflected {axis}: a drag from {c:?} left it at {after:?} (was {b:?})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An open path (an anchor deleted from a closed one) still fills as if closed, and its fill is
|
||||
/// clicked on both sides of that closing line, reflected or not.
|
||||
#[test]
|
||||
fn an_open_filled_path_is_clicked_by_its_fill_either_way_round() {
|
||||
// The sliver of a strawberry, open, its closing line down its left side.
|
||||
let svg = r##"<svg xmlns="http://www.w3.org/2000/svg" width="200" height="200"><path fill="#c22a3f" d="M86.311 69.612C100.544 78.261 110.506 88.64 110.506 100.748C110.506 111.127 100.544 118.046 86.311 118.046C93.214 102.045 91.407 102.403 91.407 97.61"/></svg>"##;
|
||||
for axis in [None, Some("vertical"), Some("horizontal")] {
|
||||
let mut s = Session::new();
|
||||
s.add_document(vectorcraft_svg::import(svg).unwrap(), None);
|
||||
s.execute("select.all", &json!({})).unwrap();
|
||||
let ids = s.doc().unwrap().selection.objects.clone();
|
||||
if let Some(axis) = axis {
|
||||
s.execute("object.reflect", &json!({"axis": axis})).unwrap();
|
||||
}
|
||||
let b = s.doc().unwrap().doc.bounds_of(&ids, false).unwrap();
|
||||
s.execute("select.set", &json!({"ids": []})).unwrap();
|
||||
// The middle of the sliver's widest part, well inside it.
|
||||
let x = if axis == Some("vertical") { b.x0 + b.width() * 0.4 } else { b.x0 + b.width() * 0.6 };
|
||||
assert_eq!(click(&mut s, x, b.center().y), ids, "reflected {axis:?}: a click in the fill (bounds {b:?})");
|
||||
}
|
||||
}
|
||||
+70
-2
@@ -1,18 +1,60 @@
|
||||
//! Hit testing primitives.
|
||||
|
||||
use kurbo::{BezPath, ParamCurveNearest, Point, Rect, Shape};
|
||||
use std::borrow::Cow;
|
||||
|
||||
use kurbo::{BezPath, ParamCurveNearest, PathEl, Point, Rect, Shape};
|
||||
|
||||
use crate::path::{FillRule, PathData};
|
||||
|
||||
/// Is `p` inside the filled area of `path` under `rule`? Open subpaths are implicitly closed (as when filled).
|
||||
pub fn fill_contains(path: &BezPath, rule: FillRule, p: Point) -> bool {
|
||||
let w = path.winding(p);
|
||||
let w = closed(path).winding(p);
|
||||
match rule {
|
||||
FillRule::NonZero => w != 0,
|
||||
FillRule::EvenOdd => w % 2 != 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// `path` with each open subpath closed by a straight line back to its start, the edge its fill
|
||||
/// has. kurbo's winding leaves that edge out, so a point beside it counted as outside the fill:
|
||||
/// an open path lost its fill clicks on that side (a reflection moved the edge to the other one).
|
||||
fn closed(path: &BezPath) -> Cow<'_, BezPath> {
|
||||
let mut open = false;
|
||||
let mut needs = false;
|
||||
for el in path.elements() {
|
||||
match el {
|
||||
PathEl::MoveTo(_) => {
|
||||
needs |= open;
|
||||
open = true;
|
||||
}
|
||||
PathEl::ClosePath => open = false,
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
if !(needs || open) {
|
||||
return Cow::Borrowed(path);
|
||||
}
|
||||
let mut out = BezPath::new();
|
||||
let mut open = false;
|
||||
for &el in path.elements() {
|
||||
match el {
|
||||
PathEl::MoveTo(_) => {
|
||||
if open {
|
||||
out.close_path();
|
||||
}
|
||||
open = true;
|
||||
}
|
||||
PathEl::ClosePath => open = false,
|
||||
_ => {}
|
||||
}
|
||||
out.push(el);
|
||||
}
|
||||
if open {
|
||||
out.close_path();
|
||||
}
|
||||
Cow::Owned(out)
|
||||
}
|
||||
|
||||
/// Distance from `p` to the nearest point on the path outline.
|
||||
pub fn distance_to_outline(path: &BezPath, p: Point) -> f64 {
|
||||
path.segments().map(|s| s.nearest(p, 1e-9).distance_sq).fold(f64::INFINITY, f64::min).sqrt()
|
||||
@@ -77,6 +119,32 @@ mod tests {
|
||||
assert!(fill_contains(&bp, FillRule::EvenOdd, Point::new(1.0, 5.0)));
|
||||
}
|
||||
|
||||
/// An open path fills as if closed by a straight line back to its start, and is hit there,
|
||||
/// whichever side that line falls on (a reflected open path lost its fill clicks).
|
||||
#[test]
|
||||
fn open_paths_fill_as_closed_on_either_side() {
|
||||
// A filled sliver left open by a deleted anchor; its closing line runs down its left side.
|
||||
let open = BezPath::from_svg(
|
||||
"M86.311 69.612C100.544 78.261 110.506 88.64 110.506 100.748C110.506 111.127 100.544 118.046 86.311 118.046C93.214 102.045 91.407 102.403 91.407 97.61",
|
||||
)
|
||||
.unwrap();
|
||||
let mut closed = open.clone();
|
||||
closed.close_path();
|
||||
for p in [Point::new(100.0, 100.0), Point::new(95.0, 85.0), Point::new(92.0, 112.0)] {
|
||||
assert!(closed.winding(p) != 0, "{p:?} is inside the closed sliver");
|
||||
assert!(fill_contains(&open, FillRule::NonZero, p), "{p:?}: the open sliver's fill");
|
||||
assert!(fill_contains(&open, FillRule::EvenOdd, p), "{p:?}: even-odd too");
|
||||
}
|
||||
assert!(!fill_contains(&open, FillRule::NonZero, Point::new(80.0, 100.0)), "left of the closing line");
|
||||
// Mirrored, the closing line is on the other side: still filled.
|
||||
let flipped = kurbo::Affine::new([-1.0, 0.0, 0.0, 1.0, 200.0, 0.0]) * open.clone();
|
||||
assert!(fill_contains(&flipped, FillRule::NonZero, Point::new(100.0, 100.0)));
|
||||
// Two open subpaths: each closes on its own.
|
||||
let mut two = open.clone();
|
||||
two.extend(kurbo::Affine::translate((0.0, 100.0)) * open.clone());
|
||||
assert!(fill_contains(&two, FillRule::NonZero, Point::new(100.0, 100.0)) && fill_contains(&two, FillRule::NonZero, Point::new(100.0, 200.0)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn marquee() {
|
||||
let p = shapes::ellipse(Rect::new(0.0, 0.0, 10.0, 10.0));
|
||||
|
||||
@@ -425,6 +425,11 @@ pub enum Cursor {
|
||||
ResizeNwSe,
|
||||
ResizeNeSw,
|
||||
Rotate,
|
||||
/// The rotate cursor beside a bounding box, bowed toward the pointer's side of the box: off
|
||||
/// its centre in direction `0..8` (0 straight up, then clockwise in 45° steps). Its arc wraps
|
||||
/// the corner it is at, so at the top right it opens toward the bottom left. Make it with
|
||||
/// [`Cursor::rotate_about`].
|
||||
RotateToward(u8),
|
||||
/// Over a Live Corners widget (drag to round the corners).
|
||||
CornerRadius,
|
||||
Pen,
|
||||
@@ -476,6 +481,18 @@ pub enum Cursor {
|
||||
}
|
||||
|
||||
impl Cursor {
|
||||
/// The rotate cursor for the pointer at `p` turning art about `center`: [`Cursor::RotateToward`]
|
||||
/// the pointer's side of it, or the plain [`Cursor::Rotate`] on the centre itself.
|
||||
pub fn rotate_about(center: Point, p: Point) -> Self {
|
||||
let d = p - center;
|
||||
if !(d.x.is_finite() && d.y.is_finite()) || d.hypot() < 1e-9 {
|
||||
return Cursor::Rotate;
|
||||
}
|
||||
// y points down the page: atan2(x, -y) is 0 straight up and grows clockwise.
|
||||
let octant = (d.x.atan2(-d.y) / std::f64::consts::FRAC_PI_4).round() as i64;
|
||||
Cursor::RotateToward(octant.rem_euclid(8) as u8)
|
||||
}
|
||||
|
||||
/// General › Use Precise Cursors: the drawing tools' pointers (the Pen's in every state, the
|
||||
/// Eyedropper's, the Slice and Blend tools') become a plain crosshair at the hotspot.
|
||||
pub fn precise(self) -> Self {
|
||||
|
||||
@@ -444,7 +444,7 @@ impl Tool for SelectionTool {
|
||||
|
||||
fn cursor(&self, cx: &ToolContext, p: Point, m: Mods) -> Cursor {
|
||||
match self.state {
|
||||
State::Rotating { .. } => return Cursor::Rotate,
|
||||
State::Rotating { center, .. } => return Cursor::rotate_about(center, p),
|
||||
State::Scaling { handle, bx, .. } => return handle_cursor(handle, bx.angle),
|
||||
State::Moving { began: true, .. } => return Cursor::Arrow,
|
||||
State::Corner(_) => return Cursor::CornerRadius,
|
||||
@@ -468,7 +468,7 @@ impl Tool for SelectionTool {
|
||||
{
|
||||
match box_hit(cx, &bx, p) {
|
||||
Some(BoxHit::Handle(h)) => return handle_cursor(h, bx.angle),
|
||||
Some(BoxHit::Rotate) => return Cursor::Rotate,
|
||||
Some(BoxHit::Rotate) => return Cursor::rotate_about(bx.center(), p),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
@@ -588,6 +588,24 @@ mod tests {
|
||||
assert!(t.pointer(&cx, &ev(PointerKind::DoubleClick, 160.0, 150.0)).is_empty());
|
||||
}
|
||||
|
||||
/// Just outside each corner of the box the rotate cursor bows toward that corner, its arc
|
||||
/// opening toward the box: at the top right it opens to the bottom left.
|
||||
#[test]
|
||||
fn the_rotate_cursor_wraps_the_corner_it_is_at() {
|
||||
let (d, id) = doc_with_rect();
|
||||
let mut s = Selection::default();
|
||||
s.add(id);
|
||||
let p = paint();
|
||||
let cx = cx(&d, &s, &p);
|
||||
let t = SelectionTool::default();
|
||||
// The box runs from (100, 100) to (200, 200); 10 pt out from each corner, diagonally.
|
||||
for ((x, y), octant) in [((93.0, 93.0), 7), ((207.0, 93.0), 1), ((207.0, 207.0), 3), ((93.0, 207.0), 5)] {
|
||||
assert_eq!(t.cursor(&cx, Point::new(x, y), Mods::default()), Cursor::RotateToward(octant), "at ({x}, {y})");
|
||||
}
|
||||
assert_eq!(Cursor::rotate_about(Point::new(1.0, 1.0), Point::new(1.0, 1.0)), Cursor::Rotate, "on the centre");
|
||||
assert_eq!(Cursor::rotate_about(Point::ORIGIN, Point::new(0.0, -5.0)), Cursor::RotateToward(0), "straight up");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn drag_moves_with_single_undo() {
|
||||
let (d, id) = doc_with_rect();
|
||||
|
||||
@@ -313,7 +313,7 @@ impl Tool for FreeTransformTool {
|
||||
fn cursor(&self, cx: &ToolContext, p: Point, m: Mods) -> Cursor {
|
||||
let Some(r) = selection_bounds(cx) else { return Cursor::Arrow };
|
||||
match self.drag.map(|d| d.op).or_else(|| self.classify(cx, r, p, m)) {
|
||||
Some(Op::Rotate) => Cursor::Rotate,
|
||||
Some(Op::Rotate) => Cursor::rotate_about(r.center(), p),
|
||||
Some(Op::Move) => Cursor::Move,
|
||||
Some(Op::Scale(h) | Op::Shear(h)) => match h {
|
||||
Handle::Top | Handle::Bottom => Cursor::ResizeV,
|
||||
|
||||
@@ -487,7 +487,7 @@ fn cursor_icon(c: Cursor) -> egui::CursorIcon {
|
||||
Cursor::ResizeV => C::ResizeVertical,
|
||||
Cursor::ResizeNwSe => C::ResizeNwSe,
|
||||
Cursor::ResizeNeSw => C::ResizeNeSw,
|
||||
Cursor::Rotate => C::Alias,
|
||||
Cursor::Rotate | Cursor::RotateToward(_) => C::Alias,
|
||||
Cursor::Pen | Cursor::PenAdd | Cursor::PenDelete | Cursor::PenClose | Cursor::PenContinue | Cursor::PenJoin | Cursor::PenConvert => {
|
||||
C::Crosshair
|
||||
}
|
||||
|
||||
@@ -113,17 +113,21 @@ fn double_arrow(p: &mut Ink, o: Pos2, dir: egui::Vec2) {
|
||||
}
|
||||
|
||||
/// A curved double arrow over the hotspot: an arc with an arrowhead at each end, pointing on along
|
||||
/// the arc (#701).
|
||||
fn rotate(p: &mut Ink, o: Pos2) {
|
||||
/// the arc (#701). The arc bows up, or toward `octant` (0 up, then clockwise in 45° steps): beside a
|
||||
/// bounding box it wraps the corner the pointer is at.
|
||||
fn rotate(p: &mut Ink, o: Pos2, octant: u8) {
|
||||
const R: f32 = 8.0;
|
||||
let (a0, a1) = (std::f32::consts::PI * 0.12, std::f32::consts::PI * 0.88);
|
||||
let at = |a: f32| o + vec2(a.cos() * R, -a.sin() * R);
|
||||
// Clockwise on screen (y down) by whole octants.
|
||||
let (s, c) = (f32::from(octant % 8) * std::f32::consts::FRAC_PI_4).sin_cos();
|
||||
let turn = |v: Vec2| vec2(v.x * c - v.y * s, v.x * s + v.y * c);
|
||||
let at = |a: f32| o + turn(vec2(a.cos() * R, -a.sin() * R));
|
||||
let pts: Vec<Pos2> = (0..=12).map(|i| at(a0 + (a1 - a0) * i as f32 / 12.0)).collect();
|
||||
p.add(Shape::line(pts.clone(), Stroke::new(3.2, HALO)));
|
||||
p.add(Shape::line(pts, Stroke::new(1.3, INK)));
|
||||
// Each head points away from the arc, along its tangent there.
|
||||
for (a, away) in [(a0, -1.0f32), (a1, 1.0)] {
|
||||
let t = vec2(-a.sin(), -a.cos()) * away;
|
||||
let t = turn(vec2(-a.sin(), -a.cos()) * away);
|
||||
let n = vec2(-t.y, t.x);
|
||||
let (end, tip) = (at(a) - t * 1.5, at(a) + t * 4.5);
|
||||
poly(p, vec![tip, end + n * 1.8, end - n * 1.8], INK, INK);
|
||||
@@ -269,7 +273,8 @@ fn glyph(c: Cursor, p: Pos2) -> Option<Vec<Shape>> {
|
||||
Cursor::ResizeV => double_arrow(ink, p, vec2(0.0, 1.0)),
|
||||
Cursor::ResizeNwSe => double_arrow(ink, p, vec2(1.0, 1.0)),
|
||||
Cursor::ResizeNeSw => double_arrow(ink, p, vec2(1.0, -1.0)),
|
||||
Cursor::Rotate => rotate(ink, p),
|
||||
Cursor::Rotate => rotate(ink, p, 0),
|
||||
Cursor::RotateToward(octant) => rotate(ink, p, octant),
|
||||
Cursor::CornerRadius => corner_radius(ink, p),
|
||||
Cursor::Pen => pen(ink, p, "*"),
|
||||
Cursor::PenAdd => pen(ink, p, "+"),
|
||||
@@ -432,7 +437,7 @@ mod tests {
|
||||
use crate::VectorcraftApp;
|
||||
|
||||
/// Every cursor with a glyph.
|
||||
const GLYPHS: [Cursor; 33] = [
|
||||
const GLYPHS: [Cursor; 34] = [
|
||||
Cursor::Arrow,
|
||||
Cursor::ArrowHollow,
|
||||
Cursor::Move,
|
||||
@@ -442,6 +447,7 @@ mod tests {
|
||||
Cursor::ResizeNwSe,
|
||||
Cursor::ResizeNeSw,
|
||||
Cursor::Rotate,
|
||||
Cursor::RotateToward(1),
|
||||
Cursor::CornerRadius,
|
||||
Cursor::Pen,
|
||||
Cursor::PenAdd,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
//! The Color Picker (double-click a Fill/Stroke proxy, or `ui.colorPicker`): a colour field with a
|
||||
//! channel slider for H, S, B, R, G or B; HSB, RGB, Lab and CMYK fields and hex; Only Web Colors;
|
||||
//! the out-of-gamut and out-of-web warnings (click to correct); New/Original chips; and a Color
|
||||
//! Swatches list. OK applies the colour to its proxy through `paint.setFill` / `paint.setStroke`.
|
||||
//! Swatches list. With art selected the colour shows on it live as it is picked; OK keeps it as one
|
||||
//! undo step through `paint.setFill` / `paint.setStroke`, Cancel puts the art back.
|
||||
//!
|
||||
//! Fields agents set with `ui.dialog.set`: `hex` (`"00FF00"`) or `color` (any colour the paint
|
||||
//! commands take), `channel` (`hue`, `saturation`, `brightness`, `red`, `green`, `blue`), `webOnly`
|
||||
@@ -15,6 +16,7 @@ use vectorcraft_doc::ColorMode;
|
||||
use vectorcraft_engine::cmd::color_value;
|
||||
|
||||
use super::DialogSpec;
|
||||
use super::form::PREVIEWED;
|
||||
use crate::panels::color::{GAMUT_WARNING, WEB_WARNING, gamut_fix, hex_digits, is_web_safe, parse_hex, warning_chip, web_safe};
|
||||
use crate::panels::{c32, color_json};
|
||||
use crate::state::Dialog;
|
||||
@@ -23,8 +25,15 @@ use crate::{VectorcraftApp, widgets};
|
||||
|
||||
pub(super) const KIND: &str = "colorPicker";
|
||||
|
||||
pub(super) const SPEC: DialogSpec =
|
||||
DialogSpec { heading: |_| tl!("Color Picker").into(), body, confirm, min_width: 600.0, max_width: Some(640.0), ..DialogSpec::FORM };
|
||||
pub(super) const SPEC: DialogSpec = DialogSpec {
|
||||
heading: |_| tl!("Color Picker").into(),
|
||||
body,
|
||||
confirm,
|
||||
preview: true,
|
||||
min_width: 600.0,
|
||||
max_width: Some(640.0),
|
||||
..DialogSpec::FORM
|
||||
};
|
||||
|
||||
/// Side of the colour field (and height of the channel slider).
|
||||
const FIELD: f32 = 240.0;
|
||||
@@ -242,9 +251,39 @@ fn body(app: &mut VectorcraftApp, ui: &mut Ui, d: &mut Dialog) -> bool {
|
||||
d.fields.insert("swatches".into(), json!(!swatches));
|
||||
}
|
||||
store(d, &pick.unwrap_or(p).snapped(d.bool("webOnly")));
|
||||
preview(app, d);
|
||||
false
|
||||
}
|
||||
|
||||
/// Show the picked colour on the selected art while the picker is open: each new colour replaces
|
||||
/// the last preview. Nothing previews until the colour first changes, nor without a selection
|
||||
/// (the colour then only becomes the default paint, on OK).
|
||||
fn preview(app: &mut VectorcraftApp, d: &mut Dialog) {
|
||||
let (cmd, params) = paint_command(app, d);
|
||||
let first = !app.session.in_interaction();
|
||||
let unchanged = d.fields.get(PREVIEWED) == Some(¶ms) || first && d.fields.get("original").and_then(color_value) == Some(picked(d).color);
|
||||
let selected = app.session.active().is_some_and(|st| !st.selection.objects.is_empty());
|
||||
if unchanged || !selected {
|
||||
return;
|
||||
}
|
||||
let _ = app.session.begin_interaction(if d.bool("stroke") { "Stroke Color" } else { "Fill Color" });
|
||||
if let Err(e) = app.session.preview(cmd, ¶ms) {
|
||||
app.status(e.to_string());
|
||||
}
|
||||
d.fields.insert(PREVIEWED.into(), params);
|
||||
}
|
||||
|
||||
/// The command that applies the picked colour to the proxy, and its params (as CMYK in a CMYK
|
||||
/// document).
|
||||
fn paint_command(app: &VectorcraftApp, d: &Dialog) -> (&'static str, Value) {
|
||||
let mut c = picked(d).color;
|
||||
if matches!(c, Color::Rgb { .. }) && app.session.active().is_some_and(|s| s.doc.color_mode == ColorMode::Cmyk) {
|
||||
let [cy, m, y, k] = c.to_cmyk_managed(cms::active().settings().intent);
|
||||
c = Color::cmyk(cy, m, y, k);
|
||||
}
|
||||
(if d.bool("stroke") { "paint.setStroke" } else { "paint.setFill" }, json!({ "color": color_json(&c) }))
|
||||
}
|
||||
|
||||
/// The New (top) and Original (bottom) chips. Returns true when Original is clicked.
|
||||
fn new_original_chips(ui: &mut Ui, new: &Color, original: &Color) -> bool {
|
||||
let t = Tokens::get(ui.ctx());
|
||||
@@ -363,15 +402,10 @@ fn swatch_list(app: &VectorcraftApp, ui: &mut Ui, current: &Color) -> Option<Col
|
||||
chosen
|
||||
}
|
||||
|
||||
/// OK: apply the colour to the proxy (as CMYK in a CMYK document).
|
||||
/// OK: apply the colour to the proxy, keeping the live preview as one undo step.
|
||||
fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
|
||||
let mut c = picked(d).color;
|
||||
if matches!(c, Color::Rgb { .. }) && app.session.active().is_some_and(|s| s.doc.color_mode == ColorMode::Cmyk) {
|
||||
let [cy, m, y, k] = c.to_cmyk_managed(cms::active().settings().intent);
|
||||
c = Color::cmyk(cy, m, y, k);
|
||||
}
|
||||
let cmd = if d.bool("stroke") { "paint.setStroke" } else { "paint.setFill" };
|
||||
super::run_and_close(app, cmd, json!({ "color": color_json(&c) }))
|
||||
let (cmd, params) = paint_command(app, d);
|
||||
super::form::commit_preview(app, cmd, params)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -453,6 +487,34 @@ mod tests {
|
||||
assert_eq!(gamut_fix(&mut app, &ctx, "picker-gamut", &Color::rgb8(128, 128, 128)), None);
|
||||
}
|
||||
|
||||
/// The selected art takes each colour as it is picked; OK keeps the last as one undo step and
|
||||
/// Cancel puts the art back.
|
||||
#[test]
|
||||
fn the_selection_shows_the_colour_live() {
|
||||
let mut app = app();
|
||||
let undo = |app: &VectorcraftApp| app.session.doc().unwrap().history.undo.len();
|
||||
let before = undo(&app);
|
||||
app.run("ui.colorPicker", json!({})).unwrap();
|
||||
frame(&mut app);
|
||||
assert!(!app.session.in_interaction(), "nothing previews before the colour changes");
|
||||
for hex in ["ff0000", "00ff00"] {
|
||||
app.ui.dialog.as_mut().unwrap().fields.insert("hex".into(), json!(hex));
|
||||
frame(&mut app);
|
||||
assert_eq!(fill_hex(&app), format!("#{hex}"), "live, before OK");
|
||||
}
|
||||
super::super::confirm(&mut app).unwrap();
|
||||
assert_eq!(fill_hex(&app), "#00ff00");
|
||||
assert_eq!(undo(&app), before + 1, "one step");
|
||||
// Cancel: back to the colour it had.
|
||||
app.run("ui.colorPicker", json!({})).unwrap();
|
||||
app.ui.dialog.as_mut().unwrap().fields.insert("hex".into(), json!("0000ff"));
|
||||
frame(&mut app);
|
||||
assert_eq!(fill_hex(&app), "#0000ff");
|
||||
super::super::cancel(&mut app);
|
||||
assert_eq!(fill_hex(&app), "#00ff00");
|
||||
assert_eq!(undo(&app), before + 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn open_set_hex_and_confirm_fills() {
|
||||
let mut app = app();
|
||||
|
||||
@@ -442,12 +442,26 @@ pub fn transform_section(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
app.run("object.rotate", json!({"angle": a, "absolute": true})).ok();
|
||||
}
|
||||
ui.add_space(10.0);
|
||||
if widgets::icon_button(ui, "flip-horizontal-2", tl!("Flip Along Horizontal Axis"), false, 24.0).clicked() {
|
||||
// Lucide's flip icons are named for their mirror line: flip-vertical-2 draws a vertical
|
||||
// line (the left-right flip), flip-horizontal-2 a horizontal one (top-bottom).
|
||||
if widgets::icon_button(ui, "flip-vertical-2", tl!("Flip Along Horizontal Axis"), false, 24.0).clicked() {
|
||||
app.run("object.reflect", json!({"axis": "vertical"})).ok();
|
||||
}
|
||||
if widgets::icon_button(ui, "flip-vertical-2", tl!("Flip Along Vertical Axis"), false, 24.0).clicked() {
|
||||
if widgets::icon_button(ui, "flip-horizontal-2", tl!("Flip Along Vertical Axis"), false, 24.0).clicked() {
|
||||
app.run("object.reflect", json!({"axis": "horizontal"})).ok();
|
||||
}
|
||||
// More Options: the Transform panel's Scale Corners and Scale Strokes & Effects, open
|
||||
// while they are toggled.
|
||||
let more = widgets::icon_button(ui, "ellipsis", tl!("More Options"), false, 24.0);
|
||||
egui::Popup::menu(&more).close_behavior(egui::PopupCloseBehavior::CloseOnClickOutside).show(|ui| {
|
||||
let (sc, ss) = (app.session.prefs.scale_corners, app.session.prefs.scale_strokes);
|
||||
if widgets::check(ui, tl!("Scale Corners"), sc, true) {
|
||||
super::transform::set_pref(app, "scaleCorners", !sc);
|
||||
}
|
||||
if widgets::check(ui, tl!("Scale Strokes & Effects"), ss, true) {
|
||||
super::transform::set_pref(app, "scaleStrokes", !ss);
|
||||
}
|
||||
});
|
||||
});
|
||||
// Live shape properties.
|
||||
if let Some(n) = first_selected(app)
|
||||
@@ -623,6 +637,44 @@ mod tests {
|
||||
assert!(widths.iter().all(|w| (w - widths[0]).abs() < 1.0), "X, rotation, corner radius, opacity: {widths:?}");
|
||||
}
|
||||
|
||||
/// The Transform section's More Options holds Scale Corners and Scale Strokes & Effects, and
|
||||
/// stays open while they are toggled.
|
||||
#[test]
|
||||
fn transform_more_options_toggles_scale_corners_and_strokes() {
|
||||
let mut app = VectorcraftApp::new(Session::new(), Default::default());
|
||||
app.run("file.new", json!({"width": 200, "height": 100})).unwrap();
|
||||
app.run("shape.rectangle", json!({"x": 10, "y": 20, "width": 30, "height": 40})).unwrap();
|
||||
let ctx = egui::Context::default();
|
||||
let has = |texts: &[(String, Rect)], s: &str| texts.iter().any(|(t, _)| t == s);
|
||||
frame(&mut app, &ctx, vec![]);
|
||||
let texts = frame(&mut app, &ctx, vec![]);
|
||||
assert!(!has(&texts, "Scale Corners"), "closed at first");
|
||||
// The button is an icon at the end of the angle row, under the H: field: sweep that row.
|
||||
let h = texts.iter().find(|(t, _)| t == "H:").unwrap().1;
|
||||
let mut open = None;
|
||||
'sweep: for dy in (12..60).step_by(6) {
|
||||
for x in (120..300).step_by(6) {
|
||||
let at = egui::pos2(x as f32, h.bottom() + dy as f32);
|
||||
let b = |pressed| Event::PointerButton { pos: at, button: PointerButton::Primary, pressed, modifiers: Default::default() };
|
||||
frame(&mut app, &ctx, vec![Event::PointerMoved(at), b(true)]);
|
||||
frame(&mut app, &ctx, vec![b(false)]);
|
||||
let texts = frame(&mut app, &ctx, vec![]);
|
||||
if has(&texts, "Scale Corners") {
|
||||
open = Some(texts);
|
||||
break 'sweep;
|
||||
}
|
||||
}
|
||||
}
|
||||
let texts = open.expect("More Options opens the popup");
|
||||
let (sc, ss) = (app.session.prefs.scale_corners, app.session.prefs.scale_strokes);
|
||||
click(&mut app, &ctx, &texts, "Scale Corners");
|
||||
assert_eq!(app.session.prefs.scale_corners, !sc);
|
||||
let texts = frame(&mut app, &ctx, vec![]);
|
||||
assert!(has(&texts, "Scale Strokes & Effects"), "still open after a toggle");
|
||||
click(&mut app, &ctx, &texts, "Scale Strokes & Effects");
|
||||
assert_eq!(app.session.prefs.scale_strokes, !ss);
|
||||
}
|
||||
|
||||
/// Both panels forward the Move Artwork toggle when their actual X field is edited.
|
||||
#[test]
|
||||
fn artboard_coordinate_fields_move_art_in_properties_and_transform() {
|
||||
|
||||
@@ -167,10 +167,12 @@ pub fn show(app: &mut VectorcraftApp, ui: &mut Ui) {
|
||||
});
|
||||
});
|
||||
ui.horizontal(|ui| {
|
||||
if widgets::icon_button_enabled(ui, "flip-horizontal-2", tl!("Flip Horizontal"), false, has, 24.0).clicked() {
|
||||
// Lucide's flip icons are named for their mirror line: flip-vertical-2 draws a vertical
|
||||
// line (the left-right flip), flip-horizontal-2 a horizontal one (top-bottom).
|
||||
if widgets::icon_button_enabled(ui, "flip-vertical-2", tl!("Flip Horizontal"), false, has, 24.0).clicked() {
|
||||
app.run("object.reflect", json!({"axis": "vertical", "origin": origin})).ok();
|
||||
}
|
||||
if widgets::icon_button_enabled(ui, "flip-vertical-2", tl!("Flip Vertical"), false, has, 24.0).clicked() {
|
||||
if widgets::icon_button_enabled(ui, "flip-horizontal-2", tl!("Flip Vertical"), false, has, 24.0).clicked() {
|
||||
app.run("object.reflect", json!({"axis": "horizontal", "origin": origin})).ok();
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user