1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use anyhow::Result;
use femtovg::{FontId, Path};
use relm4::{
gtk::gdk::{Key, ModifierType},
Sender,
};
use crate::{
math::{Angle, Vec2D},
sketch_board::{MouseButton, MouseEventMsg, MouseEventType, SketchBoardInput},
style::Style,
};
use super::{Drawable, DrawableClone, Tool, ToolUpdateResult, Tools};
#[derive(Clone, Copy, Debug)]
pub struct Arrow {
start: Vec2D,
end: Option<Vec2D>,
style: Style,
}
#[derive(Default)]
pub struct ArrowTool {
arrow: Option<Arrow>,
style: Style,
input_enabled: bool,
sender: Option<Sender<SketchBoardInput>>,
}
impl Tool for ArrowTool {
fn input_enabled(&self) -> bool {
self.input_enabled
}
fn set_input_enabled(&mut self, value: bool) {
self.input_enabled = value;
}
fn get_tool_type(&self) -> super::Tools {
Tools::Arrow
}
fn handle_mouse_event(&mut self, event: MouseEventMsg) -> ToolUpdateResult {
match event.type_ {
MouseEventType::BeginDrag => {
if event.button == MouseButton::Middle {
return ToolUpdateResult::Unmodified;
}
// start new
self.arrow = Some(Arrow {
start: event.pos,
end: None,
style: self.style,
});
ToolUpdateResult::Redraw
}
MouseEventType::EndDrag => {
if event.button == MouseButton::Middle {
return ToolUpdateResult::Unmodified;
}
if let Some(a) = &mut self.arrow {
if event.pos == Vec2D::zero() {
self.arrow = None;
ToolUpdateResult::Redraw
} else {
if event.modifier.intersects(ModifierType::SHIFT_MASK) {
a.end = Some(a.start + event.pos.snapped_vector_15deg());
} else {
a.end = Some(a.start + event.pos);
}
let result = a.clone_box();
self.arrow = None;
ToolUpdateResult::Commit(result)
}
} else {
ToolUpdateResult::Unmodified
}
}
MouseEventType::UpdateDrag => {
if event.button == MouseButton::Middle {
return ToolUpdateResult::Unmodified;
}
if let Some(a) = &mut self.arrow {
if event.pos == Vec2D::zero() {
return ToolUpdateResult::Unmodified;
}
if event.modifier.intersects(ModifierType::SHIFT_MASK) {
a.end = Some(a.start + event.pos.snapped_vector_15deg());
} else {
a.end = Some(a.start + event.pos);
}
ToolUpdateResult::Redraw
} else {
ToolUpdateResult::Unmodified
}
}
_ => ToolUpdateResult::Unmodified,
}
}
fn handle_key_event(&mut self, event: crate::sketch_board::KeyEventMsg) -> ToolUpdateResult {
if event.key == Key::Escape && self.arrow.is_some() {
self.arrow = None;
ToolUpdateResult::Redraw
} else {
ToolUpdateResult::Unmodified
}
}
fn get_drawable(&self) -> Option<&dyn Drawable> {
match &self.arrow {
Some(d) => Some(d),
None => None,
}
}
fn handle_style_event(&mut self, style: Style) -> ToolUpdateResult {
self.style = style;
ToolUpdateResult::Unmodified
}
fn set_sender(&mut self, sender: Sender<SketchBoardInput>) {
self.sender = Some(sender);
}
}
impl Drawable for Arrow {
fn draw(
&self,
canvas: &mut femtovg::Canvas<femtovg::renderer::OpenGl>,
_font: FontId,
_bounds: (Vec2D, Vec2D),
) -> Result<()> {
let end = match self.end {
Some(e) => e,
None => return Ok(()), // exit if no end
};
// Fat arrow:
// C
// E #
// ######G###
// A ######D##### B
// ##########
// F #
//
//
// Thin arrow:
// C
// \
// A -------- B
// /
//
// A: start
// B: end
// C: head side
// D: midpoint
// E: tail side
// F: tail side
// G: the cross-section of C - D on the tail side.
// Head: the point of the head at the end of the arrow (2, 3, 4).
// Tail: the line from the start to the midpoint (1 - 4).
// Side: the sloped side of the arrow head (3 - 2).
// Midpoint: where the tail ends and the head begins (4).
// Arrow length: the distance from the start to the end (1 - 2).
// Head angle: the angle of the head point at end (2).
// Tail width: the distance from tail side to tail side (5 - 6).
let arrow_offset = end - self.start;
let arrow_length = arrow_offset.norm();
let arrow_direction = arrow_offset * (1.0 / arrow_length);
// We rotate the canvas so that we can draw the arrow on the x-axis.
// start will be at (0,0)
// end will be at (length, 0)
canvas.save();
canvas.translate(self.start.x, self.start.y);
canvas.rotate(arrow_direction.angle().radians);
// The width of the tail (double distance from start to head side)
let tail_width = self
.style
.size
.to_arrow_tail_width(self.style.annotation_size_factor);
// The length of the (sloped) side of the arrow head (distance from end to head side).
let head_side_length = self
.style
.size
.to_arrow_head_length(self.style.annotation_size_factor);
// The offset of the midpoint is the distance the midpoint moves toward the end of the arrow.
// A offset of 0 will place the midpoint right below the head side.
// A negative value will result in a diamond head.
// A positive value will result in a sharper head.
let midpoint_offset = head_side_length * 0.1;
let head_angle = Angle::from_degrees(60.0); // The angle of the point of the arrow head.
let tail_half_width = tail_width / 2.0;
let head_half_angle = head_angle * 0.5;
let head_left =
Vec2D::new(arrow_length, 0.0) - Vec2D::from_angle(head_half_angle) * head_side_length;
let midpoint_x = head_left.x + midpoint_offset;
if self.style.fill {
// Draw a 'fat' arrow.
let mut path = Path::new();
path.move_to(midpoint_x, tail_half_width); // G
path.line_to(head_left.x, -head_left.y); // C
path.line_to(arrow_length, 0.0); // B
path.line_to(head_left.x, head_left.y); // C (mirrored)
path.line_to(midpoint_x, -tail_half_width); // G (mirrored)
if midpoint_x > 0.0 {
// If the midpoint is placed _before_ the start, there is only a head and no tail.
// We can skip the beginning of the tail.
path.line_to(0.0, -tail_half_width); // F
path.line_to(0.0, tail_half_width); // E
}
path.close();
canvas.fill_path(&path, &self.style.into());
} else {
// Draw a 'thin' arrow head.
let mut path = Path::new();
path.move_to(head_left.x, -head_left.y); // C
path.line_to(arrow_length, 0.0); // B
path.line_to(head_left.x, head_left.y); // C (mirrored)
path.move_to(0.0, 0.0); // A
path.line_to(arrow_length, 0.0); // B
canvas.stroke_path(&path, &self.style.into());
}
canvas.restore();
Ok(())
}
}