refactor engine and layout into input and output

This commit is contained in:
🪞👃🪞 2025-01-07 21:30:07 +01:00
parent f052891473
commit 4d0f98acd2
40 changed files with 104 additions and 109 deletions

View file

@ -1,80 +0,0 @@
#![feature(associated_type_defaults)]
//mod component; pub use self::component::*;
mod engine; pub use self::engine::*;
mod handle; pub use self::handle::*;
mod command; pub use self::command::*;
mod event_map; pub use self::event_map::*;
mod coordinate; pub use self::coordinate::*;
mod size; pub use self::size::*;
mod area; pub use self::area::*;
mod render; pub use self::render::*;
mod content; pub use self::content::*;
mod thunk; pub use self::thunk::*;
pub use std::error::Error;
/// Standard result type.
pub type Usually<T> = Result<T, Box<dyn Error>>;
/// Standard optional result type.
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
/// Prototypal case of implementor macro.
/// Saves 4loc per data pats.
#[macro_export] macro_rules! from {
($(<$($lt:lifetime),+>)?|$state:ident:$Source:ty|$Target:ty=$cb:expr) => {
impl $(<$($lt),+>)? From<$Source> for $Target {
fn from ($state:$Source) -> Self { $cb }
}
};
}
#[cfg(test)] #[test] fn test_dimensions () {
assert_eq!(Area::center(&[10u16, 10, 20, 20]), [20, 20]);
}
#[cfg(test)] #[test] fn test_stub_engine () -> Usually<()> {
struct TestEngine(bool);
struct TestInput(bool);
struct TestOutput([u16;4]);
enum TestEvent { Test1 }
impl Engine for TestEngine {
type Input = TestInput;
type Handled = ();
type Output = TestOutput;
type Unit = u16;
type Size = [u16;2];
type Area = [u16;4];
fn exited (&self) -> bool {
self.0
}
}
impl Input<TestEngine> for TestInput {
type Event = TestEvent;
fn event (&self) -> &Self::Event {
&TestEvent::Test1
}
fn is_done (&self) -> bool {
self.0
}
fn done (&self) {}
}
impl Output<TestEngine> for TestOutput {
fn area (&self) -> [u16;4] {
self.0
}
fn area_mut (&mut self) -> &mut [u16;4] {
&mut self.0
}
fn place (&mut self, _: [u16;4], _: &impl Render<TestEngine>) {
()
}
}
impl Content<TestEngine> for String {
fn render (&self, to: &mut TestOutput) {
to.area_mut().set_w(self.len() as u16);
}
}
Ok(())
}