From a987a683e8f3f5866bb2a79f94e2a9521c2ce489 Mon Sep 17 00:00:00 2001 From: Nitish-bot <86357181+Nitish-bot@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:05:20 +0530 Subject: [PATCH 1/5] Fix text selection --- .../messages/tool/tool_messages/text_tool.rs | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/editor/src/messages/tool/tool_messages/text_tool.rs b/editor/src/messages/tool/tool_messages/text_tool.rs index 9d5978e8aa..1888e6f97e 100644 --- a/editor/src/messages/tool/tool_messages/text_tool.rs +++ b/editor/src/messages/tool/tool_messages/text_tool.rs @@ -309,7 +309,54 @@ impl TextToolData { font_cache: &FontCache, responses: &mut VecDeque, ) -> TextToolFsmState { - // Check if the user has selected an existing text layer + fn is_point_in_triangle(p: DVec2, a: DVec2, b: DVec2, c: DVec2) -> bool { + let v0 = c - a; + let v1 = b - a; + let v2 = p - a; + + let dot00 = v0.dot(v0); + let dot01 = v0.dot(v1); + let dot02 = v0.dot(v2); + let dot11 = v1.dot(v1); + let dot12 = v1.dot(v2); + + let denom = dot00 * dot11 - dot01 * dot01; + if denom == 0.0 { + return false; // Degenerate triangle + } + let inv_denom = 1.0 / denom; + + let u = (dot11 * dot02 - dot01 * dot12) * inv_denom; + let v = (dot00 * dot12 - dot01 * dot02) * inv_denom; + + u >= 0.0 && v >= 0.0 && (u + v) <= 1.0 + } + + // Check if the user has selected an existing text layerQ + if let Some(clicked_text_layer_path) = document + .metadata() + .all_layers() + .filter(|&layer| is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text")) + .find(|&layer| { + let (text, font, font_size, line_height_ratio, character_spacing) = graph_modification_utils::get_text(layer, &document.network_interface).unwrap(); + let buzz_face = font_cache.get(font).map(|data| load_face(data)); + let far = graphene_core::text::bounding_box(text, buzz_face, font_size, line_height_ratio, character_spacing, None); + let quad = Quad::from_box([DVec2::ZERO, far]); + let transformed_quad = document.metadata().transform_to_viewport(layer) * quad; + let mouse = DVec2::new(input.mouse.position.x, input.mouse.position.y); + + if is_point_in_triangle(mouse, transformed_quad.0[0], transformed_quad.0[1], transformed_quad.0[2]) + || is_point_in_triangle(mouse, transformed_quad.0[0], transformed_quad.0[2], transformed_quad.0[3]) + { + return true; + } + false + }) { + self.start_editing_layer(clicked_text_layer_path, state, document, font_cache, responses); + + return TextToolFsmState::Editing; + } + if let Some(clicked_text_layer_path) = document.click(input).filter(|&layer| is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text")) { self.start_editing_layer(clicked_text_layer_path, state, document, font_cache, responses); From be1638e3d9ae8acc564fd6974795be7dc7023a46 Mon Sep 17 00:00:00 2001 From: Nitish-bot <86357181+Nitish-bot@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:05:53 +0530 Subject: [PATCH 2/5] Remove old implementation --- editor/src/messages/tool/tool_messages/text_tool.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/editor/src/messages/tool/tool_messages/text_tool.rs b/editor/src/messages/tool/tool_messages/text_tool.rs index 1888e6f97e..62b1bceaa1 100644 --- a/editor/src/messages/tool/tool_messages/text_tool.rs +++ b/editor/src/messages/tool/tool_messages/text_tool.rs @@ -356,12 +356,6 @@ impl TextToolData { return TextToolFsmState::Editing; } - - if let Some(clicked_text_layer_path) = document.click(input).filter(|&layer| is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text")) { - self.start_editing_layer(clicked_text_layer_path, state, document, font_cache, responses); - - TextToolFsmState::Editing - } // Create new text else if let Some(editing_text) = self.editing_text.as_ref().filter(|_| state == TextToolFsmState::Ready) { responses.add(DocumentMessage::AddTransaction); From 5fcade37fbb04fa2d592487e105cb782453c2f16 Mon Sep 17 00:00:00 2001 From: Nitish-bot <86357181+Nitish-bot@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:08:28 +0530 Subject: [PATCH 3/5] minor change --- editor/src/messages/tool/tool_messages/text_tool.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/src/messages/tool/tool_messages/text_tool.rs b/editor/src/messages/tool/tool_messages/text_tool.rs index 62b1bceaa1..491dbaada8 100644 --- a/editor/src/messages/tool/tool_messages/text_tool.rs +++ b/editor/src/messages/tool/tool_messages/text_tool.rs @@ -354,7 +354,7 @@ impl TextToolData { }) { self.start_editing_layer(clicked_text_layer_path, state, document, font_cache, responses); - return TextToolFsmState::Editing; + TextToolFsmState::Editing } // Create new text else if let Some(editing_text) = self.editing_text.as_ref().filter(|_| state == TextToolFsmState::Ready) { From fbaad915f48a816f7da86a4a0d1cccd32af9bb98 Mon Sep 17 00:00:00 2001 From: Nitish-bot <86357181+Nitish-bot@users.noreply.github.com> Date: Fri, 20 Dec 2024 19:29:55 +0530 Subject: [PATCH 4/5] Use inbuilt function instead of helper --- .../messages/tool/tool_messages/text_tool.rs | 27 +------------------ 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/editor/src/messages/tool/tool_messages/text_tool.rs b/editor/src/messages/tool/tool_messages/text_tool.rs index 491dbaada8..4cea79a03e 100644 --- a/editor/src/messages/tool/tool_messages/text_tool.rs +++ b/editor/src/messages/tool/tool_messages/text_tool.rs @@ -309,29 +309,6 @@ impl TextToolData { font_cache: &FontCache, responses: &mut VecDeque, ) -> TextToolFsmState { - fn is_point_in_triangle(p: DVec2, a: DVec2, b: DVec2, c: DVec2) -> bool { - let v0 = c - a; - let v1 = b - a; - let v2 = p - a; - - let dot00 = v0.dot(v0); - let dot01 = v0.dot(v1); - let dot02 = v0.dot(v2); - let dot11 = v1.dot(v1); - let dot12 = v1.dot(v2); - - let denom = dot00 * dot11 - dot01 * dot01; - if denom == 0.0 { - return false; // Degenerate triangle - } - let inv_denom = 1.0 / denom; - - let u = (dot11 * dot02 - dot01 * dot12) * inv_denom; - let v = (dot00 * dot12 - dot01 * dot02) * inv_denom; - - u >= 0.0 && v >= 0.0 && (u + v) <= 1.0 - } - // Check if the user has selected an existing text layerQ if let Some(clicked_text_layer_path) = document .metadata() @@ -345,9 +322,7 @@ impl TextToolData { let transformed_quad = document.metadata().transform_to_viewport(layer) * quad; let mouse = DVec2::new(input.mouse.position.x, input.mouse.position.y); - if is_point_in_triangle(mouse, transformed_quad.0[0], transformed_quad.0[1], transformed_quad.0[2]) - || is_point_in_triangle(mouse, transformed_quad.0[0], transformed_quad.0[2], transformed_quad.0[3]) - { + if transformed_quad.contains(mouse) { return true; } false From 7a23c7f9b828c7494ace1d47cfc68200fad1e6b5 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Sat, 28 Dec 2024 13:56:40 -0800 Subject: [PATCH 5/5] Code review --- editor/src/messages/tool/tool_messages/text_tool.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/editor/src/messages/tool/tool_messages/text_tool.rs b/editor/src/messages/tool/tool_messages/text_tool.rs index 4cea79a03e..311952eef1 100644 --- a/editor/src/messages/tool/tool_messages/text_tool.rs +++ b/editor/src/messages/tool/tool_messages/text_tool.rs @@ -309,23 +309,23 @@ impl TextToolData { font_cache: &FontCache, responses: &mut VecDeque, ) -> TextToolFsmState { - // Check if the user has selected an existing text layerQ + // Check if the user has selected an existing text layer if let Some(clicked_text_layer_path) = document .metadata() .all_layers() .filter(|&layer| is_layer_fed_by_node_of_name(layer, &document.network_interface, "Text")) .find(|&layer| { - let (text, font, font_size, line_height_ratio, character_spacing) = graph_modification_utils::get_text(layer, &document.network_interface).unwrap(); + let (text, font, font_size, line_height_ratio, character_spacing) = + graph_modification_utils::get_text(layer, &document.network_interface).expect("Text layer should have text when interacting with the Text tool in `interact()`"); + let buzz_face = font_cache.get(font).map(|data| load_face(data)); let far = graphene_core::text::bounding_box(text, buzz_face, font_size, line_height_ratio, character_spacing, None); let quad = Quad::from_box([DVec2::ZERO, far]); let transformed_quad = document.metadata().transform_to_viewport(layer) * quad; + let mouse = DVec2::new(input.mouse.position.x, input.mouse.position.y); - if transformed_quad.contains(mouse) { - return true; - } - false + transformed_quad.contains(mouse) }) { self.start_editing_layer(clicked_text_layer_path, state, document, font_cache, responses);