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
use std::f64::consts::PI;
use gdk::{EventButton, ModifierType};
use cairo::Context;
use shakmaty::Square;
use ground::{EventContext, GroundMsg};
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum DrawBrush {
Green,
Red,
Blue,
Yellow,
}
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct DrawShape {
orig: Square,
dest: Square,
brush: DrawBrush,
}
pub struct Drawable {
drawing: Option<DrawShape>,
shapes: Vec<DrawShape>,
enabled: bool,
erase_on_click: bool,
}
impl Drawable {
pub fn new() -> Drawable {
Drawable {
drawing: None,
shapes: Vec::new(),
enabled: true,
erase_on_click: true,
}
}
pub(crate) fn mouse_down(&mut self, ctx: &EventContext, e: &EventButton) {
if !self.enabled {
return;
}
match e.get_button() {
1 => {
if self.erase_on_click && !self.shapes.is_empty() {
self.shapes.clear();
ctx.stream().emit(GroundMsg::ShapesChanged(self.shapes.clone()));
ctx.widget().queue_draw();
}
}
3 => {
self.drawing = ctx.square().map(|square| {
let brush = if e.get_state().contains(ModifierType::MOD1_MASK | ModifierType::SHIFT_MASK) {
DrawBrush::Yellow
} else if e.get_state().contains(ModifierType::MOD1_MASK) {
DrawBrush::Blue
} else if e.get_state().contains(ModifierType::SHIFT_MASK) {
DrawBrush::Red
} else {
DrawBrush::Green
};
DrawShape {
orig: square,
dest: square,
brush,
}
});
ctx.widget().queue_draw();
}
_ => {}
}
}
pub(crate) fn mouse_move(&mut self, ctx: &EventContext) {
if let Some(ref mut drawing) = self.drawing {
let dest = ctx.square().unwrap_or(drawing.orig);
if drawing.dest != dest {
ctx.widget().queue_draw();
}
drawing.dest = dest;
}
}
pub(crate) fn mouse_up(&mut self, ctx: &EventContext) {
if let Some(mut drawing) = self.drawing.take() {
if self.enabled {
drawing.dest = ctx.square().unwrap_or(drawing.orig);
let num_shapes = self.shapes.len();
self.shapes.retain(|s| s.orig != drawing.orig || s.dest != drawing.dest);
if num_shapes == self.shapes.len() {
self.shapes.push(drawing);
}
ctx.stream().emit(GroundMsg::ShapesChanged(self.shapes.clone()));
}
ctx.widget().queue_draw();
}
}
pub(crate) fn draw(&self, cr: &Context) {
for shape in &self.shapes {
shape.draw(cr);
}
self.drawing.as_ref().map(|shape| shape.draw(cr));
}
}
impl DrawShape {
pub fn orig(&self) -> Square {
self.orig
}
pub fn dest(&self) -> Square {
self.dest
}
pub fn brush(&self) -> DrawBrush {
self.brush
}
pub fn is_circle(&self) -> bool {
self.orig == self.dest
}
pub fn is_arrow(&self) -> bool {
self.orig != self.dest
}
fn draw(&self, cr: &Context) {
let opacity = 0.5;
match self.brush {
DrawBrush::Green => cr.set_source_rgba(0.08, 0.47, 0.11, opacity),
DrawBrush::Red => cr.set_source_rgba(0.53, 0.13, 0.13, opacity),
DrawBrush::Blue => cr.set_source_rgba(0.0, 0.19, 0.53, opacity),
DrawBrush::Yellow => cr.set_source_rgba(0.90, 0.94, 0.0, opacity),
}
let orig_x = 0.5 + f64::from(self.orig.file());
let orig_y = 7.5 - f64::from(self.orig.rank());
let dest_x = 0.5 + f64::from(self.dest.file());
let dest_y = 7.5 - f64::from(self.dest.rank());
if self.is_circle() {
let stroke = 0.05;
cr.set_line_width(stroke);
cr.arc(dest_x, dest_y, 0.5 * (1.0 - stroke), 0.0, 2.0 * PI);
cr.stroke();
} else {
let marker_size = 0.75;
let margin = 0.1;
let (dx, dy) = (dest_x - orig_x, dest_y - orig_y);
let hypot = dx.hypot(dy);
let shaft_x = dest_x - dx * (marker_size + margin) / hypot;
let shaft_y = dest_y - dy * (marker_size + margin) / hypot;
let head_x = dest_x - dx * margin / hypot;
let head_y = dest_y - dy * margin / hypot;
let stroke = 0.2;
cr.set_line_width(stroke);
cr.move_to(orig_x, orig_y);
cr.line_to(shaft_x, shaft_y);
cr.stroke();
cr.move_to(head_x, head_y);
cr.line_to(shaft_x - dy * 0.5 * marker_size / hypot,
shaft_y + dx * 0.5 * marker_size / hypot);
cr.line_to(shaft_x + dy * 0.5 * marker_size / hypot,
shaft_y - dx * 0.5 * marker_size / hypot);
cr.fill();
}
}
}