From 57b8ee0e86afa36cc05ec0e04997f69ec869d0ea Mon Sep 17 00:00:00 2001 From: TrueDoctor Date: Tue, 13 Jul 2021 20:38:58 +0200 Subject: [PATCH] Implement snapping for rotation (#257) --- .../src/document/document_message_handler.rs | 23 +++++++++++++++---- core/editor/src/input/input_mapper.rs | 4 +++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/core/editor/src/document/document_message_handler.rs b/core/editor/src/document/document_message_handler.rs index 594f628b..d7b7c4ba 100644 --- a/core/editor/src/document/document_message_handler.rs +++ b/core/editor/src/document/document_message_handler.rs @@ -42,6 +42,8 @@ pub enum DocumentMessage { TranslateCanvasBegin, WheelCanvasTranslate { use_y_as_x: bool }, RotateCanvasBegin { snap: bool }, + EnableSnapping, + DisableSnapping, ZoomCanvasBegin, TranslateCanvasEnd, SetCanvasZoom(f64), @@ -69,6 +71,7 @@ pub struct DocumentMessageHandler { translating: bool, rotating: bool, zooming: bool, + snapping: bool, mouse_pos: ViewportPosition, copy_buffer: Vec, } @@ -165,6 +168,7 @@ impl Default for DocumentMessageHandler { translating: false, rotating: false, zooming: false, + snapping: false, mouse_pos: ViewportPosition::default(), copy_buffer: vec![], } @@ -383,13 +387,16 @@ impl MessageHandler for DocumentMessageHand self.translating = true; self.mouse_pos = ipp.mouse.position; } + RotateCanvasBegin { snap } => { self.rotating = true; + self.snapping = snap; let layerdata = self.layerdata_mut(&vec![]); - // TODO: Set up the input system to allow the addition of the Shift key to begin snapping while rotating without snapping layerdata.snap_rotate = snap; self.mouse_pos = ipp.mouse.position; } + EnableSnapping => self.snapping = true, + DisableSnapping => self.snapping = false, ZoomCanvasBegin => { self.zooming = true; self.mouse_pos = ipp.mouse.position; @@ -419,8 +426,10 @@ impl MessageHandler for DocumentMessageHand start_vec.angle_between(end_vec) }; + let snapping = self.snapping; let layerdata = self.layerdata_mut(&vec![]); layerdata.rotation += rotation; + layerdata.snap_rotate = snapping; responses.push_back( FrontendMessage::SetRotation { new_radians: layerdata.snapped_angle(), @@ -504,10 +513,16 @@ impl MessageHandler for DocumentMessageHand } } fn actions(&self) -> ActionList { + let mut common = actions!(DocumentMessageDiscriminant; Undo, SelectAllLayers, DeselectAllLayers, RenderDocument, ExportDocument, NewDocument, CloseActiveDocument, NextDocument, PrevDocument, MouseMove, TranslateCanvasEnd, TranslateCanvasBegin, PasteLayers, RotateCanvasBegin, ZoomCanvasBegin, SetCanvasZoom, MultiplyCanvasZoom, SetRotation, WheelCanvasZoom, WheelCanvasTranslate); + if self.active_document().layer_data.values().any(|data| data.selected) { - actions!(DocumentMessageDiscriminant; Undo, SelectAllLayers, DeselectAllLayers, DeleteSelectedLayers, DuplicateSelectedLayers, RenderDocument, ExportDocument, NewDocument, CloseActiveDocument, NextDocument, PrevDocument, MouseMove, TranslateCanvasEnd, TranslateCanvasBegin, CopySelectedLayers, PasteLayers, NudgeSelectedLayers, RotateCanvasBegin, ZoomCanvasBegin, SetCanvasZoom, MultiplyCanvasZoom, SetRotation, WheelCanvasZoom, WheelCanvasTranslate) - } else { - actions!(DocumentMessageDiscriminant; Undo, SelectAllLayers, DeselectAllLayers, RenderDocument, ExportDocument, NewDocument, CloseActiveDocument, NextDocument, PrevDocument, MouseMove, TranslateCanvasEnd, TranslateCanvasBegin, PasteLayers, RotateCanvasBegin, ZoomCanvasBegin, SetCanvasZoom, MultiplyCanvasZoom, SetRotation, WheelCanvasZoom, WheelCanvasTranslate) + let select = actions!(DocumentMessageDiscriminant; DeleteSelectedLayers, DuplicateSelectedLayers, CopySelectedLayers, NudgeSelectedLayers ); + common.extend(select); } + if self.rotating { + let snapping = actions!(DocumentMessageDiscriminant; EnableSnapping, DisableSnapping); + common.extend(snapping); + } + common } } diff --git a/core/editor/src/input/input_mapper.rs b/core/editor/src/input/input_mapper.rs index ed885e18..eeee035e 100644 --- a/core/editor/src/input/input_mapper.rs +++ b/core/editor/src/input/input_mapper.rs @@ -112,6 +112,8 @@ impl Default for Mapping { fn default() -> Self { let (up, down, pointer_move, mouse_scroll) = mapping![ entry! {action=DocumentMessage::PasteLayers, key_down=KeyV, modifiers=[KeyControl]}, + entry! {action=DocumentMessage::EnableSnapping, key_down=KeyShift}, + entry! {action=DocumentMessage::DisableSnapping, key_up=KeyShift}, // Select entry! {action=SelectMessage::MouseMove, message=InputMapperMessage::PointerMove}, entry! {action=SelectMessage::DragStart, key_down=Lmb}, @@ -186,8 +188,8 @@ impl Default for Mapping { entry! {action=DocumentMessage::ExportDocument, key_down=KeyS, modifiers=[KeyControl, KeyShift]}, entry! {action=DocumentMessage::ExportDocument, key_down=KeyE, modifiers=[KeyControl]}, entry! {action=DocumentMessage::MouseMove, message=InputMapperMessage::PointerMove}, - entry! {action=DocumentMessage::RotateCanvasBegin{snap:true}, key_down=Mmb, modifiers=[KeyControl, KeyShift]}, entry! {action=DocumentMessage::RotateCanvasBegin{snap:false}, key_down=Mmb, modifiers=[KeyControl]}, + entry! {action=DocumentMessage::RotateCanvasBegin{snap:true}, key_down=Mmb, modifiers=[KeyControl, KeyShift]}, entry! {action=DocumentMessage::ZoomCanvasBegin, key_down=Mmb, modifiers=[KeyShift]}, entry! {action=DocumentMessage::TranslateCanvasBegin, key_down=Mmb}, entry! {action=DocumentMessage::TranslateCanvasEnd, key_up=Mmb},