mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
add Input and Output traits to Engine
This commit is contained in:
parent
c2e91fb432
commit
d9535b707f
2 changed files with 35 additions and 18 deletions
|
|
@ -7,21 +7,29 @@ pub trait App<T: Engine> {
|
||||||
|
|
||||||
/// Platform backend.
|
/// Platform backend.
|
||||||
pub trait Engine: Send + Sync + Sized {
|
pub trait Engine: Send + Sync + Sized {
|
||||||
fn setup (&mut self) -> Usually<()> { Ok(()) }
|
type Input: Input<Self>;
|
||||||
fn exited (&self) -> bool;
|
type Handled;
|
||||||
fn teardown (&mut self) -> Usually<()> { Ok(()) }
|
type Output: Output<Self>;
|
||||||
|
|
||||||
/// Unit of distance.
|
|
||||||
type Unit: Number;
|
type Unit: Number;
|
||||||
type Area: Area<Self::Unit> + From<[Self::Unit;4]> + Debug;
|
type Area: Area<Self::Unit> + From<[Self::Unit;4]> + Debug;
|
||||||
type Size: Size<Self::Unit> + From<[Self::Unit;2]> + Debug;
|
type Size: Size<Self::Unit> + From<[Self::Unit;2]> + Debug;
|
||||||
|
|
||||||
type Input;
|
fn setup (&mut self) -> Usually<()> { Ok(()) }
|
||||||
type Handled;
|
fn exited (&self) -> bool;
|
||||||
type Output: RenderTarget<Self>;
|
fn teardown (&mut self) -> Usually<()> { Ok(()) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait RenderTarget<E: Engine> {
|
pub trait Input<E: Engine> {
|
||||||
|
type Event;
|
||||||
|
fn event (&self)
|
||||||
|
-> Self::Event;
|
||||||
|
fn is_done (&self)
|
||||||
|
-> bool;
|
||||||
|
fn done (&self);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Output<E: Engine> {
|
||||||
fn area (&self)
|
fn area (&self)
|
||||||
-> E::Area;
|
-> E::Area;
|
||||||
fn area_mut (&mut self)
|
fn area_mut (&mut self)
|
||||||
|
|
|
||||||
|
|
@ -21,12 +21,12 @@ pub struct Tui {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Engine for Tui {
|
impl Engine for Tui {
|
||||||
type Unit = u16;
|
type Unit = u16;
|
||||||
type Size = [Self::Unit;2];
|
type Size = [Self::Unit;2];
|
||||||
type Area = [Self::Unit;4];
|
type Area = [Self::Unit;4];
|
||||||
type Input = Self;
|
type Input = Self;
|
||||||
type Handled = bool;
|
type Handled = bool;
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
fn exited (&self) -> bool {
|
fn exited (&self) -> bool {
|
||||||
self.exited.fetch_and(true, Ordering::Relaxed)
|
self.exited.fetch_and(true, Ordering::Relaxed)
|
||||||
}
|
}
|
||||||
|
|
@ -48,7 +48,19 @@ impl Engine for Tui {
|
||||||
disable_raw_mode().map_err(Into::into)
|
disable_raw_mode().map_err(Into::into)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl RenderTarget<Tui> for Tui {
|
impl Input<Tui> for Tui {
|
||||||
|
type Event = TuiEvent;
|
||||||
|
fn event (&self) -> TuiEvent {
|
||||||
|
self.event.read().unwrap().clone().unwrap()
|
||||||
|
}
|
||||||
|
fn is_done (&self) -> bool {
|
||||||
|
self.exited.fetch_and(true, Ordering::Relaxed)
|
||||||
|
}
|
||||||
|
fn done (&self) {
|
||||||
|
self.exited.store(true, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl Output<Tui> for Tui {
|
||||||
#[inline] fn area (&self) -> <Self as Engine>::Area {
|
#[inline] fn area (&self) -> <Self as Engine>::Area {
|
||||||
self.area
|
self.area
|
||||||
}
|
}
|
||||||
|
|
@ -136,9 +148,6 @@ impl Tui {
|
||||||
std::thread::sleep(sleep);
|
std::thread::sleep(sleep);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
pub fn event (&self) -> TuiEvent {
|
|
||||||
self.event.read().unwrap().clone().unwrap()
|
|
||||||
}
|
|
||||||
pub fn buffer (&mut self) -> &mut Buffer {
|
pub fn buffer (&mut self) -> &mut Buffer {
|
||||||
&mut self.buffers[self.buffer]
|
&mut self.buffers[self.buffer]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue