#[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
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 Wakeup
s 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
impl BasicInput
Sourcepub fn mouse_pressed(&self, button: MouseButton) -> bool
pub fn mouse_pressed(&self, button: MouseButton) -> bool
If the mouse was pressed this last frame.
Sourcepub fn mouse_is_down(&self, button: MouseButton) -> bool
pub fn mouse_is_down(&self, button: MouseButton) -> bool
If the mouse is currently down.
Examples found in repository?
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}
Sourcepub fn mouse_released(&self, button: MouseButton) -> bool
pub fn mouse_released(&self, button: MouseButton) -> bool
If the mouse was released this last frame.
Sourcepub fn key_pressed(&self, button: VirtualKeyCode) -> bool
pub fn key_pressed(&self, button: VirtualKeyCode) -> bool
If the key was pressed this last frame.
Examples found in repository?
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}
Sourcepub fn key_is_down(&self, button: VirtualKeyCode) -> bool
pub fn key_is_down(&self, button: VirtualKeyCode) -> bool
If the key is currently down.
Examples found in repository?
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}
Sourcepub fn key_released(&self, button: VirtualKeyCode) -> bool
pub fn key_released(&self, button: VirtualKeyCode) -> bool
If the key was released this last frame.
Examples found in repository?
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}
Sourcepub fn schedule_wakeup(&mut self, when: Instant) -> u32
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?
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}
Sourcepub fn reschedule_wakeup(&mut self, wakeup: Wakeup)
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?
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}
Sourcepub fn cancel_wakeup(&mut self, id: u32) -> Option<Wakeup>
pub fn cancel_wakeup(&mut self, id: u32) -> Option<Wakeup>
Sourcepub fn adjust_wakeup(&mut self, id: u32, when: Instant) -> bool
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?
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
impl Clone for BasicInput
Source§fn clone(&self) -> BasicInput
fn clone(&self) -> BasicInput
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more