From 1bb658f4aadb94ddf2be57dfd21e2a5bdee14cc2 Mon Sep 17 00:00:00 2001 From: pashutk Date: Fri, 28 Oct 2022 01:30:58 +0200 Subject: [PATCH] Add notes remove combo (sketchy impl) --- src/inputs.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++---- src/lib.rs | 9 ++++++- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/inputs.rs b/src/inputs.rs index 916d832..d92815e 100644 --- a/src/inputs.rs +++ b/src/inputs.rs @@ -1,9 +1,14 @@ -use crate::wasm4::{ - BUTTON_1, BUTTON_2, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_UP, GAMEPAD1, +use std::{collections::HashMap, time::Duration}; + +use crate::{ + wasm4::{BUTTON_1, BUTTON_2, BUTTON_DOWN, BUTTON_LEFT, BUTTON_RIGHT, BUTTON_UP, GAMEPAD1}, + wtime::{FromFrames, Winstant}, }; +#[derive(Eq, Hash, PartialEq)] pub enum InputEvent { Button1Press, + Button1DoublePress, Button2Press, ButtonUpPress, ButtonDownPress, @@ -17,12 +22,25 @@ struct StoredHandler { } pub struct Inputs { + initialized: bool, handlers: Vec, + last_fire: Option>, + double_press_activated: bool, } impl Inputs { pub const fn new() -> Self { - Inputs { handlers: vec![] } + Inputs { + initialized: false, + handlers: vec![], + last_fire: None, + double_press_activated: false, + } + } + + fn init(&mut self) { + self.last_fire = Some(HashMap::new()); + self.initialized = true; } pub fn listen(&mut self, event: InputEvent, handler: F) -> &mut Self @@ -36,11 +54,34 @@ impl Inputs { self } - pub fn tick(&self) { + fn get_last_fire(&self, event: InputEvent) -> Option<&Winstant> { + let map = self.last_fire.as_ref()?; + map.get(&event) + } + + pub fn tick(&mut self) { + if !self.initialized { + self.init() + } let gamepad = unsafe { *GAMEPAD1 }; for StoredHandler { ref handler, event } in &self.handlers { match event { - InputEvent::Button1Press if self.is_button1_pressed() => handler(), + InputEvent::Button1Press + if self.is_button1_pressed() && !self.double_press_activated => + { + handler() + } + InputEvent::Button1DoublePress if self.is_button1_pressed() => { + if let Some(last) = self.get_last_fire(InputEvent::Button1Press) { + let now = Winstant::now(); + let max = *last + Duration::from_millis(200); + let min = *last + Duration::from_frames(2); + if now < max && now > min { + handler(); + self.double_press_activated = true; + } + } + } InputEvent::Button2Press if self.is_button2_pressed() => handler(), InputEvent::ButtonDownPress if gamepad & BUTTON_DOWN != 0 => handler(), InputEvent::ButtonUpPress if gamepad & BUTTON_UP != 0 => handler(), @@ -49,6 +90,28 @@ impl Inputs { _ => {} } } + let now = Winstant::now(); + match self.get_last_fire(InputEvent::Button1Press) { + None => { + if self.is_button1_pressed() { + let map = self.last_fire.as_mut().unwrap(); + map.insert(InputEvent::Button1Press, now); + } + } + Some(last) => { + if self.double_press_activated && *last + Duration::from_millis(200) <= now { + self.double_press_activated = false; + } + if self.is_button1_pressed() { + let map = self.last_fire.as_mut().unwrap(); + map.insert(InputEvent::Button1Press, now); + } + } + } + + // if *last + Duration::from_frames(2) >= now { + + // } } pub fn is_button2_pressed(&self) -> bool { diff --git a/src/lib.rs b/src/lib.rs index 1ee2784..c86deab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -192,6 +192,12 @@ fn start() { TRACKER.pattern[cursor as usize] = Some(note_c3_index) } }) + .listen(InputEvent::Button1DoublePress, || { + let cursor = TRACKER.cursor_tick; + if let Some(_) = TRACKER.pattern[cursor as usize] { + TRACKER.pattern[cursor as usize] = None + } + }) .listen(InputEvent::ButtonRightPress, || { if INPUTS.is_button1_pressed() { let cursor = TRACKER.cursor_tick; @@ -271,5 +277,6 @@ fn update() { TRACKER.update(); INPUTS.tick(); } - unsafe { Winstant::tick() } + + Winstant::tick(); }