mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
Rect -> [u16;4] in core
This commit is contained in:
parent
4b92465073
commit
fa739a49b2
7 changed files with 86 additions and 112 deletions
|
|
@ -1,5 +1,15 @@
|
|||
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
|
||||
pub trait Layout<E: Engine>: Render<E> {
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area>;
|
||||
|
|
|
|||
|
|
@ -3,30 +3,18 @@ use crate::*;
|
|||
pub trait Point<N: Number> {
|
||||
fn x (&self) -> N;
|
||||
fn y (&self) -> N;
|
||||
fn w (&self) -> N {
|
||||
self.x()
|
||||
}
|
||||
fn h (&self) -> N {
|
||||
self.y()
|
||||
}
|
||||
fn w (&self) -> N { self.x() }
|
||||
fn h (&self) -> N { self.y() }
|
||||
}
|
||||
|
||||
impl<N: Number> Point<N> for (N, N) {
|
||||
fn x (&self) -> N {
|
||||
self.0
|
||||
}
|
||||
fn y (&self) -> N {
|
||||
self.1
|
||||
}
|
||||
fn x (&self) -> N { self.0 }
|
||||
fn y (&self) -> N { self.1 }
|
||||
}
|
||||
|
||||
impl<N: Number> Point<N> for [N;2] {
|
||||
fn x (&self) -> N {
|
||||
self[0]
|
||||
}
|
||||
fn y (&self) -> N {
|
||||
self[1]
|
||||
}
|
||||
fn x (&self) -> N { self[0] }
|
||||
fn y (&self) -> N { self[1] }
|
||||
}
|
||||
|
||||
pub trait Area<N: Number> {
|
||||
|
|
@ -34,42 +22,28 @@ pub trait Area<N: Number> {
|
|||
fn y (&self) -> N;
|
||||
fn w (&self) -> N;
|
||||
fn h (&self) -> N;
|
||||
fn x2 (&self) -> N {
|
||||
self.x() + self.w()
|
||||
fn x2 (&self) -> N { 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 {
|
||||
self.y() + self.h()
|
||||
fn lrtb (&self) -> [N;4] {
|
||||
[self.x(), self.x2(), self.y(), self.y2()]
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Number> Area<N> for (N, N, N, N) {
|
||||
fn x (&self) -> N {
|
||||
self.0
|
||||
}
|
||||
fn y (&self) -> N {
|
||||
self.1
|
||||
}
|
||||
fn w (&self) -> N {
|
||||
self.2
|
||||
}
|
||||
fn h (&self) -> N {
|
||||
self.3
|
||||
}
|
||||
fn x (&self) -> N { self.0 }
|
||||
fn y (&self) -> N { self.1 }
|
||||
fn w (&self) -> N { self.2 }
|
||||
fn h (&self) -> N { self.3 }
|
||||
}
|
||||
|
||||
impl<N: Number> Area<N> for [N;4] {
|
||||
fn x (&self) -> N {
|
||||
self[0]
|
||||
}
|
||||
fn y (&self) -> N {
|
||||
self[1]
|
||||
}
|
||||
fn w (&self) -> N {
|
||||
self[2]
|
||||
}
|
||||
fn h (&self) -> N {
|
||||
self[3]
|
||||
}
|
||||
fn x (&self) -> N { self[0] }
|
||||
fn y (&self) -> N { self[1] }
|
||||
fn w (&self) -> N { self[2] }
|
||||
fn h (&self) -> N { self[3] }
|
||||
}
|
||||
|
||||
macro_rules! impl_axis_common { ($A:ident $T:ty) => {
|
||||
|
|
|
|||
|
|
@ -19,16 +19,16 @@ pub struct Tui {
|
|||
buffers: [Buffer;2],
|
||||
backend: CrosstermBackend<Stdout>,
|
||||
event: RwLock<Option<TuiEvent>>,
|
||||
area: Rect,
|
||||
area: [u16;4],
|
||||
}
|
||||
|
||||
impl Engine for Tui {
|
||||
type Unit = u16;
|
||||
type Area = Rect;
|
||||
type Area = [Self::Unit;4];
|
||||
type HandleInput = Self;
|
||||
type Handled = bool;
|
||||
type RenderInput = Self;
|
||||
type Rendered = Rect;
|
||||
type Rendered = Self::Area;
|
||||
fn exited (&self) -> bool {
|
||||
self.exited.fetch_and(true, Ordering::Relaxed)
|
||||
}
|
||||
|
|
@ -54,7 +54,7 @@ impl Engine for Tui {
|
|||
}
|
||||
#[inline]
|
||||
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
|
||||
}
|
||||
}
|
||||
|
|
@ -71,7 +71,7 @@ impl Tui {
|
|||
buffer: 0,
|
||||
buffers: [Buffer::empty(area), Buffer::empty(area)],
|
||||
backend,
|
||||
area,
|
||||
area: area.xywh(),
|
||||
};
|
||||
engine.setup()?;
|
||||
let engine = Arc::new(RwLock::new(engine));
|
||||
|
|
@ -109,7 +109,7 @@ impl Tui {
|
|||
if engine.exited() {
|
||||
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");
|
||||
engine.flip();
|
||||
}
|
||||
|
|
@ -134,22 +134,22 @@ impl Tui {
|
|||
self.buffers[1 - self.buffer].reset();
|
||||
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)
|
||||
}
|
||||
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);})
|
||||
}
|
||||
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);})
|
||||
}
|
||||
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,_,_|{
|
||||
cell.modifier = ratatui::prelude::Modifier::UNDERLINED;
|
||||
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);})
|
||||
}
|
||||
pub fn make_dim (&mut self) {
|
||||
|
|
@ -161,25 +161,23 @@ impl Tui {
|
|||
}
|
||||
pub fn blit (
|
||||
&mut self, text: &impl AsRef<str>, x: u16, y: u16, style: Option<Style>
|
||||
) -> Perhaps<Rect> {
|
||||
) -> Perhaps<[u16;4]> {
|
||||
let text = text.as_ref();
|
||||
let buf = self.buffer();
|
||||
if x < buf.area.width && y < buf.area.height {
|
||||
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]
|
||||
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 {
|
||||
let (x, y, width, height) = alter(
|
||||
self.area.x, self.area.y, self.area.width, self.area.height
|
||||
);
|
||||
self.with_area(x, y, width, height)
|
||||
let [x, y, w, h] = alter(self.area.xywh());
|
||||
self.with_area(x, y, w, h)
|
||||
}
|
||||
#[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
|
||||
}
|
||||
|
|
@ -200,27 +198,10 @@ pub enum TuiEvent {
|
|||
// 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 () {
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
|
||||
Ok(Some(Rect { x: to.area.x, y: to.area.y, width: 0, height: 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
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
|
||||
Ok(Some([to.area.x(), to.area.y(), 0, 0]))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -231,3 +212,11 @@ impl Area<u16> for Rect {
|
|||
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
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,7 @@ pub trait BorderStyle {
|
|||
const W: &'static str = "";
|
||||
|
||||
#[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_vertical(to, None)?;
|
||||
self.draw_corners(to, None)?;
|
||||
|
|
@ -19,41 +19,42 @@ pub trait BorderStyle {
|
|||
}
|
||||
|
||||
#[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 style = style.or_else(||self.style_horizontal());
|
||||
for x in area.x..(area.x+area.width).saturating_sub(1) {
|
||||
self.draw_north(to, x, area.y, style)?;
|
||||
self.draw_south(to, x, (area.y + area.height).saturating_sub(1), style)?;
|
||||
let [x, x2, y, y2] = area.lrtb();
|
||||
for x in x..x2.saturating_sub(1) {
|
||||
self.draw_north(to, x, y, style)?;
|
||||
self.draw_south(to, x, y2.saturating_sub(1), style)?;
|
||||
}
|
||||
Ok(area)
|
||||
}
|
||||
#[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)
|
||||
}
|
||||
#[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)
|
||||
}
|
||||
|
||||
#[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 style = style.or_else(||self.style_vertical());
|
||||
let Rect { x, y, width, height } = area;
|
||||
for y in y..(y+height).saturating_sub(1) {
|
||||
let [x, x2, y, y2] = area.lrtb();
|
||||
for y in y..y2.saturating_sub(1) {
|
||||
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)
|
||||
}
|
||||
|
||||
#[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 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 {
|
||||
to.blit(&Self::NW, x, y, style)?;
|
||||
to.blit(&Self::NE, x + width - 1, y, style)?;
|
||||
|
|
|
|||
|
|
@ -24,11 +24,11 @@ impl BigBuffer {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn buffer_update (buf: &mut Buffer, area: Rect, callback: &impl Fn(&mut Cell, u16, u16)) {
|
||||
for row in 0..area.height {
|
||||
let y = area.y + row;
|
||||
for col in 0..area.width {
|
||||
let x = area.x + col;
|
||||
pub fn buffer_update (buf: &mut Buffer, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) {
|
||||
for row in 0..area.h() {
|
||||
let y = area.y() + row;
|
||||
for col in 0..area.w() {
|
||||
let x = area.x() + col;
|
||||
if x < buf.area.width && y < buf.area.height {
|
||||
callback(buf.get_mut(x, y), col, row);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@ use crate::*;
|
|||
pub struct FillBg(pub Color);
|
||||
|
||||
impl Render<Tui> for FillBg {
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<Rect> {
|
||||
to.fill_bg(to.area, self.0);
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
|
||||
to.fill_bg(to.area(), self.0);
|
||||
Ok(Some(to.area))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::*;
|
||||
|
||||
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();
|
||||
for layer in self.0.0.iter() {
|
||||
layer.render(to)?;
|
||||
|
|
@ -11,14 +11,14 @@ impl<'a> Render<Tui> for Layered<'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))
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
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 mut w = 0u16;
|
||||
let mut h = 0u16;
|
||||
|
|
@ -31,12 +31,12 @@ impl<'a> Split<'a, Tui> {
|
|||
}
|
||||
let result = Offset::Y(h, component).render(to)?;
|
||||
areas.push(result);
|
||||
if let Some(Rect { width, height, .. }) = result {
|
||||
if let Some([_, _, width, height]) = result {
|
||||
h += height;
|
||||
w = w.max(width)
|
||||
}
|
||||
}
|
||||
Rect { x: area.x(), y: area.y(), width: w, height: h }
|
||||
[area.x(), area.y(), w, h]
|
||||
},
|
||||
Direction::Right => {
|
||||
for component in self.items.0.iter() {
|
||||
|
|
@ -45,12 +45,12 @@ impl<'a> Split<'a, Tui> {
|
|||
}
|
||||
let result = Offset::X(w, component).render(to)?;
|
||||
areas.push(result);
|
||||
if let Some(Rect { width, height, .. }) = result {
|
||||
if let Some([_, _, width, height]) = result {
|
||||
w += width;
|
||||
h = h.max(height)
|
||||
}
|
||||
}
|
||||
Rect { x: area.x(), y: area.y(), width: w, height: h }
|
||||
[area.x(), area.y(), w, h]
|
||||
},
|
||||
_ => todo!()
|
||||
}, areas))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue