| 1 | //! The fully animated GPUI pet as a stateful Ratatui component. The host |
| 2 | //! observes and advances one shared Stage; painting never advances time. |
| 3 | |
| 4 | use std::borrow::Cow; |
| 5 | |
| 6 | use ratatui::{buffer::Buffer, layout::Rect, widgets::StatefulWidget}; |
| 7 | |
| 8 | use crate::whale_motion::{CoveScene, Stage, View, pixels, rasterize_colored, rig}; |
| 9 | use crate::{Paint, Theme, Whale, WhaleState, avatar_sprite::Sprite}; |
| 10 | |
| 11 | /// The native pet's rendering style. Both read the same live contours. |
| 12 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] |
| 13 | pub enum PetStyle { |
| 14 | /// Full color curves, apertures, props and cove, using terminal half-blocks. |
| 15 | #[default] |
| 16 | Color, |
| 17 | /// The canonical colored Braille geometry, without scenery. |
| 18 | Braille, |
| 19 | } |
| 20 | |
| 21 | /// A live whale in its GPUI cove: springs, fins, eyes, blinks, props, particles |
| 22 | /// and calves all come from the existing canonical performance. Pointer input |
| 23 | /// is decorative and enters through the host's [`Stage::cove_observe`] and |
| 24 | /// [`Stage::cove_tap`]; it never changes Engine state. |
| 25 | /// |
| 26 | /// The host supplies authoritative [`crate::whale_motion::Inputs`], calls |
| 27 | /// [`Stage::advance`] once per frame, and schedules with |
| 28 | /// [`Stage::cadence`] (Hero for color, Terminal for Braille). Reduced motion |
| 29 | /// paints a settled poster and schedules no animation. Unsupported color |
| 30 | /// profiles use Braille; ASCII and small areas show the status words alone. |
| 31 | /// |
| 32 | /// ```no_run |
| 33 | /// use codewhale_ratatui::{Theme, WhalePet, whale_motion::Stage}; |
| 34 | /// # fn draw(frame: &mut ratatui::Frame<'_>, stage: &mut Stage) { |
| 35 | /// let theme = Theme::detect(); |
| 36 | /// stage.advance(std::time::Instant::now()); |
| 37 | /// frame.render_stateful_widget(WhalePet::new(&theme), frame.area(), stage); |
| 38 | /// # } |
| 39 | /// ``` |
| 40 | pub struct WhalePet<'a> { |
| 41 | theme: &'a Theme, |
| 42 | words: Option<Cow<'a, str>>, |
| 43 | cove: bool, |
| 44 | style: PetStyle, |
| 45 | direction: rig::Direction, |
| 46 | } |
| 47 | |
| 48 | impl<'a> WhalePet<'a> { |
| 49 | #[must_use] |
| 50 | pub const fn new(theme: &'a Theme) -> Self { |
| 51 | Self { |
| 52 | theme, |
| 53 | words: None, |
| 54 | cove: true, |
| 55 | style: PetStyle::Color, |
| 56 | direction: rig::MARK_DIRECTION, |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Localized words from the host. The default names the current action; |
| 61 | /// empty words hide the caption when the host already presents the state. |
| 62 | #[must_use] |
| 63 | pub fn words(mut self, words: impl Into<Cow<'a, str>>) -> Self { |
| 64 | self.words = Some(words.into()); |
| 65 | self |
| 66 | } |
| 67 | |
| 68 | #[must_use] |
| 69 | pub const fn cove(mut self, shown: bool) -> Self { |
| 70 | self.cove = shown; |
| 71 | self |
| 72 | } |
| 73 | |
| 74 | #[must_use] |
| 75 | pub const fn style(mut self, style: PetStyle) -> Self { |
| 76 | self.style = style; |
| 77 | self |
| 78 | } |
| 79 | |
| 80 | /// The shared rig's mark, cruise or open view; never a separate animation. |
| 81 | #[must_use] |
| 82 | pub const fn direction(mut self, direction: rig::Direction) -> Self { |
| 83 | self.direction = direction; |
| 84 | self |
| 85 | } |
| 86 | |
| 87 | /// The bounded square art rectangle, reserving one row for state words. |
| 88 | /// Hosts use this same rectangle to map mouse input with [`Self::point`]. |
| 89 | #[must_use] |
| 90 | pub fn art_area(area: Rect) -> Rect { |
| 91 | let side = area |
| 92 | .width |
| 93 | .min(area.height.saturating_sub(1).saturating_mul(2)) |
| 94 | .min(pixels::MAX_SIDE as u16); |
| 95 | let side = side - side % 2; |
| 96 | Rect::new( |
| 97 | area.x.saturating_add((area.width - side) / 2), |
| 98 | area.y |
| 99 | .saturating_add((area.height.saturating_sub(1) - side / 2) / 2), |
| 100 | side, |
| 101 | side / 2, |
| 102 | ) |
| 103 | } |
| 104 | |
| 105 | /// Convert a terminal cell to the GPUI cove's −62…62 design coordinates. |
| 106 | /// Outside points return `None` so the host can release the resting gaze. |
| 107 | #[must_use] |
| 108 | pub fn point(area: Rect, column: u16, row: u16) -> Option<(f64, f64)> { |
| 109 | let art = Self::art_area(area); |
| 110 | if art.is_empty() || !art.contains((column, row).into()) { |
| 111 | return None; |
| 112 | } |
| 113 | Some(( |
| 114 | (f64::from(column - art.x) + 0.5) / f64::from(art.width) * 124. - 62., |
| 115 | (f64::from(row - art.y) + 0.5) / f64::from(art.height) * 124. - 62., |
| 116 | )) |
| 117 | } |
| 118 | |
| 119 | /// Paint the current shared frame. The only mutable decoration is the |
| 120 | /// cove's idempotent gaze sample; time and owner truth remain host-owned. |
| 121 | pub fn paint(&self, area: Rect, buf: &mut Buffer, stage: &mut Stage) { |
| 122 | let area = area.intersection(buf.area); |
| 123 | if area.is_empty() { |
| 124 | return; |
| 125 | } |
| 126 | let mut state = WhaleState::from_key(stage.acting().id()).unwrap_or(WhaleState::Rest); |
| 127 | let mut words = None; |
| 128 | if matches!(state, WhaleState::Pod { .. }) { |
| 129 | let calves = stage |
| 130 | .director() |
| 131 | .activity |
| 132 | .as_ref() |
| 133 | .and_then(|a| a.parallel) |
| 134 | .filter(|n| n.is_finite() && *n >= 0. && n.fract() == 0.) |
| 135 | .unwrap_or(0.); |
| 136 | state = WhaleState::Pod { |
| 137 | calves: calves.min(f64::from(u8::MAX)) as u8, |
| 138 | }; |
| 139 | if calves > f64::from(u8::MAX) { |
| 140 | words = Some(format!("Working with {calves:.0} agents")); |
| 141 | } |
| 142 | } |
| 143 | let mut whale = Whale::new(state); |
| 144 | if let Some(words) = words { |
| 145 | whale = whale.words(words); |
| 146 | } |
| 147 | if let Some(words) = &self.words { |
| 148 | whale = whale.words(words.to_string()); |
| 149 | } |
| 150 | let art = Self::art_area(area); |
| 151 | let words_only = crate::whale::Grid { |
| 152 | cols: 0, |
| 153 | rows: 0, |
| 154 | cells: Vec::new(), |
| 155 | }; |
| 156 | if self.theme.ascii() || art.width < 16 || art.height < 8 { |
| 157 | // A deliberately invalid frame asks the shared painter for words. |
| 158 | if self.words.as_deref() != Some("") { |
| 159 | whale.paint_frame(area, buf, self.theme, &words_only); |
| 160 | } |
| 161 | return; |
| 162 | } |
| 163 | let dark = self.theme.caps().appearance != crate::detect::Appearance::Light; |
| 164 | let view = View { |
| 165 | dir: self.direction, |
| 166 | ..View::hero(f64::from(art.width), 1.) |
| 167 | }; |
| 168 | if self.style == PetStyle::Braille || !self.theme.paints_grounds() { |
| 169 | let parts = stage.scene(view); |
| 170 | rasterize_colored( |
| 171 | &parts, |
| 172 | usize::from(art.width), |
| 173 | usize::from(art.height), |
| 174 | 0.5, |
| 175 | dark, |
| 176 | ) |
| 177 | .paint_with_contrast(&whale, area, buf, self.theme, 3.); |
| 178 | return; |
| 179 | } |
| 180 | let scene = if self.cove { |
| 181 | stage.cove_scene(view, dark) |
| 182 | } else { |
| 183 | CoveScene { |
| 184 | behind: Vec::new(), |
| 185 | parts: stage.scene(view), |
| 186 | front: Vec::new(), |
| 187 | } |
| 188 | }; |
| 189 | let pixels = pixels::render(&scene, usize::from(art.width), dark); |
| 190 | if let Ok(sprite) = Sprite::image(&pixels, usize::from(art.width), usize::from(art.width)) { |
| 191 | sprite.paint(art, buf, self.theme); |
| 192 | } |
| 193 | if self.words.as_deref() != Some("") { |
| 194 | whale.paint_frame( |
| 195 | Rect::new(area.x, art.bottom(), area.width, 1), |
| 196 | buf, |
| 197 | self.theme, |
| 198 | &words_only, |
| 199 | ); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | impl StatefulWidget for WhalePet<'_> { |
| 205 | type State = Stage; |
| 206 | fn render(self, area: Rect, buf: &mut Buffer, state: &mut Stage) { |
| 207 | self.paint(area, buf, state); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | impl StatefulWidget for &WhalePet<'_> { |
| 212 | type State = Stage; |
| 213 | fn render(self, area: Rect, buf: &mut Buffer, state: &mut Stage) { |
| 214 | self.paint(area, buf, state); |
| 215 | } |
| 216 | } |
| 217 |