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
use fltk::{
app,
draw::{
draw_line, draw_point, draw_rect_fill, set_draw_color, set_line_style, LineStyle, Offscreen,
},
enums::{Color, Event, FrameType},
frame::Frame,
prelude::*,
window::Window,
};
use std::cell::RefCell;
use std::rc::Rc;
const WIDTH: i32 = 800;
const HEIGHT: i32 = 600;
fn main() {
let app = app::App::default().with_scheme(app::Scheme::Gtk);
let mut wind = Window::default()
.with_size(WIDTH, HEIGHT)
.with_label("RustyPainter");
let mut frame = Frame::default()
.with_size(WIDTH - 10, HEIGHT - 10)
.center_of(&wind);
frame.set_color(Color::White);
frame.set_frame(FrameType::DownBox);
wind.end();
wind.show();
let offs = Offscreen::new(frame.width(), frame.height()).unwrap();
#[cfg(not(target_os = "macos"))]
{
offs.begin();
draw_rect_fill(0, 0, WIDTH - 10, HEIGHT - 10, Color::White);
offs.end();
}
let offs = Rc::from(RefCell::from(offs));
frame.draw({
let offs = offs.clone();
move |_| {
let mut offs = offs.borrow_mut();
if offs.is_valid() {
offs.rescale();
offs.copy(5, 5, WIDTH - 10, HEIGHT - 10, 0, 0);
} else {
offs.begin();
draw_rect_fill(0, 0, WIDTH - 10, HEIGHT - 10, Color::White);
offs.copy(5, 5, WIDTH - 10, HEIGHT - 10, 0, 0);
offs.end();
}
}
});
frame.handle({
let mut x = 0;
let mut y = 0;
move |f, ev| {
let offs = offs.borrow_mut();
match ev {
Event::Push => {
offs.begin();
set_draw_color(Color::Red);
set_line_style(LineStyle::Solid, 3);
let coords = app::event_coords();
x = coords.0;
y = coords.1;
draw_point(x, y);
offs.end();
f.redraw();
set_line_style(LineStyle::Solid, 0);
true
}
Event::Drag => {
offs.begin();
set_draw_color(Color::Red);
set_line_style(LineStyle::Solid, 3);
let coords = app::event_coords();
draw_line(x, y, coords.0, coords.1);
x = coords.0;
y = coords.1;
offs.end();
f.redraw();
set_line_style(LineStyle::Solid, 0);
true
}
_ => false,
}
}
});
app.run().unwrap();
}