1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
use std::cell::Cell; #[derive(Debug, Clone, Copy, Default)] pub struct InputState { pub joypad_1: JoypadState, pub joypad_2: JoypadState, } #[derive(Debug, Clone, Copy)] pub struct JoypadState { pub a: bool, pub b: bool, pub start: bool, pub select: bool, pub up: bool, pub down: bool, pub left: bool, pub right: bool, } impl Default for JoypadState { fn default() -> Self { JoypadState { a: false, b: false, start: false, select: false, up: false, down: false, left: false, right: false, } } } pub trait Input { fn input_state(&self) -> InputState; } pub struct NullInput; impl Input for NullInput { fn input_state(&self) -> InputState { InputState::default() } } pub struct SampledInput { state: Cell<InputState>, } impl SampledInput { pub fn new(state: InputState) -> Self { SampledInput { state: Cell::new(state) } } pub fn set_state(&self, new_state: InputState) { self.state.set(new_state) } } impl Input for SampledInput { fn input_state(&self) -> InputState { let state = self.state.take(); self.state.set(state.clone()); state } } impl<'a, I> Input for &'a I where I: Input { fn input_state(&self) -> InputState { (*self).input_state() } }