-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathline-thickness.rs
108 lines (92 loc) · 3.11 KB
/
line-thickness.rs
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
//! A debugging tool for thick lines
//!
//! Use the up/down arrow keys to increase or decrease the line thickness. Click and drag to move
//! the end point of the line around.
//!
//! The thickness, DX and DY components of the line are displayed in the top right corner of the
//! window.
extern crate embedded_graphics;
extern crate embedded_graphics_simulator;
use embedded_graphics::{
fonts::{Font6x8, Text},
pixelcolor::Rgb888,
prelude::*,
primitives::Line,
style::{MonoTextStyle, PrimitiveStyle},
};
use embedded_graphics_simulator::{
OutputSettingsBuilder, SimulatorDisplay, SimulatorEvent, Window,
};
use sdl2::keyboard::Keycode;
const BACKGROUND_COLOR: Rgb888 = Rgb888::BLACK;
fn draw(
display: &mut SimulatorDisplay<Rgb888>,
position: Point,
stroke_width: u32,
) -> Result<(), core::convert::Infallible> {
display.clear(BACKGROUND_COLOR)?;
let start = Point::new(
display.size().width as i32 / 2,
display.size().height as i32 / 2,
);
Text::new(
&format!(
"W: {}\nDX {}, DY {}",
stroke_width,
position.x - start.x,
position.y - start.y
),
Point::zero(),
)
.into_styled(MonoTextStyle::new(Font6x8, Rgb888::MAGENTA))
.draw(display)?;
Line::new(start, position)
.into_styled(PrimitiveStyle::with_stroke(
Rgb888::new(0x80, 0xf2, 0x91),
stroke_width,
))
.draw(display)?;
Ok(())
}
fn main() -> Result<(), core::convert::Infallible> {
let mut display: SimulatorDisplay<Rgb888> = SimulatorDisplay::new(Size::new(200, 200));
let output_settings = OutputSettingsBuilder::new()
.scale(4)
.pixel_spacing(1)
.build();
let mut window = Window::new("Line thickness debugger", &output_settings);
let mut position = Point::new(150, 120);
let mut stroke_width = 5;
let mut mouse_down = false;
draw(&mut display, position, stroke_width)?;
'running: loop {
window.update(&display);
for event in window.events() {
match event {
SimulatorEvent::Quit => break 'running,
SimulatorEvent::KeyDown { keycode, .. } => {
match keycode {
Keycode::Up => stroke_width += 1,
Keycode::Down => stroke_width = (stroke_width as i32 - 1).max(0) as u32,
_ => (),
}
draw(&mut display, position, stroke_width)?;
}
SimulatorEvent::MouseButtonDown { point, .. } => {
mouse_down = true;
position = point;
draw(&mut display, position, stroke_width)?;
}
SimulatorEvent::MouseButtonUp { .. } => mouse_down = false,
SimulatorEvent::MouseMove { point, .. } => {
if mouse_down {
position = point;
draw(&mut display, position, stroke_width)?;
}
}
_ => {}
}
}
}
Ok(())
}