input.rs
1 //! Input. 2 3 use super::*; 4 5 pub struct Input { 6 pub hover_pos: GridPos, 7 pub hover_side: HoverSide, 8 pub fast: bool, 9 pub events: Vec<InputEvent>, 10 } 11 12 impl Input { 13 pub fn update(&mut self, camera: &Camera2D, y_offset_percent: f32) { 14 self.events.clear(); 15 16 // Compute hover. 17 let mouse_moved = mouse_delta_position().length() > 0.0; 18 if mouse_moved { 19 let mut pos = camera.screen_to_world(mouse_position().into()); 20 pos.x -= 50.0; 21 pos.y += 50.0 - 100.0 * y_offset_percent; 22 if pos.x > 0.0 && pos.x < 600.0 && pos.y > 0.0 && pos.y < 1000.0 { 23 let x_exact = pos.x / 100.0; 24 let x: u32 = ((x_exact).round() as u32).min(NUM_COLS - 1); 25 let y_exact = pos.y / 100.0; 26 let y: u32 = (10 - ((y_exact).round() as u32)).min(NUM_ROWS); 27 self.hover_pos = GridPos::new(x, y); 28 if x_exact.rem_euclid(1.0) < 0.5 { 29 self.hover_side = HoverSide::Right; 30 } else { 31 self.hover_side = HoverSide::Left; 32 } 33 } 34 } else { 35 if is_key_pressed(KeyCode::Right) || is_key_pressed(KeyCode::D) { 36 self.hover_pos.x = self.hover_pos.x.saturating_add(1).min(NUM_COLS - 1); 37 } 38 if is_key_pressed(KeyCode::Left) || is_key_pressed(KeyCode::A) { 39 self.hover_pos.x = self.hover_pos.x.saturating_sub(1); 40 } 41 if is_key_pressed(KeyCode::Up) || is_key_pressed(KeyCode::W) { 42 self.hover_pos.y = self.hover_pos.y.saturating_add(1).min(NUM_ROWS); 43 } 44 if is_key_pressed(KeyCode::Down) || is_key_pressed(KeyCode::S) { 45 self.hover_pos.y = self.hover_pos.y.saturating_sub(1); 46 } 47 } 48 49 // Sanity check hover pos & side. 50 if self.hover_pos.x == 0 { 51 self.hover_side = HoverSide::Right; 52 } 53 if self.hover_pos.x == NUM_COLS - 1 { 54 self.hover_side = HoverSide::Left; 55 } 56 57 // Check for alert. 58 if is_key_pressed(KeyCode::Tab) || is_mouse_button_pressed(MouseButton::Right) { 59 self.events.push(InputEvent::Alert(self.hover_pos)); 60 } 61 62 // Fast flag. 63 self.fast = is_key_down(KeyCode::LeftShift); 64 65 // Handle swap/click. 66 let confirmed = 67 is_mouse_button_pressed(MouseButton::Left) || is_key_pressed(KeyCode::Space); 68 if confirmed { 69 if (self.hover_side == HoverSide::Right && self.hover_pos.x < 5) 70 || self.hover_pos.x == 0 71 { 72 self.events 73 .push(InputEvent::SwapRight(self.hover_pos.clone())); 74 } 75 if (self.hover_side == HoverSide::Left && self.hover_pos.x > 0) || self.hover_pos.x == 5 76 { 77 self.events.push(InputEvent::SwapRight(GridPos::new( 78 self.hover_pos.x - 1, 79 self.hover_pos.y, 80 ))); 81 } 82 } 83 84 if is_key_pressed(KeyCode::Escape) { 85 self.events.push(InputEvent::Restart); 86 } 87 } 88 } 89 90 impl Default for Input { 91 fn default() -> Self { 92 Self { 93 hover_pos: GridPos::new(0, 2), 94 hover_side: HoverSide::Left, 95 fast: false, 96 events: Vec::new(), 97 } 98 } 99 } 100 101 pub enum InputEvent { 102 SwapRight(GridPos), 103 Alert(GridPos), 104 Restart, 105 } 106 107 #[derive(Clone, Copy)] 108 pub struct GridPos { 109 pub x: u32, 110 pub y: u32, 111 } 112 113 impl GridPos { 114 pub fn new(x: u32, y: u32) -> Self { 115 Self { x, y } 116 } 117 } 118 119 #[derive(Copy, Clone, PartialEq)] 120 pub enum HoverSide { 121 Left, 122 Right, 123 }