Function fltk::app::add_idle3

source ·
pub fn add_idle3<F: FnMut(IdleHandle) + 'static>(cb: F) -> IdleHandle
Expand description

Add an idle callback to run within the event loop. This function returns a handle that can be used for future interaction with the callback. Calls to WidgetExt::redraw within the callback require an explicit sleep

Examples found in repository?
examples/fb.rs (lines 31-38)
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
fn main() -> Result<(), Box<dyn std::error::Error>> {
    let app = app::App::default();
    let mut win = Window::default()
        .with_size(WIDTH, HEIGHT)
        .with_label("Framebuffer");
    let mut frame = frame::Frame::default().size_of(&win);
    win.end();
    win.make_resizable(true);
    win.show();

    let mut framebuf: Vec<u8> = vec![0; (WIDTH * HEIGHT * 4) as usize];
    let mut world = World::new();

    unsafe {
        draw::draw_rgba_nocopy(&mut frame, &framebuf);
    }

    app::add_idle3(move |_| {
        world.update();
        world.draw(&mut framebuf);
        // draw::draw_rgba(&mut frame, &framebuf).unwrap(); // A safe variant of draw_rgba_nocopy
        win.redraw();
        // sleeps are necessary when calling redraw in the event loop
        app::sleep(0.016);
    });

    app.run()?;
    Ok(())
}
More examples
Hide additional examples
examples/pong.rs (lines 55-81)
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
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 {
        wid: valuator::FillDial::new(0, 0, 40, 40, ""),
        pos: (0, 0),
        dir: (Direction::Positive, Direction::Positive),
    };
    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
    let paddle_c = paddle_pos.clone();

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

    let paddle_c = paddle_pos.clone();
    wind.handle(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_c.borrow() - 40 && ball.pos.0 < *paddle_c.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();
}