shuyi_ui/screens/layout_picker/view.rs
1//! What the layout picker shows, as plain data: no cells, glyphs, colours
2//! or positions. The screen builds a [`LayoutPickerView`] each frame
3//! ([`LayoutPickerScreen::view`](super::LayoutPickerScreen::view)) and a
4//! skin paints it ([`super::glyph::paint`] today).
5
6use crate::prelude::*;
7use shuyi_assets::keymap::{Key, Layout};
8
9/// The whole screen, as it is this frame.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub struct LayoutPickerView {
12 /// The screen's title.
13 pub title: String,
14 /// One entry per layout, in the order they are offered.
15 pub layouts: Vec<LayoutView>,
16 /// The help line: what the keys do here.
17 pub help: String,
18}
19
20impl LayoutPickerView {
21 /// The layout the cursor is on.
22 pub fn focused(&self) -> Option<&LayoutView> {
23 self.layouts.iter().find(|l| l.focused)
24 }
25}
26
27/// One layout, with its keyboard and what its keys do.
28#[derive(Debug, Clone, PartialEq, Eq)]
29pub struct LayoutView {
30 /// Which layout it is.
31 pub layout: Layout,
32 /// Its name.
33 pub name: String,
34 /// Whether the cursor is on it.
35 pub focused: bool,
36 /// What its keys do, one line per thing the player does.
37 pub legend: Vec<LegendRow>,
38 /// Every key of the keyboard with its role in this layout. A skin draws
39 /// the keys its picture has and where it puts them.
40 pub keys: Vec<KeyCapView>,
41}
42
43impl LayoutView {
44 /// The role of `key` in this layout ([`KeyRole::Unbound`] for a key
45 /// the view doesn't list).
46 pub fn role(&self, key: Key) -> KeyRole {
47 let cap = self.keys.iter().find(|cap| cap.key == key);
48 cap.map_or(KeyRole::Unbound, |cap| cap.role)
49 }
50}
51
52/// One line of a legend: the keys, and what they do.
53#[derive(Debug, Clone, PartialEq, Eq)]
54pub struct LegendRow {
55 /// The keys' names (`! not mapped` if there are none).
56 pub keys: String,
57 /// What they do.
58 pub what: String,
59 /// Whether they move the cursor (a skin sets those apart).
60 pub movement: bool,
61}
62
63/// One key of the keyboard and what it does in a layout.
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub struct KeyCapView {
66 /// The key.
67 pub key: Key,
68 /// What it does.
69 pub role: KeyRole,
70}
71
72/// What a key does in a layout.
73#[derive(Debug, Clone, Copy, PartialEq, Eq)]
74pub enum KeyRole {
75 /// Moves the cursor (a key that repeats while held).
76 Movement,
77 /// Does something else.
78 Bound,
79 /// Does nothing.
80 Unbound,
81}