Clean up and polish some code from the previous commit (#255)

This commit is contained in:
Keavon Chambers
2021-07-13 00:52:45 -07:00
committed by GitHub
parent 39e6893630
commit 7ae91e8330
9 changed files with 88 additions and 79 deletions

View File

@@ -27,7 +27,7 @@ impl Dispatcher {
| Message::InputMapper(_)
| Message::Document(DocumentMessage::RenderDocument)
| Message::Frontend(FrontendMessage::UpdateCanvas { .. })
| Message::Frontend(FrontendMessage::SetZoom { .. })
| Message::Frontend(FrontendMessage::SetCanvasZoom { .. })
| Message::Frontend(FrontendMessage::SetRotation { .. })
| Message::Document(DocumentMessage::DispatchOperation { .. })
) || MessageDiscriminant::from(&message).local_name().ends_with("MouseMove"))

View File

@@ -1,5 +1,8 @@
pub const PLUS_KEY_MULTIPLIER: f64 = 1.25;
pub const MINUS_KEY_MULTIPLIER: f64 = 0.8;
pub const PLUS_KEY_ZOOM_MULTIPLIER: f64 = 1.25;
pub const MINUS_KEY_ZOOM_MULTIPLIER: f64 = 0.8;
pub const VIEWPORT_ZOOM_SCALE_MIN: f64 = 0.000_001;
pub const VIEWPORT_ZOOM_SCALE_MAX: f64 = 1_000_000.;
pub const WHEEL_ZOOM_DIVISOR: f64 = 500.;
pub const MOUSE_ZOOM_DIVISOR: f64 = 400.;

View File

@@ -1,6 +1,6 @@
use crate::message_prelude::*;
use crate::{
consts::{MOUSE_ZOOM_DIVISOR, WHEEL_ZOOM_DIVISOR},
consts::{MOUSE_ZOOM_DIVISOR, VIEWPORT_ZOOM_SCALE_MAX, VIEWPORT_ZOOM_SCALE_MIN, WHEEL_ZOOM_DIVISOR},
input::{mouse::ViewportPosition, InputPreprocessor},
};
use document_core::layers::Layer;
@@ -39,15 +39,14 @@ pub enum DocumentMessage {
RenderDocument,
Undo,
MouseMove,
TranslateDown,
TranslateUp,
WheelTranslate,
RotateDown { snap: bool },
ZoomDown,
TransformUp,
SetZoom(f64),
MultiplyZoom(f64),
WheelZoom,
TranslateCanvasBegin,
WheelCanvasTranslate,
RotateCanvasBegin { snap: bool },
ZoomCanvasBegin,
TranslateCanvasEnd,
SetCanvasZoom(f64),
MultiplyCanvasZoom(f64),
WheelCanvasZoom,
SetRotation(f64),
NudgeSelectedLayers(f64, f64),
}
@@ -380,21 +379,22 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
}
.into(),
),
TranslateDown => {
TranslateCanvasBegin => {
self.translating = true;
self.mouse_pos = ipp.mouse.position;
}
RotateDown { snap } => {
RotateCanvasBegin { snap } => {
self.rotating = true;
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;
}
ZoomDown => {
ZoomCanvasBegin => {
self.zooming = true;
self.mouse_pos = ipp.mouse.position;
}
TransformUp => {
TranslateCanvasEnd => {
let layerdata = self.layerdata_mut(&vec![]);
layerdata.rotation = layerdata.snapped_angle();
layerdata.snap_rotate = false;
@@ -433,25 +433,27 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
let difference = self.mouse_pos.y as f64 - ipp.mouse.position.y as f64;
let amount = 1. + difference / MOUSE_ZOOM_DIVISOR;
let layerdata = self.layerdata_mut(&vec![]);
layerdata.scale *= amount;
responses.push_back(FrontendMessage::SetZoom { new_zoom: layerdata.scale }.into());
let new = (layerdata.scale * amount).clamp(VIEWPORT_ZOOM_SCALE_MIN, VIEWPORT_ZOOM_SCALE_MAX);
layerdata.scale = new;
responses.push_back(FrontendMessage::SetCanvasZoom { new_zoom: layerdata.scale }.into());
self.create_document_transform_from_layerdata(&ipp.viewport_size, responses);
}
self.mouse_pos = ipp.mouse.position;
}
SetZoom(new) => {
SetCanvasZoom(new) => {
let layerdata = self.layerdata_mut(&vec![]);
layerdata.scale = new.clamp(VIEWPORT_ZOOM_SCALE_MIN, VIEWPORT_ZOOM_SCALE_MAX);
responses.push_back(FrontendMessage::SetCanvasZoom { new_zoom: layerdata.scale }.into());
self.create_document_transform_from_layerdata(&ipp.viewport_size, responses);
}
MultiplyCanvasZoom(multiplier) => {
let layerdata = self.layerdata_mut(&vec![]);
let new = (layerdata.scale * multiplier).clamp(VIEWPORT_ZOOM_SCALE_MIN, VIEWPORT_ZOOM_SCALE_MAX);
layerdata.scale = new;
responses.push_back(FrontendMessage::SetZoom { new_zoom: layerdata.scale }.into());
responses.push_back(FrontendMessage::SetCanvasZoom { new_zoom: layerdata.scale }.into());
self.create_document_transform_from_layerdata(&ipp.viewport_size, responses);
}
MultiplyZoom(mult) => {
let layerdata = self.layerdata_mut(&vec![]);
layerdata.scale *= mult;
responses.push_back(FrontendMessage::SetZoom { new_zoom: layerdata.scale }.into());
self.create_document_transform_from_layerdata(&ipp.viewport_size, responses);
}
WheelZoom => {
WheelCanvasZoom => {
let scroll = ipp.mouse.scroll_delta.y as f64;
let amount = if ipp.mouse.scroll_delta.y > 0 {
1. + scroll / -WHEEL_ZOOM_DIVISOR
@@ -459,11 +461,12 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
1. / (1. + scroll / WHEEL_ZOOM_DIVISOR)
};
let layerdata = self.layerdata_mut(&vec![]);
layerdata.scale *= amount;
responses.push_back(FrontendMessage::SetZoom { new_zoom: layerdata.scale }.into());
let new = (layerdata.scale * amount).clamp(VIEWPORT_ZOOM_SCALE_MIN, VIEWPORT_ZOOM_SCALE_MAX);
layerdata.scale = new;
responses.push_back(FrontendMessage::SetCanvasZoom { new_zoom: layerdata.scale }.into());
self.create_document_transform_from_layerdata(&ipp.viewport_size, responses);
}
WheelTranslate => {
WheelCanvasTranslate => {
let delta = -ipp.mouse.scroll_delta.to_dvec2();
let transformed_delta = self.active_document().document.root.transform.inverse().transform_vector2(delta);
let layerdata = self.layerdata_mut(&vec![]);
@@ -491,9 +494,9 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
}
fn actions(&self) -> ActionList {
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, TransformUp, TranslateDown, CopySelectedLayers, PasteLayers, NudgeSelectedLayers, RotateDown, ZoomDown, SetZoom, MultiplyZoom, SetRotation, WheelZoom, WheelTranslate)
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, TransformUp, TranslateDown, PasteLayers, RotateDown, ZoomDown, SetZoom, MultiplyZoom, SetRotation, WheelZoom, WheelTranslate)
actions!(DocumentMessageDiscriminant; Undo, SelectAllLayers, DeselectAllLayers, RenderDocument, ExportDocument, NewDocument, CloseActiveDocument, NextDocument, PrevDocument, MouseMove, TranslateCanvasEnd, TranslateCanvasBegin, PasteLayers, RotateCanvasBegin, ZoomCanvasBegin, SetCanvasZoom, MultiplyCanvasZoom, SetRotation, WheelCanvasZoom, WheelCanvasTranslate)
}
}
}

View File

@@ -20,7 +20,7 @@ pub enum FrontendMessage {
DisableTextInput,
UpdateWorkingColors { primary: Color, secondary: Color },
PromptCloseConfirmationModal,
SetZoom { new_zoom: f64 },
SetCanvasZoom { new_zoom: f64 },
SetRotation { new_radians: f64 },
}
@@ -48,7 +48,7 @@ impl MessageHandler<FrontendMessage, ()> for FrontendMessageHandler {
UpdateCanvas,
EnableTextInput,
DisableTextInput,
SetZoom,
SetCanvasZoom,
SetRotation,
);
}

View File

@@ -1,4 +1,4 @@
use crate::consts::{MINUS_KEY_MULTIPLIER, PLUS_KEY_MULTIPLIER};
use crate::consts::{MINUS_KEY_ZOOM_MULTIPLIER, PLUS_KEY_ZOOM_MULTIPLIER};
use crate::message_prelude::*;
use crate::tool::ToolType;
@@ -186,18 +186,18 @@ 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::RotateDown{snap:true}, key_down=Mmb, modifiers=[KeyControl, KeyShift]},
entry! {action=DocumentMessage::RotateDown{snap:false}, key_down=Mmb, modifiers=[KeyControl]},
entry! {action=DocumentMessage::ZoomDown, key_down=Mmb, modifiers=[KeyShift]},
entry! {action=DocumentMessage::TranslateDown, key_down=Mmb},
entry! {action=DocumentMessage::TransformUp, key_up=Mmb},
entry! {action=DocumentMessage::MultiplyZoom(PLUS_KEY_MULTIPLIER), key_down=KeyPlus, modifiers=[KeyControl]},
entry! {action=DocumentMessage::MultiplyZoom(PLUS_KEY_MULTIPLIER), key_down=KeyEquals, modifiers=[KeyControl]},
entry! {action=DocumentMessage::MultiplyZoom(MINUS_KEY_MULTIPLIER), key_down=KeyMinus, modifiers=[KeyControl]},
entry! {action=DocumentMessage::SetZoom(1.), key_down=Key1, modifiers=[KeyControl]},
entry! {action=DocumentMessage::SetZoom(2.), key_down=Key2, modifiers=[KeyControl]},
entry! {action=DocumentMessage::WheelZoom, message=InputMapperMessage::MouseScroll, modifiers=[KeyControl]},
entry! {action=DocumentMessage::WheelTranslate, message=InputMapperMessage::MouseScroll},
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::ZoomCanvasBegin, key_down=Mmb, modifiers=[KeyShift]},
entry! {action=DocumentMessage::TranslateCanvasBegin, key_down=Mmb},
entry! {action=DocumentMessage::TranslateCanvasEnd, key_up=Mmb},
entry! {action=DocumentMessage::MultiplyCanvasZoom(PLUS_KEY_ZOOM_MULTIPLIER), key_down=KeyPlus, modifiers=[KeyControl]},
entry! {action=DocumentMessage::MultiplyCanvasZoom(PLUS_KEY_ZOOM_MULTIPLIER), key_down=KeyEquals, modifiers=[KeyControl]},
entry! {action=DocumentMessage::MultiplyCanvasZoom(MINUS_KEY_ZOOM_MULTIPLIER), key_down=KeyMinus, modifiers=[KeyControl]},
entry! {action=DocumentMessage::SetCanvasZoom(1.), key_down=Key1, modifiers=[KeyControl]},
entry! {action=DocumentMessage::SetCanvasZoom(2.), key_down=Key2, modifiers=[KeyControl]},
entry! {action=DocumentMessage::WheelCanvasZoom, message=InputMapperMessage::MouseScroll, modifiers=[KeyControl]},
entry! {action=DocumentMessage::WheelCanvasTranslate, message=InputMapperMessage::MouseScroll},
entry! {action=DocumentMessage::NewDocument, key_down=KeyN, modifiers=[KeyShift]},
entry! {action=DocumentMessage::NextDocument, key_down=KeyTab, modifiers=[KeyShift]},
entry! {action=DocumentMessage::CloseActiveDocument, key_down=KeyW, modifiers=[KeyShift]},