shuyi_ui/input/fixed.rs
1//! The keys no player can rebind (`docs/design/controls.md`, *Rebinding
2//! keys*; game ADR-0031): plain `Escape` backs out of the Key bindings screen's
3//! "Press a key…" and plain `Delete` empties a slot there, so neither can
4//! be bound ([`Chord::is_reserved`]); and a text box's own `Enter`,
5//! `Backspace` and `Escape`. Which action a fixed key also triggers in
6//! play is the game's data ([`ActionSet::fixed_keys`]).
7//!
8//! Generic over the game (ticket 1106).
9//!
10//! [`ActionSet::fixed_keys`]: shuyi_assets::keymap::ActionSet::fixed_keys
11
12use crate::prelude::*;
13use shuyi_assets::keymap::{Chord, Key};
14
15/// The chord that backs out of the Key bindings screen's "Press a key…"
16/// without changing anything.
17const CAPTURE_ABORT: Chord = Chord::plain(Key::Escape);
18
19/// The chord that empties the highlighted slot on the Key bindings screen.
20const CLEAR_SLOT: Chord = Chord::plain(Key::Delete);
21
22/// Whether `chord` backs out of the Key bindings screen's "Press a key…"
23/// (`docs/design/controls.md`, *Rebinding keys*). The screen asks this
24/// rather than naming the key (the `keyboard-input` skill).
25pub fn is_capture_abort(chord: Chord) -> bool {
26 chord == CAPTURE_ABORT
27}
28
29/// Whether `chord` empties the highlighted slot on the Key bindings screen.
30pub fn is_clear_slot(chord: Chord) -> bool {
31 chord == CLEAR_SLOT
32}
33
34/// The name of the [`is_capture_abort`] key, for help text.
35pub fn capture_abort_key_name() -> String {
36 CAPTURE_ABORT.to_string()
37}
38
39/// The name of the [`is_clear_slot`] key, for help text.
40pub fn clear_slot_key_name() -> String {
41 CLEAR_SLOT.to_string()
42}
43
44/// A text box's own keys (the lead's name, 0801): fixed like `Escape` for
45/// Cancel, since while a player types, every letter key is a letter.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum TextKey {
48 /// `Enter`: keep the text.
49 Done,
50 /// `Backspace`: delete the last character.
51 Delete,
52 /// `Escape`: close the box, text unchanged.
53 Cancel,
54}
55
56/// The text-box key `chord` is, if any (Shift or not).
57pub fn text_key(chord: Chord) -> Option<TextKey> {
58 match chord.key {
59 Key::Enter => Some(TextKey::Done),
60 Key::Backspace => Some(TextKey::Delete),
61 Key::Escape => Some(TextKey::Cancel),
62 _ => None,
63 }
64}
65
66/// The names of the text-box keys, for its help line: done, delete and
67/// cancel.
68pub fn text_key_names() -> [String; 3] {
69 [Key::Enter, Key::Backspace, Key::Escape].map(|key| Chord::plain(key).to_string())
70}