forked from GraphiteEditor/Graphite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_modification_nodes.rs
More file actions
48 lines (41 loc) · 1.79 KB
/
vector_modification_nodes.rs
File metadata and controls
48 lines (41 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
use core_types::Ctx;
use core_types::table::Table;
use core_types::uuid::NodeId;
use glam::DAffine2;
use graphic_types::Vector;
use vector_types::vector::VectorModification;
/// Applies a differential modification to a vector path, associating changes made by the Pen and Path tools to indices of edited points and segments.
#[node_macro::node(category(""))]
async fn path_modify(_ctx: impl Ctx, mut vector: Table<Vector>, modification: Box<VectorModification>, node_path: Vec<NodeId>) -> Table<Vector> {
use core_types::table::TableRow;
if vector.is_empty() {
vector.push(TableRow::default());
}
let row = vector.get_mut(0).expect("push should give one item");
modification.apply(row.element);
// Update the source node id
let this_node_path = node_path.iter().rev().nth(1).copied();
*row.source_node_id = row.source_node_id.or(this_node_path);
if vector.len() > 1 {
warn!("The path modify ran on {} vector rows. Only the first can be modified.", vector.len());
}
vector
}
/// Applies the vector path's local transformation to its geometry and resets the transform to the identity.
#[node_macro::node(category("Vector"))]
async fn apply_transform(_ctx: impl Ctx, mut vector: Table<Vector>) -> Table<Vector> {
for row in vector.iter_mut() {
let vector = row.element;
let transform = *row.transform;
//iterating over points to apply the transformation(geometry to vector space)
for (_, point) in vector.point_domain.positions_mut() {
*point = transform.transform_point2(*point);
}
//iterating over segment handles to transform them as well(geometry to vector space)
for (handles, _start_index, _end_index) in vector.segment_domain.handles_and_points_mut() {
*handles = handles.apply_transformation(|p| transform.transform_point2(p));
}
*row.transform = DAffine2::IDENTITY;
}
vector
}