Struct BasicInput

Source
#[non_exhaustive]
pub struct BasicInput { pub mouse_pos: (f64, f64), pub mouse: HashMap<MouseButton, (bool, bool)>, pub keys: HashMap<VirtualKeyCode, (bool, bool)>, pub modifiers: ModifiersState, pub resized: bool, pub wait: bool, pub wakeups: Vec<Wakeup>, pub wakeup: Option<Wakeup>, /* private fields */ }
Expand description

Used for MiniGlFb::glutin_handle_basic_input. Contains the current state of the window in a polling-like fashion.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§mouse_pos: (f64, f64)

The mouse position in buffer coordinates.

The bottom left of the window is (0, 0). Pixel centers are at multiples of (0.5, 0.5). If you want to use this to index into your buffer, in general the following is sufficient:

  • clamp each coordinate to the half-open range [0.0, buffer_size)
  • take the floor of each component
  • cast to usize and compute an index: let index = y * WIDTH + x
§mouse: HashMap<MouseButton, (bool, bool)>

Stores whether a mouse button was down and is down, in that order.

If a button has not been pressed yet it will not be in the map.

§keys: HashMap<VirtualKeyCode, (bool, bool)>

Stores the previous and current “key down” states, in that order.

If a key has not been pressed yet it will not be in the map.

§modifiers: ModifiersState

The current modifier keys that are being pressed.

§resized: bool

This is set to true when the window is resized outside of your callback. If you do not update the buffer in your callback, you should still draw it if this is true.

§wait: bool

If this is set to true by your callback, it will not be called as fast as possible, but rather only when the input changes.

§wakeups: Vec<Wakeup>

A record of all the Wakeups that are scheduled to happen. If your callback is being called because of a wakeup, BasicInput::wakeup will be set to Some(id) where id is the unique identifier of the Wakeup.

Wakeups can be scheduled using BasicInput::schedule_wakeup. Wakeups can be cancelled using BasicInput::cancel_wakeup, or by removing the item from the Vec.

§wakeup: Option<Wakeup>

Indicates to your callback which Wakeup it should be handling. Normally, it’s okay to ignore this, as it will always be None unless you manually schedule wakeups using BasicInput::schedule_wakeup.

Implementations§

Source§

impl BasicInput

Source

pub fn mouse_pressed(&self, button: MouseButton) -> bool

If the mouse was pressed this last frame.

Source

pub fn mouse_is_down(&self, button: MouseButton) -> bool

If the mouse is currently down.

Examples found in repository?
examples/game_of_life.rs (line 78)
18fn main() {
19    let mut event_loop = EventLoop::new();
20    let mut fb = mini_gl_fb::get_fancy(config! {
21        window_title: String::from("PSA: Conway wants you to appreciate group theory instead"),
22        window_size: LogicalSize::new(800.0, 800.0),
23        buffer_size: Some(LogicalSize::new(WIDTH as _, HEIGHT as _))
24    }, &event_loop);
25
26    fb.change_buffer_format::<u8>(BufferFormat::R);
27    fb.use_post_process_shader(POST_PROCESS);
28
29    let mut neighbors = vec![0; WIDTH * HEIGHT];
30    let mut cells = vec![false; WIDTH * HEIGHT];
31
32    cells[5 * WIDTH + 10] = true;
33    cells[5 * WIDTH + 11] = true;
34    cells[5 * WIDTH + 12] = true;
35
36    cells[50 * WIDTH + 50] = true;
37    cells[51 * WIDTH + 51] = true;
38    cells[52 * WIDTH + 49] = true;
39    cells[52 * WIDTH + 50] = true;
40    cells[52 * WIDTH + 51] = true;
41
42    // ID of the Wakeup which means we should update the board
43    let mut update_id: Option<u32> = None;
44
45    fb.glutin_handle_basic_input(&mut event_loop, |fb, input| {
46        // We're going to use wakeups to update the grid
47        input.wait = true;
48
49        if update_id.is_none() {
50            update_id = Some(input.schedule_wakeup(Instant::now() + Duration::from_millis(500)))
51        } else if let Some(mut wakeup) = input.wakeup {
52            if Some(wakeup.id) == update_id {
53                // Time to update our grid
54                calculate_neighbors(&mut cells, &mut neighbors);
55                make_some_babies(&mut cells, &mut neighbors);
56                fb.update_buffer(&cells);
57
58                // Reschedule another update
59                wakeup.when = Instant::now() + Duration::from_millis(
60                    if input.key_is_down(VirtualKeyCode::LShift) {
61                        TURBO_SPEED
62                    } else {
63                        NORMAL_SPEED
64                    }
65                );
66
67                input.reschedule_wakeup(wakeup);
68            }
69
70            // We will get called again after all wakeups are handled
71            return true;
72        }
73
74        if input.key_is_down(VirtualKeyCode::Escape) {
75            return false;
76        }
77
78        if input.mouse_is_down(MouseButton::Left) || input.mouse_is_down(MouseButton::Right) {
79            // Mouse was pressed
80            let (x, y) = input.mouse_pos;
81            let x = x.min(WIDTH as f64 - 0.0001).max(0.0).floor() as usize;
82            let y = y.min(HEIGHT as f64 - 0.0001).max(0.0).floor() as usize;
83            cells[y * WIDTH + x] = input.mouse_is_down(MouseButton::Left);
84            fb.update_buffer(&cells);
85            // Give the user extra time to make something pretty each time they click
86            if !input.key_is_down(VirtualKeyCode::LShift) {
87                input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(2000));
88            }
89        }
90
91        // TODO support right shift. Probably by querying modifiers somehow. (modifiers support)
92        if input.key_pressed(VirtualKeyCode::LShift) {
93            // immediately update
94            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(0));
95        } else if input.key_released(VirtualKeyCode::LShift) {
96            // immediately stop updating
97            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(NORMAL_SPEED));
98        }
99
100        true
101    });
102}
Source

pub fn mouse_released(&self, button: MouseButton) -> bool

If the mouse was released this last frame.

Source

pub fn key_pressed(&self, button: VirtualKeyCode) -> bool

If the key was pressed this last frame.

Examples found in repository?
examples/game_of_life.rs (line 92)
18fn main() {
19    let mut event_loop = EventLoop::new();
20    let mut fb = mini_gl_fb::get_fancy(config! {
21        window_title: String::from("PSA: Conway wants you to appreciate group theory instead"),
22        window_size: LogicalSize::new(800.0, 800.0),
23        buffer_size: Some(LogicalSize::new(WIDTH as _, HEIGHT as _))
24    }, &event_loop);
25
26    fb.change_buffer_format::<u8>(BufferFormat::R);
27    fb.use_post_process_shader(POST_PROCESS);
28
29    let mut neighbors = vec![0; WIDTH * HEIGHT];
30    let mut cells = vec![false; WIDTH * HEIGHT];
31
32    cells[5 * WIDTH + 10] = true;
33    cells[5 * WIDTH + 11] = true;
34    cells[5 * WIDTH + 12] = true;
35
36    cells[50 * WIDTH + 50] = true;
37    cells[51 * WIDTH + 51] = true;
38    cells[52 * WIDTH + 49] = true;
39    cells[52 * WIDTH + 50] = true;
40    cells[52 * WIDTH + 51] = true;
41
42    // ID of the Wakeup which means we should update the board
43    let mut update_id: Option<u32> = None;
44
45    fb.glutin_handle_basic_input(&mut event_loop, |fb, input| {
46        // We're going to use wakeups to update the grid
47        input.wait = true;
48
49        if update_id.is_none() {
50            update_id = Some(input.schedule_wakeup(Instant::now() + Duration::from_millis(500)))
51        } else if let Some(mut wakeup) = input.wakeup {
52            if Some(wakeup.id) == update_id {
53                // Time to update our grid
54                calculate_neighbors(&mut cells, &mut neighbors);
55                make_some_babies(&mut cells, &mut neighbors);
56                fb.update_buffer(&cells);
57
58                // Reschedule another update
59                wakeup.when = Instant::now() + Duration::from_millis(
60                    if input.key_is_down(VirtualKeyCode::LShift) {
61                        TURBO_SPEED
62                    } else {
63                        NORMAL_SPEED
64                    }
65                );
66
67                input.reschedule_wakeup(wakeup);
68            }
69
70            // We will get called again after all wakeups are handled
71            return true;
72        }
73
74        if input.key_is_down(VirtualKeyCode::Escape) {
75            return false;
76        }
77
78        if input.mouse_is_down(MouseButton::Left) || input.mouse_is_down(MouseButton::Right) {
79            // Mouse was pressed
80            let (x, y) = input.mouse_pos;
81            let x = x.min(WIDTH as f64 - 0.0001).max(0.0).floor() as usize;
82            let y = y.min(HEIGHT as f64 - 0.0001).max(0.0).floor() as usize;
83            cells[y * WIDTH + x] = input.mouse_is_down(MouseButton::Left);
84            fb.update_buffer(&cells);
85            // Give the user extra time to make something pretty each time they click
86            if !input.key_is_down(VirtualKeyCode::LShift) {
87                input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(2000));
88            }
89        }
90
91        // TODO support right shift. Probably by querying modifiers somehow. (modifiers support)
92        if input.key_pressed(VirtualKeyCode::LShift) {
93            // immediately update
94            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(0));
95        } else if input.key_released(VirtualKeyCode::LShift) {
96            // immediately stop updating
97            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(NORMAL_SPEED));
98        }
99
100        true
101    });
102}
Source

pub fn key_is_down(&self, button: VirtualKeyCode) -> bool

If the key is currently down.

Examples found in repository?
examples/game_of_life.rs (line 60)
18fn main() {
19    let mut event_loop = EventLoop::new();
20    let mut fb = mini_gl_fb::get_fancy(config! {
21        window_title: String::from("PSA: Conway wants you to appreciate group theory instead"),
22        window_size: LogicalSize::new(800.0, 800.0),
23        buffer_size: Some(LogicalSize::new(WIDTH as _, HEIGHT as _))
24    }, &event_loop);
25
26    fb.change_buffer_format::<u8>(BufferFormat::R);
27    fb.use_post_process_shader(POST_PROCESS);
28
29    let mut neighbors = vec![0; WIDTH * HEIGHT];
30    let mut cells = vec![false; WIDTH * HEIGHT];
31
32    cells[5 * WIDTH + 10] = true;
33    cells[5 * WIDTH + 11] = true;
34    cells[5 * WIDTH + 12] = true;
35
36    cells[50 * WIDTH + 50] = true;
37    cells[51 * WIDTH + 51] = true;
38    cells[52 * WIDTH + 49] = true;
39    cells[52 * WIDTH + 50] = true;
40    cells[52 * WIDTH + 51] = true;
41
42    // ID of the Wakeup which means we should update the board
43    let mut update_id: Option<u32> = None;
44
45    fb.glutin_handle_basic_input(&mut event_loop, |fb, input| {
46        // We're going to use wakeups to update the grid
47        input.wait = true;
48
49        if update_id.is_none() {
50            update_id = Some(input.schedule_wakeup(Instant::now() + Duration::from_millis(500)))
51        } else if let Some(mut wakeup) = input.wakeup {
52            if Some(wakeup.id) == update_id {
53                // Time to update our grid
54                calculate_neighbors(&mut cells, &mut neighbors);
55                make_some_babies(&mut cells, &mut neighbors);
56                fb.update_buffer(&cells);
57
58                // Reschedule another update
59                wakeup.when = Instant::now() + Duration::from_millis(
60                    if input.key_is_down(VirtualKeyCode::LShift) {
61                        TURBO_SPEED
62                    } else {
63                        NORMAL_SPEED
64                    }
65                );
66
67                input.reschedule_wakeup(wakeup);
68            }
69
70            // We will get called again after all wakeups are handled
71            return true;
72        }
73
74        if input.key_is_down(VirtualKeyCode::Escape) {
75            return false;
76        }
77
78        if input.mouse_is_down(MouseButton::Left) || input.mouse_is_down(MouseButton::Right) {
79            // Mouse was pressed
80            let (x, y) = input.mouse_pos;
81            let x = x.min(WIDTH as f64 - 0.0001).max(0.0).floor() as usize;
82            let y = y.min(HEIGHT as f64 - 0.0001).max(0.0).floor() as usize;
83            cells[y * WIDTH + x] = input.mouse_is_down(MouseButton::Left);
84            fb.update_buffer(&cells);
85            // Give the user extra time to make something pretty each time they click
86            if !input.key_is_down(VirtualKeyCode::LShift) {
87                input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(2000));
88            }
89        }
90
91        // TODO support right shift. Probably by querying modifiers somehow. (modifiers support)
92        if input.key_pressed(VirtualKeyCode::LShift) {
93            // immediately update
94            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(0));
95        } else if input.key_released(VirtualKeyCode::LShift) {
96            // immediately stop updating
97            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(NORMAL_SPEED));
98        }
99
100        true
101    });
102}
Source

pub fn key_released(&self, button: VirtualKeyCode) -> bool

If the key was released this last frame.

Examples found in repository?
examples/game_of_life.rs (line 95)
18fn main() {
19    let mut event_loop = EventLoop::new();
20    let mut fb = mini_gl_fb::get_fancy(config! {
21        window_title: String::from("PSA: Conway wants you to appreciate group theory instead"),
22        window_size: LogicalSize::new(800.0, 800.0),
23        buffer_size: Some(LogicalSize::new(WIDTH as _, HEIGHT as _))
24    }, &event_loop);
25
26    fb.change_buffer_format::<u8>(BufferFormat::R);
27    fb.use_post_process_shader(POST_PROCESS);
28
29    let mut neighbors = vec![0; WIDTH * HEIGHT];
30    let mut cells = vec![false; WIDTH * HEIGHT];
31
32    cells[5 * WIDTH + 10] = true;
33    cells[5 * WIDTH + 11] = true;
34    cells[5 * WIDTH + 12] = true;
35
36    cells[50 * WIDTH + 50] = true;
37    cells[51 * WIDTH + 51] = true;
38    cells[52 * WIDTH + 49] = true;
39    cells[52 * WIDTH + 50] = true;
40    cells[52 * WIDTH + 51] = true;
41
42    // ID of the Wakeup which means we should update the board
43    let mut update_id: Option<u32> = None;
44
45    fb.glutin_handle_basic_input(&mut event_loop, |fb, input| {
46        // We're going to use wakeups to update the grid
47        input.wait = true;
48
49        if update_id.is_none() {
50            update_id = Some(input.schedule_wakeup(Instant::now() + Duration::from_millis(500)))
51        } else if let Some(mut wakeup) = input.wakeup {
52            if Some(wakeup.id) == update_id {
53                // Time to update our grid
54                calculate_neighbors(&mut cells, &mut neighbors);
55                make_some_babies(&mut cells, &mut neighbors);
56                fb.update_buffer(&cells);
57
58                // Reschedule another update
59                wakeup.when = Instant::now() + Duration::from_millis(
60                    if input.key_is_down(VirtualKeyCode::LShift) {
61                        TURBO_SPEED
62                    } else {
63                        NORMAL_SPEED
64                    }
65                );
66
67                input.reschedule_wakeup(wakeup);
68            }
69
70            // We will get called again after all wakeups are handled
71            return true;
72        }
73
74        if input.key_is_down(VirtualKeyCode::Escape) {
75            return false;
76        }
77
78        if input.mouse_is_down(MouseButton::Left) || input.mouse_is_down(MouseButton::Right) {
79            // Mouse was pressed
80            let (x, y) = input.mouse_pos;
81            let x = x.min(WIDTH as f64 - 0.0001).max(0.0).floor() as usize;
82            let y = y.min(HEIGHT as f64 - 0.0001).max(0.0).floor() as usize;
83            cells[y * WIDTH + x] = input.mouse_is_down(MouseButton::Left);
84            fb.update_buffer(&cells);
85            // Give the user extra time to make something pretty each time they click
86            if !input.key_is_down(VirtualKeyCode::LShift) {
87                input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(2000));
88            }
89        }
90
91        // TODO support right shift. Probably by querying modifiers somehow. (modifiers support)
92        if input.key_pressed(VirtualKeyCode::LShift) {
93            // immediately update
94            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(0));
95        } else if input.key_released(VirtualKeyCode::LShift) {
96            // immediately stop updating
97            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(NORMAL_SPEED));
98        }
99
100        true
101    });
102}
Source

pub fn schedule_wakeup(&mut self, when: Instant) -> u32

Given an Instant in the future (or in the past, in which case it will be triggered immediately), schedules a wakeup to be triggered then. Returns the ID of the wakeup, which will be the ID of BasicInput::wakeup if your callback is getting called by the wakeup.

Examples found in repository?
examples/game_of_life.rs (line 50)
18fn main() {
19    let mut event_loop = EventLoop::new();
20    let mut fb = mini_gl_fb::get_fancy(config! {
21        window_title: String::from("PSA: Conway wants you to appreciate group theory instead"),
22        window_size: LogicalSize::new(800.0, 800.0),
23        buffer_size: Some(LogicalSize::new(WIDTH as _, HEIGHT as _))
24    }, &event_loop);
25
26    fb.change_buffer_format::<u8>(BufferFormat::R);
27    fb.use_post_process_shader(POST_PROCESS);
28
29    let mut neighbors = vec![0; WIDTH * HEIGHT];
30    let mut cells = vec![false; WIDTH * HEIGHT];
31
32    cells[5 * WIDTH + 10] = true;
33    cells[5 * WIDTH + 11] = true;
34    cells[5 * WIDTH + 12] = true;
35
36    cells[50 * WIDTH + 50] = true;
37    cells[51 * WIDTH + 51] = true;
38    cells[52 * WIDTH + 49] = true;
39    cells[52 * WIDTH + 50] = true;
40    cells[52 * WIDTH + 51] = true;
41
42    // ID of the Wakeup which means we should update the board
43    let mut update_id: Option<u32> = None;
44
45    fb.glutin_handle_basic_input(&mut event_loop, |fb, input| {
46        // We're going to use wakeups to update the grid
47        input.wait = true;
48
49        if update_id.is_none() {
50            update_id = Some(input.schedule_wakeup(Instant::now() + Duration::from_millis(500)))
51        } else if let Some(mut wakeup) = input.wakeup {
52            if Some(wakeup.id) == update_id {
53                // Time to update our grid
54                calculate_neighbors(&mut cells, &mut neighbors);
55                make_some_babies(&mut cells, &mut neighbors);
56                fb.update_buffer(&cells);
57
58                // Reschedule another update
59                wakeup.when = Instant::now() + Duration::from_millis(
60                    if input.key_is_down(VirtualKeyCode::LShift) {
61                        TURBO_SPEED
62                    } else {
63                        NORMAL_SPEED
64                    }
65                );
66
67                input.reschedule_wakeup(wakeup);
68            }
69
70            // We will get called again after all wakeups are handled
71            return true;
72        }
73
74        if input.key_is_down(VirtualKeyCode::Escape) {
75            return false;
76        }
77
78        if input.mouse_is_down(MouseButton::Left) || input.mouse_is_down(MouseButton::Right) {
79            // Mouse was pressed
80            let (x, y) = input.mouse_pos;
81            let x = x.min(WIDTH as f64 - 0.0001).max(0.0).floor() as usize;
82            let y = y.min(HEIGHT as f64 - 0.0001).max(0.0).floor() as usize;
83            cells[y * WIDTH + x] = input.mouse_is_down(MouseButton::Left);
84            fb.update_buffer(&cells);
85            // Give the user extra time to make something pretty each time they click
86            if !input.key_is_down(VirtualKeyCode::LShift) {
87                input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(2000));
88            }
89        }
90
91        // TODO support right shift. Probably by querying modifiers somehow. (modifiers support)
92        if input.key_pressed(VirtualKeyCode::LShift) {
93            // immediately update
94            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(0));
95        } else if input.key_released(VirtualKeyCode::LShift) {
96            // immediately stop updating
97            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(NORMAL_SPEED));
98        }
99
100        true
101    });
102}
Source

pub fn reschedule_wakeup(&mut self, wakeup: Wakeup)

Reschedules a wakeup. It is perfectly valid to re-use IDs of wakeups that have already been triggered; that is why BasicInput::wakeup is a Wakeup and not just a u32.

Examples found in repository?
examples/game_of_life.rs (line 67)
18fn main() {
19    let mut event_loop = EventLoop::new();
20    let mut fb = mini_gl_fb::get_fancy(config! {
21        window_title: String::from("PSA: Conway wants you to appreciate group theory instead"),
22        window_size: LogicalSize::new(800.0, 800.0),
23        buffer_size: Some(LogicalSize::new(WIDTH as _, HEIGHT as _))
24    }, &event_loop);
25
26    fb.change_buffer_format::<u8>(BufferFormat::R);
27    fb.use_post_process_shader(POST_PROCESS);
28
29    let mut neighbors = vec![0; WIDTH * HEIGHT];
30    let mut cells = vec![false; WIDTH * HEIGHT];
31
32    cells[5 * WIDTH + 10] = true;
33    cells[5 * WIDTH + 11] = true;
34    cells[5 * WIDTH + 12] = true;
35
36    cells[50 * WIDTH + 50] = true;
37    cells[51 * WIDTH + 51] = true;
38    cells[52 * WIDTH + 49] = true;
39    cells[52 * WIDTH + 50] = true;
40    cells[52 * WIDTH + 51] = true;
41
42    // ID of the Wakeup which means we should update the board
43    let mut update_id: Option<u32> = None;
44
45    fb.glutin_handle_basic_input(&mut event_loop, |fb, input| {
46        // We're going to use wakeups to update the grid
47        input.wait = true;
48
49        if update_id.is_none() {
50            update_id = Some(input.schedule_wakeup(Instant::now() + Duration::from_millis(500)))
51        } else if let Some(mut wakeup) = input.wakeup {
52            if Some(wakeup.id) == update_id {
53                // Time to update our grid
54                calculate_neighbors(&mut cells, &mut neighbors);
55                make_some_babies(&mut cells, &mut neighbors);
56                fb.update_buffer(&cells);
57
58                // Reschedule another update
59                wakeup.when = Instant::now() + Duration::from_millis(
60                    if input.key_is_down(VirtualKeyCode::LShift) {
61                        TURBO_SPEED
62                    } else {
63                        NORMAL_SPEED
64                    }
65                );
66
67                input.reschedule_wakeup(wakeup);
68            }
69
70            // We will get called again after all wakeups are handled
71            return true;
72        }
73
74        if input.key_is_down(VirtualKeyCode::Escape) {
75            return false;
76        }
77
78        if input.mouse_is_down(MouseButton::Left) || input.mouse_is_down(MouseButton::Right) {
79            // Mouse was pressed
80            let (x, y) = input.mouse_pos;
81            let x = x.min(WIDTH as f64 - 0.0001).max(0.0).floor() as usize;
82            let y = y.min(HEIGHT as f64 - 0.0001).max(0.0).floor() as usize;
83            cells[y * WIDTH + x] = input.mouse_is_down(MouseButton::Left);
84            fb.update_buffer(&cells);
85            // Give the user extra time to make something pretty each time they click
86            if !input.key_is_down(VirtualKeyCode::LShift) {
87                input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(2000));
88            }
89        }
90
91        // TODO support right shift. Probably by querying modifiers somehow. (modifiers support)
92        if input.key_pressed(VirtualKeyCode::LShift) {
93            // immediately update
94            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(0));
95        } else if input.key_released(VirtualKeyCode::LShift) {
96            // immediately stop updating
97            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(NORMAL_SPEED));
98        }
99
100        true
101    });
102}
Source

pub fn cancel_wakeup(&mut self, id: u32) -> Option<Wakeup>

Cancels a previously scheduled Wakeup by its ID. Returns the Wakeup if it is found, otherwise returns None.

Source

pub fn adjust_wakeup(&mut self, id: u32, when: Instant) -> bool

Changing the time of an upcoming wakeup is common enough that there’s a utility method to do it for you. Given an ID and an Instant, finds the Wakeup with the given ID and sets its time to when. Returns true if a wakeup was found, false otherwise.

Examples found in repository?
examples/game_of_life.rs (line 87)
18fn main() {
19    let mut event_loop = EventLoop::new();
20    let mut fb = mini_gl_fb::get_fancy(config! {
21        window_title: String::from("PSA: Conway wants you to appreciate group theory instead"),
22        window_size: LogicalSize::new(800.0, 800.0),
23        buffer_size: Some(LogicalSize::new(WIDTH as _, HEIGHT as _))
24    }, &event_loop);
25
26    fb.change_buffer_format::<u8>(BufferFormat::R);
27    fb.use_post_process_shader(POST_PROCESS);
28
29    let mut neighbors = vec![0; WIDTH * HEIGHT];
30    let mut cells = vec![false; WIDTH * HEIGHT];
31
32    cells[5 * WIDTH + 10] = true;
33    cells[5 * WIDTH + 11] = true;
34    cells[5 * WIDTH + 12] = true;
35
36    cells[50 * WIDTH + 50] = true;
37    cells[51 * WIDTH + 51] = true;
38    cells[52 * WIDTH + 49] = true;
39    cells[52 * WIDTH + 50] = true;
40    cells[52 * WIDTH + 51] = true;
41
42    // ID of the Wakeup which means we should update the board
43    let mut update_id: Option<u32> = None;
44
45    fb.glutin_handle_basic_input(&mut event_loop, |fb, input| {
46        // We're going to use wakeups to update the grid
47        input.wait = true;
48
49        if update_id.is_none() {
50            update_id = Some(input.schedule_wakeup(Instant::now() + Duration::from_millis(500)))
51        } else if let Some(mut wakeup) = input.wakeup {
52            if Some(wakeup.id) == update_id {
53                // Time to update our grid
54                calculate_neighbors(&mut cells, &mut neighbors);
55                make_some_babies(&mut cells, &mut neighbors);
56                fb.update_buffer(&cells);
57
58                // Reschedule another update
59                wakeup.when = Instant::now() + Duration::from_millis(
60                    if input.key_is_down(VirtualKeyCode::LShift) {
61                        TURBO_SPEED
62                    } else {
63                        NORMAL_SPEED
64                    }
65                );
66
67                input.reschedule_wakeup(wakeup);
68            }
69
70            // We will get called again after all wakeups are handled
71            return true;
72        }
73
74        if input.key_is_down(VirtualKeyCode::Escape) {
75            return false;
76        }
77
78        if input.mouse_is_down(MouseButton::Left) || input.mouse_is_down(MouseButton::Right) {
79            // Mouse was pressed
80            let (x, y) = input.mouse_pos;
81            let x = x.min(WIDTH as f64 - 0.0001).max(0.0).floor() as usize;
82            let y = y.min(HEIGHT as f64 - 0.0001).max(0.0).floor() as usize;
83            cells[y * WIDTH + x] = input.mouse_is_down(MouseButton::Left);
84            fb.update_buffer(&cells);
85            // Give the user extra time to make something pretty each time they click
86            if !input.key_is_down(VirtualKeyCode::LShift) {
87                input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(2000));
88            }
89        }
90
91        // TODO support right shift. Probably by querying modifiers somehow. (modifiers support)
92        if input.key_pressed(VirtualKeyCode::LShift) {
93            // immediately update
94            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(0));
95        } else if input.key_released(VirtualKeyCode::LShift) {
96            // immediately stop updating
97            input.adjust_wakeup(update_id.unwrap(), Wakeup::after_millis(NORMAL_SPEED));
98        }
99
100        true
101    });
102}

Trait Implementations§

Source§

impl Clone for BasicInput

Source§

fn clone(&self) -> BasicInput

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BasicInput

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for BasicInput

Source§

fn default() -> BasicInput

Returns the “default value” for a type. Read more
Source§

impl PartialEq for BasicInput

Source§

fn eq(&self, other: &BasicInput) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for BasicInput

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.