Skip to main content

shuyi_ui/
screens.rs

1//! Screens any game can use, each with rows, entries or a legend the game
2//! gives: the [`layout_picker`], the [`key_bindings`] screen, the
3//! [`options`] screen and the player [`profiles`] screen. Each
4//! says what it shows as plain data (a view) and a skin paints it
5//! (`glyph` for glyphs).
6
7pub mod key_bindings;
8pub mod layout_picker;
9pub mod options;
10pub mod profiles;
11
12use crate::prelude::*;
13
14use shuyi_assets::lang::text_width;
15
16pub use crate::frame::draw::cut;
17
18use crate::ctx::{Ctx, GameData};
19use crate::frame::color::Rgb;
20use crate::frame::glyph_buffer::GlyphBuffer;
21use crate::input::fixed;
22
23/// The text key of a text box's help line, with `{done}`, `{delete}` and
24/// `{cancel}`: the text box's fixed keys.
25pub const TEXT_BOX_HELP: &str = "text_box.help";
26
27/// The help line of a text box: `Enter done · Backspace delete · Escape
28/// cancel`, in `ctx`'s language.
29pub fn text_keys_help<G: GameData>(ctx: &Ctx<G>) -> String {
30    let [done, delete, cancel] = fixed::text_key_names();
31    ctx.text_with(
32        TEXT_BOX_HELP,
33        &[("done", &done), ("delete", &delete), ("cancel", &cancel)],
34    )
35}
36
37/// Column at which `width` cells are centred in `buf` (rounded left).
38pub fn centre_x(buf: &GlyphBuffer, width: usize) -> i32 {
39    let width = i32::try_from(width).unwrap_or(i32::MAX);
40    (i32::from(buf.width()) - width) / 2
41}
42
43/// Prints `text` centred on row `y`. A text wider than `buf` starts at
44/// column 0, [`cut`] to the buffer's width.
45pub fn print_centred(buf: &mut GlyphBuffer, y: i32, text: &str, fg: Rgb, bg: Rgb) {
46    let shown = cut(text, usize::from(buf.width()), buf.widths());
47    let x = centre_x(buf, text_width(&shown, buf.widths()));
48    buf.print(x, y, &shown, fg, bg);
49}
50
51#[cfg(test)]
52mod tests;