Skip to content

Commit d0c80e2

Browse files
committed
Avoid more nesting, and other code cleanup
1 parent 9cb7fa7 commit d0c80e2

File tree

4 files changed

+56
-67
lines changed

4 files changed

+56
-67
lines changed

editor/src/messages/tool/tool_message_handler.rs

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ impl MessageHandler<ToolMessage, ToolMessageData<'_>> for ToolMessageHandler {
101101
tool.process_message(ToolMessage::UpdateCursor, responses, &mut data);
102102
}
103103
}
104+
104105
if matches!(old_tool, ToolType::Path | ToolType::Select) {
105106
responses.add(TransformLayerMessage::CancelTransformOperation);
106107
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'a> MessageHandler<ToolMessage, &mut ToolActionHandlerData<'a>> for PenTool
176176
PointerMove,
177177
Confirm,
178178
Abort,
179-
RemovePreviousHandle
179+
RemovePreviousHandle,
180180
),
181181
}
182182
}

editor/src/messages/tool/transform_layer/transform_layer_message_handler.rs

+49-61
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
8080
let points = shape_editor.selected_points();
8181
let selected_points: Vec<&ManipulatorPointId> = points.collect();
8282

83-
if selected_points.len() == 1 {
84-
let point = &selected_points[0];
83+
if let [point] = selected_points.as_slice() {
8584
if let ManipulatorPointId::PrimaryHandle(_) | ManipulatorPointId::EndHandle(_) = point {
8685
let anchor_position = point.get_anchor_position(&vector_data).unwrap();
8786
*selected.pivot = viewspace.transform_point2(anchor_position);
@@ -114,10 +113,10 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
114113
responses.add(NodeGraphMessage::RunDocumentGraph);
115114
}
116115
TransformLayerMessage::BeginGrab => {
117-
if !(using_path_tool || using_select_tool)
116+
if (!using_path_tool && !using_select_tool)
118117
|| (using_path_tool && shape_editor.selected_points().next().is_none())
119-
|| matches!(self.transform_operation, TransformOperation::Grabbing(_))
120118
|| selected_layers.is_empty()
119+
|| matches!(self.transform_operation, TransformOperation::Grabbing(_))
121120
{
122121
selected.original_transforms.clear();
123122

@@ -132,7 +131,8 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
132131
}
133132
TransformLayerMessage::BeginRotate => {
134133
let selected_points: Vec<&ManipulatorPointId> = shape_editor.selected_points().collect();
135-
if !(using_path_tool || using_select_tool)
134+
135+
if (!using_path_tool && !using_select_tool)
136136
|| (using_path_tool && selected_points.is_empty())
137137
|| selected_layers.is_empty()
138138
|| matches!(self.transform_operation, TransformOperation::Rotating(_))
@@ -141,37 +141,31 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
141141
return;
142142
}
143143

144-
if let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) {
145-
if selected_points.len() == 1 {
146-
let point = &selected_points[0];
147-
148-
match point {
149-
ManipulatorPointId::Anchor(_) => {
150-
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
151-
if (handle1.get_handle_length(&vector_data) == 0. && handle2.get_handle_length(&vector_data) == 0.)
152-
|| (handle1.get_handle_length(&vector_data) == f64::MAX && handle2.get_handle_length(&vector_data) == f64::MAX)
153-
{
154-
return;
155-
}
156-
}
157-
}
158-
_ => {
159-
// TODO: Need to fix handle snap to anchor issue. SEE (https://discord.com/channels/731730685944922173/1217752903209713715).
144+
let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) else {
145+
selected.original_transforms.clear();
146+
return;
147+
};
160148

161-
let handle_length = point.as_handle().and_then(|handle| Some(handle.get_handle_length(&vector_data)));
149+
if let [point] = selected_points.as_slice() {
150+
if matches!(point, ManipulatorPointId::Anchor(_)) {
151+
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
152+
let handle1_length = handle1.length(&vector_data);
153+
let handle2_length = handle2.length(&vector_data);
162154

163-
if let Some(length) = handle_length {
164-
if length == 0. {
165-
selected.original_transforms.clear();
166-
return;
167-
}
168-
}
155+
if (handle1_length == 0. && handle2_length == 0.) || (handle1_length == f64::MAX && handle2_length == f64::MAX) {
156+
return;
169157
}
170158
}
159+
} else {
160+
// TODO: Fix handle snap to anchor issue, see <https://discord.com/channels/731730685944922173/1217752903209713715>
161+
162+
let handle_length = point.as_handle().map(|handle| handle.length(&vector_data));
163+
164+
if handle_length == Some(0.) {
165+
selected.original_transforms.clear();
166+
return;
167+
}
171168
}
172-
} else {
173-
selected.original_transforms.clear();
174-
return;
175169
}
176170

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

186180
if (using_path_tool && selected_points.is_empty())
187-
|| !(using_path_tool || using_select_tool)
188-
|| matches!(self.transform_operation, TransformOperation::Scaling(_))
181+
|| (!using_path_tool && !using_select_tool)
189182
|| selected_layers.is_empty()
183+
|| matches!(self.transform_operation, TransformOperation::Scaling(_))
190184
{
191185
selected.original_transforms.clear();
192186
return;
193187
}
194188

195-
if let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) {
196-
if selected_points.len() == 1 {
197-
let point = &selected_points[0];
198-
199-
match point {
200-
ManipulatorPointId::Anchor(_) => {
201-
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
202-
let handle1_length = handle1.get_handle_length(&vector_data);
203-
let handle2_length = handle2.get_handle_length(&vector_data);
204-
205-
if (handle1_length == 0. && handle2_length == 0.) || (handle1_length == f64::MAX && handle2_length == f64::MAX) {
206-
selected.original_transforms.clear();
207-
return;
208-
}
209-
}
210-
}
211-
_ => {
212-
let handle_length = point.as_handle().and_then(|handle| Some(handle.get_handle_length(&vector_data)));
213-
214-
if let Some(length) = handle_length {
215-
if length == 0. {
216-
selected.original_transforms.clear();
217-
return;
218-
}
219-
}
189+
let Some(vector_data) = selected_layers.first().and_then(|&layer| document.network_interface.compute_modified_vector(layer)) else {
190+
selected.original_transforms.clear();
191+
return;
192+
};
193+
194+
if let [point] = selected_points.as_slice() {
195+
if matches!(point, ManipulatorPointId::Anchor(_)) {
196+
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
197+
let handle1_length = handle1.length(&vector_data);
198+
let handle2_length = handle2.length(&vector_data);
199+
200+
if (handle1_length == 0. && handle2_length == 0.) || (handle1_length == f64::MAX && handle2_length == f64::MAX) {
201+
selected.original_transforms.clear();
202+
return;
220203
}
221204
}
205+
} else {
206+
let handle_length = point.as_handle().map(|handle| handle.length(&vector_data));
207+
208+
if handle_length == Some(0.) {
209+
selected.original_transforms.clear();
210+
return;
211+
}
222212
}
223-
} else {
224-
selected.original_transforms.clear();
225-
return;
226213
}
227214

228215
begin_operation(self.transform_operation, &mut self.typing, &mut self.mouse_position, &mut self.start_mouse);
@@ -295,6 +282,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
295282
}
296283
};
297284
}
285+
298286
self.mouse_position = input.mouse.position;
299287
}
300288
TransformLayerMessage::SelectionChanged => {

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -403,11 +403,11 @@ impl HandleId {
403403
}
404404
}
405405

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

413413
/// 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.

0 commit comments

Comments
 (0)