diff options
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/teleia/src/state.rs | 27 | ||||
| -rw-r--r-- | crates/teleia/src/ui.rs | 103 |
2 files changed, 74 insertions, 56 deletions
diff --git a/crates/teleia/src/state.rs b/crates/teleia/src/state.rs index 2e50fbf..b7b8899 100644 --- a/crates/teleia/src/state.rs +++ b/crates/teleia/src/state.rs @@ -72,19 +72,19 @@ impl Keys { pub fn start(&self) -> bool { self.pressed[Key::Start] } pub fn select(&self) -> bool { self.pressed[Key::Select] } pub fn debug(&self) -> bool { self.pressed[Key::Debug] } - pub fn new_up(&mut self) -> bool { let ret = self.new[Key::Up]; self.new[Key::Up] = false; ret } - pub fn new_down(&mut self) -> bool { let ret = self.new[Key::Down]; self.new[Key::Down] = false; ret } - pub fn new_left(&mut self) -> bool { let ret = self.new[Key::Left]; self.new[Key::Left] = false; ret } - pub fn new_right(&mut self) -> bool { let ret = self.new[Key::Right]; self.new[Key::Right] = false; ret } - pub fn new_a(&mut self) -> bool { let ret = self.new[Key::A]; self.new[Key::A] = false; ret } - pub fn new_b(&mut self) -> bool { let ret = self.new[Key::B]; self.new[Key::B] = false; ret } - pub fn new_x(&mut self) -> bool { let ret = self.new[Key::X]; self.new[Key::X] = false; ret } - pub fn new_y(&mut self) -> bool { let ret = self.new[Key::Y]; self.new[Key::Y] = false; ret } - pub fn new_l(&mut self) -> bool { let ret = self.new[Key::L]; self.new[Key::L] = false; ret } - pub fn new_r(&mut self) -> bool { let ret = self.new[Key::R]; self.new[Key::R] = false; ret } - pub fn new_start(&mut self) -> bool { let ret = self.new[Key::Start]; self.new[Key::Start] = false; ret } - pub fn new_select(&mut self) -> bool { let ret = self.new[Key::Select]; self.new[Key::Select] = false; ret } - pub fn new_debug(&mut self) -> bool { let ret = self.new[Key::Debug]; self.new[Key::Debug] = false; ret } + pub fn new_up(&self) -> bool { self.new[Key::Up] } + pub fn new_down(&self) -> bool { self.new[Key::Down] } + pub fn new_left(&self) -> bool { self.new[Key::Left] } + pub fn new_right(&self) -> bool { self.new[Key::Right] } + pub fn new_a(&self) -> bool { self.new[Key::A] } + pub fn new_b(&self) -> bool { self.new[Key::B] } + pub fn new_x(&self) -> bool { self.new[Key::X] } + pub fn new_y(&self) -> bool { self.new[Key::Y] } + pub fn new_l(&self) -> bool { self.new[Key::L] } + pub fn new_r(&self) -> bool { self.new[Key::R] } + pub fn new_start(&self) -> bool { self.new[Key::Start] } + pub fn new_select(&self) -> bool { self.new[Key::Select] } + pub fn new_debug(&self) -> bool { self.new[Key::Debug] } } pub struct PointLight { @@ -544,6 +544,7 @@ impl State { self.tick += 1; self.frames_this_second += 1; game.update(ctx, self)?; + self.keys.new = enum_map! { _ => false }; } if now - self.start_this_second > 1.0 { // track FPS self.start_this_second = now; diff --git a/crates/teleia/src/ui.rs b/crates/teleia/src/ui.rs index 09afdf9..056f78a 100644 --- a/crates/teleia/src/ui.rs +++ b/crates/teleia/src/ui.rs @@ -1,4 +1,4 @@ -use crate::utils; +use crate::{state, utils}; fn compute_reverse(frames: u64, tick: u64, start: u64) -> u64 { let leftover = frames - (tick - start) @@ -14,7 +14,6 @@ pub enum ModeToggle { pub struct Mode { frames: u64, toggle: ModeToggle, - locked: bool, } impl Mode { @@ -22,7 +21,6 @@ impl Mode { Self { frames, toggle: ModeToggle::Inactive { start: 0 }, - locked: false, } } @@ -34,10 +32,6 @@ impl Mode { } } - pub fn is_locked(&self) -> bool { - self.locked - } - /// Has the current transition finished? pub fn is_ready(&self, tick: u64) -> bool { let started = match self.toggle { @@ -61,35 +55,22 @@ impl Mode { } pub fn reset(&mut self) { - self.locked = false; self.toggle = ModeToggle::Inactive { start: 0 }; } - pub fn reverse(&mut self, tick: u64) -> bool { - if !self.locked { - self.locked = true; - match self.toggle { - ModeToggle::Inactive { start } => { - self.toggle = ModeToggle::Active { - start: compute_reverse(self.frames, tick, start) - }; - }, - ModeToggle::Active { start } => { - self.toggle = ModeToggle::Inactive { - start: compute_reverse(self.frames, tick, start) - }; - }, - } - true - } else { false } - } - - pub fn lock(&mut self) { - self.locked = true; - } - - pub fn unlock(&mut self) { - self.locked = false; + pub fn toggle(&mut self, tick: u64) { + match self.toggle { + ModeToggle::Inactive { start } => { + self.toggle = ModeToggle::Active { + start: compute_reverse(self.frames, tick, start) + }; + }, + ModeToggle::Active { start } => { + self.toggle = ModeToggle::Inactive { + start: compute_reverse(self.frames, tick, start) + }; + }, + } } } @@ -99,7 +80,6 @@ pub struct Cursor { pub change_started: u64, pub bound: i32, pub frames: u64, - pub locked: bool, } impl Cursor { @@ -110,7 +90,6 @@ impl Cursor { change_started: 0, bound, frames, - locked: false, } } @@ -129,25 +108,63 @@ impl Cursor { } pub fn set(&mut self, val: i32, tick: u64) -> bool { - if self.is_ready(tick) || !self.locked { + if self.is_ready(tick) { self.change_started = tick; self.prev_index = self.index; self.index = val; self.index %= self.bound; - self.locked = true; true } else { false } } + pub fn increment(&mut self, tick: u64) -> bool { self.set(self.index + 1, tick) } + pub fn decrement(&mut self, tick: u64) -> bool { self.set(self.index + self.bound - 1, tick) } - pub fn increment(&mut self, tick: u64) -> bool { - self.set(self.index + 1, tick) + pub fn set_unlocked(&mut self, val: i32, tick: u64) { + self.change_started = tick; + self.prev_index = self.index; + self.index = val; + self.index %= self.bound; } + pub fn increment_unlocked(&mut self, tick: u64) -> bool { self.set_unlocked(self.index + 1, tick); true } + pub fn decrement_unlocked(&mut self, tick: u64) -> bool { self.set_unlocked(self.index + self.bound - 1, tick); true } - pub fn decrement(&mut self, tick: u64) -> bool { - self.set(self.index + self.bound - 1, tick) + /// Read keypresses to update this cursor (assuming that the left/right keys decrement/increment) + /// Returns true if an update was performed (e.g. to determine whether to play a sound). + pub fn update_horizontal(&mut self, st: &state::State) -> bool { + if st.keys.new_left() { + self.decrement_unlocked(st.tick); + true + } else if st.keys.new_right() { + self.increment_unlocked(st.tick); + true + } else if st.keys.left() { + self.decrement(st.tick) + } else if st.keys.right() { + self.increment(st.tick) + } else { false } } - pub fn unlock(&mut self) { - self.locked = false; + pub fn update_vertical(&mut self, st: &state::State) -> bool { + if st.keys.new_up() { + self.decrement_unlocked(st.tick) + } else if st.keys.new_down() { + self.increment_unlocked(st.tick) + } else if st.keys.up() { + self.decrement(st.tick) + } else if st.keys.down() { + self.increment(st.tick) + } else { false } + } + + pub fn update_lr(&mut self, st: &state::State) -> bool { + if st.keys.new_l() { + self.decrement_unlocked(st.tick) + } else if st.keys.new_r() { + self.increment_unlocked(st.tick) + } else if st.keys.l() { + self.decrement(st.tick) + } else if st.keys.r() { + self.increment(st.tick) + } else { false } } } |
