Function fltk::app::event_coords

source ·
pub fn event_coords() -> (i32, i32)
Expand description

Returns the x and y coordinates of the captured event

Examples found in repository?
examples/popup_browser.rs (line 31)
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
fn main() {
    let app = app::App::default().with_scheme(app::Scheme::Gtk);
    app::background(211, 211, 211);

    let mut win = window::Window::default().with_size(900, 300);
    let mut b = browser::HoldBrowser::default()
        .with_size(900 - 10, 300 - 10)
        .center_of(&win);
    let widths = &[50, 50, 50, 70, 70, 40, 40, 70, 70, 50];

    b.set_column_widths(widths);
    b.set_column_char('\t');
    b.add("USER\tPID\t%CPU\t%MEM\tVSZ\tRSS\tTTY\tSTAT\tSTART\tTIME\tCOMMAND");
    b.add("root\t2888\t0.0\t0.0\t1352\t0\ttty3\tSW\tAug15\t0:00\t@b@f/sbin/mingetty tty3");
    b.add("erco\t2889\t0.0\t13.0\t221352\t0\ttty3\tR\tAug15\t1:34\t@b@f/usr/local/bin/render a35 0004");
    b.add("uucp\t2892\t0.0\t0.0\t1352\t0\tttyS0\tSW\tAug15\t0:00\t@b@f/sbin/agetty -h 19200 ttyS0 vt100");
    b.add("root\t13115\t0.0\t0.0\t1352\t0\ttty2\tSW\tAug30\t0:00\t@b@f/sbin/mingetty tty2");
    b.add(
        "root\t13464\t0.0\t0.0\t1352\t0\ttty1\tSW\tAug30\t0:00\t@b@f/sbin/mingetty tty1 --noclear",
    );

    let menu = menu::MenuItem::new(&["1st menu item\t", "2nd menu item\t", "3rd menu item\t"]);

    b.set_callback(move |_| {
        if app::event_mouse_button() == app::MouseButton::Right {
            // or app::event_button() == 3
            let coords = app::event_coords();
            match menu.popup(coords.0, coords.1) {
                None => println!("No value was chosen!"),
                Some(val) => println!("{}", val.label().unwrap()),
            }
        }
    });

    win.make_resizable(true);
    win.end();
    win.show();
    app.run().unwrap();
}
More examples
Hide additional examples
examples/paint.rs (line 55)
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
    pub fn new(w: i32, h: i32) -> Self {
        let mut frame = Frame::default().with_size(w, h).center_of_parent();
        frame.set_color(Color::White);
        frame.set_frame(FrameType::DownBox);

        let surf = ImageSurface::new(frame.width(), frame.height(), false);
        ImageSurface::push_current(&surf);
        draw_rect_fill(0, 0, w, h, Color::White);
        ImageSurface::pop_current();

        let surf = Rc::from(RefCell::from(surf));

        frame.draw({
            let surf = surf.clone();
            move |f| {
                let surf = surf.borrow();
                let mut img = surf.image().unwrap();
                img.draw(f.x(), f.y(), f.w(), f.h());
            }
        });

        frame.handle({
            let mut x = 0;
            let mut y = 0;
            let surf = surf.clone();
            move |f, ev| {
                // println!("{}", ev);
                // println!("coords {:?}", app::event_coords());
                // println!("get mouse {:?}", app::get_mouse());
                let surf = surf.borrow_mut();
                match ev {
                    Event::Push => {
                        ImageSurface::push_current(&surf);
                        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);
                        ImageSurface::pop_current();
                        f.redraw();
                        true
                    }
                    Event::Drag => {
                        ImageSurface::push_current(&surf);
                        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;
                        ImageSurface::pop_current();
                        f.redraw();
                        true
                    }
                    _ => false,
                }
            }
        });
        Self { frame, surf }
    }
examples/pong.rs (line 60)
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
fn main() {
    let app = app::App::default();
    let mut wind = window::Window::default()
        .with_size(800, 600)
        .center_screen()
        .with_label("Pong!");
    let mut ball = Ball::new(40, 40);
    ball.wid.set_color(enums::Color::White);
    wind.set_color(enums::Color::Black);
    wind.end();
    wind.show();

    let paddle_pos = Rc::from(RefCell::from(320)); // paddle's starting x position

    // This is called whenever the window is drawn and redrawn (in the event loop)
    wind.draw({
        let paddle_pos = paddle_pos.clone();
        move |_| {
            draw::set_draw_color(enums::Color::White);
            draw::draw_rectf(*paddle_pos.borrow(), 540, 160, 20);
        }
    });

    wind.handle({
        let paddle_pos = paddle_pos.clone();
        move |_, ev| {
            match ev {
                enums::Event::Move => {
                    // Mouse's x position relative to the paddle's center
                    *paddle_pos.borrow_mut() = app::event_coords().0 - 80;
                    true
                }
                _ => false,
            }
        }
    });

    app::add_idle3(move |_| {
        ball.pos.0 += 10 * ball.dir.0 as i32; // The increment in x position
        ball.pos.1 += 10 * ball.dir.1 as i32; // The increment in y position
        if ball.pos.1 == 540 - 40
            && (ball.pos.0 > *paddle_pos.borrow() - 40 && ball.pos.0 < *paddle_pos.borrow() + 160)
        {
            ball.dir.1 = Direction::Negative; // Reversal of motion when hitting the paddle
        }
        if ball.pos.1 == 0 {
            ball.dir.1 = Direction::Positive; // Reversal of motion when hitting the top border
        }
        if ball.pos.0 == 800 - 40 {
            ball.dir.0 = Direction::Negative; // Reversal of motion when hitting the right border
        }
        if ball.pos.0 == 0 {
            ball.dir.0 = Direction::Positive; // Reversal of motion when hitting the left border
        }
        if ball.pos.1 > 600 {
            // Resetting the ball position after it bypasses the paddle
            ball.pos = (0, 0);
            ball.dir = (Direction::Positive, Direction::Positive);
        }
        ball.wid.resize(ball.pos.0, ball.pos.1, 40, 40); // Moves the ball
        wind.redraw();
        // sleeps are necessary when calling redraw in the event loop
        app::sleep(0.016);
    });
    app.run().unwrap();
}
examples/shapedwindow_taskbar.rs (line 60)
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
fn main() {
    let app = app::App::default();

    // Act as the application in the taskbar (scroll to event handling)
    let mut dock_win = window::Window::default()
        .with_size(1, 1) // So we can place it at the center of the screen (needs a size >0 to be centered)
        .with_label("TestApplication")
        .center_screen();
    dock_win.size_range(0, 0, 0, 0);
    dock_win.make_resizable(false);

    dock_win.show();
    dock_win.end();

    let mut win = window::Window::default()
        .with_size(900, 500)
        .with_label("TestApplication")
        .center_screen();
    win.set_color(enums::Color::from_rgb(26, 25, 55));

    let mut but = button::Button::default()
        .with_label("Button")
        .with_size(80, 80)
        .center_of_parent();
    but.set_frame(enums::FrameType::OFlatFrame);
    but.set_color(enums::Color::Cyan);
    but.clear_visible_focus();
    but.set_callback(|_| println!("Clicked"));

    win.show();
    win.end();

    let win_shape = prep_shape(win.w(), win.h());

    // Called after showing window
    win.set_shape(Some(win_shape));

    win.handle({
        let mut x = 0;
        let mut y = 0;
        let mut dock_win = dock_win.clone();
        move |wself, event| match event {
            enums::Event::Push => {
                let coords = app::event_coords();
                x = coords.0;
                y = coords.1;

                true
            }
            enums::Event::Drag => {
                wself.set_pos(app::event_x_root() - x, app::event_y_root() - y);

                // Changing dock window position so it's close enough to the center of the application (not "visible" to user)
                dock_win.set_pos(wself.x() + (wself.w() / 2), wself.y() + (wself.w() / 2));

                true
            }
            enums::Event::Close => {
                app.quit();

                true
            }
            enums::Event::Hide => {
                app.quit();

                true
            }
            _ => false,
        }
    });

    // Make main window appear when "opened" via Alt+Tab or Taskbar
    dock_win.handle({
        let mut win = win.clone();
        move |_wself, event| match event {
            enums::Event::Focus => {
                let win_shape = prep_shape(win.w(), win.h());

                win.show();
                win.set_shape(Some(win_shape));

                true
            }
            enums::Event::Hide => {
                win.hide();

                true
            }
            enums::Event::Close => {
                app.quit();

                true
            }
            _ => false,
        }
    });

    app.run().unwrap();
}
examples/tree.rs (line 53)
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
    fn new(x: i32, y: i32, width: i32, height: i32, title: &'static str) -> Self {
        let mut t_widget = Tree::new(x, y, width, height, title);
        let previous_focus = Rc::new(RefCell::new(None::<TreeItem>));
        let pfr = Rc::clone(&previous_focus);
        t_widget.set_callback_reason(TreeReason::Selected);
        t_widget.set_callback(|_t| println!("clicked an item"));
        t_widget.handle(move |t, e| match e {
            Event::Move => {
                let (_, mouse_y) = app::event_coords();
                let mut state = State::Undefined;
                let mut pf = pfr.borrow_mut();
                loop {
                    match &*pf {
                        Some(item) => {
                            let item_y = item.y();
                            match state {
                                State::MovingUp => {
                                    if verify_open_till_root(&pf) {
                                        if mouse_y < item_y {
                                            *pf = pf.as_ref().unwrap().prev();
                                            continue;
                                        };
                                        break;
                                    } else {
                                        *pf = pf.as_ref().unwrap().prev();
                                        continue;
                                    }
                                }
                                State::MovingDown => {
                                    if verify_open_till_root(&pf) {
                                        if mouse_y > item_y + item.h() {
                                            *pf = pf.as_ref().unwrap().next();
                                            continue;
                                        };
                                        break;
                                    } else {
                                        *pf = pf.as_ref().unwrap().next();
                                        continue;
                                    }
                                }
                                State::Undefined => {
                                    if mouse_y < item_y {
                                        *pf = pf.as_ref().unwrap().prev();
                                        state = State::MovingUp;
                                        continue;
                                    };
                                    if mouse_y > item_y + item.h() {
                                        *pf = pf.as_ref().unwrap().next();
                                        state = State::MovingDown;
                                        continue;
                                    };
                                    return true; // If in same range, don't update 'previous_focus'
                                }
                            }
                        }
                        // End up here if y is outside tree boundaries, or no tree item is present
                        None => match &state {
                            State::MovingUp | State::MovingDown => return true,
                            State::Undefined => {
                                *pf = t.first();
                                state = State::MovingDown;
                                if pf.is_none() {
                                    return true;
                                }
                                continue;
                            }
                        },
                    };
                }
                if verify_open_till_root(&pf) {
                    t.take_focus().ok();
                    t.set_item_focus(pf.as_ref().unwrap());
                    println!("Set focus to item: {:?}", pf.as_ref().unwrap().label());
                }
                true
            }
            _ => false,
        });
        Self {
            t_widget,
            previous_focus,
        }
    }