From 22947933b0376be5a7156e892c6c0a11cb2aba71 Mon Sep 17 00:00:00 2001 From: Keavon Chambers Date: Tue, 10 Aug 2021 16:21:18 -0700 Subject: [PATCH] Improve document zooming to work based on nice steps (#336) * Improve document zooming to work based on nice steps * Code review improvements --- editor/src/consts.rs | 22 ++++++----- editor/src/document/layer_panel.rs | 4 +- editor/src/document/movement_handler.rs | 26 ++++++++----- editor/src/input/input_mapper.rs | 7 ++-- frontend/src/components/panels/Document.vue | 23 +++++++---- .../components/widgets/inputs/NumberInput.vue | 39 ++++++++++++++----- frontend/src/components/widgets/widgets.ts | 4 +- frontend/wasm/src/document.rs | 16 +++++++- rustfmt.toml | 1 + 9 files changed, 97 insertions(+), 45 deletions(-) diff --git a/editor/src/consts.rs b/editor/src/consts.rs index 28efbc13..9dbdc7c3 100644 --- a/editor/src/consts.rs +++ b/editor/src/consts.rs @@ -1,16 +1,20 @@ -pub const PLUS_KEY_ZOOM_RATE: f64 = 1.25; -pub const MINUS_KEY_ZOOM_RATE: f64 = 0.8; - -pub const VIEWPORT_ZOOM_SCALE_MIN: f64 = 0.000_001; -pub const VIEWPORT_ZOOM_SCALE_MAX: f64 = 1_000_000.; +// VIEWPORT +pub const VIEWPORT_ZOOM_WHEEL_RATE: f64 = 1. / 600.; +pub const VIEWPORT_ZOOM_MOUSE_RATE: f64 = 1. / 400.; +pub const VIEWPORT_ZOOM_SCALE_MIN: f64 = 0.000_000_1; +pub const VIEWPORT_ZOOM_SCALE_MAX: f64 = 10_000.; +pub const VIEWPORT_ZOOM_LEVELS: [f64; 74] = [ + 0.0001, 0.000125, 0.00016, 0.0002, 0.00025, 0.00032, 0.0004, 0.0005, 0.00064, 0.0008, 0.001, 0.0016, 0.002, 0.0025, 0.0032, 0.004, 0.005, 0.0064, 0.008, 0.01, 0.01125, 0.015, 0.02, 0.025, 0.03, + 0.04, 0.05, 0.06, 0.08, 0.1, 0.125, 0.15, 0.2, 0.25, 0.33333333, 0.4, 0.5, 0.66666666, 0.8, 1., 1.25, 1.6, 2., 2.5, 3.2, 4., 5., 6.4, 8., 10., 12.5, 16., 20., 25., 32., 40., 50., 64., 80., 100., + 128., 160., 200., 256., 320., 400., 512., 640., 800., 1024., 1280., 1600., 2048., 2560., +]; pub const VIEWPORT_SCROLL_RATE: f64 = 0.6; -pub const WHEEL_ZOOM_RATE: f64 = 1. / 600.; -pub const MOUSE_ZOOM_RATE: f64 = 1. / 400.; - -pub const ROTATE_SNAP_INTERVAL: f64 = 15.; +pub const VIEWPORT_ROTATE_SNAP_INTERVAL: f64 = 15.; +// LINE TOOL pub const LINE_ROTATE_SNAP_ANGLE: f64 = 15.; +// SELECT TOOL pub const SELECTION_TOLERANCE: f64 = 1.0; diff --git a/editor/src/document/layer_panel.rs b/editor/src/document/layer_panel.rs index fbef9be6..adb0d22c 100644 --- a/editor/src/document/layer_panel.rs +++ b/editor/src/document/layer_panel.rs @@ -1,4 +1,4 @@ -use crate::{consts::ROTATE_SNAP_INTERVAL, frontend::layer_panel::*}; +use crate::{consts::VIEWPORT_ROTATE_SNAP_INTERVAL, frontend::layer_panel::*}; use glam::{DAffine2, DVec2}; use graphene::{ layers::{Layer, LayerData as DocumentLayerData}, @@ -30,7 +30,7 @@ impl LayerData { } pub fn snapped_angle(&self) -> f64 { - let increment_radians: f64 = ROTATE_SNAP_INTERVAL.to_radians(); + let increment_radians: f64 = VIEWPORT_ROTATE_SNAP_INTERVAL.to_radians(); if self.snap_rotate { (self.rotation / increment_radians).round() * increment_radians } else { diff --git a/editor/src/document/movement_handler.rs b/editor/src/document/movement_handler.rs index 1517b432..d28d604e 100644 --- a/editor/src/document/movement_handler.rs +++ b/editor/src/document/movement_handler.rs @@ -4,7 +4,7 @@ use super::LayerData; use crate::message_prelude::*; use crate::{ - consts::{MOUSE_ZOOM_RATE, VIEWPORT_SCROLL_RATE, VIEWPORT_ZOOM_SCALE_MAX, VIEWPORT_ZOOM_SCALE_MIN, WHEEL_ZOOM_RATE}, + consts::{VIEWPORT_SCROLL_RATE, VIEWPORT_ZOOM_LEVELS, VIEWPORT_ZOOM_MOUSE_RATE, VIEWPORT_ZOOM_SCALE_MAX, VIEWPORT_ZOOM_SCALE_MIN, VIEWPORT_ZOOM_WHEEL_RATE}, input::{mouse::ViewportPosition, InputPreprocessor}, }; use glam::DVec2; @@ -24,10 +24,11 @@ pub enum MovementMessage { DisableSnapping, ZoomCanvasBegin, TranslateCanvasEnd, - SetCanvasZoom(f64), - MultiplyCanvasZoom(f64), - WheelCanvasZoom, SetCanvasRotation(f64), + SetCanvasZoom(f64), + IncreaseCanvasZoom, + DecreaseCanvasZoom, + WheelCanvasZoom, ZoomCanvasToFitAll, } @@ -113,7 +114,7 @@ impl MessageHandler { - let new = (layerdata.scale * multiplier).clamp(VIEWPORT_ZOOM_SCALE_MIN, VIEWPORT_ZOOM_SCALE_MAX); - layerdata.scale = new; + IncreaseCanvasZoom => { + layerdata.scale = *VIEWPORT_ZOOM_LEVELS.iter().find(|scale| **scale > layerdata.scale).unwrap_or(&layerdata.scale); + responses.push_back(FrontendMessage::SetCanvasZoom { new_zoom: layerdata.scale }.into()); + self.create_document_transform_from_layerdata(layerdata, &ipp.viewport_size, responses); + } + DecreaseCanvasZoom => { + layerdata.scale = *VIEWPORT_ZOOM_LEVELS.iter().rev().find(|scale| **scale < layerdata.scale).unwrap_or(&layerdata.scale); responses.push_back(FrontendMessage::SetCanvasZoom { new_zoom: layerdata.scale }.into()); self.create_document_transform_from_layerdata(layerdata, &ipp.viewport_size, responses); } @@ -137,7 +142,7 @@ impl MessageHandler 0 { zoom_factor = 1. / zoom_factor }; @@ -195,9 +200,10 @@ impl MessageHandler - + @@ -54,11 +54,12 @@