From 4b58ccec8752dc42fa63fe5c18a79dc5bac23c1b Mon Sep 17 00:00:00 2001 From: mtvare6 Date: Tue, 14 Jan 2025 13:39:41 +0530 Subject: [PATCH] Replaces dynamic hint used by transform layer with overlays --- .../portfolio/document/document_message.rs | 1 + .../document/document_message_handler.rs | 15 +++++ .../document/utility_types/transformation.rs | 34 ++--------- .../transform_layer_message.rs | 4 +- .../transform_layer_message_handler.rs | 59 +++++++------------ 5 files changed, 45 insertions(+), 68 deletions(-) diff --git a/editor/src/messages/portfolio/document/document_message.rs b/editor/src/messages/portfolio/document/document_message.rs index 27fb1a227f..cab6efc4a2 100644 --- a/editor/src/messages/portfolio/document/document_message.rs +++ b/editor/src/messages/portfolio/document/document_message.rs @@ -54,6 +54,7 @@ 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 a1775eafc7..bc7d2ede48 100644 --- a/editor/src/messages/portfolio/document/document_message_handler.rs +++ b/editor/src/messages/portfolio/document/document_message_handler.rs @@ -373,6 +373,21 @@ 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/portfolio/document/utility_types/transformation.rs b/editor/src/messages/portfolio/document/utility_types/transformation.rs index 27c2567da2..902287140c 100644 --- a/editor/src/messages/portfolio/document/utility_types/transformation.rs +++ b/editor/src/messages/portfolio/document/utility_types/transformation.rs @@ -265,7 +265,7 @@ pub enum TransformOperation { } impl TransformOperation { - pub fn apply_transform_operation(&self, selected: &mut Selected, snapping: bool, axis_constraint: Axis) { + pub fn apply_transform_operation(&self, selected: &mut Selected, snapping: bool) { if self != &TransformOperation::None { let transformation = match self { TransformOperation::Grabbing(translation) => DAffine2::from_translation(translation.to_dvec()), @@ -275,7 +275,7 @@ impl TransformOperation { }; selected.update_transforms(transformation); - self.hints(snapping, axis_constraint, selected.responses); + self.hints(selected.responses); } } @@ -287,7 +287,7 @@ impl TransformOperation { TransformOperation::Scaling(scale) => scale.constraint.set_or_toggle(axis), }; - self.apply_transform_operation(selected, snapping, axis); + self.apply_transform_operation(selected, snapping); } pub fn grs_typed(&mut self, typed: Option, selected: &mut Selected, snapping: bool) { @@ -298,16 +298,10 @@ impl TransformOperation { TransformOperation::Scaling(scale) => scale.typed_factor = typed, }; - let axis_constraint = match self { - TransformOperation::Grabbing(grabbing) => grabbing.constraint, - TransformOperation::Scaling(scaling) => scaling.constraint, - _ => Axis::Both, - }; - - self.apply_transform_operation(selected, snapping, axis_constraint); + self.apply_transform_operation(selected, snapping); } - pub fn hints(&self, snapping: bool, axis_constraint: Axis, responses: &mut VecDeque) { + pub fn hints(&self, responses: &mut VecDeque) { use crate::messages::input_mapper::utility_types::input_keyboard::Key; use crate::messages::tool::utility_types::{HintData, HintGroup, HintInfo}; @@ -321,23 +315,7 @@ impl TransformOperation { input_hints.push(HintInfo::keys([Key::KeyY], "Along Y Axis")); } - // TODO: Eventually, move this somewhere else (maybe an overlay in the corner of the viewport, design is TBD) since servicable but not ideal for UI design consistency to have it in the hints bar - 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 { - 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(snapping) * 360. / std::f64::consts::TAU), - TransformOperation::Scaling(scale) => format!("Scaling {}", axis_text(scale.to_dvec(snapping), false)), - }; - let grs_value = vec![HintInfo::label(grs_value_text)]; - - let hint_data = HintData(vec![HintGroup(input_hints), HintGroup(grs_value)]); + let hint_data = HintData(vec![HintGroup(input_hints)]); responses.add(FrontendMessage::UpdateInputHints { hint_data }); } } 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 bac233d154..6bb4e5c260 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,12 @@ 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), + Overlays(OverlayContext, DAffine2), // 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 fecdf5d977..5350c04701 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,16 +1,18 @@ -use crate::consts::{SLOWING_DIVISOR, COLOR_OVERLAY_WHITE, COLOR_OVERLAY_SNAP_BACKGROUND}; -use crate::messages::portfolio::document::overlays::utility_types::Pivot; +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::prelude::*; use crate::messages::tool::common_functionality::shape_editor::ShapeState; -use crate::messages::tool::utility_types::{ToolData, ToolType, ToolTransition, EventToMessageMap}; - +use crate::messages::tool::utility_types::{ToolData, ToolType}; +use crate::consts::COLOR_OVERLAY_SNAP_BACKGROUND; use graphene_core::vector::ManipulatorPointId; use glam::DVec2; +const TRANSFORM_GRS_OVERLAY_PROVIDER: OverlayProvider = |context| DocumentMessage::DrawTransformGRSOverlays(context).into(); + #[derive(Debug, Clone, Default)] pub struct TransformLayerMessageHandler { pub transform_operation: TransformOperation, @@ -32,22 +34,7 @@ impl TransformLayerMessageHandler { } pub fn hints(&self, responses: &mut VecDeque) { - let axis_constraint = match self.transform_operation { - TransformOperation::Grabbing(grabbing) => grabbing.constraint, - TransformOperation::Scaling(scaling) => scaling.constraint, - _ => Axis::Both, - }; - self.transform_operation.hints(self.snap, axis_constraint, responses); - } -} - -impl ToolTransition for TransformLayerMessageHandler { - fn event_to_message_map(&self) -> EventToMessageMap { - EventToMessageMap { - tool_abort: Some(TransformLayerMessage::CancelTransformOperation.into()), - overlay_provider: Some(|overlay_context| TransformLayerMessage::Overlays(overlay_context).into()), - ..Default::default() - } + self.transform_operation.hints(responses); } } @@ -104,8 +91,7 @@ impl MessageHandler> for TransformLayer }; match message { - TransformLayerMessage::Overlays(overlay_context) => { - debug!("We were here"); + TransformLayerMessage::Overlays(overlay_context, transform) => { let axis_constraint = match self.transform_operation { TransformOperation::Grabbing(grabbing) => grabbing.constraint, TransformOperation::Scaling(scaling) => scaling.constraint, @@ -118,6 +104,7 @@ impl MessageHandler> for TransformLayer (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 @@ -125,8 +112,8 @@ impl MessageHandler> for TransformLayer 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)), }; - let transform = glam::DAffine2::from_translation( DVec2::new(327.0, -35.0)); - overlay_context.text(&grs_value_text, COLOR_OVERLAY_WHITE, Some(COLOR_OVERLAY_SNAP_BACKGROUND), transform, 4., [Pivot::Start, Pivot::End]); + + overlay_context.text(&grs_value_text, COLOR_OVERLAY_SNAP_BACKGROUND, None, transform, 0., [Pivot::Start, Pivot::End]); } TransformLayerMessage::ApplyTransformOperation => { selected.original_transforms.clear(); @@ -138,6 +125,7 @@ impl MessageHandler> for TransformLayer responses.add(DocumentMessage::EndTransaction); responses.add(ToolMessage::UpdateHints); responses.add(NodeGraphMessage::RunDocumentGraph); + responses.add(OverlaysMessage::RemoveProvider(TRANSFORM_GRS_OVERLAY_PROVIDER)); } TransformLayerMessage::BeginGrab => { if let TransformOperation::Grabbing(_) = self.transform_operation { @@ -155,7 +143,7 @@ impl MessageHandler> for TransformLayer selected.original_transforms.clear(); - responses.add(OverlaysMessage::Draw); + responses.add(OverlaysMessage::AddProvider(TRANSFORM_GRS_OVERLAY_PROVIDER)); } TransformLayerMessage::BeginRotate => { if let TransformOperation::Rotating(_) = self.transform_operation { @@ -173,7 +161,7 @@ impl MessageHandler> for TransformLayer selected.original_transforms.clear(); - responses.add(OverlaysMessage::Draw); + responses.add(OverlaysMessage::AddProvider(TRANSFORM_GRS_OVERLAY_PROVIDER)); } TransformLayerMessage::BeginScale => { if let TransformOperation::Scaling(_) = self.transform_operation { @@ -191,7 +179,7 @@ impl MessageHandler> for TransformLayer selected.original_transforms.clear(); - responses.add(OverlaysMessage::Draw); + responses.add(OverlaysMessage::AddProvider(TRANSFORM_GRS_OVERLAY_PROVIDER)); } TransformLayerMessage::CancelTransformOperation => { selected.revert_operation(); @@ -203,6 +191,8 @@ impl MessageHandler> for TransformLayer responses.add(DocumentMessage::AbortTransaction); responses.add(ToolMessage::UpdateHints); + + responses.add(OverlaysMessage::RemoveProvider(TRANSFORM_GRS_OVERLAY_PROVIDER)); } TransformLayerMessage::ConstrainX => self.transform_operation.constrain_axis(Axis::X, &mut selected, self.snap), TransformLayerMessage::ConstrainY => self.transform_operation.constrain_axis(Axis::Y, &mut selected, self.snap), @@ -212,12 +202,7 @@ impl MessageHandler> for TransformLayer let new_snap = input.keyboard.get(snap_key as usize); if new_snap != self.snap { self.snap = new_snap; - let axis_constraint = match self.transform_operation { - TransformOperation::Grabbing(grabbing) => grabbing.constraint, - TransformOperation::Scaling(scaling) => scaling.constraint, - _ => Axis::Both, - }; - self.transform_operation.apply_transform_operation(&mut selected, self.snap, axis_constraint); + self.transform_operation.apply_transform_operation(&mut selected, self.snap); } if self.typing.digits.is_empty() { @@ -227,9 +212,8 @@ impl MessageHandler> for TransformLayer TransformOperation::None => unreachable!(), TransformOperation::Grabbing(translation) => { let change = if self.slow { delta_pos / SLOWING_DIVISOR } else { delta_pos }; - let axis_constraint = translation.constraint; self.transform_operation = TransformOperation::Grabbing(translation.increment_amount(change)); - self.transform_operation.apply_transform_operation(&mut selected, self.snap, axis_constraint); + self.transform_operation.apply_transform_operation(&mut selected, self.snap); } TransformOperation::Rotating(rotation) => { let start_offset = *selected.pivot - self.mouse_position; @@ -239,7 +223,7 @@ impl MessageHandler> for TransformLayer let change = if self.slow { angle / SLOWING_DIVISOR } else { angle }; self.transform_operation = TransformOperation::Rotating(rotation.increment_amount(change)); - self.transform_operation.apply_transform_operation(&mut selected, self.snap, Axis::Both); + self.transform_operation.apply_transform_operation(&mut selected, self.snap); } TransformOperation::Scaling(scale) => { let change = { @@ -251,9 +235,8 @@ impl MessageHandler> for TransformLayer }; let change = if self.slow { change / SLOWING_DIVISOR } else { change }; - let axis_constraint = scale.constraint; self.transform_operation = TransformOperation::Scaling(scale.increment_amount(change)); - self.transform_operation.apply_transform_operation(&mut selected, self.snap, axis_constraint); + self.transform_operation.apply_transform_operation(&mut selected, self.snap); } }; }