Skip to content

Commit

Permalink
minor improvements and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
0SlowPoke0 authored and Keavon committed Jan 13, 2025
1 parent 00bc03a commit ba3cc5a
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 17 deletions.
12 changes: 8 additions & 4 deletions editor/src/messages/tool/tool_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ impl MessageHandler<ToolMessage, ToolMessageData<'_>> for ToolMessageHandler {
self.tool_is_active = true;

// Send the old and new tools a transition to their FSM Abort states
let mut send_abort_to_tool = |tool_type, update_hints_and_cursor: bool| {
if let Some(tool) = tool_data.tools.get_mut(&tool_type) {
let mut send_abort_to_tool = |old_tool: ToolType, new_tool: ToolType, update_hints_and_cursor: bool| {
if let Some(tool) = tool_data.tools.get_mut(&new_tool) {
let mut data = ToolActionHandlerData {
document,
document_id,
Expand All @@ -101,9 +101,13 @@ 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);
}
};
send_abort_to_tool(tool_type, true);
send_abort_to_tool(old_tool, false);

send_abort_to_tool(old_tool, tool_type, true);
send_abort_to_tool(old_tool, old_tool, false);

// Unsubscribe old tool from the broadcaster
tool_data.tools.get(&tool_type).unwrap().deactivate(responses);
Expand Down
14 changes: 1 addition & 13 deletions editor/src/messages/tool/tool_messages/path_tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,19 +645,7 @@ impl Fsm for PathToolFsmState {
self
}
(Self::InsertPoint, PathToolMessage::Escape | PathToolMessage::Delete | PathToolMessage::RightClick) => tool_data.end_insertion(shape_editor, responses, InsertEndKind::Abort),
(Self::InsertPoint, PathToolMessage::GRS { key: propagate }) => {
// MAYBE: use `InputMapperMessage::KeyDown(..)` instead
match propagate {
// TODO: Don't use `Key::G` directly, instead take it as a variable from the input mappings list like in all other places
Key::KeyG => responses.add(TransformLayerMessage::BeginGrab),
// TODO: Don't use `Key::R` directly, instead take it as a variable from the input mappings list like in all other places
Key::KeyR => responses.add(TransformLayerMessage::BeginRotate),
// TODO: Don't use `Key::S` directly, instead take it as a variable from the input mappings list like in all other places
Key::KeyS => responses.add(TransformLayerMessage::BeginScale),
_ => warn!("Unexpected GRS key"),
}
tool_data.end_insertion(shape_editor, responses, InsertEndKind::Abort)
}
(Self::InsertPoint, PathToolMessage::GRS { key: _ }) => PathToolFsmState::InsertPoint,
// Mouse down
(
_,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type TransformData<'a> = (&'a DocumentMessageHandler, &'a InputPreprocessorMessa
impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayerMessageHandler {
fn process_message(&mut self, message: TransformLayerMessage, responses: &mut VecDeque<Message>, (document, input, tool_data, shape_editor): TransformData) {
let using_path_tool = tool_data.active_tool_type == ToolType::Path;
let using_select_tool = tool_data.active_tool_type == ToolType::Select;

// TODO: Add support for transforming layer not in the document network
let selected_layers = document
Expand Down Expand Up @@ -120,6 +121,17 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
responses.add(NodeGraphMessage::RunDocumentGraph);
}
TransformLayerMessage::BeginGrab => {
if !(using_path_tool || using_select_tool) {
return;
}
let points = shape_editor.selected_points();
let selected_points: Vec<&ManipulatorPointId> = points.collect();

if using_path_tool && selected_points.is_empty() {
responses.add(TransformLayerMessage::CancelTransformOperation);
return;
}

if let TransformOperation::Grabbing(_) = self.transform_operation {
return;
}
Expand All @@ -136,6 +148,56 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
selected.original_transforms.clear();
}
TransformLayerMessage::BeginRotate => {
if !(using_path_tool || using_select_tool) {
return;
}
let Some(&layer) = selected_layers.first() else {
return;
};
let Some(vector_data) = document.network_interface.compute_modified_vector(layer) else {
return;
};
let points = shape_editor.selected_points();
let selected_points: Vec<&ManipulatorPointId> = points.collect();

if using_path_tool && selected_points.is_empty() {
responses.add(TransformLayerMessage::CancelTransformOperation);
return;
}

if selected_points.len() == 1 {
if let Some(point) = selected_points.first() {
match point {
ManipulatorPointId::Anchor(_) => {
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
if (handle1.get_handle_length(&vector_data) == 0.0 && handle2.get_handle_length(&vector_data) == 0.0)
|| (handle1.get_handle_length(&vector_data) == f64::MAX && handle2.get_handle_length(&vector_data) == f64::MAX)
{
self.transform_operation = TransformOperation::None;

responses.add(TransformLayerMessage::CancelTransformOperation);
return;
}
}
}
_ => {
let handle_position = point.get_position(&vector_data);
let anchor_position = point.get_anchor_position(&vector_data);

if let (Some(handle_pos), Some(anchor_pos)) = (handle_position, anchor_position) {
// Calculate the distance between the handle and anchor
let distance = (handle_pos - anchor_pos).length();

// If the distance is zero, return early
if distance == 0.0 {
return;
}
}
}
}
}
}

if let TransformOperation::Rotating(_) = self.transform_operation {
return;
}
Expand All @@ -152,6 +214,39 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer
selected.original_transforms.clear();
}
TransformLayerMessage::BeginScale => {
if !(using_path_tool || using_select_tool) {
return;
}
let Some(&layer) = selected_layers.first() else {
return;
};
let Some(vector_data) = document.network_interface.compute_modified_vector(layer) else {
return;
};

let points = shape_editor.selected_points();
let selected_points: Vec<&ManipulatorPointId> = points.collect();

if using_path_tool && selected_points.is_empty() {
responses.add(TransformLayerMessage::CancelTransformOperation);
return;
}

if selected_points.len() == 1 {
if let Some(point) = selected_points.first() {
if let ManipulatorPointId::Anchor(_) = point {
if let Some([handle1, handle2]) = point.get_handle_pair(&vector_data) {
if (handle1.get_handle_length(&vector_data) == 0.0 && handle2.get_handle_length(&vector_data) == 0.0)
|| (handle1.get_handle_length(&vector_data) == f64::MAX && handle2.get_handle_length(&vector_data) == f64::MAX)
{
responses.add(TransformLayerMessage::CancelTransformOperation);
return;
}
}
}
}
}

if let TransformOperation::Scaling(_) = self.transform_operation {
return;
}
Expand Down
7 changes: 7 additions & 0 deletions node-graph/gcore/src/vector/vector_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,13 @@ 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
}

/// 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.
#[must_use]
pub fn set_relative_position(self, relative_position: DVec2) -> VectorModificationType {
Expand Down

0 comments on commit ba3cc5a

Please sign in to comment.