paint/
paint.rs

1use fltk::{
2    app,
3    draw::{LineStyle, draw_line, draw_point, draw_rect_fill, set_draw_color, set_line_style},
4    enums::{Color, Event, FrameType},
5    frame::Frame,
6    prelude::*,
7    surface::ImageSurface,
8    window::Window,
9};
10use std::cell::RefCell;
11use std::rc::Rc;
12
13struct Canvas {
14    frame: Frame,
15    #[allow(dead_code)]
16    surf: Rc<RefCell<ImageSurface>>,
17}
18
19impl Canvas {
20    pub fn new(w: i32, h: i32) -> Self {
21        let mut frame = Frame::default().with_size(w, h).center_of_parent();
22        frame.set_color(Color::White);
23        frame.set_frame(FrameType::DownBox);
24
25        let surf = ImageSurface::new(frame.w(), frame.h(), false);
26        ImageSurface::push_current(&surf);
27        draw_rect_fill(0, 0, w, h, Color::White);
28        ImageSurface::pop_current();
29
30        let surf = Rc::from(RefCell::from(surf));
31
32        frame.draw({
33            let surf = surf.clone();
34            move |f| {
35                let surf = surf.borrow();
36                let mut img = surf.image().unwrap();
37                img.draw(f.x(), f.y(), f.w(), f.h());
38            }
39        });
40
41        frame.handle({
42            let mut x = 0;
43            let mut y = 0;
44            let surf = surf.clone();
45            move |f, ev| {
46                // println!("{}", ev);
47                // println!("coords {:?}", app::event_coords());
48                // println!("get mouse {:?}", app::get_mouse());
49                let surf = surf.borrow_mut();
50                match ev {
51                    Event::Push => {
52                        ImageSurface::push_current(&surf);
53                        set_draw_color(Color::Red);
54                        set_line_style(LineStyle::Solid, 3);
55                        let coords = app::event_coords();
56                        x = coords.0;
57                        y = coords.1;
58                        draw_point(x, y);
59                        ImageSurface::pop_current();
60                        f.redraw();
61                        true
62                    }
63                    Event::Drag => {
64                        ImageSurface::push_current(&surf);
65                        set_draw_color(Color::Red);
66                        set_line_style(LineStyle::Solid, 3);
67                        let coords = app::event_coords();
68                        draw_line(x, y, coords.0, coords.1);
69                        x = coords.0;
70                        y = coords.1;
71                        ImageSurface::pop_current();
72                        f.redraw();
73                        true
74                    }
75                    _ => false,
76                }
77            }
78        });
79        Self { frame, surf }
80    }
81}
82
83const WIDTH: i32 = 800;
84const HEIGHT: i32 = 600;
85
86fltk::widget_extends!(Canvas, Frame, frame);
87
88fn main() {
89    let app = app::App::default().with_scheme(app::Scheme::Gtk);
90
91    let mut wind = Window::default()
92        .with_size(WIDTH, HEIGHT)
93        .with_label("RustyPainter");
94
95    Canvas::new(WIDTH - 10, HEIGHT - 10);
96
97    wind.end();
98    wind.show();
99
100    app.run().unwrap();
101}