Rect -> [u16;4] in core

This commit is contained in:
🪞👃🪞 2024-09-07 12:40:27 +03:00
parent 4b92465073
commit fa739a49b2
7 changed files with 86 additions and 112 deletions

View file

@ -1,5 +1,15 @@
use crate::*; use crate::*;
// TODO: Convert to component
// pub enum Align { Center, NW, N, NE, E, SE, S, SW, W, }
pub fn center_box (area: Rect, w: u16, h: u16) -> Rect {
let width = w.min(area.width * 3 / 5);
let height = h.min(area.width * 3 / 5);
let x = area.x + (area.width - width) / 2;
let y = area.y + (area.height - height) / 2;
Rect { x, y, width, height }
}
/// Trait for structs that compute drawing area before rendering /// Trait for structs that compute drawing area before rendering
pub trait Layout<E: Engine>: Render<E> { pub trait Layout<E: Engine>: Render<E> {
fn layout (&self, area: E::Area) -> Perhaps<E::Area>; fn layout (&self, area: E::Area) -> Perhaps<E::Area>;

View file

@ -3,30 +3,18 @@ use crate::*;
pub trait Point<N: Number> { pub trait Point<N: Number> {
fn x (&self) -> N; fn x (&self) -> N;
fn y (&self) -> N; fn y (&self) -> N;
fn w (&self) -> N { fn w (&self) -> N { self.x() }
self.x() fn h (&self) -> N { self.y() }
}
fn h (&self) -> N {
self.y()
}
} }
impl<N: Number> Point<N> for (N, N) { impl<N: Number> Point<N> for (N, N) {
fn x (&self) -> N { fn x (&self) -> N { self.0 }
self.0 fn y (&self) -> N { self.1 }
}
fn y (&self) -> N {
self.1
}
} }
impl<N: Number> Point<N> for [N;2] { impl<N: Number> Point<N> for [N;2] {
fn x (&self) -> N { fn x (&self) -> N { self[0] }
self[0] fn y (&self) -> N { self[1] }
}
fn y (&self) -> N {
self[1]
}
} }
pub trait Area<N: Number> { pub trait Area<N: Number> {
@ -34,42 +22,28 @@ pub trait Area<N: Number> {
fn y (&self) -> N; fn y (&self) -> N;
fn w (&self) -> N; fn w (&self) -> N;
fn h (&self) -> N; fn h (&self) -> N;
fn x2 (&self) -> N { fn x2 (&self) -> N { self.x() + self.w() }
self.x() + self.w() fn y2 (&self) -> N { self.y() + self.h() }
fn xywh (&self) -> [N;4] {
[self.x(), self.y(), self.w(), self.h()]
} }
fn y2 (&self) -> N { fn lrtb (&self) -> [N;4] {
self.y() + self.h() [self.x(), self.x2(), self.y(), self.y2()]
} }
} }
impl<N: Number> Area<N> for (N, N, N, N) { impl<N: Number> Area<N> for (N, N, N, N) {
fn x (&self) -> N { fn x (&self) -> N { self.0 }
self.0 fn y (&self) -> N { self.1 }
} fn w (&self) -> N { self.2 }
fn y (&self) -> N { fn h (&self) -> N { self.3 }
self.1
}
fn w (&self) -> N {
self.2
}
fn h (&self) -> N {
self.3
}
} }
impl<N: Number> Area<N> for [N;4] { impl<N: Number> Area<N> for [N;4] {
fn x (&self) -> N { fn x (&self) -> N { self[0] }
self[0] fn y (&self) -> N { self[1] }
} fn w (&self) -> N { self[2] }
fn y (&self) -> N { fn h (&self) -> N { self[3] }
self[1]
}
fn w (&self) -> N {
self[2]
}
fn h (&self) -> N {
self[3]
}
} }
macro_rules! impl_axis_common { ($A:ident $T:ty) => { macro_rules! impl_axis_common { ($A:ident $T:ty) => {

View file

@ -19,16 +19,16 @@ pub struct Tui {
buffers: [Buffer;2], buffers: [Buffer;2],
backend: CrosstermBackend<Stdout>, backend: CrosstermBackend<Stdout>,
event: RwLock<Option<TuiEvent>>, event: RwLock<Option<TuiEvent>>,
area: Rect, area: [u16;4],
} }
impl Engine for Tui { impl Engine for Tui {
type Unit = u16; type Unit = u16;
type Area = Rect; type Area = [Self::Unit;4];
type HandleInput = Self; type HandleInput = Self;
type Handled = bool; type Handled = bool;
type RenderInput = Self; type RenderInput = Self;
type Rendered = Rect; type Rendered = Self::Area;
fn exited (&self) -> bool { fn exited (&self) -> bool {
self.exited.fetch_and(true, Ordering::Relaxed) self.exited.fetch_and(true, Ordering::Relaxed)
} }
@ -54,7 +54,7 @@ impl Engine for Tui {
} }
#[inline] #[inline]
fn with_area (&mut self, x: u16, y: u16, w: u16, h: u16) -> &mut Self { fn with_area (&mut self, x: u16, y: u16, w: u16, h: u16) -> &mut Self {
self.with_rect(Rect { x, y, width: w, height: h }); self.with_rect([x, y, w, h]);
self self
} }
} }
@ -71,7 +71,7 @@ impl Tui {
buffer: 0, buffer: 0,
buffers: [Buffer::empty(area), Buffer::empty(area)], buffers: [Buffer::empty(area), Buffer::empty(area)],
backend, backend,
area, area: area.xywh(),
}; };
engine.setup()?; engine.setup()?;
let engine = Arc::new(RwLock::new(engine)); let engine = Arc::new(RwLock::new(engine));
@ -109,7 +109,7 @@ impl Tui {
if engine.exited() { if engine.exited() {
break break
} }
engine.area = engine.backend.size().expect("get size failed"); engine.area = engine.backend.size().expect("get size failed").xywh();
state.render(&mut engine).expect("render failed"); state.render(&mut engine).expect("render failed");
engine.flip(); engine.flip();
} }
@ -134,22 +134,22 @@ impl Tui {
self.buffers[1 - self.buffer].reset(); self.buffers[1 - self.buffer].reset();
self.buffer = 1 - self.buffer; self.buffer = 1 - self.buffer;
} }
pub fn buffer_update (&mut self, area: Rect, callback: &impl Fn(&mut Cell, u16, u16)) { pub fn buffer_update (&mut self, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) {
buffer_update(self.buffer(), area, callback) buffer_update(self.buffer(), area, callback)
} }
pub fn fill_bg (&mut self, area: Rect, color: Color) { pub fn fill_bg (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{cell.set_bg(color);}) self.buffer_update(area, &|cell,_,_|{cell.set_bg(color);})
} }
pub fn fill_fg (&mut self, area: Rect, color: Color) { pub fn fill_fg (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{cell.set_fg(color);}) self.buffer_update(area, &|cell,_,_|{cell.set_fg(color);})
} }
pub fn fill_ul (&mut self, area: Rect, color: Color) { pub fn fill_ul (&mut self, area: [u16;4], color: Color) {
self.buffer_update(area, &|cell,_,_|{ self.buffer_update(area, &|cell,_,_|{
cell.modifier = ratatui::prelude::Modifier::UNDERLINED; cell.modifier = ratatui::prelude::Modifier::UNDERLINED;
cell.underline_color = color; cell.underline_color = color;
}) })
} }
pub fn fill_char (&mut self, area: Rect, c: char) { pub fn fill_char (&mut self, area: [u16;4], c: char) {
self.buffer_update(area, &|cell,_,_|{cell.set_char(c);}) self.buffer_update(area, &|cell,_,_|{cell.set_char(c);})
} }
pub fn make_dim (&mut self) { pub fn make_dim (&mut self) {
@ -161,25 +161,23 @@ impl Tui {
} }
pub fn blit ( pub fn blit (
&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style> &mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>
) -> Perhaps<Rect> { ) -> Perhaps<[u16;4]> {
let text = text.as_ref(); let text = text.as_ref();
let buf = self.buffer(); let buf = self.buffer();
if x < buf.area.width && y < buf.area.height { if x < buf.area.width && y < buf.area.height {
buf.set_string(x, y, text, style.unwrap_or(Style::default())); buf.set_string(x, y, text, style.unwrap_or(Style::default()));
} }
Ok(Some(Rect { x, y, width: text.len() as u16, height: 1 })) Ok(Some([x, y, text.len() as u16, 1]))
} }
#[inline] #[inline]
pub fn alter_area ( pub fn alter_area (
&mut self, alter: impl Fn(u16, u16, u16, u16)->(u16, u16, u16, u16) &mut self, alter: impl Fn([u16;4])->[u16;4]
) -> &mut Self { ) -> &mut Self {
let (x, y, width, height) = alter( let [x, y, w, h] = alter(self.area.xywh());
self.area.x, self.area.y, self.area.width, self.area.height self.with_area(x, y, w, h)
);
self.with_area(x, y, width, height)
} }
#[inline] #[inline]
pub fn with_rect (&mut self, area: Rect) -> &mut Self { pub fn with_rect (&mut self, area: [u16;4]) -> &mut Self {
self.area = area; self.area = area;
self self
} }
@ -200,27 +198,10 @@ pub enum TuiEvent {
// Jack(JackEvent) // Jack(JackEvent)
} }
/// Rendering unit struct to Ratatui returns zero-sized [Rect] at render coordinates. /// Rendering unit struct to Ratatui returns zero-sized [Area] at render coordinates.
impl Render<Tui> for () { impl Render<Tui> for () {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> { fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
Ok(Some(Rect { x: to.area.x, y: to.area.y, width: 0, height: 0 })) Ok(Some([to.area.x(), to.area.y(), 0, 0]))
}
}
pub fn center_box (area: Rect, w: u16, h: u16) -> Rect {
let width = w.min(area.width * 3 / 5);
let height = h.min(area.width * 3 / 5);
let x = area.x + (area.width - width) / 2;
let y = area.y + (area.height - height) / 2;
Rect { x, y, width, height }
}
pub fn half_block (lower: bool, upper: bool) -> Option<char> {
match (lower, upper) {
(true, true) => Some('█'),
(true, false) => Some('▄'),
(false, true) => Some('▀'),
_ => None
} }
} }
@ -231,3 +212,11 @@ impl Area<u16> for Rect {
fn h (&self) -> u16 { self.height } fn h (&self) -> u16 { self.height }
} }
pub fn half_block (lower: bool, upper: bool) -> Option<char> {
match (lower, upper) {
(true, true) => Some('█'),
(true, false) => Some('▄'),
(false, true) => Some('▀'),
_ => None
}
}

View file

@ -11,7 +11,7 @@ pub trait BorderStyle {
const W: &'static str = ""; const W: &'static str = "";
#[inline] #[inline]
fn draw <'a> (&self, to: &mut Tui) -> Perhaps<Rect> { fn draw <'a> (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
self.draw_horizontal(to, None)?; self.draw_horizontal(to, None)?;
self.draw_vertical(to, None)?; self.draw_vertical(to, None)?;
self.draw_corners(to, None)?; self.draw_corners(to, None)?;
@ -19,41 +19,42 @@ pub trait BorderStyle {
} }
#[inline] #[inline]
fn draw_horizontal (&self, to: &mut Tui, style: Option<Style>) -> Usually<Rect> { fn draw_horizontal (&self, to: &mut Tui, style: Option<Style>) -> Usually<[u16;4]> {
let area = to.area(); let area = to.area();
let style = style.or_else(||self.style_horizontal()); let style = style.or_else(||self.style_horizontal());
for x in area.x..(area.x+area.width).saturating_sub(1) { let [x, x2, y, y2] = area.lrtb();
self.draw_north(to, x, area.y, style)?; for x in x..x2.saturating_sub(1) {
self.draw_south(to, x, (area.y + area.height).saturating_sub(1), style)?; self.draw_north(to, x, y, style)?;
self.draw_south(to, x, y2.saturating_sub(1), style)?;
} }
Ok(area) Ok(area)
} }
#[inline] #[inline]
fn draw_north (&self, to: &mut Tui, x: u16, y: u16, style: Option<Style>) -> Perhaps<Rect> { fn draw_north (&self, to: &mut Tui, x: u16, y: u16, style: Option<Style>) -> Perhaps<[u16;4]> {
to.blit(&Self::N, x, y, style) to.blit(&Self::N, x, y, style)
} }
#[inline] #[inline]
fn draw_south (&self, to: &mut Tui, x: u16, y: u16, style: Option<Style>) -> Perhaps<Rect> { fn draw_south (&self, to: &mut Tui, x: u16, y: u16, style: Option<Style>) -> Perhaps<[u16;4]> {
to.blit(&Self::S, x, y, style) to.blit(&Self::S, x, y, style)
} }
#[inline] #[inline]
fn draw_vertical (&self, to: &mut Tui, style: Option<Style>) -> Usually<Rect> { fn draw_vertical (&self, to: &mut Tui, style: Option<Style>) -> Usually<[u16;4]> {
let area = to.area(); let area = to.area();
let style = style.or_else(||self.style_vertical()); let style = style.or_else(||self.style_vertical());
let Rect { x, y, width, height } = area; let [x, x2, y, y2] = area.lrtb();
for y in y..(y+height).saturating_sub(1) { for y in y..y2.saturating_sub(1) {
to.blit(&Self::W, x, y, style)?; to.blit(&Self::W, x, y, style)?;
to.blit(&Self::E, x + width - 1, y, style)?; to.blit(&Self::E, x2.saturating_sub(1), y, style)?;
} }
Ok(area) Ok(area)
} }
#[inline] #[inline]
fn draw_corners (&self, to: &mut Tui, style: Option<Style>) -> Usually<Rect> { fn draw_corners (&self, to: &mut Tui, style: Option<Style>) -> Usually<[u16;4]> {
let area = to.area(); let area = to.area();
let style = style.or_else(||self.style_corners()); let style = style.or_else(||self.style_corners());
let Rect { x, y, width, height } = area; let [x, y, width, height] = area.xywh();
if width > 0 && height > 0 { if width > 0 && height > 0 {
to.blit(&Self::NW, x, y, style)?; to.blit(&Self::NW, x, y, style)?;
to.blit(&Self::NE, x + width - 1, y, style)?; to.blit(&Self::NE, x + width - 1, y, style)?;

View file

@ -24,11 +24,11 @@ impl BigBuffer {
} }
} }
pub fn buffer_update (buf: &mut Buffer, area: Rect, callback: &impl Fn(&mut Cell, u16, u16)) { pub fn buffer_update (buf: &mut Buffer, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) {
for row in 0..area.height { for row in 0..area.h() {
let y = area.y + row; let y = area.y() + row;
for col in 0..area.width { for col in 0..area.w() {
let x = area.x + col; let x = area.x() + col;
if x < buf.area.width && y < buf.area.height { if x < buf.area.width && y < buf.area.height {
callback(buf.get_mut(x, y), col, row); callback(buf.get_mut(x, y), col, row);
} }

View file

@ -3,8 +3,8 @@ use crate::*;
pub struct FillBg(pub Color); pub struct FillBg(pub Color);
impl Render<Tui> for FillBg { impl Render<Tui> for FillBg {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> { fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
to.fill_bg(to.area, self.0); to.fill_bg(to.area(), self.0);
Ok(Some(to.area)) Ok(Some(to.area))
} }
} }

View file

@ -1,7 +1,7 @@
use crate::*; use crate::*;
impl<'a> Render<Tui> for Layered<'a, Tui> { impl<'a> Render<Tui> for Layered<'a, Tui> {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> { fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
let area = to.area(); let area = to.area();
for layer in self.0.0.iter() { for layer in self.0.0.iter() {
layer.render(to)?; layer.render(to)?;
@ -11,14 +11,14 @@ impl<'a> Render<Tui> for Layered<'a, Tui> {
} }
impl<'a> Render<Tui> for Split<'a, Tui> { impl<'a> Render<Tui> for Split<'a, Tui> {
fn render (&self, to: &mut Tui) -> Perhaps<Rect> { fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
Ok(Some(self.render_areas(to)?.0)) Ok(Some(self.render_areas(to)?.0))
} }
} }
// TODO // TODO
impl<'a> Split<'a, Tui> { impl<'a> Split<'a, Tui> {
pub fn render_areas (&self, to: &mut Tui) -> Usually<(Rect, Vec<Option<Rect>>)> { pub fn render_areas (&self, to: &mut Tui) -> Usually<([u16;4], Vec<Option<[u16;4]>>)> {
let area = to.area(); let area = to.area();
let mut w = 0u16; let mut w = 0u16;
let mut h = 0u16; let mut h = 0u16;
@ -31,12 +31,12 @@ impl<'a> Split<'a, Tui> {
} }
let result = Offset::Y(h, component).render(to)?; let result = Offset::Y(h, component).render(to)?;
areas.push(result); areas.push(result);
if let Some(Rect { width, height, .. }) = result { if let Some([_, _, width, height]) = result {
h += height; h += height;
w = w.max(width) w = w.max(width)
} }
} }
Rect { x: area.x(), y: area.y(), width: w, height: h } [area.x(), area.y(), w, h]
}, },
Direction::Right => { Direction::Right => {
for component in self.items.0.iter() { for component in self.items.0.iter() {
@ -45,12 +45,12 @@ impl<'a> Split<'a, Tui> {
} }
let result = Offset::X(w, component).render(to)?; let result = Offset::X(w, component).render(to)?;
areas.push(result); areas.push(result);
if let Some(Rect { width, height, .. }) = result { if let Some([_, _, width, height]) = result {
w += width; w += width;
h = h.max(height) h = h.max(height)
} }
} }
Rect { x: area.x(), y: area.y(), width: w, height: h } [area.x(), area.y(), w, h]
}, },
_ => todo!() _ => todo!()
}, areas)) }, areas))