| 1 |
use bevy::{input::InputSystems, prelude::*}; |
| 2 |
|
| 3 |
use crate::sim::SimSpeed; |
| 4 |
|
| 5 |
pub struct ActionsPlugin; |
| 6 |
|
| 7 |
impl Plugin for ActionsPlugin { |
| 8 |
fn build(&self, app: &mut App) { |
| 9 |
app.add_message::<Action>() |
| 10 |
.add_systems(PreUpdate, keyboard_actions.after(InputSystems)); |
| 11 |
} |
| 12 |
} |
| 13 |
|
| 14 |
#[derive(Message, Debug, Clone, Copy, PartialEq, Eq)] |
| 15 |
pub enum Action { |
| 16 |
TogglePause, |
| 17 |
SetSpeed(SimSpeed), |
| 18 |
ToggleStockpileTool, |
| 19 |
ToggleFarmTool, |
| 20 |
CycleBuildTool, |
| 21 |
ToggleDesignateTool, |
| 22 |
Cancel, |
| 23 |
NewWorld, |
| 24 |
ToggleUi, |
| 25 |
ZoomIn, |
| 26 |
ZoomOut, |
| 27 |
} |
| 28 |
|
| 29 |
pub const KEYBINDS: [(KeyCode, Action); 14] = [ |
| 30 |
(KeyCode::Space, Action::TogglePause), |
| 31 |
(KeyCode::Digit1, Action::SetSpeed(SimSpeed::Normal)), |
| 32 |
(KeyCode::Digit2, Action::SetSpeed(SimSpeed::Fast)), |
| 33 |
(KeyCode::Digit3, Action::SetSpeed(SimSpeed::Fastest)), |
| 34 |
(KeyCode::Digit4, Action::SetSpeed(SimSpeed::Max)), |
| 35 |
(KeyCode::KeyZ, Action::ToggleStockpileTool), |
| 36 |
(KeyCode::KeyF, Action::ToggleFarmTool), |
| 37 |
(KeyCode::KeyB, Action::CycleBuildTool), |
| 38 |
(KeyCode::KeyX, Action::ToggleDesignateTool), |
| 39 |
(KeyCode::Escape, Action::Cancel), |
| 40 |
(KeyCode::KeyR, Action::NewWorld), |
| 41 |
(KeyCode::KeyH, Action::ToggleUi), |
| 42 |
(KeyCode::Equal, Action::ZoomIn), |
| 43 |
(KeyCode::Minus, Action::ZoomOut), |
| 44 |
]; |
| 45 |
|
| 46 |
impl Action { |
| 47 |
pub fn label(self) -> &'static str { |
| 48 |
match self { |
| 49 |
Action::TogglePause => "Pause", |
| 50 |
Action::SetSpeed(SimSpeed::Normal) => "1x", |
| 51 |
Action::SetSpeed(SimSpeed::Fast) => "3x", |
| 52 |
Action::SetSpeed(SimSpeed::Fastest) => "6x", |
| 53 |
Action::SetSpeed(SimSpeed::Max) => "Max", |
| 54 |
Action::ToggleStockpileTool => "Stockpile", |
| 55 |
Action::ToggleFarmTool => "Farm", |
| 56 |
Action::CycleBuildTool => "Build", |
| 57 |
Action::ToggleDesignateTool => "Mark", |
| 58 |
Action::Cancel => "Cancel", |
| 59 |
Action::NewWorld => "New map", |
| 60 |
Action::ToggleUi => "Hide", |
| 61 |
Action::ZoomIn => "+", |
| 62 |
Action::ZoomOut => "-", |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
pub fn key(self) -> Option<KeyCode> { |
| 67 |
KEYBINDS |
| 68 |
.iter() |
| 69 |
.find(|(_, action)| *action == self) |
| 70 |
.map(|(key, _)| *key) |
| 71 |
} |
| 72 |
|
| 73 |
pub fn key_label(self) -> String { |
| 74 |
match self.key() { |
| 75 |
Some(KeyCode::Space) => "Space".into(), |
| 76 |
Some(KeyCode::Escape) => "Esc".into(), |
| 77 |
Some(KeyCode::Equal) => "=".into(), |
| 78 |
Some(KeyCode::Minus) => "-".into(), |
| 79 |
Some(key) => format!("{key:?}") |
| 80 |
.trim_start_matches("Digit") |
| 81 |
.trim_start_matches("Key") |
| 82 |
.to_string(), |
| 83 |
None => String::new(), |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
fn keyboard_actions(keys: Res<ButtonInput<KeyCode>>, mut actions: MessageWriter<Action>) { |
| 89 |
for (key, action) in KEYBINDS { |
| 90 |
if keys.just_pressed(key) { |
| 91 |
actions.write(action); |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
#[cfg(test)] |
| 97 |
mod tests { |
| 98 |
use super::*; |
| 99 |
|
| 100 |
#[test] |
| 101 |
fn pressing_a_bound_key_triggers_its_action() { |
| 102 |
let mut app = App::new(); |
| 103 |
app.init_resource::<ButtonInput<KeyCode>>() |
| 104 |
.add_message::<Action>() |
| 105 |
.add_systems(Update, keyboard_actions); |
| 106 |
app.world_mut() |
| 107 |
.resource_mut::<ButtonInput<KeyCode>>() |
| 108 |
.press(KeyCode::Digit3); |
| 109 |
app.update(); |
| 110 |
let actions: Vec<Action> = app |
| 111 |
.world_mut() |
| 112 |
.resource_mut::<Messages<Action>>() |
| 113 |
.drain() |
| 114 |
.collect(); |
| 115 |
assert_eq!(actions, vec![Action::SetSpeed(SimSpeed::Fastest)]); |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn every_action_has_exactly_one_key() { |
| 120 |
for (_, action) in KEYBINDS { |
| 121 |
let bound = KEYBINDS.iter().filter(|(_, a)| *a == action).count(); |
| 122 |
assert_eq!(bound, 1, "{action:?}"); |
| 123 |
} |
| 124 |
let mut keys: Vec<_> = KEYBINDS.iter().map(|(k, _)| format!("{k:?}")).collect(); |
| 125 |
keys.sort(); |
| 126 |
keys.dedup(); |
| 127 |
assert_eq!(keys.len(), KEYBINDS.len()); |
| 128 |
} |
| 129 |
|
| 130 |
#[test] |
| 131 |
fn key_labels_are_short() { |
| 132 |
assert_eq!(Action::SetSpeed(SimSpeed::Fast).key_label(), "2"); |
| 133 |
assert_eq!(Action::ToggleStockpileTool.key_label(), "Z"); |
| 134 |
assert_eq!(Action::Cancel.key_label(), "Esc"); |
| 135 |
} |
| 136 |
} |