pub struct PressedKeys { /* private fields */ }Expand description
Contains the keys that have been pressed since the last update.
Implementations§
Source§impl PressedKeys
impl PressedKeys
Sourcepub fn inner(&self) -> &Map<KeyCode, u8, 16>
pub fn inner(&self) -> &Map<KeyCode, u8, 16>
Returns the raw map of pressed keys.
Examples found in repository?
examples/fabrik/main.rs (line 250)
211 fn update(&mut self, update_info: UpdateInfo, shared_state: &mut SharedState<()>) {
212 let (x, y) = shared_state.mouse_info.last_mouse_pos;
213 let mouse_point = Point {
214 x: x as f64,
215 y: y as f64 * 2.0,
216 };
217
218 if shared_state.mouse_pressed.right {
219 // if we are not creating a segment but we do already have a last point, connect it immediately
220 if let Some(last_point) = self.last_point {
221 if self.currently_creating_segment.is_none() {
222 let new_segment = Segment {
223 start: last_point,
224 length: last_point.distance(&mouse_point),
225 };
226 self.segments.push(new_segment);
227 if self.base_anchor.is_none() {
228 self.base_anchor = Some(last_point);
229 }
230 self.last_point = Some(mouse_point);
231 }
232 }
233
234 if let Some(start_point) = self.currently_creating_segment.take() {
235 let length = start_point.distance(&mouse_point);
236 self.last_point = Some(mouse_point);
237 let new_segment = Segment {
238 start: start_point,
239 length,
240 };
241 self.segments.push(new_segment);
242 if self.base_anchor.is_none() {
243 self.base_anchor = Some(start_point);
244 }
245 }
246
247 self.currently_creating_segment = Some(mouse_point);
248 }
249
250 if shared_state.pressed_keys.inner().contains_key(&KeyCode::Esc) {
251 self.currently_creating_segment = None;
252 }
253
254 if shared_state.mouse_info.left_mouse_down {
255 // set target
256 self.target = Some(mouse_point);
257 self.currently_creating_segment = None;
258
259 self.forward_reach();
260 self.backward_reach();
261 }
262
263 shared_state.mouse_events.for_each_linerp_only_fresh(|mi| {
264 if mi.middle_mouse_down {
265 self.currently_creating_segment = None;
266 let mouse_point = Point {
267 x: mi.last_mouse_pos.0 as f64,
268 y: mi.last_mouse_pos.1 as f64 * 2.0,
269 };
270
271 if self.base_anchor.is_none() {
272 self.base_anchor = Some(mouse_point);
273 }
274 let Some(last_point) = self.last_point else {
275 self.last_point = Some(mouse_point);
276 return;
277 };
278
279 let new_segment = Segment {
280 start: last_point,
281 length: last_point.distance(&mouse_point),
282 };
283 self.segments.push(new_segment);
284 self.last_point = Some(mouse_point);
285 }
286 });
287
288 // if shared_state.pressed_keys.did_press_char_ignore_case('f') {
289 // self.forward_reach();
290 // }
291 // if shared_state.pressed_keys.did_press_char_ignore_case('b') {
292 // self.backward_reach();
293 // }
294
295 if shared_state.pressed_keys.did_press_char_ignore_case('c') {
296 self.segments.clear();
297 self.base_anchor = None;
298 self.target = None;
299 self.currently_creating_segment = None;
300 self.last_point = None;
301 }
302
303
304 // render into half block display
305 self.render_to_half_block_display(mouse_point);
306 }Sourcepub fn insert(&mut self, key: KeyCode)
pub fn insert(&mut self, key: KeyCode)
Not recommended to use. However, it is useful to hack key actions in other components if the update order is known.
Sourcepub fn did_press_char(&self, c: char) -> bool
pub fn did_press_char(&self, c: char) -> bool
Returns true if the given key was pressed since the last update.
Sourcepub fn did_press_char_ignore_case(&self, c: char) -> bool
pub fn did_press_char_ignore_case(&self, c: char) -> bool
Returns true if the given key was pressed since the last update, ignoring case.
Examples found in repository?
More examples
examples/boundschecker.rs (line 86)
49 fn update(&mut self, _update_info: UpdateInfo, shared_state: &mut SharedState) {
50 // was there a mouse down event?
51 let pressed = shared_state.mouse_pressed.left;
52 if pressed {
53 self.first_loc = None;
54 self.second_loc = None;
55
56 self.first_loc = Some(shared_state.mouse_info.last_mouse_pos);
57 }
58
59 // was there a mouse up event?
60 if !shared_state.mouse_info.left_mouse_down
61 && self.first_loc.is_some()
62 && self.second_loc.is_none()
63 {
64 // set the second location
65 self.second_loc = Some(shared_state.mouse_info.last_mouse_pos);
66 }
67
68 // was there a mouse down event for right?
69 let pressed = shared_state.mouse_pressed.right;
70 if pressed {
71 self.sub_first_loc = None;
72 self.sub_second_loc = None;
73
74 self.sub_first_loc = Some(shared_state.mouse_info.last_mouse_pos);
75 }
76
77 // was there a mouse up event for right?
78 if !shared_state.mouse_info.right_mouse_down
79 && self.sub_first_loc.is_some()
80 && self.sub_second_loc.is_none()
81 {
82 // set the second location
83 self.sub_second_loc = Some(shared_state.mouse_info.last_mouse_pos);
84 }
85
86 if shared_state.pressed_keys.did_press_char_ignore_case(' ') {
87 self.union_instead_of_subtract = !self.union_instead_of_subtract;
88 }
89 }examples/fabrik/main.rs (line 295)
211 fn update(&mut self, update_info: UpdateInfo, shared_state: &mut SharedState<()>) {
212 let (x, y) = shared_state.mouse_info.last_mouse_pos;
213 let mouse_point = Point {
214 x: x as f64,
215 y: y as f64 * 2.0,
216 };
217
218 if shared_state.mouse_pressed.right {
219 // if we are not creating a segment but we do already have a last point, connect it immediately
220 if let Some(last_point) = self.last_point {
221 if self.currently_creating_segment.is_none() {
222 let new_segment = Segment {
223 start: last_point,
224 length: last_point.distance(&mouse_point),
225 };
226 self.segments.push(new_segment);
227 if self.base_anchor.is_none() {
228 self.base_anchor = Some(last_point);
229 }
230 self.last_point = Some(mouse_point);
231 }
232 }
233
234 if let Some(start_point) = self.currently_creating_segment.take() {
235 let length = start_point.distance(&mouse_point);
236 self.last_point = Some(mouse_point);
237 let new_segment = Segment {
238 start: start_point,
239 length,
240 };
241 self.segments.push(new_segment);
242 if self.base_anchor.is_none() {
243 self.base_anchor = Some(start_point);
244 }
245 }
246
247 self.currently_creating_segment = Some(mouse_point);
248 }
249
250 if shared_state.pressed_keys.inner().contains_key(&KeyCode::Esc) {
251 self.currently_creating_segment = None;
252 }
253
254 if shared_state.mouse_info.left_mouse_down {
255 // set target
256 self.target = Some(mouse_point);
257 self.currently_creating_segment = None;
258
259 self.forward_reach();
260 self.backward_reach();
261 }
262
263 shared_state.mouse_events.for_each_linerp_only_fresh(|mi| {
264 if mi.middle_mouse_down {
265 self.currently_creating_segment = None;
266 let mouse_point = Point {
267 x: mi.last_mouse_pos.0 as f64,
268 y: mi.last_mouse_pos.1 as f64 * 2.0,
269 };
270
271 if self.base_anchor.is_none() {
272 self.base_anchor = Some(mouse_point);
273 }
274 let Some(last_point) = self.last_point else {
275 self.last_point = Some(mouse_point);
276 return;
277 };
278
279 let new_segment = Segment {
280 start: last_point,
281 length: last_point.distance(&mouse_point),
282 };
283 self.segments.push(new_segment);
284 self.last_point = Some(mouse_point);
285 }
286 });
287
288 // if shared_state.pressed_keys.did_press_char_ignore_case('f') {
289 // self.forward_reach();
290 // }
291 // if shared_state.pressed_keys.did_press_char_ignore_case('b') {
292 // self.backward_reach();
293 // }
294
295 if shared_state.pressed_keys.did_press_char_ignore_case('c') {
296 self.segments.clear();
297 self.base_anchor = None;
298 self.target = None;
299 self.currently_creating_segment = None;
300 self.last_point = None;
301 }
302
303
304 // render into half block display
305 self.render_to_half_block_display(mouse_point);
306 }examples/circlerasterizer.rs (line 267)
266 fn update(&mut self, update_info: UpdateInfo, shared_state: &mut SharedState<()>) {
267 if shared_state.pressed_keys.did_press_char_ignore_case('c') {
268 self.free_balls.clear();
269 }
270
271 if shared_state.mouse_info.left_mouse_down {
272 let current_ball = self.current_ball.get_or_insert(Ball {
273 x: shared_state.mouse_info.last_mouse_pos.0 as f64,
274 y: shared_state.mouse_info.last_mouse_pos.1 as f64,
275 radius: self.default_radius,
276 mass: self.default_radius * self.default_radius,
277 x_vel: 0.0,
278 y_vel: 0.0,
279 });
280
281 current_ball.x = shared_state.mouse_info.last_mouse_pos.0 as f64;
282 current_ball.y = shared_state.mouse_info.last_mouse_pos.1 as f64;
283 current_ball.y_vel = 0.0;
284 current_ball.x_vel = 0.0;
285 if !self.did_hold_last {
286 // first time we're holding again, so we clear the samples
287 self.center_samples.clear();
288 }
289 self.did_hold_last = true;
290 } else if self.did_hold_last {
291 // must have current ball
292 let current_ball = self.current_ball.as_mut().unwrap();
293 // just released
294 self.did_hold_last = false;
295 // compute a force based on average velocity over the samples
296 let mut sum_x_delta = 0.0;
297 let mut sum_y_delta = 0.0;
298 for i in 1..self.center_samples.len() {
299 let (x1, y1) = self.center_samples[i - 1];
300 let (x2, y2) = self.center_samples[i];
301 sum_x_delta += x2 - x1;
302 sum_y_delta += y2 - y1;
303 }
304 let delta_length = self.center_samples.len() as f64 / 60.0;
305 let avg_x_vel = sum_x_delta / delta_length;
306 let avg_y_vel = sum_y_delta / delta_length;
307 let strength = 1.0;
308 current_ball.x_vel = avg_x_vel * strength;
309 current_ball.y_vel = avg_y_vel * strength;
310 // release ball
311 self.free_balls.push(current_ball.clone());
312 self.current_ball = None;
313 }
314
315 if let Some(current_ball) = &mut self.current_ball {
316 shared_state.debug_info.custom.insert("Circle Radius".to_string(), format!("{:.2}", current_ball.radius));
317 shared_state.debug_info.custom.insert("Circle Center".to_string(), format!("({}, {})", current_ball.x, current_ball.y));
318 }
319
320 // simple physics
321 // but don't update if we're holding LMB
322 if !shared_state.mouse_info.left_mouse_down {
323 self.current_ball.as_mut().map(|ball| ball.update(update_info.dt, shared_state.display_info.height() as f64));
324 }
325 // update all other balls
326 // for ball in &mut self.free_balls {
327 // ball.update(update_info.dt, shared_state.display_info.height() as f64);
328 // }
329
330 update_balls(update_info.dt, &mut self.free_balls, shared_state.display_info.height() as f64, &self.static_collision);
331
332 self.fixed_update_runner.fuel(update_info.dt);
333 while self.fixed_update_runner.has_gas() {
334 self.fixed_update_runner.consume();
335 if let Some(current_ball) = &mut self.current_ball {
336 self.center_samples.push((current_ball.x, current_ball.y));
337 if self.center_samples.len() > MAX_SAMPLES {
338 self.center_samples.remove(0);
339 }
340 }
341 }
342
343 // update static collision board
344 shared_state.mouse_events.for_each_linerp_only_fresh(|mi| {
345 if mi.right_mouse_down {
346 self.static_collision.set(mi.last_mouse_pos.0, mi.last_mouse_pos.1, true);
347 }
348 })
349 }examples/circlerasterizer2.rs (line 388)
387 fn update(&mut self, update_info: UpdateInfo, shared_state: &mut SharedState<()>) {
388 if shared_state.pressed_keys.did_press_char_ignore_case('c') {
389 self.free_balls.clear();
390 }
391
392 if shared_state.pressed_keys.did_press_char_ignore_case('r') {
393 self.static_collision.fill(false);
394 }
395
396 if shared_state.mouse_info.left_mouse_down {
397 let world_x = shared_state.mouse_info.last_mouse_pos.0 as f64;
398 let world_y = shared_state.mouse_info.last_mouse_pos.1 as f64;
399 let current_ball = self.current_ball.get_or_insert_with(|| {
400 Ball::new(world_x, world_y, self.default_radius)
401 });
402
403 current_ball.set_world_x(world_x);
404 current_ball.set_world_y(world_y);
405 current_ball.x_vel = 0.0;
406 if !self.did_hold_last {
407 // first time we're holding again, so we clear the samples
408 self.center_samples.clear();
409 }
410 self.did_hold_last = true;
411 } else if self.did_hold_last {
412 // must have current ball
413 let current_ball = self.current_ball.as_mut().unwrap();
414 // just released
415 self.did_hold_last = false;
416 // compute a force based on average velocity over the samples
417 let mut sum_x_delta = 0.0;
418 let mut sum_y_delta = 0.0;
419 for i in 1..self.center_samples.len() {
420 let (x1, y1) = self.center_samples[i - 1];
421 let (x2, y2) = self.center_samples[i];
422 sum_x_delta += x2 - x1;
423 sum_y_delta += y2 - y1;
424 }
425 let delta_length = self.center_samples.len() as f64 / 60.0;
426 let avg_x_vel = sum_x_delta / delta_length;
427 let avg_y_vel = sum_y_delta / delta_length;
428 let strength = 1.0;
429 current_ball.x_vel = avg_x_vel * strength;
430 current_ball.y_vel = avg_y_vel * strength;
431 // release ball
432 self.free_balls.push(current_ball.clone());
433 self.current_ball = None;
434 }
435
436 if let Some(current_ball) = &mut self.current_ball {
437 shared_state.debug_info.custom.insert("Circle Radius".to_string(), format!("{:.2}", current_ball.radius));
438 shared_state.debug_info.custom.insert("Circle Center (local)".to_string(), format!("({}, {})", current_ball.local_x(), current_ball.local_y()));
439 shared_state.debug_info.custom.insert("Circle Center (world)".to_string(), format!("({}, {})", current_ball.world_x(), current_ball.world_y()));
440 }
441
442 if let Some(first_ball) = self.free_balls.first() {
443 shared_state.debug_info.custom.insert("First Ball Center (local)".to_string(), format!("({:.2}, {:.2})", first_ball.local_x(), first_ball.local_y()));
444 shared_state.debug_info.custom.insert("First Ball Center (world)".to_string(), format!("({:.2}, {:.2})", first_ball.world_x(), first_ball.world_y()));
445 shared_state.debug_info.custom.insert("First Ball velocity".to_string(), format!("({:.2}, {:.2})", first_ball.x_vel, first_ball.y_vel));
446
447 }
448
449 update_balls(update_info.dt, &mut self.free_balls, shared_state.display_info.height() as f64, &self.static_collision);
450
451 self.fixed_update_runner.fuel(update_info.dt);
452 while self.fixed_update_runner.has_gas() {
453 self.fixed_update_runner.consume();
454 if let Some(current_ball) = &mut self.current_ball {
455 self.center_samples.push((current_ball.local_x(), current_ball.local_y()));
456 if self.center_samples.len() > MAX_SAMPLES {
457 self.center_samples.remove(0);
458 }
459 }
460 }
461
462 // update static collision board
463 shared_state.mouse_events.for_each_linerp_only_fresh(|mi| {
464 if mi.right_mouse_down {
465 self.static_collision.set(mi.last_mouse_pos.0, mi.last_mouse_pos.1, true);
466 }
467 })
468 }Auto Trait Implementations§
impl Freeze for PressedKeys
impl RefUnwindSafe for PressedKeys
impl Send for PressedKeys
impl Sync for PressedKeys
impl Unpin for PressedKeys
impl UnwindSafe for PressedKeys
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more