Skip to content

Commit 98fde72

Browse files
authored
Add a stroke width option to the Line Tool (#355)
* Add a stroke width option to the Line Tool * Fix title case for line options * Add px unit to line stroke width * Add stroke width to pen tool * Rename stroke width to weight * Change number input width to min-width * Remove the word "stroke" from "stroke weight"
1 parent 79012ba commit 98fde72

File tree

5 files changed

+33
-8
lines changed

5 files changed

+33
-8
lines changed

editor/src/tool/tool_options.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ pub enum ToolOptions {
55
Select { append_mode: SelectAppendMode },
66
Ellipse,
77
Shape { shape_type: ShapeType },
8+
Line { weight: u32 },
9+
Pen { weight: u32 },
810
}
911

1012
#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize, Hash)]

editor/src/tool/tools/line.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::consts::LINE_ROTATE_SNAP_ANGLE;
22
use crate::input::keyboard::Key;
33
use crate::input::{mouse::ViewportPosition, InputPreprocessor};
4-
use crate::tool::{DocumentToolData, Fsm, ToolActionHandlerData};
4+
use crate::tool::{DocumentToolData, Fsm, ToolActionHandlerData, ToolOptions, ToolType};
55
use crate::{document::DocumentMessageHandler, message_prelude::*};
66
use glam::{DAffine2, DVec2};
77
use graphene::{layers::style, Operation};
@@ -50,6 +50,7 @@ struct LineToolData {
5050
drag_start: ViewportPosition,
5151
drag_current: ViewportPosition,
5252
angle: f64,
53+
weight: u32,
5354
path: Option<Vec<LayerId>>,
5455
}
5556

@@ -75,12 +76,17 @@ impl Fsm for LineToolFsmState {
7576
data.path = Some(vec![generate_uuid()]);
7677
responses.push_back(DocumentMessage::DeselectAllLayers.into());
7778

79+
data.weight = match tool_data.tool_options.get(&ToolType::Line) {
80+
Some(&ToolOptions::Line { weight }) => weight,
81+
_ => 5,
82+
};
83+
7884
responses.push_back(
7985
Operation::AddLine {
8086
path: data.path.clone().unwrap(),
8187
insert_index: -1,
8288
transform: DAffine2::ZERO.to_cols_array(),
83-
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, 5.)), None),
89+
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), None),
8490
}
8591
.into(),
8692
);

editor/src/tool/tools/pen.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::input::InputPreprocessor;
2-
use crate::tool::{DocumentToolData, Fsm, ToolActionHandlerData};
2+
use crate::tool::{DocumentToolData, Fsm, ToolActionHandlerData, ToolOptions, ToolType};
33
use crate::{document::DocumentMessageHandler, message_prelude::*};
44
use glam::DAffine2;
55
use graphene::{layers::style, Operation};
@@ -49,6 +49,7 @@ impl Default for PenToolFsmState {
4949
struct PenToolData {
5050
points: Vec<DAffine2>,
5151
next_point: DAffine2,
52+
weight: u32,
5253
path: Option<Vec<LayerId>>,
5354
}
5455

@@ -79,6 +80,11 @@ impl Fsm for PenToolFsmState {
7980
data.points.push(pos);
8081
data.next_point = pos;
8182

83+
data.weight = match tool_data.tool_options.get(&ToolType::Pen) {
84+
Some(&ToolOptions::Pen { weight }) => weight,
85+
_ => 5,
86+
};
87+
8288
Dragging
8389
}
8490
(Dragging, DragStop) => {
@@ -140,7 +146,7 @@ fn make_operation(data: &PenToolData, tool_data: &DocumentToolData, show_preview
140146
insert_index: -1,
141147
transform: DAffine2::IDENTITY.to_cols_array(),
142148
points,
143-
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, 5.)), Some(style::Fill::none())),
149+
style: style::PathStyle::new(Some(style::Stroke::new(tool_data.primary_color, data.weight as f32)), Some(style::Fill::none())),
144150
}
145151
.into(),
146152
]

frontend/src/components/widgets/inputs/NumberInput.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<style lang="scss">
2121
.number-input {
22-
width: 80px;
22+
min-width: 80px;
2323
height: 24px;
2424
position: relative;
2525
border-radius: 2px;
@@ -38,7 +38,8 @@
3838
3939
input {
4040
flex: 1 1 100%;
41-
width: 100%;
41+
width: 0;
42+
min-width: 30px;
4243
height: 18px;
4344
line-height: 18px;
4445
margin: 0 8px;

frontend/src/components/widgets/options/ToolOptions.vue

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export default defineComponent({
4141
},
4242
computed: {},
4343
methods: {
44-
async setToolOptions(newValue: number) {
44+
async setShapeOptions(newValue: number) {
4545
// TODO: Each value-input widget (i.e. not a button) should map to a field in an options struct,
4646
// and updating a widget should send the whole updated struct to the backend.
4747
// Later, it could send a single-field update to the backend.
@@ -50,6 +50,14 @@ export default defineComponent({
5050
// eslint-disable-next-line camelcase
5151
(await wasm).set_tool_options(this.$props.activeTool || "", { Shape: { shape_type: { Polygon: { vertices: newValue } } } });
5252
},
53+
async setLineOptions(newValue: number) {
54+
// eslint-disable-next-line camelcase
55+
(await wasm).set_tool_options(this.$props.activeTool || "", { Line: { weight: newValue } });
56+
},
57+
async setPenOptions(newValue: number) {
58+
// eslint-disable-next-line camelcase
59+
(await wasm).set_tool_options(this.$props.activeTool || "", { Pen: { weight: newValue } });
60+
},
5361
async sendToolMessage(message: string | object) {
5462
(await wasm).send_tool_message(this.$props.activeTool || "", message);
5563
},
@@ -126,7 +134,9 @@ export default defineComponent({
126134
props: {},
127135
},
128136
],
129-
Shape: [{ kind: "NumberInput", callback: this.setToolOptions, props: { value: 6, min: 3, isInteger: true, label: "Sides" } }],
137+
Shape: [{ kind: "NumberInput", callback: this.setShapeOptions, props: { value: 6, min: 3, isInteger: true, label: "Sides" } }],
138+
Line: [{ kind: "NumberInput", callback: this.setLineOptions, props: { value: 5, min: 1, isInteger: true, unit: " px", label: "Weight" } }],
139+
Pen: [{ kind: "NumberInput", callback: this.setPenOptions, props: { value: 5, min: 1, isInteger: true, unit: " px", label: "Weight" } }],
130140
};
131141
132142
return {

0 commit comments

Comments
 (0)