Skip to content

Commit

Permalink
Replaces dynamic hint used by transform layer with overlays
Browse files Browse the repository at this point in the history
  • Loading branch information
mTvare6 committed Jan 14, 2025
1 parent eeab927 commit 4b58cce
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 68 deletions.
1 change: 1 addition & 0 deletions editor/src/messages/portfolio/document/document_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub enum DocumentMessage {
DocumentHistoryForward,
DocumentStructureChanged,
DrawArtboardOverlays(OverlayContext),
DrawTransformGRSOverlays(OverlayContext),
DuplicateSelectedLayers,
EnterNestedNetwork {
node_id: NodeId,
Expand Down
15 changes: 15 additions & 0 deletions editor/src/messages/portfolio/document/document_message_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,21 @@ impl MessageHandler<DocumentMessage, DocumentMessageData<'_>> 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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -275,7 +275,7 @@ impl TransformOperation {
};

selected.update_transforms(transformation);
self.hints(snapping, axis_constraint, selected.responses);
self.hints(selected.responses);
}
}

Expand All @@ -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<f64>, selected: &mut Selected, snapping: bool) {
Expand All @@ -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<Message>) {
pub fn hints(&self, responses: &mut VecDeque<Message>) {
use crate::messages::input_mapper::utility_types::input_keyboard::Key;
use crate::messages::tool::utility_types::{HintData, HintGroup, HintInfo};

Expand All @@ -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 });
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -32,22 +34,7 @@ impl TransformLayerMessageHandler {
}

pub fn hints(&self, responses: &mut VecDeque<Message>) {
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);
}
}

Expand Down Expand Up @@ -104,8 +91,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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,
Expand All @@ -118,15 +104,16 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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
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)),
};
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();
Expand All @@ -138,6 +125,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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 {
Expand All @@ -155,7 +143,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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 {
Expand All @@ -173,7 +161,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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 {
Expand All @@ -191,7 +179,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> for TransformLayer

selected.original_transforms.clear();

responses.add(OverlaysMessage::Draw);
responses.add(OverlaysMessage::AddProvider(TRANSFORM_GRS_OVERLAY_PROVIDER));
}
TransformLayerMessage::CancelTransformOperation => {
selected.revert_operation();
Expand All @@ -203,6 +191,8 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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),
Expand All @@ -212,12 +202,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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() {
Expand All @@ -227,9 +212,8 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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;
Expand All @@ -239,7 +223,7 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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 = {
Expand All @@ -251,9 +235,8 @@ impl MessageHandler<TransformLayerMessage, TransformData<'_>> 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);
}
};
}
Expand Down

0 comments on commit 4b58cce

Please sign in to comment.