mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
more built-in centering
This commit is contained in:
parent
8454b95df8
commit
a6a4eb80fd
5 changed files with 33 additions and 17 deletions
|
|
@ -126,6 +126,18 @@ pub trait Area<N: Coordinate> {
|
||||||
#[inline] fn center (&self) -> [N;2] {
|
#[inline] fn center (&self) -> [N;2] {
|
||||||
[self.x() + self.w()/2.into(), self.y() + self.h()/2.into()]
|
[self.x() + self.w()/2.into(), self.y() + self.h()/2.into()]
|
||||||
}
|
}
|
||||||
|
#[inline] fn center_x (&self, n: N) -> [N;4] {
|
||||||
|
let [x, y, w, h] = self.xywh();
|
||||||
|
[x + w / 2.into() - n, y + h / 2.into(), n, 1.into()]
|
||||||
|
}
|
||||||
|
#[inline] fn center_y (&self, m: N) -> [N;4] {
|
||||||
|
let [x, y, w, h] = self.xywh();
|
||||||
|
[x + w / 2.into(), y + h / 2.into() - m, 1.into(), m]
|
||||||
|
}
|
||||||
|
#[inline] fn center_xy (&self, [n, m]: [N;2]) -> [N;4] {
|
||||||
|
let [x, y, w, h] = self.xywh();
|
||||||
|
[x + w / 2.into() - n, y + h / 2.into() - n - m, n, m]
|
||||||
|
}
|
||||||
#[inline] fn centered (&self) -> [N;2] {
|
#[inline] fn centered (&self) -> [N;2] {
|
||||||
[self.x().minus(self.w()/2.into()), self.y().minus(self.h()/2.into())]
|
[self.x().minus(self.w()/2.into()), self.y().minus(self.h()/2.into())]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,10 @@ pub type Usually<T> = Result<T, Box<dyn Error>>;
|
||||||
/// Standard optional result type.
|
/// Standard optional result type.
|
||||||
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
||||||
|
|
||||||
|
#[cfg(test)] #[test] fn test_dimensions () {
|
||||||
|
assert_eq!(Area::center(&[10u16, 10, 20, 20]), [20, 20]);
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)] #[test] fn test_stub_engine () -> Usually<()> {
|
#[cfg(test)] #[test] fn test_stub_engine () -> Usually<()> {
|
||||||
struct TestEngine(bool);
|
struct TestEngine(bool);
|
||||||
struct TestInput(bool);
|
struct TestInput(bool);
|
||||||
|
|
@ -46,11 +50,11 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
||||||
fn area_mut (&mut self) -> &mut [u16;4] {
|
fn area_mut (&mut self) -> &mut [u16;4] {
|
||||||
&mut self.0
|
&mut self.0
|
||||||
}
|
}
|
||||||
fn place (&mut self, _: [u16;4], _: &impl Layout<TestEngine>) {
|
fn place (&mut self, _: [u16;4], _: &impl Content<TestEngine>) {
|
||||||
()
|
()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl Layout<TestEngine> for String {
|
impl Content<TestEngine> for String {
|
||||||
fn render (&self, to: &mut TestOutput) {
|
fn render (&self, to: &mut TestOutput) {
|
||||||
to.area_mut().set_w(self.len() as u16);
|
to.area_mut().set_w(self.len() as u16);
|
||||||
}
|
}
|
||||||
|
|
@ -62,8 +66,8 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
||||||
use crate::tui::*;
|
use crate::tui::*;
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
struct TestComponent(String);
|
struct TestComponent(String);
|
||||||
impl Layout<Tui> for TestComponent {
|
impl Content<Tui> for TestComponent {
|
||||||
fn layout (&self) -> Option<impl Layout<Tui>> {
|
fn content (&self) -> Option<impl Content<Tui>> {
|
||||||
Some(self.0.as_str())
|
Some(self.0.as_str())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -71,10 +71,7 @@ impl TuiOut {
|
||||||
|
|
||||||
impl Content<Tui> for &str {
|
impl Content<Tui> for &str {
|
||||||
fn layout (&self, to: [u16;4]) -> [u16;4] {
|
fn layout (&self, to: [u16;4]) -> [u16;4] {
|
||||||
let w = self.chars().count() as u16;
|
to.center_x(self.chars().count() as u16)
|
||||||
let y = to.y() + to.h() / 2;
|
|
||||||
let x = (to.x() + to.w() / 2).minus(w / 2);
|
|
||||||
[x, y, w, 1]
|
|
||||||
}
|
}
|
||||||
fn render (&self, to: &mut TuiOut) {
|
fn render (&self, to: &mut TuiOut) {
|
||||||
to.blit(self, to.area.x(), to.area.y(), None)
|
to.blit(self, to.area.x(), to.area.y(), None)
|
||||||
|
|
@ -83,10 +80,7 @@ impl Content<Tui> for &str {
|
||||||
|
|
||||||
impl Content<Tui> for String {
|
impl Content<Tui> for String {
|
||||||
fn layout (&self, to: [u16;4]) -> [u16;4] {
|
fn layout (&self, to: [u16;4]) -> [u16;4] {
|
||||||
let w = self.chars().count() as u16;
|
to.center_x(self.chars().count() as u16)
|
||||||
let y = to.y() + to.h() / 2;
|
|
||||||
let x = (to.x() + to.w() / 2).minus(w / 2);
|
|
||||||
[x, y, w, 1]
|
|
||||||
}
|
}
|
||||||
fn render (&self, to: &mut TuiOut) {
|
fn render (&self, to: &mut TuiOut) {
|
||||||
to.blit(self, to.area.x(), to.area.y(), None)
|
to.blit(self, to.area.x(), to.area.y(), None)
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ impl Direction {
|
||||||
Self::East => Self::South,
|
Self::East => Self::South,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Return next direction counterclockwise
|
/// Return next direction counterclockwise01.
|
||||||
pub fn ccw (&self) -> Self {
|
pub fn ccw (&self) -> Self {
|
||||||
match self {
|
match self {
|
||||||
Self::North => Self::West,
|
Self::North => Self::West,
|
||||||
|
|
|
||||||
|
|
@ -14,12 +14,18 @@ pub(crate) use std::marker::PhantomData;
|
||||||
|
|
||||||
#[cfg(test)] #[test] fn test_layout () -> Usually<()> {
|
#[cfg(test)] #[test] fn test_layout () -> Usually<()> {
|
||||||
use crate::tui::Tui;
|
use crate::tui::Tui;
|
||||||
|
|
||||||
let area: [u16;4] = [10, 10, 20, 20];
|
let area: [u16;4] = [10, 10, 20, 20];
|
||||||
let unit = ();
|
let unit = ();
|
||||||
// should be independent over E, isn't
|
|
||||||
assert_eq!(Content::<Tui>::layout(&unit, area), [15, 15, 0, 0]);
|
assert_eq!(Content::<Tui>::layout(&unit, area), [20, 20, 0, 0]);
|
||||||
assert_eq!(Fill::<Tui, _>::x(unit).layout(area), [10, 15, 20, 0]);
|
|
||||||
assert_eq!(Fill::<Tui, _>::y(unit).layout(area), [15, 10, 0, 20]);
|
assert_eq!(Fill::<Tui, _>::x(unit).layout(area), [10, 20, 20, 0]);
|
||||||
|
assert_eq!(Fill::<Tui, _>::y(unit).layout(area), [20, 10, 0, 20]);
|
||||||
assert_eq!(Fill::<Tui, _>::xy(unit).layout(area), area);
|
assert_eq!(Fill::<Tui, _>::xy(unit).layout(area), area);
|
||||||
|
|
||||||
|
assert_eq!(Align::<Tui, _>::c(unit).layout(area), [20, 20, 0, 0]);
|
||||||
|
assert_eq!(Align::<Tui, _>::c(" ").layout(area), [20, 20, 0, 0]);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue