Skip to content

Commit 9ec4cad

Browse files
committed
Code review
1 parent 1b1dcd0 commit 9ec4cad

3 files changed

Lines changed: 50 additions & 47 deletions

File tree

editor/src/messages/tool/common_functionality/shape_editor.rs

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::f64::consts::PI;
2-
31
use super::graph_modification_utils::merge_layers;
42
use super::snapping::{SnapCache, SnapCandidatePoint, SnapData, SnapManager, SnappedPoint};
53
use super::utility_functions::{adjust_handle_colinearity, calculate_bezier_bbox, calculate_segment_angle, restore_g1_continuity, restore_previous_handle_position};
@@ -17,6 +15,7 @@ use bezier_rs::{Bezier, BezierHandles, Subpath, TValue};
1715
use glam::{DAffine2, DVec2};
1816
use graphene_std::vector::{HandleExt, HandleId, SegmentId};
1917
use graphene_std::vector::{ManipulatorPointId, PointId, VectorData, VectorModificationType};
18+
use std::f64::consts::TAU;
2019

2120
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
2221
pub enum SelectionChange {
@@ -1034,8 +1033,8 @@ impl ShapeState {
10341033
/// If both or neither handles are selected, the angle of both handles will be averaged from their current angles, weighted by their lengths.
10351034
/// Assumes all selected manipulators have handles that are already not colinear.
10361035
///
1037-
/// For vector meshes, non colinear handle which is nearest in the direction of 180° angle separation, becomes colinear with current handle,
1038-
/// if there is no such handle, nothing happens
1036+
/// For vector meshes, the non-colinear handle which is nearest in the direction of 180° angle separation becomes colinear with current handle.
1037+
/// If there is no such handle, nothing happens.
10391038
pub fn convert_selected_manipulators_to_colinear_handles(&self, responses: &mut VecDeque<Message>, document: &DocumentMessageHandler) {
10401039
let mut skip_set = HashSet::new();
10411040

@@ -1055,43 +1054,42 @@ impl ShapeState {
10551054

10561055
// Here we take handles as the current handle and the most opposite non-colinear-handle
10571056

1058-
let is_handle_non_colinear = |handle: HandleId| -> bool { !(vector_data.colinear_manipulators.iter().any(|&handles| handles[0] == handle || handles[1] == handle)) };
1057+
let is_handle_colinear = |handle: HandleId| -> bool { vector_data.colinear_manipulators.iter().any(|&handles| handles[0] == handle || handles[1] == handle) };
10591058

1060-
let other_handles = match point {
1061-
ManipulatorPointId::Anchor(_) => point.get_handle_pair(&vector_data),
1062-
_ => {
1063-
point.get_all_connected_handles(&vector_data).and_then(|handles| {
1064-
let mut non_colinear_handles = handles.iter().filter(|&handle| is_handle_non_colinear(*handle)).clone().collect::<Vec<_>>();
1059+
let other_handles = if matches!(point, ManipulatorPointId::Anchor(_)) {
1060+
point.get_handle_pair(&vector_data)
1061+
} else {
1062+
point.get_all_connected_handles(&vector_data).and_then(|handles| {
1063+
let mut non_colinear_handles = handles.iter().filter(|&handle| !is_handle_colinear(*handle)).clone().collect::<Vec<_>>();
10651064

1066-
// Sort these by angle from the current handle
1067-
non_colinear_handles.sort_by(|&handle_a, &handle_b| {
1068-
let anchor = point.get_anchor_position(&vector_data).expect("No anchor position for handle");
1069-
let orig_handle_pos = point.get_position(&vector_data).expect("No handle position");
1065+
// Sort these by angle from the current handle
1066+
non_colinear_handles.sort_by(|&handle_a, &handle_b| {
1067+
let anchor = point.get_anchor_position(&vector_data).expect("No anchor position for handle");
1068+
let orig_handle_pos = point.get_position(&vector_data).expect("No handle position");
10701069

1071-
let a_pos = handle_a.to_manipulator_point().get_position(&vector_data).expect("No handle position");
1072-
let b_pos = handle_b.to_manipulator_point().get_position(&vector_data).expect("No handle position");
1070+
let a_pos = handle_a.to_manipulator_point().get_position(&vector_data).expect("No handle position");
1071+
let b_pos = handle_b.to_manipulator_point().get_position(&vector_data).expect("No handle position");
10731072

1074-
let v_orig = (orig_handle_pos - anchor).normalize_or_zero();
1073+
let v_orig = (orig_handle_pos - anchor).normalize_or_zero();
10751074

1076-
let v_a = (a_pos - anchor).normalize_or_zero();
1077-
let v_b = (b_pos - anchor).normalize_or_zero();
1075+
let v_a = (a_pos - anchor).normalize_or_zero();
1076+
let v_b = (b_pos - anchor).normalize_or_zero();
10781077

1079-
let angle_a = v_orig.angle_to(v_a).abs();
1080-
let angle_b = v_orig.angle_to(v_b).abs();
1078+
let angle_a = v_orig.angle_to(v_a).abs();
1079+
let angle_b = v_orig.angle_to(v_b).abs();
10811080

1082-
// Sort by descending angle (180° is farthest)
1083-
angle_b.partial_cmp(&angle_a).unwrap_or(std::cmp::Ordering::Equal)
1084-
});
1081+
// Sort by descending angle (180° is furthest)
1082+
angle_b.partial_cmp(&angle_a).unwrap_or(std::cmp::Ordering::Equal)
1083+
});
10851084

1086-
let current = match point {
1087-
ManipulatorPointId::EndHandle(segment) => HandleId::end(segment),
1088-
ManipulatorPointId::PrimaryHandle(segment) => HandleId::primary(segment),
1089-
ManipulatorPointId::Anchor(_) => unreachable!(),
1090-
};
1085+
let current = match point {
1086+
ManipulatorPointId::EndHandle(segment) => HandleId::end(segment),
1087+
ManipulatorPointId::PrimaryHandle(segment) => HandleId::primary(segment),
1088+
ManipulatorPointId::Anchor(_) => unreachable!(),
1089+
};
10911090

1092-
if let Some(other) = non_colinear_handles.iter().next() { Some([current, **other]) } else { None }
1093-
})
1094-
}
1091+
non_colinear_handles.first().map(|other| [current, **other])
1092+
})
10951093
};
10961094

10971095
let Some(handles) = other_handles else { continue };
@@ -1554,14 +1552,12 @@ impl ShapeState {
15541552
/// Disable colinear handles colinear.
15551553
pub fn disable_colinear_handles_state_on_selected(&self, network_interface: &NodeNetworkInterface, responses: &mut VecDeque<Message>) {
15561554
for (&layer, state) in &self.selected_shape_state {
1557-
let Some(vector_data) = network_interface.compute_modified_vector(layer) else {
1558-
continue;
1559-
};
1555+
let Some(vector_data) = network_interface.compute_modified_vector(layer) else { continue };
15601556

15611557
for &point in &state.selected_points {
15621558
if let ManipulatorPointId::Anchor(point) = point {
15631559
for connected in vector_data.all_connected(point) {
1564-
if let Some(&handles) = vector_data.colinear_manipulators.iter().find(|target| target.iter().any(|&target| target == connected)) {
1560+
if let Some(&handles) = vector_data.colinear_manipulators.iter().find(|target| target.contains(&connected)) {
15651561
let modification_type = VectorModificationType::SetG1Continuous { handles, enabled: false };
15661562
responses.add(GraphOperationMessage::Vector { layer, modification_type });
15671563
}
@@ -1800,7 +1796,7 @@ impl ShapeState {
18001796
let angle = base.angle_to(to);
18011797
let cross = base.perp_dot(to);
18021798

1803-
if cross < 0.0 { 2.0 * PI - angle } else { angle }
1799+
if cross < 0. { TAU - angle } else { angle }
18041800
};
18051801

18061802
let angle_a = signed_angle(v_orig, v_a);

editor/src/messages/tool/tool_messages/path_tool.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ impl PathToolData {
585585
let vector_data = document.network_interface.compute_modified_vector(single_selected_point.layer).unwrap();
586586
if single_selected_point.id.get_handle_pair(&vector_data).is_some() {
587587
let anchor = single_selected_point.id.get_anchor(&vector_data).expect("Cannot find connected anchor");
588-
if vector_data.all_connected(anchor).count() > 2 { false } else { true }
588+
vector_data.all_connected(anchor).count() <= 2
589589
} else {
590590
false
591591
}
@@ -605,7 +605,7 @@ impl PathToolData {
605605
}
606606

607607
fn next_drill_through_cycle(&mut self, position: DVec2) -> usize {
608-
if self.last_drill_through_click_position.map_or(true, |last_pos| last_pos.distance(position) > DRILL_THROUGH_THRESHOLD) {
608+
if self.last_drill_through_click_position.is_none_or(|last_pos| last_pos.distance(position) > DRILL_THROUGH_THRESHOLD) {
609609
// New position, reset cycle
610610
self.drill_through_cycle_index = 0;
611611
} else {
@@ -625,7 +625,7 @@ impl PathToolData {
625625
}
626626

627627
fn has_drill_through_mouse_moved(&self, position: DVec2) -> bool {
628-
self.last_drill_through_click_position.map_or(true, |last_pos| last_pos.distance(position) > DRILL_THROUGH_THRESHOLD)
628+
self.last_drill_through_click_position.is_none_or(|last_pos| last_pos.distance(position) > DRILL_THROUGH_THRESHOLD)
629629
}
630630

631631
fn set_ghost_outline(&mut self, shape_editor: &ShapeState, document: &DocumentMessageHandler) {
@@ -2411,15 +2411,15 @@ impl Fsm for PathToolFsmState {
24112411

24122412
let compatible_type = first_layer.and_then(|layer| {
24132413
let graph_layer = graph_modification_utils::NodeGraphLayer::new(layer, &document.network_interface);
2414-
graph_layer.horizontal_layer_flow().nth(1).and_then(|node_id| {
2414+
graph_layer.horizontal_layer_flow().nth(1).map(|node_id| {
24152415
let (output_type, _) = document.network_interface.output_type(&node_id, 0, &[]);
2416-
Some(format!("type:{}", output_type.nested_type()))
2416+
format!("type:{}", output_type.nested_type())
24172417
})
24182418
});
24192419

24202420
let is_compatible = compatible_type.as_deref() == Some("type:Instances<VectorData>");
24212421

2422-
let is_modifiable = first_layer.map_or(false, |layer| {
2422+
let is_modifiable = first_layer.is_some_and(|layer| {
24232423
let graph_layer = graph_modification_utils::NodeGraphLayer::new(layer, &document.network_interface);
24242424
matches!(graph_layer.find_input("Path", 1), Some(TaggedValue::VectorModification(_)))
24252425
});

node-graph/gcore/src/vector/vector_data.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -561,19 +561,26 @@ impl ManipulatorPointId {
561561
}
562562
}
563563

564-
/// Finds all the connected handles of a point. For an anchor it is all the connected handles. For a handle it is all the handles connected to its corresponding anchor other than current handle
564+
/// Finds all the connected handles of a point.
565+
/// For an anchor it is all the connected handles.
566+
/// For a handle it is all the handles connected to its corresponding anchor other than the current handle.
565567
pub fn get_all_connected_handles(self, vector_data: &VectorData) -> Option<Vec<HandleId>> {
566568
match self {
567-
ManipulatorPointId::Anchor(point) => Some(vector_data.all_connected(point).collect::<Vec<_>>()),
569+
ManipulatorPointId::Anchor(point) => {
570+
let connected = vector_data.all_connected(point).collect::<Vec<_>>();
571+
Some(connected)
572+
}
568573
ManipulatorPointId::PrimaryHandle(segment) => {
569574
let point = vector_data.segment_domain.segment_start_from_id(segment)?;
570575
let current = HandleId::primary(segment);
571-
Some(vector_data.segment_domain.all_connected(point).filter(|&value| value != current).collect::<Vec<_>>())
576+
let connected = vector_data.segment_domain.all_connected(point).filter(|&value| value != current).collect::<Vec<_>>();
577+
Some(connected)
572578
}
573579
ManipulatorPointId::EndHandle(segment) => {
574580
let point = vector_data.segment_domain.segment_end_from_id(segment)?;
575581
let current = HandleId::end(segment);
576-
Some(vector_data.segment_domain.all_connected(point).filter(|&value| value != current).collect::<Vec<_>>())
582+
let connected = vector_data.segment_domain.all_connected(point).filter(|&value| value != current).collect::<Vec<_>>();
583+
Some(connected)
577584
}
578585
}
579586
}

0 commit comments

Comments
 (0)