Skip to content

Commit

Permalink
Add notes remove combo (sketchy impl)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashutk committed Oct 27, 2022
1 parent e1eec0a commit 1bb658f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
73 changes: 68 additions & 5 deletions src/inputs.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -17,12 +22,25 @@ struct StoredHandler {
}

pub struct Inputs {
initialized: bool,
handlers: Vec<StoredHandler>,
last_fire: Option<HashMap<InputEvent, Winstant>>,
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<F>(&mut self, event: InputEvent, handler: F) -> &mut Self
Expand All @@ -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(),
Expand All @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -271,5 +277,6 @@ fn update() {
TRACKER.update();
INPUTS.tick();
}
unsafe { Winstant::tick() }

Winstant::tick();
}

0 comments on commit 1bb658f

Please sign in to comment.