codecraft 0.1.1

A minimalist 3D game engine built on parts of Bevy (ECS, color) with wgpu and winit: OpenPBR materials, clustered lighting, an immediate-mode UI, audio and gamepad haptics
Documentation
//! Keyboard state, published into the ECS world each frame.
use std::collections::HashSet;

use crate::ecs::Resource;

pub use winit::keyboard::KeyCode;

/// Which keys are down, and which went down this frame.
///
/// `just_pressed` is an edge: it holds for exactly the frame the key went
/// down, so a scene can act on a press without tracking the previous state.
#[derive(Resource, Default, Debug)]
pub struct Keys {
    pressed: HashSet<KeyCode>,
    just_pressed: HashSet<KeyCode>,
}

impl Keys {
    /// Whether the key is currently held.
    pub fn pressed(&self, key: KeyCode) -> bool {
        self.pressed.contains(&key)
    }

    /// Whether the key went down this frame.
    pub fn just_pressed(&self, key: KeyCode) -> bool {
        self.just_pressed.contains(&key)
    }

    pub(crate) fn press(&mut self, key: KeyCode, repeat: bool) {
        // Key repeat is the OS re-sending a key that is already down; it must
        // not read as a fresh press.
        if !repeat && self.pressed.insert(key) {
            self.just_pressed.insert(key);
        }
    }

    pub(crate) fn release(&mut self, key: KeyCode) {
        self.pressed.remove(&key);
    }

    /// Clears the single-frame edges. Called once a frame has been handled.
    pub(crate) fn end_frame(&mut self) {
        self.just_pressed.clear();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_press_is_an_edge_that_lasts_one_frame() {
        let mut keys = Keys::default();
        keys.press(KeyCode::Escape, false);

        assert!(keys.just_pressed(KeyCode::Escape));
        assert!(keys.pressed(KeyCode::Escape));

        keys.end_frame();
        assert!(!keys.just_pressed(KeyCode::Escape), "the edge is spent");
        assert!(keys.pressed(KeyCode::Escape), "but the key is still held");
    }

    #[test]
    fn key_repeat_does_not_read_as_a_new_press() {
        let mut keys = Keys::default();
        keys.press(KeyCode::Escape, false);
        keys.end_frame();

        // The OS re-sends a held key; a menu toggle must not fire again.
        keys.press(KeyCode::Escape, true);
        assert!(!keys.just_pressed(KeyCode::Escape));

        // Nor does a non-repeat event for a key already down.
        keys.press(KeyCode::Escape, false);
        assert!(!keys.just_pressed(KeyCode::Escape));
    }

    #[test]
    fn releasing_and_pressing_again_is_a_fresh_edge() {
        let mut keys = Keys::default();
        keys.press(KeyCode::Escape, false);
        keys.end_frame();
        keys.release(KeyCode::Escape);
        assert!(!keys.pressed(KeyCode::Escape));

        keys.press(KeyCode::Escape, false);
        assert!(keys.just_pressed(KeyCode::Escape));
    }

    #[test]
    fn keys_are_tracked_independently() {
        let mut keys = Keys::default();
        keys.press(KeyCode::Escape, false);
        keys.press(KeyCode::Space, false);
        keys.release(KeyCode::Escape);

        assert!(!keys.pressed(KeyCode::Escape));
        assert!(keys.pressed(KeyCode::Space));
    }
}