mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 12:46:42 +01:00
54 lines
1.7 KiB
Rust
54 lines
1.7 KiB
Rust
use crate::*;
|
|
|
|
/// Entry point for main loop
|
|
pub trait App<T: Engine> {
|
|
fn run (self, context: T) -> Usually<T>;
|
|
}
|
|
|
|
/// Platform backend.
|
|
pub trait Engine: Send + Sync + Sized {
|
|
/// Input event type
|
|
type Input: Input<Self>;
|
|
/// Result of handling input
|
|
type Handled;
|
|
/// Render target
|
|
type Output: Output<Self>;
|
|
/// Unit of length
|
|
type Unit: Coordinate;
|
|
/// Rectangle without offset
|
|
type Size: Size<Self::Unit> + From<[Self::Unit;2]> + Debug + Copy;
|
|
/// Rectangle with offset
|
|
type Area: Area<Self::Unit> + From<[Self::Unit;4]> + Debug + Copy;
|
|
/// Prepare before run
|
|
fn setup (&mut self) -> Usually<()> { Ok(()) }
|
|
/// True if done
|
|
fn exited (&self) -> bool;
|
|
/// Clean up after run
|
|
fn teardown (&mut self) -> Usually<()> { Ok(()) }
|
|
}
|
|
|
|
/// A UI component that can render itself as a [Render], and [Handle] input.
|
|
pub trait Component<E: Engine>: Render<E> + Handle<E> {}
|
|
|
|
/// Everything that implements [Render] and [Handle] is a [Component].
|
|
impl<E: Engine, C: Render<E> + Handle<E>> Component<E> for C {}
|
|
|
|
/// A component that can exit.
|
|
pub trait Exit: Send {
|
|
fn exited (&self) -> bool;
|
|
fn exit (&mut self);
|
|
fn boxed (self) -> Box<dyn Exit> where Self: Sized + 'static {
|
|
Box::new(self)
|
|
}
|
|
}
|
|
|
|
/// Marker trait for [Component]s that can [Exit].
|
|
pub trait ExitableComponent<E>: Exit + Component<E> where E: Engine {
|
|
/// Perform type erasure for collecting heterogeneous components.
|
|
fn boxed (self) -> Box<dyn ExitableComponent<E>> where Self: Sized + 'static {
|
|
Box::new(self)
|
|
}
|
|
}
|
|
|
|
/// All [Components]s that implement [Exit] implement [ExitableComponent].
|
|
impl<E: Engine, C: Component<E> + Exit> ExitableComponent<E> for C {}
|