From 04d04106d10318d1cf2962ed5709715e6a30d396 Mon Sep 17 00:00:00 2001 From: mtvare6 Date: Tue, 14 Jan 2025 16:53:16 +0530 Subject: [PATCH] Adds a scaling factor accounting for document and viewport scales Also moves whole code to single unit --- .../portfolio/document/document_message.rs | 1 - .../document/document_message_handler.rs | 15 ----- .../transform_layer_message.rs | 3 +- .../transform_layer_message_handler.rs | 65 +++++++++++-------- 4 files changed, 40 insertions(+), 44 deletions(-) diff --git a/editor/src/messages/portfolio/document/document_message.rs b/editor/src/messages/portfolio/document/document_message.rs index cab6efc4a2..27fb1a227f 100644 --- a/editor/src/messages/portfolio/document/document_message.rs +++ b/editor/src/messages/portfolio/document/document_message.rs @@ -54,7 +54,6 @@ pub enum DocumentMessage { DocumentHistoryForward, DocumentStructureChanged, DrawArtboardOverlays(OverlayContext), - DrawTransformGRSOverlays(OverlayContext), DuplicateSelectedLayers, EnterNestedNetwork { node_id: NodeId, diff --git a/editor/src/messages/portfolio/document/document_message_handler.rs b/editor/src/messages/portfolio/document/document_message_handler.rs index bc7d2ede48..a1775eafc7 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -373,21 +373,6 @@ impl MessageHandler> for DocumentMessag overlay_context.text(&name, COLOR_OVERLAY_GRAY, None, transform, 0., [Pivot::Start, Pivot::End]); } } - DocumentMessage::DrawTransformGRSOverlays(overlay_context) => { - for layer in self.metadata().all_layers() { - if !self.network_interface.is_artboard(&layer.to_node(), &[]) { - continue; - } - let Some(bounds) = self.metadata().bounding_box_document(layer) else { continue }; - let transform = self.metadata().document_to_viewport - * DAffine2::from_translation(DVec2::new(bounds[0].x.min(bounds[1].x), bounds[1].y.max(bounds[0].y))) - * DAffine2::from_scale(DVec2::splat(self.document_ptz.zoom().recip())) - * DAffine2::from_translation(-DVec2::Y * 4.); - - responses.add(TransformLayerMessage::Overlays(overlay_context, transform)); - break; - } - } DocumentMessage::DuplicateSelectedLayers => { let parent = self.new_layer_parent(false); let calculated_insert_index = diff --git a/editor/src/messages/tool/transform_layer/transform_layer_message.rs b/editor/src/messages/tool/transform_layer/transform_layer_message.rs index 6bb4e5c260..d83c00a2e4 100644 --- a/editor/src/messages/tool/transform_layer/transform_layer_message.rs +++ b/editor/src/messages/tool/transform_layer/transform_layer_message.rs @@ -1,12 +1,11 @@ use crate::messages::input_mapper::utility_types::input_keyboard::Key; use crate::messages::portfolio::document::overlays::utility_types::OverlayContext; use crate::messages::prelude::*; -use glam::DAffine2; #[impl_message(Message, ToolMessage, TransformLayer)] #[derive(PartialEq, Clone, Debug, serde::Serialize, serde::Deserialize)] pub enum TransformLayerMessage { - Overlays(OverlayContext, DAffine2), + Overlays(OverlayContext), // Messages ApplyTransformOperation, BeginGrab, diff --git a/editor/src/messages/tool/transform_layer/transform_layer_message_handler.rs b/editor/src/messages/tool/transform_layer/transform_layer_message_handler.rs index 5350c04701..3bd556158e 100644 --- a/editor/src/messages/tool/transform_layer/transform_layer_message_handler.rs +++ b/editor/src/messages/tool/transform_layer/transform_layer_message_handler.rs @@ -1,17 +1,18 @@ +use crate::consts::COLOR_OVERLAY_SNAP_BACKGROUND; use crate::consts::SLOWING_DIVISOR; use crate::messages::input_mapper::utility_types::input_mouse::ViewportPosition; -use crate::messages::portfolio::document::utility_types::transformation::{Axis, OriginalTransforms, Selected, TransformOperation, Typing}; use crate::messages::portfolio::document::overlays::utility_types::{OverlayProvider, Pivot}; +use crate::messages::portfolio::document::utility_types::transformation::{Axis, OriginalTransforms, Selected, TransformOperation, Typing}; use crate::messages::prelude::*; use crate::messages::tool::common_functionality::shape_editor::ShapeState; use crate::messages::tool::utility_types::{ToolData, ToolType}; -use crate::consts::COLOR_OVERLAY_SNAP_BACKGROUND; +use glam::DAffine2; use graphene_core::vector::ManipulatorPointId; use glam::DVec2; -const TRANSFORM_GRS_OVERLAY_PROVIDER: OverlayProvider = |context| DocumentMessage::DrawTransformGRSOverlays(context).into(); +const TRANSFORM_GRS_OVERLAY_PROVIDER: OverlayProvider = |context| TransformLayerMessage::Overlays(context).into(); #[derive(Debug, Clone, Default)] pub struct TransformLayerMessageHandler { @@ -91,29 +92,41 @@ impl MessageHandler> for TransformLayer }; match message { - TransformLayerMessage::Overlays(overlay_context, transform) => { - let axis_constraint = match self.transform_operation { - TransformOperation::Grabbing(grabbing) => grabbing.constraint, - TransformOperation::Scaling(scaling) => scaling.constraint, - _ => Axis::Both, - }; - - let axis_text = |vector: DVec2, separate: bool| match (axis_constraint, separate) { - (Axis::Both, false) => format!("by {:.3}", vector.x), - (Axis::Both, true) => format!("by {:.3}, {:.3}", vector.x, vector.y), - (Axis::X, _) => format!("X by {:.3}", vector.x), - (Axis::Y, _) => format!("Y by {:.3}", vector.y), - }; - - let grs_value_text = match self.transform_operation { - TransformOperation::None => String::new(), - // TODO: Fix that the translation is showing numbers in viewport space, not document space - TransformOperation::Grabbing(translation) => format!("Translating {}", axis_text(translation.to_dvec(), true)), - TransformOperation::Rotating(rotation) => format!("Rotating by {:.3}°", rotation.to_f64(self.snap) * 360. / std::f64::consts::TAU), - TransformOperation::Scaling(scale) => format!("Scaling {}", axis_text(scale.to_dvec(self.snap), false)), - }; - - overlay_context.text(&grs_value_text, COLOR_OVERLAY_SNAP_BACKGROUND, None, transform, 0., [Pivot::Start, Pivot::End]); + TransformLayerMessage::Overlays(overlay_context) => { + for layer in document.metadata().all_layers() { + if !document.network_interface.is_artboard(&layer.to_node(), &[]) { + continue; + } + let Some(bounds) = document.metadata().bounding_box_document(layer) else { continue }; + let transform = document.metadata().document_to_viewport + * DAffine2::from_translation(DVec2::new(bounds[0].x.min(bounds[1].x), bounds[1].y.max(bounds[0].y))) + * DAffine2::from_scale(DVec2::splat(document.document_ptz.zoom().recip())) + * DAffine2::from_translation(-DVec2::Y * 4.); + + let axis_constraint = match self.transform_operation { + TransformOperation::Grabbing(grabbing) => grabbing.constraint, + TransformOperation::Scaling(scaling) => scaling.constraint, + _ => Axis::Both, + }; + + let axis_text = |vector: DVec2, separate: bool| match (axis_constraint, separate) { + (Axis::Both, false) => format!("by {:.3}", vector.x), + (Axis::Both, true) => format!("by {:.3}, {:.3}", vector.x, vector.y), + (Axis::X, _) => format!("X by {:.3}", vector.x), + (Axis::Y, _) => format!("Y by {:.3}", vector.y), + }; + let grs_value_text = match self.transform_operation { + TransformOperation::None => String::new(), + TransformOperation::Grabbing(translation) => format!( + "Translating {}", + axis_text(document.metadata().document_to_viewport.inverse().transform_vector2(translation.to_dvec()), true) + ), + TransformOperation::Rotating(rotation) => format!("Rotating by {:.3}°", rotation.to_f64(self.snap).to_degrees()), + TransformOperation::Scaling(scale) => format!("Scaling {}", axis_text(scale.to_dvec(self.snap), false)), + }; + + overlay_context.text(&grs_value_text, COLOR_OVERLAY_SNAP_BACKGROUND, None, transform, 0., [Pivot::Start, Pivot::End]); + } } TransformLayerMessage::ApplyTransformOperation => { selected.original_transforms.clear();