mirror of
https://github.com/storytold/cadcraft.git
synced 2026-12-02 02:38:30 -06:00
Merge #2368: Array base points: the Key point option in ARRAYRECT, ARRAYPOLAR and ARRAYEDIT (F5)
This commit is contained in:
@@ -18,7 +18,7 @@ use super::super::helpers::v3;
|
||||
use super::super::machines::{SelOutcome, SelectPhase, number};
|
||||
use super::super::{CommandSpec, bad, bool_or, point_param, targets};
|
||||
|
||||
use super::{KEYWORDS, angle, check_size, count, fmt, nesting, other, yes, yes_no};
|
||||
use super::{KEY_POINT_PROMPT, KEYWORDS, angle, check_size, count, fmt, key_point_of, nearest_key_point, nesting, other, yes, yes_no};
|
||||
use crate::{Accept, Input, Interactive, Prompt, Result, Session, Step};
|
||||
|
||||
pub(super) fn specs() -> Vec<CommandSpec> {
|
||||
@@ -26,7 +26,7 @@ pub(super) fn specs() -> Vec<CommandSpec> {
|
||||
CommandSpec::new("arrayedit", "Edit Array", run_arrayedit)
|
||||
.menu(&["Modify", "Object", "Array"])
|
||||
.params(
|
||||
"{handle, cols?, rows?, colSpacing?, rowSpacing?, axisAngle? (degrees), rowElevation?, levels?, levelSpacing?, items?, angle? (fill, degrees), angleBetween?, rotate?: bool, center? ([x, y]), base? ([x, y], the new base point), reset?: bool, replace?: {handles, base? ([x, y], default the objects' centre), items: [[i, j, level]…] | \"source\"}, source?: {item? ([i, j, level], default [0, 0, 0])} (places the source objects for editing; ARRAYCLOSE ends)}",
|
||||
"{handle, cols?, rows?, colSpacing?, rowSpacing?, axisAngle? (degrees), rowElevation?, levels?, levelSpacing?, items?, angle? (fill, degrees), angleBetween?, rotate?: bool, center? ([x, y]), base? ([x, y], the new base point), reset?: bool, replace?: {handles, base? ([x, y], default the objects' centre), items: [[i, j, level]…] | \"source\"}, keyPoint? ([x, y]: the new base point is the source objects' key point nearest it, on the item there), replace?.keyPoint? ([x, y]: the replacement objects' key point nearest it is their base point), source?: {item? ([i, j, level], default [0, 0, 0])} (places the source objects for editing; ARRAYCLOSE ends)}",
|
||||
)
|
||||
.interactive(|_| Ok(Box::new(EditM::default()))),
|
||||
CommandSpec::new("arrayclose", "Close Array Editing", run_arrayclose)
|
||||
@@ -215,6 +215,16 @@ fn item_at(d: &Drawing, h: Handle, at: Vec2, tol: f64) -> Option<(u32, u32, u32)
|
||||
best.map(|b| b.0)
|
||||
}
|
||||
|
||||
/// ARRAYEDIT Key point: the key point of the source objects nearest `at`, with the objects as
|
||||
/// placed at the item under `at` (within `tol`).
|
||||
fn source_key_point(d: &Drawing, h: Handle, at: Vec2, tol: f64) -> Result<Vec2> {
|
||||
let item = item_at(d, h, at, tol).ok_or_else(|| other("No array item there; specify a point on an item of the array."))?;
|
||||
let (ins, blk, p) = array_of(d, h)?;
|
||||
let src = d.block(&p.source).ok_or_else(|| other("The array's source block is missing."))?;
|
||||
let m = ins.transform(blk.base.xy()).then_before(item_place(&p, item)?).then_before(Mat3::translate(-src.base.xy()));
|
||||
nearest_key_point(src.entities.iter().map(|e| e.as_ref()), &m, at).ok_or_else(|| other("The source objects have no key point; specify a point."))
|
||||
}
|
||||
|
||||
/// Move the base point of array `h` to world point `base` without moving the items.
|
||||
fn rebase(s: &mut Session, h: Handle, base: Vec2) -> Result<()> {
|
||||
let d = s.doc()?;
|
||||
@@ -343,6 +353,9 @@ fn close_source(s: &mut Session, save: bool) -> Result<Value> {
|
||||
|
||||
// ---------------- JSON ----------------
|
||||
|
||||
/// How far outside an item's extents a JSON `keyPoint` may be.
|
||||
const KEY_POINT_TOLERANCE: f64 = 1e-6;
|
||||
|
||||
fn item_param(v: &Value) -> Option<(u32, u32, u32)> {
|
||||
let a = v.as_array()?;
|
||||
let n = |k: usize| a.get(k).and_then(Value::as_u64).and_then(|x| u32::try_from(x).ok());
|
||||
@@ -459,7 +472,10 @@ fn run_arrayedit(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
}
|
||||
if let Some(r) = p.get("replace").filter(|v| !v.is_null()) {
|
||||
let hs = targets(s, r)?;
|
||||
let base = point_param(r, "base").unwrap_or_else(|| s.doc().map(|d| centroid(d, &hs)).unwrap_or_default());
|
||||
let base = match point_param(r, "keyPoint") {
|
||||
Some(k) => key_point_of(s.doc()?, &hs, k)?,
|
||||
None => point_param(r, "base").unwrap_or_else(|| s.doc().map(|d| centroid(d, &hs)).unwrap_or_default()),
|
||||
};
|
||||
let name = block_from(s, h, &hs, base)?;
|
||||
let (_, _, mut a) = array_of(s.doc()?, h)?;
|
||||
match r.get("items") {
|
||||
@@ -477,6 +493,10 @@ fn run_arrayedit(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
if let Some(b) = point_param(p, "base") {
|
||||
rebase(s, h, b)?;
|
||||
}
|
||||
if let Some(k) = point_param(p, "keyPoint") {
|
||||
let at = source_key_point(s.doc()?, h, k, KEY_POINT_TOLERANCE)?;
|
||||
rebase(s, h, at)?;
|
||||
}
|
||||
let (_, _, mut a) = array_of(s.doc()?, h)?;
|
||||
let before = a.clone();
|
||||
apply_params(&mut a, p, &to_local)?;
|
||||
@@ -507,6 +527,8 @@ enum Stage {
|
||||
Select,
|
||||
Main,
|
||||
Base,
|
||||
/// Base point: Key point (of the source objects).
|
||||
BaseKey,
|
||||
ColsCount,
|
||||
ColsSpace,
|
||||
ColsTotal,
|
||||
@@ -524,6 +546,8 @@ enum Stage {
|
||||
/// REPlace: objects, their base point, then the items to replace.
|
||||
RepSelect,
|
||||
RepBase,
|
||||
/// REPlace base point: Key point (of the replacement objects).
|
||||
RepKey,
|
||||
RepItems,
|
||||
/// Source: the item whose objects are placed for editing.
|
||||
SourceItem,
|
||||
@@ -659,6 +683,7 @@ impl Interactive for EditM {
|
||||
.kw(&["Source", "REPlace", "Base point", "Rows", "Columns", "Levels", "RESet", "eXit"])
|
||||
.default("eXit"),
|
||||
Base => Prompt::new("Specify base point", Accept::POINT).kw(&["Key point"]).default("centroid"),
|
||||
BaseKey => Prompt::new(KEY_POINT_PROMPT, Accept::POINT),
|
||||
ColsCount => Prompt::new("Enter the number of columns", Accept::NUMBER).default(cols.to_string()),
|
||||
ColsSpace => dist("Specify the distance between columns", cs).kw(&["Total"]),
|
||||
ColsTotal => dist("Specify the distance between the first and last column", total(cols, cs)),
|
||||
@@ -687,6 +712,7 @@ impl Interactive for EditM {
|
||||
Rotate => Prompt::new("Rotate arrayed items?", KEYWORDS).kw(&["Yes", "No"]).default(yes(polar.as_ref().is_none_or(|k| k.rotate))),
|
||||
RepSelect => Prompt::new("Select replacement objects", Accept::SELECT),
|
||||
RepBase => Prompt::new("Select base point of replacement objects", Accept::POINT).kw(&["Key point"]).default("centroid"),
|
||||
RepKey => Prompt::new("Specify key point on replacement objects to use as base point", Accept::POINT),
|
||||
RepItems => Prompt::new("Select an item in array to replace", Accept::POINT).kw(&["Source objects"]),
|
||||
SourceItem => Prompt::new("Select item in array", Accept::POINT),
|
||||
}
|
||||
@@ -746,10 +772,21 @@ impl Interactive for EditM {
|
||||
}
|
||||
Main
|
||||
}
|
||||
(Base | RepBase, Input::Keyword(_)) => {
|
||||
s.echo("Key points are not available yet; specify a point.");
|
||||
stage
|
||||
(Base, Input::Keyword(_)) => BaseKey,
|
||||
(RepBase, Input::Keyword(_)) => RepKey,
|
||||
(BaseKey, Input::Point(p)) => {
|
||||
let tol = s.pixel_size() * s.settings.pickbox;
|
||||
let at = source_key_point(s.doc()?, h, *p, tol)?;
|
||||
rebase(s, h, at)?;
|
||||
Main
|
||||
}
|
||||
(BaseKey, Input::Enter) => Base,
|
||||
(RepKey, Input::Point(p)) => {
|
||||
let base = key_point_of(s.doc()?, &self.rep_objs, *p)?;
|
||||
self.rep_block = Some(block_from(s, h, &self.rep_objs.clone(), base)?);
|
||||
RepItems
|
||||
}
|
||||
(RepKey, Input::Enter) => RepBase,
|
||||
(ColsCount | Items, _) => match count(&i, c64, self.max_for(s, r64.saturating_mul(l64)))? {
|
||||
Some(n) => {
|
||||
let n = super::count32(n);
|
||||
|
||||
Reference in New Issue
Block a user