cleanup Engine API and generalize Inset/Outset

This commit is contained in:
🪞👃🪞 2024-09-13 21:27:40 +03:00
parent 4e0eb0c335
commit 0737769232
6 changed files with 118 additions and 91 deletions

View file

@ -1,32 +1,66 @@
use crate::*;
struct TestEngine([u16;4], Vec<Vec<char>>);
impl Engine for TestEngine {
type Unit = u16;
type Size = [Self::Unit;2];
type Area = [Self::Unit;4];
type HandleInput = Self;
type Handled = bool;
fn exited (&self) -> bool {
true
}
fn area (&self) -> Self::Area {
self.0
}
fn area_mut (&mut self) -> &mut Self::Area {
&mut self.0
}
}
#[derive(Copy, Clone)]
struct TestArea(u16, u16);
impl Widget for TestArea {
type Engine = Tui;
type Engine = TestEngine;
fn layout (&self, to: [u16;4]) -> Perhaps<[u16;4]> {
Ok(Some([to[0], to[1], self.0, self.1]))
}
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
self.layout(to.area())
fn render (&self, to: &mut Self::Engine) -> Perhaps<[u16;4]> {
if let Some(layout) = self.layout(to.area())? {
for y in layout.y()..layout.y()+layout.h()-1 {
for x in layout.x()..layout.x()+layout.w()-1 {
to.1[y as usize][x as usize] = '*';
}
}
Ok(Some(layout))
} else {
Ok(None)
}
}
}
#[test]
fn test_0 () -> Usually<()> {
let area: [u16;4] = [0, 0, 10, 10];
let test = TestArea(4, 4);
assert_eq!(test.layout(area)?,
Some([0, 0, 4, 4]));
assert_eq!(Outset::X(1, test).layout(area)?,
Some([0, 0, 6, 4]));
assert_eq!(Align::X(test).layout(area)?,
Some([3, 0, 4, 4]));
assert_eq!(Align::X(Outset::X(1, test)).layout(area)?,
Some([2, 0, 6, 4]));
assert_eq!(Outset::X(1, Align::X(test)).layout(area)?,
Some([2, 0, 6, 4]));
fn test_plus_minus () -> Usually<()> {
let area = [0, 0, 10, 10];
let engine = TestEngine(area, vec![vec![' ';10];10]);
let test = TestArea(4, 4);
assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
assert_eq!(Plus::X(1, test).layout(area)?, Some([1, 0, 4, 4]));
Ok(())
}
#[test]
fn test_outset_align () -> Usually<()> {
let area = [0, 0, 10, 10];
let engine = TestEngine(area, vec![vec![' ';10];10]);
let test = TestArea(4, 4);
assert_eq!(test.layout(area)?, Some([0, 0, 4, 4]));
assert_eq!(Outset::X(1, test).layout(area)?, Some([0, 0, 6, 4]));
assert_eq!(Align::X(test).layout(area)?, Some([3, 0, 4, 4]));
assert_eq!(Align::X(Outset::X(1, test)).layout(area)?, Some([2, 0, 6, 4]));
assert_eq!(Outset::X(1, Align::X(test)).layout(area)?, Some([2, 0, 6, 4]));
Ok(())
}