Skip to content

Commit 2245c65

Browse files
committed
Refactor tools to avoid state keeping
1 parent 1fb1f56 commit 2245c65

File tree

9 files changed

+33
-68
lines changed

9 files changed

+33
-68
lines changed

core/editor/src/document/document_file.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,6 @@ impl MessageHandler<DocumentMessage, &InputPreprocessor> for DocumentMessageHand
418418
FlipAxis::Y => DVec2::new(1., -1.),
419419
};
420420
if let Some([min, max]) = self.document.combined_viewport_bounding_box(self.selected_layers().map(|x| x.as_slice())) {
421-
let size = max - min;
422421
let center = (max + min) / 2.;
423422
let bbox_trans = DAffine2::from_translation(-center);
424423
for path in self.selected_layers() {

core/editor/src/input/input_mapper.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ macro_rules! mapping {
112112

113113
impl Default for Mapping {
114114
fn default() -> Self {
115+
use Key::*;
115116
let mappings = mapping![
116117
entry! {action=DocumentsMessage::PasteLayers{path: vec![], insert_index: -1}, key_down=KeyV, modifiers=[KeyControl]},
117118
entry! {action=MovementMessage::EnableSnapping, key_down=KeyShift},
@@ -130,31 +131,19 @@ impl Default for Mapping {
130131
entry! {action=RectangleMessage::DragStop, key_up=Lmb},
131132
entry! {action=RectangleMessage::Abort, key_down=Rmb},
132133
entry! {action=RectangleMessage::Abort, key_down=KeyEscape},
133-
entry! {action=RectangleMessage::Resize(ResizeMessage::Center), key_down=KeyAlt},
134-
entry! {action=RectangleMessage::Resize(ResizeMessage::UnCenter), key_up=KeyAlt},
135-
entry! {action=RectangleMessage::Resize(ResizeMessage::PointerMove), message=InputMapperMessage::PointerMove},
136-
entry! {action=RectangleMessage::Resize(ResizeMessage::LockAspectRatio), key_down=KeyShift},
137-
entry! {action=RectangleMessage::Resize(ResizeMessage::UnlockAspectRatio), key_up=KeyShift},
134+
entry! {action=RectangleMessage::Resize{center: KeyAlt, lock_ratio: KeyShift}, message=InputMapperMessage::PointerMove},
138135
// Ellipse
139136
entry! {action=EllipseMessage::DragStart, key_down=Lmb},
140137
entry! {action=EllipseMessage::DragStop, key_up=Lmb},
141138
entry! {action=EllipseMessage::Abort, key_down=Rmb},
142139
entry! {action=EllipseMessage::Abort, key_down=KeyEscape},
143-
entry! {action=EllipseMessage::Resize(ResizeMessage::Center), key_down=KeyAlt},
144-
entry! {action=EllipseMessage::Resize(ResizeMessage::UnCenter), key_up=KeyAlt},
145-
entry! {action=EllipseMessage::Resize(ResizeMessage::PointerMove), message=InputMapperMessage::PointerMove},
146-
entry! {action=EllipseMessage::Resize(ResizeMessage::LockAspectRatio), key_down=KeyShift},
147-
entry! {action=EllipseMessage::Resize(ResizeMessage::UnlockAspectRatio), key_up=KeyShift},
140+
entry! {action=EllipseMessage::Resize{center: KeyAlt, lock_ratio: KeyShift}, message=InputMapperMessage::PointerMove},
148141
// Shape
149142
entry! {action=ShapeMessage::DragStart, key_down=Lmb},
150143
entry! {action=ShapeMessage::DragStop, key_up=Lmb},
151144
entry! {action=ShapeMessage::Abort, key_down=Rmb},
152145
entry! {action=ShapeMessage::Abort, key_down=KeyEscape},
153-
entry! {action=ShapeMessage::Resize(ResizeMessage::Center), key_down=KeyAlt},
154-
entry! {action=ShapeMessage::Resize(ResizeMessage::UnCenter), key_up=KeyAlt},
155-
entry! {action=ShapeMessage::Resize(ResizeMessage::PointerMove), message=InputMapperMessage::PointerMove},
156-
entry! {action=ShapeMessage::Resize(ResizeMessage::LockAspectRatio), key_down=KeyShift},
157-
entry! {action=ShapeMessage::Resize(ResizeMessage::UnlockAspectRatio), key_up=KeyShift},
146+
entry! {action=ShapeMessage::Resize{center: KeyAlt, lock_ratio: KeyShift}, message=InputMapperMessage::PointerMove},
158147
// Line
159148
entry! {action=LineMessage::Center, key_down=KeyAlt},
160149
entry! {action=LineMessage::UnCenter, key_up=KeyAlt},

core/editor/src/input/input_preprocessor.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use bitflags::bitflags;
77

88
#[doc(inline)]
99
pub use document_core::DocumentResponse;
10-
use glam::DVec2;
1110

1211
#[impl_message(Message, InputPreprocessor)]
1312
#[derive(PartialEq, Clone, Debug)]

core/editor/src/input/keyboard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<const LENGTH: usize> BitVector<LENGTH> {
112112
let (offset, bit) = Self::convert_index(bitvector_index);
113113
self.0[offset] ^= bit;
114114
}
115-
pub fn get(&mut self, bitvector_index: usize) -> bool {
115+
pub fn get(&self, bitvector_index: usize) -> bool {
116116
let (offset, bit) = Self::convert_index(bitvector_index);
117117
(self.0[offset] & bit) != 0
118118
}

core/editor/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ pub mod message_prelude {
7676
pub use crate::tool::tools::path::{PathMessage, PathMessageDiscriminant};
7777
pub use crate::tool::tools::pen::{PenMessage, PenMessageDiscriminant};
7878
pub use crate::tool::tools::rectangle::{RectangleMessage, RectangleMessageDiscriminant};
79-
pub use crate::tool::tools::resize::{ResizeMessage, ResizeMessageDiscriminant};
8079
pub use crate::tool::tools::select::{SelectMessage, SelectMessageDiscriminant};
8180
pub use crate::tool::tools::shape::{ShapeMessage, ShapeMessageDiscriminant};
8281
pub use crate::LayerId;

core/editor/src/tool/tools/ellipse.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::input::keyboard::Key;
12
use crate::input::InputPreprocessor;
23
use crate::tool::{DocumentToolData, Fsm, ToolActionHandlerData};
34
use crate::{document::DocumentMessageHandler, message_prelude::*};
@@ -17,7 +18,7 @@ pub struct Ellipse {
1718
pub enum EllipseMessage {
1819
DragStart,
1920
DragStop,
20-
Resize(ResizeMessage),
21+
Resize { center: Key, lock_ratio: Key },
2122
Abort,
2223
}
2324

@@ -28,7 +29,7 @@ impl<'a> MessageHandler<ToolMessage, ToolActionHandlerData<'a>> for Ellipse {
2829
fn actions(&self) -> ActionList {
2930
use EllipseToolFsmState::*;
3031
match self.fsm_state {
31-
Ready => actions!(EllipseMessageDiscriminant; DragStart, Resize),
32+
Ready => actions!(EllipseMessageDiscriminant; DragStart),
3233
Dragging => actions!(EllipseMessageDiscriminant; DragStop, Abort, Resize),
3334
}
3435
}
@@ -86,8 +87,12 @@ impl Fsm for EllipseToolFsmState {
8687

8788
Dragging
8889
}
89-
(state, Resize(message)) => {
90-
shape_data.process_action(message, input, responses);
90+
(state, Resize { center, lock_ratio }) => {
91+
if let Some(message) = shape_data.calculate_transform(center, lock_ratio, input) {
92+
responses.push_back(message);
93+
log::debug!("dragging");
94+
}
95+
9196
state
9297
}
9398
(Dragging, DragStop) => {

core/editor/src/tool/tools/rectangle.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::input::keyboard::Key;
12
use crate::input::InputPreprocessor;
23
use crate::tool::{DocumentToolData, Fsm, ToolActionHandlerData};
34
use crate::{document::DocumentMessageHandler, message_prelude::*};
@@ -17,7 +18,7 @@ pub struct Rectangle {
1718
pub enum RectangleMessage {
1819
DragStart,
1920
DragStop,
20-
Resize(ResizeMessage),
21+
Resize { center: Key, lock_ratio: Key },
2122
Abort,
2223
}
2324

@@ -28,7 +29,7 @@ impl<'a> MessageHandler<ToolMessage, ToolActionHandlerData<'a>> for Rectangle {
2829
fn actions(&self) -> ActionList {
2930
use RectangleToolFsmState::*;
3031
match self.fsm_state {
31-
Ready => actions!(RectangleMessageDiscriminant; DragStart, Resize),
32+
Ready => actions!(RectangleMessageDiscriminant; DragStart),
3233
Dragging => actions!(RectangleMessageDiscriminant; DragStop, Abort, Resize),
3334
}
3435
}
@@ -86,8 +87,10 @@ impl Fsm for RectangleToolFsmState {
8687

8788
Dragging
8889
}
89-
(state, Resize(message)) => {
90-
shape_data.process_action(message, input, responses);
90+
(state, Resize { center, lock_ratio }) => {
91+
if let Some(message) = shape_data.calculate_transform(center, lock_ratio, input) {
92+
responses.push_back(message);
93+
}
9194

9295
state
9396
}

core/editor/src/tool/tools/resize.rs

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::input::keyboard::Key;
12
use crate::input::{mouse::ViewportPosition, InputPreprocessor};
23
use crate::message_prelude::*;
34
use document_core::Operation;
@@ -6,51 +7,18 @@ use glam::{DAffine2, Vec2Swizzles};
67
#[derive(Clone, Debug, Default)]
78
pub struct Resize {
89
pub drag_start: ViewportPosition,
9-
pub drag_current: ViewportPosition,
10-
pub constrain_to_square: bool,
11-
pub center_around_cursor: bool,
1210
pub path: Option<Vec<LayerId>>,
1311
}
14-
15-
#[impl_message]
16-
#[derive(PartialEq, Clone, Debug, Hash)]
17-
pub enum ResizeMessage {
18-
PointerMove,
19-
Center,
20-
UnCenter,
21-
LockAspectRatio,
22-
UnlockAspectRatio,
23-
}
24-
25-
impl<'a> MessageHandler<ResizeMessage, &InputPreprocessor> for Resize {
26-
fn process_action(&mut self, action: ResizeMessage, ipp: &InputPreprocessor, responses: &mut VecDeque<Message>) {
27-
self.drag_current = ipp.mouse.position;
28-
use ResizeMessage::*;
29-
match action {
30-
PointerMove => self.drag_current = ipp.mouse.position,
31-
LockAspectRatio => self.constrain_to_square = true,
32-
UnlockAspectRatio => self.constrain_to_square = false,
33-
Center => self.center_around_cursor = true,
34-
UnCenter => self.center_around_cursor = false,
35-
}
36-
if let Some(message) = self.calculate_transform() {
37-
responses.push_back(message);
38-
}
39-
}
40-
fn actions(&self) -> ActionList {
41-
vec![]
42-
}
43-
}
4412
impl Resize {
45-
fn calculate_transform(&self) -> Option<Message> {
13+
pub fn calculate_transform(&self, center: Key, lock_ratio: Key, ipp: &InputPreprocessor) -> Option<Message> {
4614
let mut start = self.drag_start.as_f64();
47-
let stop = self.drag_current.as_f64();
15+
let stop = ipp.mouse.position.as_f64();
4816

4917
let mut size = stop - start;
50-
if self.constrain_to_square {
18+
if ipp.keyboard.get(center as usize) {
5119
size = size.abs().max(size.abs().yx()) * size.signum();
5220
}
53-
if self.center_around_cursor {
21+
if ipp.keyboard.get(lock_ratio as usize) {
5422
start -= size / 2.;
5523
}
5624

core/editor/src/tool/tools/shape.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::input::keyboard::Key;
12
use crate::input::InputPreprocessor;
23
use crate::tool::{DocumentToolData, Fsm, ShapeType, ToolActionHandlerData, ToolOptions, ToolType};
34
use crate::{document::DocumentMessageHandler, message_prelude::*};
@@ -17,7 +18,7 @@ pub struct Shape {
1718
pub enum ShapeMessage {
1819
DragStart,
1920
DragStop,
20-
Resize(ResizeMessage),
21+
Resize { center: Key, lock_ratio: Key },
2122
Abort,
2223
}
2324

@@ -28,7 +29,7 @@ impl<'a> MessageHandler<ToolMessage, ToolActionHandlerData<'a>> for Shape {
2829
fn actions(&self) -> ActionList {
2930
use ShapeToolFsmState::*;
3031
match self.fsm_state {
31-
Ready => actions!(ShapeMessageDiscriminant; DragStart, Resize),
32+
Ready => actions!(ShapeMessageDiscriminant; DragStart),
3233
Dragging => actions!(ShapeMessageDiscriminant; DragStop, Abort, Resize),
3334
}
3435
}
@@ -93,8 +94,10 @@ impl Fsm for ShapeToolFsmState {
9394

9495
Dragging
9596
}
96-
(state, Resize(message)) => {
97-
shape_data.process_action(message, input, responses);
97+
(state, Resize { center, lock_ratio }) => {
98+
if let Some(message) = shape_data.calculate_transform(center, lock_ratio, input) {
99+
responses.push_back(message);
100+
}
98101

99102
state
100103
}

0 commit comments

Comments
 (0)