Skip to content

Commit 2c25474

Browse files
committed
support for context keys in new player_input/key_bindings gui
1 parent 7bc28b6 commit 2c25474

File tree

2 files changed

+71
-32
lines changed

2 files changed

+71
-32
lines changed

src/gui.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use crate::{
1818
display_state::DisplayState,
1919
gamelog::GameLog,
2020
map::Map,
21-
player::{get_player_pos_unwrap, get_player_unwrap, KeyBindings, PLAYER_NAME},
21+
player::{
22+
display_key_combo, get_player_pos_unwrap, get_player_unwrap, KeyBindings, PLAYER_NAME,
23+
},
2224
util::*,
2325
PsnU, RunState, State,
2426
};
@@ -218,9 +220,13 @@ pub fn show_keybindings(gs: &State, ctx: &mut BTerm) -> bool {
218220
.iter()
219221
.map(|(action_id, action_keys)| {
220222
format!(
221-
"{:max_action_name_len$} | {:?}",
223+
"{:max_action_name_len$} | {}",
222224
action_id.to_string(),
223-
action_keys.key_codes,
225+
action_keys
226+
.key_codes
227+
.iter()
228+
.map(display_key_combo)
229+
.join(", "),
224230
)
225231
})
226232
.collect();

src/player.rs

+62-29
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ pub fn try_move_player(delta_x: i32, delta_y: i32, gs: &mut State) -> RunState {
7272
}
7373

7474
macro_attr! {
75-
7675
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, EnumDisplay!)]
7776
pub enum PlayerAction {
7877
ShowInventory,
@@ -92,6 +91,31 @@ macro_attr! {
9291
}
9392
}
9493

94+
macro_attr! {
95+
#[derive(PartialEq, Eq, Clone, Copy, Debug, Hash, EnumDisplay!)]
96+
pub enum ContextKeys {
97+
Shift
98+
}
99+
100+
}
101+
102+
impl ContextKeys {
103+
pub fn display_vec(keys: &Vec<ContextKeys>) -> String {
104+
if keys.is_empty() {
105+
"".to_string()
106+
} else {
107+
" + ".to_owned() + &keys.iter().map(|k| k.to_string()).join(" + ")
108+
}
109+
}
110+
}
111+
112+
type Keys = (VirtualKeyCode, Vec<ContextKeys>);
113+
114+
pub fn display_key_combo(keys: &Keys) -> String {
115+
let (key, context_keys) = keys;
116+
format!("{:?}{}", key, ContextKeys::display_vec(context_keys))
117+
}
118+
95119
pub trait PlayerActionFnT: Fn(&mut State) -> RunState + Send + Sync + 'static {}
96120

97121
impl<F> PlayerActionFnT for F where F: Fn(&mut State) -> RunState + Send + Sync + 'static {}
@@ -106,7 +130,7 @@ impl std::fmt::Debug for dyn PlayerActionFnT {
106130

107131
#[derive(Clone, Debug)]
108132
pub struct ActionAndKeys {
109-
pub key_codes: Vec<VirtualKeyCode>,
133+
pub key_codes: Vec<Keys>,
110134
pub action: PlayerActionFn,
111135
}
112136

@@ -119,7 +143,7 @@ pub struct ActionAndId {
119143
#[derive(Debug)]
120144
pub struct KeyBindings {
121145
pub action_by_id: IndexMap<PlayerAction, ActionAndKeys>,
122-
pub action_by_key: IndexMap<VirtualKeyCode, ActionAndId>,
146+
pub action_by_key: IndexMap<Keys, ActionAndId>,
123147
}
124148

125149
pub static DEFAULT_KEY_BINDINGS: OnceCell<KeyBindings> = OnceCell::new();
@@ -136,21 +160,21 @@ impl KeyBindings {
136160
(
137161
PlayerAction::ShowInventory,
138162
ActionAndKeys {
139-
key_codes: vec![VirtualKeyCode::I],
163+
key_codes: vec![(VirtualKeyCode::I, vec![])],
140164
action: Arc::new(|_| RunState::ShowInventory),
141165
},
142166
),
143167
(
144168
PlayerAction::ShowDropItem,
145169
ActionAndKeys {
146-
key_codes: vec![VirtualKeyCode::D],
170+
key_codes: vec![(VirtualKeyCode::D, vec![ContextKeys::Shift])],
147171
action: Arc::new(|_| RunState::ShowDropItem),
148172
},
149173
),
150174
(
151175
PlayerAction::Escape,
152176
ActionAndKeys {
153-
key_codes: vec![VirtualKeyCode::Escape],
177+
key_codes: vec![(VirtualKeyCode::Escape, vec![])],
154178
action: Arc::new(|_| RunState::MainMenu {
155179
menu_selection: SaveGame,
156180
}),
@@ -159,17 +183,17 @@ impl KeyBindings {
159183
(
160184
PlayerAction::ShowRemoveItem,
161185
ActionAndKeys {
162-
key_codes: vec![VirtualKeyCode::R],
186+
key_codes: vec![(VirtualKeyCode::R, vec![])],
163187
action: Arc::new(|_| RunState::ShowRemoveItem),
164188
},
165189
),
166190
(
167191
PlayerAction::Left,
168192
ActionAndKeys {
169193
key_codes: vec![
170-
VirtualKeyCode::Left,
171-
VirtualKeyCode::A,
172-
VirtualKeyCode::Numpad4,
194+
(VirtualKeyCode::Left, vec![]),
195+
(VirtualKeyCode::A, vec![]),
196+
(VirtualKeyCode::Numpad4, vec![]),
173197
],
174198
action: Arc::new(|gs| try_move_player(-1, 0, gs)),
175199
},
@@ -178,9 +202,9 @@ impl KeyBindings {
178202
PlayerAction::Right,
179203
ActionAndKeys {
180204
key_codes: vec![
181-
VirtualKeyCode::Right,
182-
VirtualKeyCode::D,
183-
VirtualKeyCode::Numpad6,
205+
(VirtualKeyCode::Right, vec![]),
206+
(VirtualKeyCode::D, vec![]),
207+
(VirtualKeyCode::Numpad6, vec![]),
184208
],
185209
action: Arc::new(|gs| try_move_player(1, 0, gs)),
186210
},
@@ -189,9 +213,9 @@ impl KeyBindings {
189213
PlayerAction::Up,
190214
ActionAndKeys {
191215
key_codes: vec![
192-
VirtualKeyCode::Up,
193-
VirtualKeyCode::W,
194-
VirtualKeyCode::Numpad8,
216+
(VirtualKeyCode::Up, vec![]),
217+
(VirtualKeyCode::W, vec![]),
218+
(VirtualKeyCode::Numpad8, vec![]),
195219
],
196220
action: Arc::new(|gs| try_move_player(0, -1, gs)),
197221
},
@@ -200,52 +224,55 @@ impl KeyBindings {
200224
PlayerAction::Down,
201225
ActionAndKeys {
202226
key_codes: vec![
203-
VirtualKeyCode::Down,
204-
VirtualKeyCode::S,
205-
VirtualKeyCode::Numpad2,
227+
(VirtualKeyCode::Down, vec![]),
228+
(VirtualKeyCode::S, vec![]),
229+
(VirtualKeyCode::Numpad2, vec![]),
206230
],
207231
action: Arc::new(|gs| try_move_player(0, 1, gs)),
208232
},
209233
),
210234
(
211235
PlayerAction::UpLeft,
212236
ActionAndKeys {
213-
key_codes: vec![VirtualKeyCode::Numpad7],
237+
key_codes: vec![(VirtualKeyCode::Numpad7, vec![])],
214238
action: Arc::new(|gs| try_move_player(-1, -1, gs)),
215239
},
216240
),
217241
(
218242
PlayerAction::UpRight,
219243
ActionAndKeys {
220-
key_codes: vec![VirtualKeyCode::Numpad9],
244+
key_codes: vec![(VirtualKeyCode::Numpad9, vec![])],
221245
action: Arc::new(|gs| try_move_player(1, -1, gs)),
222246
},
223247
),
224248
(
225249
PlayerAction::DownLeft,
226250
ActionAndKeys {
227-
key_codes: vec![VirtualKeyCode::Numpad1],
251+
key_codes: vec![(VirtualKeyCode::Numpad1, vec![])],
228252
action: Arc::new(|gs| try_move_player(-1, 1, gs)),
229253
},
230254
),
231255
(
232256
PlayerAction::DownRight,
233257
ActionAndKeys {
234-
key_codes: vec![VirtualKeyCode::Numpad3],
258+
key_codes: vec![(VirtualKeyCode::Numpad3, vec![])],
235259
action: Arc::new(|gs| try_move_player(1, 1, gs)),
236260
},
237261
),
238262
(
239263
PlayerAction::Rest,
240264
ActionAndKeys {
241-
key_codes: vec![VirtualKeyCode::Numpad5, VirtualKeyCode::Space],
265+
key_codes: vec![
266+
(VirtualKeyCode::Numpad5, vec![]),
267+
(VirtualKeyCode::Space, vec![]),
268+
],
242269
action: Arc::new(|gs| skip_turn(&mut gs.ecs)),
243270
},
244271
),
245272
(
246273
PlayerAction::Grab,
247274
ActionAndKeys {
248-
key_codes: vec![VirtualKeyCode::G],
275+
key_codes: vec![(VirtualKeyCode::G, vec![])],
249276
action: Arc::new(|gs| interact(&mut gs.ecs)),
250277
},
251278
),
@@ -254,12 +281,12 @@ impl KeyBindings {
254281
.cloned()
255282
.collect();
256283

257-
let action_by_key: IndexMap<VirtualKeyCode, ActionAndId> = action_by_id
284+
let action_by_key: IndexMap<Keys, ActionAndId> = action_by_id
258285
.iter()
259286
.flat_map(|(id, action_and_keys)| {
260287
action_and_keys.key_codes.iter().map(|key_code| {
261288
(
262-
*key_code,
289+
key_code.clone(),
263290
ActionAndId {
264291
id: *id,
265292
action: action_and_keys.action.clone(),
@@ -278,9 +305,15 @@ impl KeyBindings {
278305

279306
pub fn player_input(gs: &mut State, ctx: &mut BTerm) -> RunState {
280307
let key_map = &KeyBindings::default().action_by_key;
281-
match ctx.key {
308+
309+
let mut ctxt_keys = vec![];
310+
if ctx.shift {
311+
ctxt_keys.push(ContextKeys::Shift);
312+
}
313+
let keys_opt = ctx.key.map(|key| (key, ctxt_keys));
314+
match keys_opt {
282315
None => RunState::AwaitingInput,
283-
Some(key) => match key_map.get(&key) {
316+
Some(keys) => match key_map.get(&keys) {
284317
None => RunState::AwaitingInput,
285318
Some(action_and_id) => (action_and_id.action)(gs),
286319
},

0 commit comments

Comments
 (0)