mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-02-21 18:49:04 +01:00
This commit is contained in:
parent
b7b1055fbc
commit
4fa5d74fa2
26 changed files with 1550 additions and 1548 deletions
124
tui/src/tui_structs.rs
Normal file
124
tui/src/tui_structs.rs
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
use crate::*;
|
||||
|
||||
/// The `Tui` struct (the *engine*) implements the
|
||||
/// `tengri_input::Input` and `tengri_output::Out` traits.
|
||||
/// At launch, the `Tui` engine spawns two threads, the render thread and the input thread.
|
||||
/// the application may further spawn other threads. All threads communicate using shared ownership:
|
||||
/// `Arc<RwLock<T>>` and `Arc<AtomicT>`. Thus, at launch the engine and application instances are expected to be wrapped in `Arc<RwLock>`.
|
||||
pub struct Tui {
|
||||
pub exited: Arc<AtomicBool>,
|
||||
pub backend: CrosstermBackend<Stdout>,
|
||||
pub buffer: Buffer,
|
||||
pub area: [u16;4],
|
||||
pub perf: PerfModel,
|
||||
}
|
||||
|
||||
#[derive(Default)] pub struct BigBuffer {
|
||||
pub width: usize,
|
||||
pub height: usize,
|
||||
pub content: Vec<Cell>
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq, PartialOrd)] pub struct TuiEvent(pub Event);
|
||||
|
||||
pub struct TuiKey(Option<KeyCode>, KeyModifiers);
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TuiIn {
|
||||
/// Exit flag
|
||||
pub exited: Arc<AtomicBool>,
|
||||
/// Input event
|
||||
pub event: TuiEvent,
|
||||
}
|
||||
|
||||
/// Performance counter
|
||||
#[derive(Debug)]
|
||||
pub struct PerfModel {
|
||||
pub enabled: bool,
|
||||
pub clock: quanta::Clock,
|
||||
// In nanoseconds. Time used by last iteration.
|
||||
pub used: AtomicF64,
|
||||
// In microseconds. Max prescribed time for iteration (frame, chunk...).
|
||||
pub window: AtomicF64,
|
||||
}
|
||||
/// A color in OKHSL and RGB representations.
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq)] pub struct ItemColor {
|
||||
pub okhsl: Okhsl<f32>,
|
||||
pub rgb: Color,
|
||||
}
|
||||
/// A color in OKHSL and RGB with lighter and darker variants.
|
||||
#[derive(Debug, Default, Copy, Clone, PartialEq)] pub struct ItemTheme {
|
||||
pub base: ItemColor,
|
||||
pub light: ItemColor,
|
||||
pub lighter: ItemColor,
|
||||
pub lightest: ItemColor,
|
||||
pub dark: ItemColor,
|
||||
pub darker: ItemColor,
|
||||
pub darkest: ItemColor,
|
||||
}
|
||||
|
||||
/// A cell that takes up 3 rows on its own,
|
||||
/// but stacks, giving (N+1)*2 rows per N cells.
|
||||
pub struct Phat<T> {
|
||||
pub width: u16,
|
||||
pub height: u16,
|
||||
pub content: T,
|
||||
pub colors: [Color;4],
|
||||
}
|
||||
pub struct Repeat<'a>(pub &'a str);
|
||||
pub struct RepeatV<'a>(pub &'a str);
|
||||
pub struct RepeatH<'a>(pub &'a str);
|
||||
|
||||
pub struct ScrollbarV {
|
||||
pub offset: usize,
|
||||
pub length: usize,
|
||||
pub total: usize,
|
||||
}
|
||||
|
||||
pub struct ScrollbarH {
|
||||
pub offset: usize,
|
||||
pub length: usize,
|
||||
pub total: usize,
|
||||
}
|
||||
/// Displays an owned [str]-like with fixed maximum width.
|
||||
///
|
||||
/// Width is computed using [unicode_width].
|
||||
pub struct TrimString<T: AsRef<str>>(pub u16, pub T);
|
||||
|
||||
/// Displays a borrowed [str]-like with fixed maximum width
|
||||
///
|
||||
/// Width is computed using [unicode_width].
|
||||
pub struct TrimStringRef<'a, T: AsRef<str>>(pub u16, pub &'a T);
|
||||
|
||||
// Thunks can be natural error boundaries!
|
||||
pub struct ErrorBoundary<O: Out, T: Draw<O>>(
|
||||
std::marker::PhantomData<O>, Perhaps<T>
|
||||
);
|
||||
|
||||
pub struct Modify<T>(pub bool, pub Modifier, pub T);
|
||||
|
||||
pub struct Styled<T>(pub Option<Style>, pub T);
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TuiOut {
|
||||
pub buffer: Buffer,
|
||||
pub area: XYWH<u16>,
|
||||
}
|
||||
|
||||
impl Out for TuiOut {
|
||||
type Unit = u16;
|
||||
#[inline] fn area (&self) -> XYWH<u16> {
|
||||
self.area
|
||||
}
|
||||
#[inline] fn area_mut (&mut self) -> &mut XYWH<u16> {
|
||||
&mut self.area
|
||||
}
|
||||
#[inline] fn place_at <'t, T: Draw<Self> + ?Sized> (
|
||||
&mut self, area: XYWH<u16>, content: &'t T
|
||||
) {
|
||||
let last = self.area();
|
||||
*self.area_mut() = area;
|
||||
content.draw(self);
|
||||
*self.area_mut() = last;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue