Merge pull request #3057 from nrq6993-a11y/stream/vc-feat-g1-grid-ref-point

G1: Rectangular and Polar Grid dialogs reference point locator (#1377)
This commit is contained in:
BFlat
2026-10-20 06:14:43 +07:00
committed by GitHub
3 changed files with 91 additions and 7 deletions
+55 -3
View File
@@ -2,10 +2,13 @@
//! Star, Line Segment Tool Options and the two grids (size, divider counts and skews, switches).
use serde_json::{Value, json};
use vectorcraft_geom::{Point, Rect};
use super::{DialogSpec, form, run_and_close};
use crate::VectorcraftApp;
use crate::panels::transform::artboard_rect;
use crate::state::Dialog;
use crate::widgets;
pub(super) const SPEC: DialogSpec = DialogSpec { heading: |d| title(&d.kind).into(), body, confirm, ..DialogSpec::FORM };
@@ -27,9 +30,57 @@ fn grid_keys(kind: &str) -> ([&'static str; 2], [&'static str; 2]) {
if kind == "polarGrid" { (["concentric", "radial"], ["concentricSkew", "radialSkew"]) } else { (["rows", "columns"], ["rowSkew", "columnSkew"]) }
}
/// The 9-point locator index for a grid dialog (`reference`, default top-left).
fn grid_reference(d: &Dialog) -> usize {
d.fields.get("reference").and_then(|v| v.as_u64()).map(|n| (n as usize).min(8)).unwrap_or(0)
}
/// A grid dialog's bounding box from its top-left `x`/`y` and size fields.
fn grid_placed(d: &Dialog) -> Rect {
let (x, y) = (d.f64("x", 0.0), d.f64("y", 0.0));
Rect::new(x, y, x + d.f64("width", 100.0), y + d.f64("height", 100.0))
}
fn set_grid_placed(d: &mut Dialog, r: Rect) {
d.fields.insert("x".into(), json!(r.x0));
d.fields.insert("y".into(), json!(r.y0));
d.fields.insert("width".into(), json!(r.width()));
d.fields.insert("height".into(), json!(r.height()));
}
/// Top-left `x`/`y` so reference point `refi` sits on the canvas click (`__anchorX`/`__anchorY`).
pub(crate) fn grid_pin_to_anchor(d: &mut Dialog) {
let anchor = Point::new(d.f64("__anchorX", d.f64("x", 0.0)), d.f64("__anchorY", d.f64("y", 0.0)));
let refi = grid_reference(d);
let (w, h) = (d.f64("width", 100.0), d.f64("height", 100.0));
let rp = vectorcraft_geom::reference_point(Rect::new(0.0, 0.0, w, h), refi);
d.fields.insert("x".into(), json!(anchor.x - rp.x));
d.fields.insert("y".into(), json!(anchor.y - rp.y));
}
/// The form's fields, then a grid's switches.
fn body(app: &mut VectorcraftApp, ui: &mut egui::Ui, d: &mut Dialog) -> bool {
form::grid(ui, d, app.session.general_unit());
let unit = app.session.general_unit();
if matches!(d.kind.as_str(), "rectangularGrid" | "polarGrid") {
ui.horizontal(|ui| {
let refi = grid_reference(d);
if let Some(i) = widgets::reference_point(ui, refi) {
d.fields.insert("reference".into(), json!(i));
grid_pin_to_anchor(d);
}
});
ui.add_space(4.0);
let refi = grid_reference(d);
let (was, w0, h0) = (grid_placed(d), d.f64("width", 100.0), d.f64("height", 100.0));
form::grid_then(ui, d, unit, |_, d| {
let (w, h) = (d.f64("width", 100.0), d.f64("height", 100.0));
if (w - w0).abs() > 1e-9 || (h - h0).abs() > 1e-9 {
set_grid_placed(d, artboard_rect(was, refi, None, None, Some(w), Some(h), false));
}
});
} else {
form::grid(ui, d, unit);
}
let own = match d.kind.as_str() {
"rectangularGrid" => ("frame", tl!("Use Outside Rectangle As Frame")),
"polarGrid" => ("compound", tl!("Create Compound Path From Ellipses")),
@@ -54,8 +105,9 @@ fn confirm(app: &mut VectorcraftApp, d: &Dialog) -> Result<Value, String> {
kind @ ("rectangularGrid" | "polarGrid") => {
// The switches as they are; the typed fields as numbers.
let (mut p, (counts, skews)) = (form::params(d), grid_keys(kind));
for (k, default) in [("width", 100.0), ("height", 100.0)] {
p[k] = json!(d.f64(k, default));
let r = grid_placed(d);
for (k, v) in [("x", r.x0), ("y", r.y0), ("width", r.width()), ("height", r.height())] {
p[k] = json!(v);
}
for (count, skew) in counts.into_iter().zip(skews) {
p[count] = json!((d.f64(count, 5.0) as u64).min(999));
@@ -159,6 +159,31 @@ fn key_frame(app: &mut VectorcraftApp, key: egui::Key) {
out.textures_delta.clear();
}
#[test]
fn rectangular_grid_reference_locator_reanchors_to_the_click() {
let mut app = app();
click(&mut app, Point::new(100.0, 100.0));
set(&mut app, "reference", json!(4));
super::shapes::grid_pin_to_anchor(app.ui.dialog.as_mut().unwrap());
assert_eq!((field(&app, "x"), field(&app, "y")), (json!(50.0), json!(50.0)));
let id = confirm(&mut app);
assert_grid(&app, id, Rect::new(50.0, 50.0, 150.0, 150.0), 5, 5);
}
/// #1377: OK uses the bounding box after a reference-point resize (`artboard_rect` keeps the
/// center pinned when width doubles).
#[test]
fn rectangular_grid_reference_point_pins_center_when_width_changes() {
let mut app = app();
click(&mut app, Point::new(100.0, 100.0));
assert_eq!(field(&app, "reference"), json!(0));
for (key, value) in [("reference", json!(4)), ("x", json!(50.0)), ("y", json!(50.0)), ("width", json!(200)), ("height", json!(100))] {
set(&mut app, key, value);
}
let id = confirm(&mut app);
assert_grid(&app, id, Rect::new(50.0, 50.0, 250.0, 150.0), 5, 5);
}
#[test]
fn rectangular_grid_canvas_click_opens_registered_dialog_with_saved_counts_and_point_defaults() {
let mut app = app();
+11 -4
View File
@@ -3,6 +3,7 @@
use serde_json::{Value, json};
use super::shapes::grid_pin_to_anchor;
use crate::VectorcraftApp;
use crate::state::Dialog;
@@ -18,11 +19,17 @@ pub fn open_tool_dialog(app: &mut VectorcraftApp, kind: &str, p: Value) {
"lineSegment" => Dialog::new(kind, json!({"x": x, "y": y, "length": "100 pt", "angle": 0})),
// The grids: the tool's stored options (divider counts and skews, switches) at the default size.
"rectangularGrid" | "polarGrid" => {
let mut fields = app.session.tool_options_of(kind);
for (key, value) in [("x", json!(x)), ("y", json!(y)), ("width", json!("100 pt")), ("height", json!("100 pt"))] {
fields[key] = value;
let mut obj = match app.session.tool_options_of(kind) {
Value::Object(o) => o,
_ => serde_json::Map::new(),
};
for (key, value) in [("__anchorX", json!(x)), ("__anchorY", json!(y)), ("width", json!("100 pt")), ("height", json!("100 pt"))] {
obj.insert(key.into(), value);
}
Dialog::new(kind, fields)
obj.insert("reference".into(), json!(0));
let mut d = Dialog::new(kind, Value::Object(obj));
grid_pin_to_anchor(&mut d);
d
}
// Graph tool click: the graph's size.
"graph" => Dialog::new(