diff --git a/engine/README.md b/engine/README.md index 0ac4a79c..03d7e53d 100644 --- a/engine/README.md +++ b/engine/README.md @@ -40,9 +40,9 @@ it composes: the manner of output is determined by the `Engine::Output` type, a mutable pointer to which is passed to the render method, e.g. in the case of -the `Tui` engine: `fn render(&self, output: &mut TuiOutput)` +the `Tui` engine: `fn render(&self, output: &mut TuiOut)` -you can use `TuiOutput::blit` and `TuiOutput::place` +you can use `TuiOut::blit` and `TuiOut::place` to draw at specified coordinates of the display, and/or directly modify the underlying `ratatui::Buffer` at `output.buffer` diff --git a/engine/src/lib.rs b/engine/src/lib.rs index dd47d059..00fb8356 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -68,7 +68,7 @@ pub type Perhaps = Result, Box>; } } impl Handle for TestComponent { - fn handle (&mut self, from: &TuiInput) -> Perhaps { + fn handle (&mut self, from: &TuiIn) -> Perhaps { Ok(None) } } diff --git a/engine/src/tui.rs b/engine/src/tui.rs index cfa2e76e..3bdb54ac 100644 --- a/engine/src/tui.rs +++ b/engine/src/tui.rs @@ -39,9 +39,9 @@ impl Engine for Tui { type Unit = u16; type Size = [Self::Unit;2]; type Area = [Self::Unit;4]; - type Input = TuiInput; + type Input = TuiIn; type Handled = bool; - type Output = TuiOutput; + type Output = TuiOut; fn exited (&self) -> bool { self.exited.fetch_and(true, Relaxed) } @@ -125,7 +125,7 @@ impl + Handle + Sized + 'static> TuiRun for Arc { let exited = exited.clone(); - if let Err(e) = state.write().unwrap().handle(&TuiInput { event, exited }) { + if let Err(e) = state.write().unwrap().handle(&TuiIn { event, exited }) { panic!("{e}") } } @@ -153,7 +153,7 @@ impl + Handle + Sized + 'static> TuiRun for Arc, pub(crate) event: crossterm::event::Event, } -impl Input for TuiInput { +impl Input for TuiIn { type Event = crossterm::event::Event; fn event (&self) -> &crossterm::event::Event { &self.event @@ -124,7 +124,7 @@ pub const fn shift (key: KeyEvent) -> KeyEvent { /* -impl TuiInput { +impl TuiIn { // TODO remove pub fn handle_keymap (&self, state: &mut T, keymap: &KeyMap) -> Usually { match self.event() { diff --git a/engine/src/tui/tui_output.rs b/engine/src/tui/tui_output.rs index b5e267e1..80334616 100644 --- a/engine/src/tui/tui_output.rs +++ b/engine/src/tui/tui_output.rs @@ -1,11 +1,11 @@ use crate::{*, tui::*}; -pub struct TuiOutput { +pub struct TuiOut { pub buffer: Buffer, pub area: [u16;4] } -impl Output for TuiOutput { +impl Output for TuiOut { #[inline] fn area (&self) -> [u16;4] { self.area } #[inline] fn area_mut (&mut self) -> &mut [u16;4] { &mut self.area } #[inline] fn place (&mut self, area: [u16;4], content: &impl Content) { @@ -20,7 +20,7 @@ impl Output for TuiOutput { } } -impl TuiOutput { +impl TuiOut { pub fn buffer_update (&mut self, area: [u16;4], callback: &impl Fn(&mut Cell, u16, u16)) { buffer_update(&mut self.buffer, area, callback); } @@ -69,6 +69,38 @@ impl TuiOutput { } } +impl Content for &str { + fn area (&self, to: [u16;4]) -> [u16;4] { + [to[0], to[1], self.chars().count() as u16, 1] + } + fn render (&self, to: &mut TuiOut) { + to.blit(self, to.area.x(), to.area.y(), None) + } +} + +impl Content for String { + fn area (&self, to: [u16;4]) -> [u16;4] { + [to[0], to[1], self.chars().count() as u16, 1] + } + fn render (&self, to: &mut TuiOut) { + to.blit(self, to.area.x(), to.area.y(), None) + } +} + +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); + } + } + } +} + +/////////////////////////////////////////////////////////////////////////////////////////////////// + //impl Area for Rect { //fn x (&self) -> u16 { self.x } //fn y (&self) -> u16 { self.y } @@ -86,33 +118,3 @@ pub fn half_block (lower: bool, upper: bool) -> Option { } //impl> Render for T {} - -impl Content for &str { - fn area (&self, to: [u16;4]) -> [u16;4] { - [to[0], to[1], self.chars().count() as u16, 1] - } - fn render (&self, to: &mut TuiOutput) { - to.blit(self, to.area.x(), to.area.y(), None) - } -} - -impl Content for String { - fn area (&self, to: [u16;4]) -> [u16;4] { - [to[0], to[1], self.chars().count() as u16, 1] - } - fn render (&self, to: &mut TuiOutput) { - to.blit(self, to.area.x(), to.area.y(), None) - } -} - -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); - } - } - } -} diff --git a/examples/demo.rs.fixme b/examples/demo.rs.fixme index d235f005..cfd7d7b0 100644 --- a/examples/demo.rs.fixme +++ b/examples/demo.rs.fixme @@ -105,7 +105,7 @@ impl Content for Demo { } impl Handle for Demo { - fn handle (&mut self, from: &TuiInput) -> Perhaps { + fn handle (&mut self, from: &TuiIn) -> Perhaps { use KeyCode::{PageUp, PageDown}; match from.event() { key_expr!(PageUp) => { diff --git a/examples/demo_bsp.rs.fixme b/examples/demo_bsp.rs.fixme index 453134f8..997956cd 100644 --- a/examples/demo_bsp.rs.fixme +++ b/examples/demo_bsp.rs.fixme @@ -13,7 +13,7 @@ render!(|self:BspDemo|Fill::xy(Align::c( ))); impl Handle for BspDemo { - fn handle (&mut self, from: &TuiInput) -> Perhaps { + fn handle (&mut self, from: &TuiIn) -> Perhaps { Ok(None) } } diff --git a/layout/src/measure.rs b/layout/src/measure.rs index 584bbef4..d0529df4 100644 --- a/layout/src/measure.rs +++ b/layout/src/measure.rs @@ -85,7 +85,7 @@ impl Measure { //fn min_size (&self, _: [u16;2]) -> Perhaps<[u16;2]> { //Ok(Some([0u16.into(), 0u16.into()].into())) //} - //fn render (&self, to: &mut TuiOutput) -> Usually<()> { + //fn render (&self, to: &mut TuiOut) -> Usually<()> { //self.set_w(to.area().w()); //self.set_h(to.area().h()); //Ok(()) @@ -98,7 +98,7 @@ impl Measure { //} //} -//render!(|self: ShowMeasure<'a>|render(|to: &mut TuiOutput|Ok({ +//render!(|self: ShowMeasure<'a>|render(|to: &mut TuiOut|Ok({ //let w = self.0.w(); //let h = self.0.h(); //to.blit(&format!(" {w} x {h} "), to.area.x(), to.area.y(), Some( @@ -114,7 +114,7 @@ impl Measure { //fn min_size (&self, to: [u16;2]) -> Perhaps<[u16;2]> { //self.1.min_size(to) //} - //fn render (&self, to: &mut TuiOutput) -> Usually<()> { + //fn render (&self, to: &mut TuiOut) -> Usually<()> { //let [x, y, w, h] = to.area(); //self.1.render(to)?; //Ok(to.blit(&format!("{w}x{h}+{x}+{y}"), x, y, Some(Style::default().green()))) diff --git a/src/arranger/arranger_command.rs b/src/arranger/arranger_command.rs index d54e8137..d9d2736a 100644 --- a/src/arranger/arranger_command.rs +++ b/src/arranger/arranger_command.rs @@ -160,7 +160,7 @@ input_to_command!(ArrangerCommand: |state: ArrangerTui, input|match input.e None })? }); -fn to_arrangement_command (state: &ArrangerTui, input: &TuiInput) -> Option { +fn to_arrangement_command (state: &ArrangerTui, input: &TuiIn) -> Option { use ArrangerCommand as Cmd; use ArrangerSelection as Selected; use ArrangerSceneCommand as Scene; diff --git a/src/arranger/arranger_v/v_cursor.rs b/src/arranger/arranger_v/v_cursor.rs index 5859f9d6..3582cd07 100644 --- a/src/arranger/arranger_v/v_cursor.rs +++ b/src/arranger/arranger_v/v_cursor.rs @@ -25,7 +25,7 @@ from!(|args:(&ArrangerTui, usize)|ArrangerVCursor = Self { }), }); impl Content for ArrangerVCursor { - fn render (&self, to: &mut TuiOutput) { + fn render (&self, to: &mut TuiOut) { let area = to.area(); let focused = true; let selected = self.selected; diff --git a/src/border.rs b/src/border.rs index afc77bd3..0959d373 100644 --- a/src/border.rs +++ b/src/border.rs @@ -53,7 +53,7 @@ pub trait BorderStyle: Send + Sync + Copy { fn sw (&self) -> &str { Self::SW } fn se (&self) -> &str { Self::SE } #[inline] fn draw <'a> ( - &self, to: &mut TuiOutput + &self, to: &mut TuiOut ) -> Usually<()> { self.draw_horizontal(to, None)?; self.draw_vertical(to, None)?; @@ -61,7 +61,7 @@ pub trait BorderStyle: Send + Sync + Copy { Ok(()) } #[inline] fn draw_horizontal ( - &self, to: &mut TuiOutput, style: Option