Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::Vec2;
use crate::{get_context, DroppedFile};
pub use miniquad::{KeyCode, MouseButton};

const KEY_REPEAT_DELAY: f64 = 5.0;

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TouchPhase {
Started,
Expand Down Expand Up @@ -132,6 +134,26 @@ pub fn is_key_released(key_code: KeyCode) -> bool {
context.keys_released.contains(&key_code)
}

/// Detect if the key has been pressed for some time
pub fn is_key_repeated(key_code: KeyCode) -> bool {
let context = get_context();

// stores the amount of time each key has been pressed for
let time_map = &mut context.keys_repeated;

time_map.entry(key_code).or_insert(0.);

let time_pressed = time_map.get_mut(&key_code).unwrap();

if is_key_down(key_code) {
*time_pressed += 1.;
} else {
*time_pressed = 0.;
}

*time_pressed >= KEY_REPEAT_DELAY
}

/// Return the last pressed char.
/// Each "get_char_pressed" call will consume a character from the input queue.
pub fn get_char_pressed() -> Option<char> {
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@
keys_down: HashSet<KeyCode>,
keys_pressed: HashSet<KeyCode>,
keys_released: HashSet<KeyCode>,
keys_repeated: HashMap<KeyCode, f64>,
mouse_down: HashSet<MouseButton>,
mouse_pressed: HashSet<MouseButton>,
mouse_released: HashSet<MouseButton>,
Expand Down Expand Up @@ -215,7 +216,7 @@
last_frame_time: f64,
frame_time: f64,

#[cfg(one_screenshot)]

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 219 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `one_screenshot`
counter: usize,

camera_stack: Vec<camera::CameraState>,
Expand Down Expand Up @@ -321,6 +322,7 @@
keys_down: HashSet::new(),
keys_pressed: HashSet::new(),
keys_released: HashSet::new(),
keys_repeated: HashMap::new(),
chars_pressed_queue: Vec::new(),
chars_pressed_ui_queue: Vec::new(),
mouse_down: HashSet::new(),
Expand Down Expand Up @@ -359,7 +361,7 @@
last_frame_time: miniquad::date::now(),
frame_time: 1. / 60.,

#[cfg(one_screenshot)]

Check warning on line 364 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 364 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 364 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 364 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 364 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `one_screenshot`
counter: 0,
unwind: false,
recovery_future: None,
Expand Down Expand Up @@ -416,7 +418,7 @@

get_quad_context().commit_frame();

#[cfg(one_screenshot)]

Check warning on line 421 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 421 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 421 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 421 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

unexpected `cfg` condition name: `one_screenshot`

Check warning on line 421 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

unexpected `cfg` condition name: `one_screenshot`
{
get_context().counter += 1;
if get_context().counter == 3 {
Expand Down Expand Up @@ -493,17 +495,17 @@
fn get_context() -> &'static mut Context {
thread_assert::same_thread();

unsafe { CONTEXT.as_mut().unwrap_or_else(|| panic!()) }

Check warning on line 498 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a mutable reference to mutable static is discouraged

Check warning on line 498 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a mutable reference to mutable static is discouraged

Check warning on line 498 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 498 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 498 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a mutable reference to mutable static is discouraged
}

fn get_quad_context() -> &'static mut dyn miniquad::RenderingBackend {
thread_assert::same_thread();

unsafe {
assert!(CONTEXT.is_some());

Check warning on line 505 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a shared reference to mutable static is discouraged

Check warning on line 505 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a shared reference to mutable static is discouraged

Check warning on line 505 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a shared reference to mutable static is discouraged

Check warning on line 505 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a shared reference to mutable static is discouraged

Check warning on line 505 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a shared reference to mutable static is discouraged
}

unsafe { &mut *CONTEXT.as_mut().unwrap().quad_context }

Check warning on line 508 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a mutable reference to mutable static is discouraged

Check warning on line 508 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a mutable reference to mutable static is discouraged

Check warning on line 508 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 508 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 508 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a mutable reference to mutable static is discouraged
}

struct Stage {
Expand Down Expand Up @@ -905,7 +907,7 @@
main_future: Box::pin(async {
future.await;
unsafe {
if let Some(ctx) = CONTEXT.as_mut() {

Check warning on line 910 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (macos-latest, x86_64-apple-darwin)

creating a mutable reference to mutable static is discouraged

Check warning on line 910 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, wasm32-unknown-unknown)

creating a mutable reference to mutable static is discouraged

Check warning on line 910 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-pc-windows-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 910 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (ubuntu-latest, x86_64-unknown-linux-gnu)

creating a mutable reference to mutable static is discouraged

Check warning on line 910 in src/lib.rs

View workflow job for this annotation

GitHub Actions / Cross-compile (windows-latest, x86_64-pc-windows-msvc)

creating a mutable reference to mutable static is discouraged
ctx.gl.reset();
}
}
Expand Down
Loading