mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 07:03:25 +00:00
Width Point Edit: the dialog and stroke.widthPoint.set share one pairing rule
The dialog and stroke.widthPoint.set each tested whether two width points are the halves of one discontinuous point. The test is now one engine function, width_point_pair: a point and the next one are the two halves when they are the only points within 1e-6 of the first one's place. The dialog still picks which point to test, and the command still returns its error when the indices are not a pair.
This commit is contained in:
@@ -214,6 +214,15 @@ fn uniform() -> WidthProfile {
|
||||
/// Width points closer than this (in t) are at the same place: a discontinuous point.
|
||||
const SAME_T: f64 = 1e-6;
|
||||
|
||||
/// The outgoing half of the discontinuous width point whose incoming half is point `a`: point `a`
|
||||
/// and the next one are the two halves when they are the only points within 1e-6 (in t) of `a`'s
|
||||
/// place. None when `a` is out of range or not such an incoming half.
|
||||
pub fn width_point_pair(points: &[(f64, f64, f64)], a: usize) -> Option<usize> {
|
||||
let t = points.get(a)?.0;
|
||||
let at: Vec<usize> = (0..points.len()).filter(|i| (points[*i].0 - t).abs() <= SAME_T).collect();
|
||||
(at == [a, a + 1]).then_some(a + 1)
|
||||
}
|
||||
|
||||
/// The stroke of `id` that width point commands edit (a compound path's for its members).
|
||||
fn stroke_mut(d: &mut Document, id: NodeId) -> Result<&mut vectorcraft_doc::StrokeLayer> {
|
||||
let id = stroke_owner(d, id);
|
||||
@@ -316,11 +325,10 @@ fn set_pair(
|
||||
single: Option<bool>,
|
||||
) -> Result<usize> {
|
||||
const C: &str = "stroke.widthPoint.set";
|
||||
let at = points.get(i).map_or(f64::NAN, |q| q.0);
|
||||
let halves: Vec<usize> = (0..points.len()).filter(|k| (points[*k].0 - at).abs() <= SAME_T).collect();
|
||||
if halves != [i, j] {
|
||||
if width_point_pair(points, i) != Some(j) {
|
||||
return Err(bad(C, "`index` and `outIndex` must be the two halves of one discontinuous width point"));
|
||||
}
|
||||
let at = points[i].0;
|
||||
let (old_in, old_out) = (points[i], points[j]);
|
||||
if (old_in.0 - inc.0).abs() > SAME_T {
|
||||
return Err(bad(C, "a discontinuous width point keeps its place"));
|
||||
|
||||
@@ -28,7 +28,7 @@ use vectorcraft_tools::{PaintDefaults, Tool};
|
||||
|
||||
pub use cmd::clipboard::{Clipboard, SourceLayer};
|
||||
pub use cmd::create::constrain_click;
|
||||
pub use cmd::distortcmds::perspective_click;
|
||||
pub use cmd::distortcmds::{perspective_click, width_point_pair};
|
||||
pub use cmd::graph::{GraphTypeSubject, calculated_axes as graph_calculated_axes, graph_type_subject};
|
||||
pub use cmd::params::{check_keys, check_params};
|
||||
pub use cmd::pathops::auto_simplify_level;
|
||||
|
||||
@@ -21,6 +21,7 @@ use egui::vec2;
|
||||
use serde_json::{Value, json};
|
||||
use vectorcraft_doc::Unit;
|
||||
use vectorcraft_engine::doc::NodeId;
|
||||
use vectorcraft_engine::width_point_pair;
|
||||
|
||||
use super::swatch_options::{grid, label};
|
||||
use super::{DialogSpec, run_and_close};
|
||||
@@ -30,10 +31,6 @@ use crate::{VectorcraftApp, widgets};
|
||||
/// The dialog kind of Width Point Edit.
|
||||
pub const KIND: &str = "widthPoint";
|
||||
|
||||
/// Width points closer than this (in t) are the two halves of one discontinuous point, as the
|
||||
/// engine and the Width tool count them.
|
||||
const SAME_T: f64 = 1e-6;
|
||||
|
||||
pub(super) const SPEC: DialogSpec =
|
||||
DialogSpec { heading: |_| tl!("Width Point Edit").into(), body, confirm, discard: Some("Delete"), min_width: 300.0, ..DialogSpec::FORM };
|
||||
|
||||
@@ -45,17 +42,12 @@ fn point(app: &VectorcraftApp, id: u64, index: usize) -> Option<(f64, f64, f64)>
|
||||
}
|
||||
|
||||
/// The incoming and outgoing halves of the discontinuous point that width point `index` of `id`
|
||||
/// belongs to, or none when it is a continuous point. As `stroke.widthPoint.set` pairs them, a
|
||||
/// point `a` and the next one are the two halves when they are the only points within
|
||||
/// [`SAME_T`] of `a`'s place, so either half finds the same pair.
|
||||
/// belongs to, or none when it is a continuous point. The halves are paired by
|
||||
/// [`width_point_pair`], as `stroke.widthPoint.set` pairs them, so either half finds the same pair.
|
||||
fn halves(app: &VectorcraftApp, id: u64, index: usize) -> Option<(usize, usize)> {
|
||||
let points = &app.session.active()?.doc.node(NodeId(id))?.appearance.stroke()?.profile.as_ref()?.points;
|
||||
points.get(index)?;
|
||||
let pair = |a: usize| {
|
||||
let t = points.get(a)?.0;
|
||||
let at: Vec<usize> = (0..points.len()).filter(|i| (points[*i].0 - t).abs() <= SAME_T).collect();
|
||||
(at == [a, a + 1]).then_some((a, a + 1))
|
||||
};
|
||||
let pair = |a: usize| width_point_pair(points, a).map(|b| (a, b));
|
||||
pair(index).or_else(|| index.checked_sub(1).and_then(pair))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user