Skip to content

Commit

Permalink
Avoid more nesting, and other code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Keavon committed Jan 15, 2025
1 parent 9cb7fa7 commit d0c80e2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 67 deletions.
1 change: 1 addition & 0 deletions editor/src/messages/tool/tool_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ impl MessageHandler<ToolMessage, ToolMessageData<'_>> for ToolMessageHandler {
tool.process_message(ToolMessage::UpdateCursor, responses, &mut data);
}
}

if matches!(old_tool, ToolType::Path | ToolType::Select) {
responses.add(TransformLayerMessage::CancelTransformOperation);
}
Expand Down
2 changes: 1 addition & 1 deletion editor/src/messages/tool/tool_messages/pen_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<'a> MessageHandler<ToolMessage, &mut ToolActionHandlerData<'a>> for PenTool
PointerMove,
Confirm,
Abort,
RemovePreviousHandle
RemovePreviousHandle,
),
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
let points = shape_editor.selected_points();
let selected_points: Vec<&ManipulatorPointId> = points.collect();

if selected_points.len() == 1 {
let point = &selected_points[0];
if let [point] = selected_points.as_slice() {
if let ManipulatorPointId::PrimaryHandle(_) | ManipulatorPointId::EndHandle(_) = point {
let anchor_position = point.get_anchor_position(&vector_data).unwrap();
*selected.pivot = viewspace.transform_point2(anchor_position);
Expand Down Expand Up @@ -114,10 +113,10 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
responses.add(NodeGraphMessage::RunDocumentGraph);
}
TransformLayerMessage::BeginGrab => {
if !(using_path_tool || using_select_tool)
if (!using_path_tool && !using_select_tool)
|| (using_path_tool && shape_editor.selected_points().next().is_none())
|| matches!(self.transform_operation, TransformOperation::Grabbing(_))
|| selected_layers.is_empty()
|| matches!(self.transform_operation, TransformOperation::Grabbing(_))
{
selected.original_transforms.clear();

Expand All @@ -132,7 +131,8 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
}
TransformLayerMessage::BeginRotate => {
let selected_points: Vec<&ManipulatorPointId> = shape_editor.selected_points().collect();
if !(using_path_tool || using_select_tool)

if (!using_path_tool && !using_select_tool)
|| (using_path_tool && selected_points.is_empty())
|| selected_layers.is_empty()
|| matches!(self.transform_operation, TransformOperation::Rotating(_))
Expand All @@ -141,37 +141,31 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
return;
}

if let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) {
if selected_points.len() == 1 {
let point = &selected_points[0];

match point {
ManipulatorPointId::Anchor(_) => {
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
if (handle1.get_handle_length(&vector_data) == 0. && handle2.get_handle_length(&vector_data) == 0.)
|| (handle1.get_handle_length(&vector_data) == f64::MAX && handle2.get_handle_length(&vector_data) == f64::MAX)
{
return;
}
}
}
_ => {
// TODO: Need to fix handle snap to anchor issue. SEE (https://discord.com/channels/731730685944922173/1217752903209713715).
let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) else {
selected.original_transforms.clear();
return;
};

let handle_length = point.as_handle().and_then(|handle| Some(handle.get_handle_length(&vector_data)));
if let [point] = selected_points.as_slice() {
if matches!(point, ManipulatorPointId::Anchor(_)) {
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
let handle1_length = handle1.length(&vector_data);
let handle2_length = handle2.length(&vector_data);

if let Some(length) = handle_length {
if length == 0. {
selected.original_transforms.clear();
return;
}
}
if (handle1_length == 0. && handle2_length == 0.) || (handle1_length == f64::MAX && handle2_length == f64::MAX) {
return;
}
}
} else {
// TODO: Fix handle snap to anchor issue, see <https://discord.com/channels/731730685944922173/1217752903209713715>

let handle_length = point.as_handle().map(|handle| handle.length(&vector_data));

if handle_length == Some(0.) {
selected.original_transforms.clear();
return;
}
}
} else {
selected.original_transforms.clear();
return;
}

begin_operation(self.transform_operation, &mut self.typing, &mut self.mouse_position, &mut self.start_mouse);
Expand All @@ -184,45 +178,38 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
let selected_points: Vec<&ManipulatorPointId> = shape_editor.selected_points().collect();

if (using_path_tool && selected_points.is_empty())
|| !(using_path_tool || using_select_tool)
|| matches!(self.transform_operation, TransformOperation::Scaling(_))
|| (!using_path_tool && !using_select_tool)
|| selected_layers.is_empty()
|| matches!(self.transform_operation, TransformOperation::Scaling(_))
{
selected.original_transforms.clear();
return;
}

if let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) {
if selected_points.len() == 1 {
let point = &selected_points[0];

match point {
ManipulatorPointId::Anchor(_) => {
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
let handle1_length = handle1.get_handle_length(&vector_data);
let handle2_length = handle2.get_handle_length(&vector_data);

if (handle1_length == 0. && handle2_length == 0.) || (handle1_length == f64::MAX && handle2_length == f64::MAX) {
selected.original_transforms.clear();
return;
}
}
}
_ => {
let handle_length = point.as_handle().and_then(|handle| Some(handle.get_handle_length(&vector_data)));

if let Some(length) = handle_length {
if length == 0. {
selected.original_transforms.clear();
return;
}
}
let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) else {
selected.original_transforms.clear();
return;
};

if let [point] = selected_points.as_slice() {
if matches!(point, ManipulatorPointId::Anchor(_)) {
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
let handle1_length = handle1.length(&vector_data);
let handle2_length = handle2.length(&vector_data);

if (handle1_length == 0. && handle2_length == 0.) || (handle1_length == f64::MAX && handle2_length == f64::MAX) {
selected.original_transforms.clear();
return;
}
}
} else {
let handle_length = point.as_handle().map(|handle| handle.length(&vector_data));

if handle_length == Some(0.) {
selected.original_transforms.clear();
return;
}
}
} else {
selected.original_transforms.clear();
return;
}

begin_operation(self.transform_operation, &mut self.typing, &mut self.mouse_position, &mut self.start_mouse);
Expand Down Expand Up @@ -295,6 +282,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
}
};
}

self.mouse_position = input.mouse.position;
}
TransformLayerMessage::SelectionChanged => {
Expand Down
10 changes: 5 additions & 5 deletions node-graph/gcore/src/vector/vector_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,11 @@ impl HandleId {
}
}

pub fn get_handle_length(self, vector_data: &VectorData) -> f64 {
let anchor_position = self.to_manipulator_point().get_anchor_position(&vector_data).unwrap();
let handle_position = self.to_manipulator_point().get_position(&vector_data);
let handle_length = handle_position.map(|pos| (pos - anchor_position).length()).unwrap_or(f64::MAX);
handle_length
/// Calculate the magnitude of the handle from the anchor.
pub fn length(self, vector_data: &VectorData) -> f64 {
let anchor_position = self.to_manipulator_point().get_anchor_position(vector_data).unwrap();
let handle_position = self.to_manipulator_point().get_position(vector_data);
handle_position.map(|pos| (pos - anchor_position).length()).unwrap_or(f64::MAX)
}

/// Set the handle's position relative to the anchor which is the start anchor for the primary handle and end anchor for the end handle.
Expand Down

0 comments on commit d0c80e2

Please sign in to comment.