Skip to content

Commit ba3cc5a

Browse files
0SlowPoke0Keavon
authored andcommitted
minor improvements and fixes
1 parent 00bc03a commit ba3cc5a

File tree

4 files changed

+111
-17
lines changed

4 files changed

+111
-17
lines changed

editor/src/messages/tool/tool_message_handler.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ impl MessageHandler<ToolMessage, ToolMessageData<'_>> for ToolMessageHandler {
7676
self.tool_is_active = true;
7777

7878
// Send the old and new tools a transition to their FSM Abort states
79-
let mut send_abort_to_tool = |tool_type, update_hints_and_cursor: bool| {
80-
if let Some(tool) = tool_data.tools.get_mut(&tool_type) {
79+
let mut send_abort_to_tool = |old_tool: ToolType, new_tool: ToolType, update_hints_and_cursor: bool| {
80+
if let Some(tool) = tool_data.tools.get_mut(&new_tool) {
8181
let mut data = ToolActionHandlerData {
8282
document,
8383
document_id,
@@ -101,9 +101,13 @@ impl MessageHandler<ToolMessage, ToolMessageData<'_>> for ToolMessageHandler {
101101
tool.process_message(ToolMessage::UpdateCursor, responses, &mut data);
102102
}
103103
}
104+
if matches!(old_tool, ToolType::Path | ToolType::Select) {
105+
responses.add(TransformLayerMessage::CancelTransformOperation);
106+
}
104107
};
105-
send_abort_to_tool(tool_type, true);
106-
send_abort_to_tool(old_tool, false);
108+
109+
send_abort_to_tool(old_tool, tool_type, true);
110+
send_abort_to_tool(old_tool, old_tool, false);
107111

108112
// Unsubscribe old tool from the broadcaster
109113
tool_data.tools.get(&tool_type).unwrap().deactivate(responses);

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

+1-13
Original file line numberDiff line numberDiff line change
@@ -645,19 +645,7 @@ impl Fsm for PathToolFsmState {
645645
self
646646
}
647647
(Self::InsertPoint, PathToolMessage::Escape | PathToolMessage::Delete | PathToolMessage::RightClick) => tool_data.end_insertion(shape_editor, responses, InsertEndKind::Abort),
648-
(Self::InsertPoint, PathToolMessage::GRS { key: propagate }) => {
649-
// MAYBE: use `InputMapperMessage::KeyDown(..)` instead
650-
match propagate {
651-
// TODO: Don't use `Key::G` directly, instead take it as a variable from the input mappings list like in all other places
652-
Key::KeyG => responses.add(TransformLayerMessage::BeginGrab),
653-
// TODO: Don't use `Key::R` directly, instead take it as a variable from the input mappings list like in all other places
654-
Key::KeyR => responses.add(TransformLayerMessage::BeginRotate),
655-
// TODO: Don't use `Key::S` directly, instead take it as a variable from the input mappings list like in all other places
656-
Key::KeyS => responses.add(TransformLayerMessage::BeginScale),
657-
_ => warn!("Unexpected GRS key"),
658-
}
659-
tool_data.end_insertion(shape_editor, responses, InsertEndKind::Abort)
660-
}
648+
(Self::InsertPoint, PathToolMessage::GRS { key: _ }) => PathToolFsmState::InsertPoint,
661649
// Mouse down
662650
(
663651
_,

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

+95
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type TransformData<'a> = (&'a DocumentMessageHandler, &'a InputPreprocessorMessa
4343
impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayerMessageHandler {
4444
fn process_message(&mut self, message: TransformLayerMessage, responses: &mut VecDeque<Message>, (document, input, tool_data, shape_editor): TransformData) {
4545
let using_path_tool = tool_data.active_tool_type == ToolType::Path;
46+
let using_select_tool = tool_data.active_tool_type == ToolType::Select;
4647

4748
// TODO: Add support for transforming layer not in the document network
4849
let selected_layers = document
@@ -120,6 +121,17 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
120121
responses.add(NodeGraphMessage::RunDocumentGraph);
121122
}
122123
TransformLayerMessage::BeginGrab => {
124+
if !(using_path_tool || using_select_tool) {
125+
return;
126+
}
127+
let points = shape_editor.selected_points();
128+
let selected_points: Vec<&ManipulatorPointId> = points.collect();
129+
130+
if using_path_tool && selected_points.is_empty() {
131+
responses.add(TransformLayerMessage::CancelTransformOperation);
132+
return;
133+
}
134+
123135
if let TransformOperation::Grabbing(_) = self.transform_operation {
124136
return;
125137
}
@@ -136,6 +148,56 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
136148
selected.original_transforms.clear();
137149
}
138150
TransformLayerMessage::BeginRotate => {
151+
if !(using_path_tool || using_select_tool) {
152+
return;
153+
}
154+
let Some(&layer) = selected_layers.first() else {
155+
return;
156+
};
157+
let Some(vector_data) = document.network_interface.compute_modified_vector(layer) else {
158+
return;
159+
};
160+
let points = shape_editor.selected_points();
161+
let selected_points: Vec<&ManipulatorPointId> = points.collect();
162+
163+
if using_path_tool && selected_points.is_empty() {
164+
responses.add(TransformLayerMessage::CancelTransformOperation);
165+
return;
166+
}
167+
168+
if selected_points.len() == 1 {
169+
if let Some(point) = selected_points.first() {
170+
match point {
171+
ManipulatorPointId::Anchor(_) => {
172+
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
173+
if (handle1.get_handle_length(&vector_data) == 0.0 && handle2.get_handle_length(&vector_data) == 0.0)
174+
|| (handle1.get_handle_length(&vector_data) == f64::MAX && handle2.get_handle_length(&vector_data) == f64::MAX)
175+
{
176+
self.transform_operation = TransformOperation::None;
177+
178+
responses.add(TransformLayerMessage::CancelTransformOperation);
179+
return;
180+
}
181+
}
182+
}
183+
_ => {
184+
let handle_position = point.get_position(&vector_data);
185+
let anchor_position = point.get_anchor_position(&vector_data);
186+
187+
if let (Some(handle_pos), Some(anchor_pos)) = (handle_position, anchor_position) {
188+
// Calculate the distance between the handle and anchor
189+
let distance = (handle_pos - anchor_pos).length();
190+
191+
// If the distance is zero, return early
192+
if distance == 0.0 {
193+
return;
194+
}
195+
}
196+
}
197+
}
198+
}
199+
}
200+
139201
if let TransformOperation::Rotating(_) = self.transform_operation {
140202
return;
141203
}
@@ -152,6 +214,39 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
152214
selected.original_transforms.clear();
153215
}
154216
TransformLayerMessage::BeginScale => {
217+
if !(using_path_tool || using_select_tool) {
218+
return;
219+
}
220+
let Some(&layer) = selected_layers.first() else {
221+
return;
222+
};
223+
let Some(vector_data) = document.network_interface.compute_modified_vector(layer) else {
224+
return;
225+
};
226+
227+
let points = shape_editor.selected_points();
228+
let selected_points: Vec<&ManipulatorPointId> = points.collect();
229+
230+
if using_path_tool && selected_points.is_empty() {
231+
responses.add(TransformLayerMessage::CancelTransformOperation);
232+
return;
233+
}
234+
235+
if selected_points.len() == 1 {
236+
if let Some(point) = selected_points.first() {
237+
if let ManipulatorPointId::Anchor(_) = point {
238+
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
239+
if (handle1.get_handle_length(&vector_data) == 0.0 && handle2.get_handle_length(&vector_data) == 0.0)
240+
|| (handle1.get_handle_length(&vector_data) == f64::MAX && handle2.get_handle_length(&vector_data) == f64::MAX)
241+
{
242+
responses.add(TransformLayerMessage::CancelTransformOperation);
243+
return;
244+
}
245+
}
246+
}
247+
}
248+
}
249+
155250
if let TransformOperation::Scaling(_) = self.transform_operation {
156251
return;
157252
}

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

+7
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,13 @@ 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
411+
}
412+
406413
/// 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.
407414
#[must_use]
408415
pub fn set_relative_position(self, relative_position: DVec2) -> VectorModificationType {

0 commit comments

Comments
 (0)