use super::view::{write_line_to_terminal, View};
use crate::core::draw::DrawBuffer;
use crate::core::event::Event;
use crate::core::geometry::Rect;
use crate::core::palette::STATIC_TEXT_NORMAL;
use crate::terminal::Terminal;
pub struct StaticText {
bounds: Rect,
text: String,
centered: bool,
owner: Option<*const dyn View>,
owner_type: super::view::OwnerType,
}
impl StaticText {
pub fn new(bounds: Rect, text: &str) -> Self {
Self {
bounds,
text: text.to_string(),
centered: false,
owner: None,
owner_type: super::view::OwnerType::Dialog, }
}
pub fn new_centered(bounds: Rect, text: &str) -> Self {
Self {
bounds,
text: text.to_string(),
centered: true,
owner: None,
owner_type: super::view::OwnerType::Dialog, }
}
}
impl View for StaticText {
fn bounds(&self) -> Rect {
self.bounds
}
fn set_bounds(&mut self, bounds: Rect) {
self.bounds = bounds;
}
fn draw(&mut self, terminal: &mut Terminal) {
let width = self.bounds.width() as usize;
let lines: Vec<&str> = self.text.split('\n').collect();
let text_attr = self.map_color(STATIC_TEXT_NORMAL);
for (i, line) in lines.iter().enumerate() {
if i >= self.bounds.height() as usize {
break;
}
let mut buf = DrawBuffer::new(width);
buf.move_char(0, ' ', text_attr, width);
let start_pos = if self.centered {
let line_len = line.len();
if width > line_len {
(width - line_len) / 2
} else {
0
}
} else {
0
};
buf.move_str_with_shortcut(start_pos, line, text_attr, text_attr);
write_line_to_terminal(terminal, self.bounds.a.x, self.bounds.a.y + i as i16, &buf);
}
}
fn handle_event(&mut self, _event: &mut Event) {
}
fn set_owner(&mut self, owner: *const dyn View) {
self.owner = Some(owner);
}
fn get_owner(&self) -> Option<*const dyn View> {
self.owner
}
fn get_owner_type(&self) -> super::view::OwnerType {
self.owner_type
}
fn set_owner_type(&mut self, owner_type: super::view::OwnerType) {
self.owner_type = owner_type;
}
fn get_palette(&self) -> Option<crate::core::palette::Palette> {
use crate::core::palette::{palettes, Palette};
Some(Palette::from_slice(palettes::CP_STATIC_TEXT))
}
}