use crate::prelude::{
string_to_cp437, to_cp437, CharacterTranslationMode, ColoredTextSpans, Console, FontCharType,
TextAlign, Tile, XpLayer,
};
use bracket_color::prelude::*;
use bracket_geometry::prelude::Rect;
use std::any::Any;
pub struct SimpleConsole {
pub width: u32,
pub height: u32,
pub tiles: Vec<Tile>,
pub is_dirty: bool,
pub offset_x: f32,
pub offset_y: f32,
pub scale: f32,
pub scale_center: (i32, i32),
pub extra_clipping: Option<Rect>,
pub translation: CharacterTranslationMode,
pub(crate) needs_resize_internal: bool,
}
impl SimpleConsole {
pub fn init(width: u32, height: u32) -> Box<SimpleConsole> {
let num_tiles: usize = (width * height) as usize;
let mut tiles: Vec<Tile> = Vec::with_capacity(num_tiles);
for _ in 0..num_tiles {
tiles.push(Tile {
glyph: 0,
fg: RGBA::from_u8(255, 255, 255, 255),
bg: RGBA::from_u8(0, 0, 0, 255),
});
}
let new_console = SimpleConsole {
width,
height,
tiles,
is_dirty: true,
offset_x: 0.0,
offset_y: 0.0,
scale: 1.0,
scale_center: (width as i32 / 2, height as i32 / 2),
extra_clipping: None,
translation: CharacterTranslationMode::Codepage437,
needs_resize_internal: false,
};
Box::new(new_console)
}
}
impl Console for SimpleConsole {
fn get_char_size(&self) -> (u32, u32) {
(self.width, self.height)
}
fn resize_pixels(&mut self, _width: u32, _height: u32) {
self.is_dirty = true;
}
fn at(&self, x: i32, y: i32) -> usize {
(((self.height - 1 - y as u32) * self.width) + x as u32) as usize
}
fn cls(&mut self) {
self.is_dirty = true;
for tile in &mut self.tiles {
tile.glyph = 32;
tile.fg = RGBA::from_u8(255, 255, 255, 255);
tile.bg = RGBA::from_u8(0, 0, 0, 255);
}
}
fn cls_bg(&mut self, background: RGBA) {
self.is_dirty = true;
for tile in &mut self.tiles {
tile.glyph = 32;
tile.fg = RGBA::from_u8(255, 255, 255, 255);
tile.bg = background;
}
}
fn print(&mut self, mut x: i32, y: i32, output: &str) {
self.is_dirty = true;
let bytes = match self.translation {
CharacterTranslationMode::Codepage437 => string_to_cp437(output),
CharacterTranslationMode::Unicode => {
output.chars().map(|c| c as FontCharType).collect()
}
};
for glyph in bytes {
if let Some(idx) = self.try_at(x, y) {
self.tiles[idx].glyph = glyph;
}
x += 1;
}
}
fn print_color(&mut self, mut x: i32, y: i32, fg: RGBA, bg: RGBA, output: &str) {
self.is_dirty = true;
let bytes = match self.translation {
CharacterTranslationMode::Codepage437 => string_to_cp437(output),
CharacterTranslationMode::Unicode => {
output.chars().map(|c| c as FontCharType).collect()
}
};
for glyph in bytes {
if let Some(idx) = self.try_at(x, y) {
self.tiles[idx].glyph = glyph;
self.tiles[idx].bg = bg;
self.tiles[idx].fg = fg;
}
x += 1;
}
}
fn set(&mut self, x: i32, y: i32, fg: RGBA, bg: RGBA, glyph: FontCharType) {
self.is_dirty = true;
if let Some(idx) = self.try_at(x, y) {
self.tiles[idx].glyph = glyph;
self.tiles[idx].fg = fg;
self.tiles[idx].bg = bg;
}
}
fn set_bg(&mut self, x: i32, y: i32, bg: RGBA) {
self.is_dirty = true;
if let Some(idx) = self.try_at(x, y) {
self.tiles[idx].bg = bg;
}
}
fn draw_box(&mut self, sx: i32, sy: i32, width: i32, height: i32, fg: RGBA, bg: RGBA) {
crate::prelude::draw_box(self, sx, sy, width, height, fg, bg);
}
fn draw_hollow_box(&mut self, sx: i32, sy: i32, width: i32, height: i32, fg: RGBA, bg: RGBA) {
crate::prelude::draw_hollow_box(self, sx, sy, width, height, fg, bg);
}
fn draw_box_double(&mut self, sx: i32, sy: i32, width: i32, height: i32, fg: RGBA, bg: RGBA) {
crate::prelude::draw_box_double(self, sx, sy, width, height, fg, bg);
}
fn draw_hollow_box_double(
&mut self,
sx: i32,
sy: i32,
width: i32,
height: i32,
fg: RGBA,
bg: RGBA,
) {
crate::prelude::draw_hollow_box_double(self, sx, sy, width, height, fg, bg);
}
fn fill_region(&mut self, target: Rect, glyph: FontCharType, fg: RGBA, bg: RGBA) {
target.for_each(|point| {
self.set(point.x, point.y, fg, bg, glyph);
});
}
fn draw_bar_horizontal(
&mut self,
sx: i32,
sy: i32,
width: i32,
n: i32,
max: i32,
fg: RGBA,
bg: RGBA,
) {
crate::prelude::draw_bar_horizontal(self, sx, sy, width, n, max, fg, bg);
}
fn draw_bar_vertical(
&mut self,
sx: i32,
sy: i32,
height: i32,
n: i32,
max: i32,
fg: RGBA,
bg: RGBA,
) {
crate::prelude::draw_bar_vertical(self, sx, sy, height, n, max, fg, bg);
}
fn print_centered(&mut self, y: i32, text: &str) {
self.is_dirty = true;
self.print(
(self.width as i32 / 2) - (text.to_string().len() as i32 / 2),
y,
text,
);
}
fn print_color_centered(&mut self, y: i32, fg: RGBA, bg: RGBA, text: &str) {
self.is_dirty = true;
self.print_color(
(self.width as i32 / 2) - (text.to_string().len() as i32 / 2),
y,
fg,
bg,
text,
);
}
fn print_centered_at(&mut self, x: i32, y: i32, text: &str) {
self.is_dirty = true;
self.print(x - (text.to_string().len() as i32 / 2), y, text);
}
fn print_color_centered_at(&mut self, x: i32, y: i32, fg: RGBA, bg: RGBA, text: &str) {
self.is_dirty = true;
self.print_color(x - (text.to_string().len() as i32 / 2), y, fg, bg, text);
}
fn print_right(&mut self, x: i32, y: i32, text: &str) {
let len = text.len() as i32;
let actual_x = x - len;
self.print(actual_x, y, text);
}
fn print_color_right(&mut self, x: i32, y: i32, fg: RGBA, bg: RGBA, text: &str) {
let len = text.len() as i32;
let actual_x = x - len;
self.print_color(actual_x, y, fg, bg, text);
}
fn printer(
&mut self,
x: i32,
y: i32,
output: &str,
align: TextAlign,
background: Option<RGBA>,
) {
let bg = if let Some(bg) = background {
bg
} else {
RGBA::from_u8(0, 0, 0, 255)
};
let split_text = ColoredTextSpans::new(output);
let mut tx = match align {
TextAlign::Left => x,
TextAlign::Center => x - (split_text.length as i32 / 2),
TextAlign::Right => x - split_text.length as i32,
};
for span in split_text.spans.iter() {
let fg = span.0;
for ch in span.1.chars() {
self.set(
tx,
y,
fg,
bg,
match self.translation {
CharacterTranslationMode::Codepage437 => to_cp437(ch),
CharacterTranslationMode::Unicode => ch as FontCharType,
},
);
tx += 1;
}
}
}
fn to_xp_layer(&self) -> XpLayer {
let mut layer = XpLayer::new(self.width as usize, self.height as usize);
for y in 0..self.height {
for x in 0..self.width {
let cell = layer.get_mut(x as usize, y as usize).unwrap();
let idx = self.at(x as i32, y as i32);
cell.ch = u32::from(self.tiles[idx].glyph);
cell.fg = self.tiles[idx].fg.into();
cell.bg = self.tiles[idx].bg.into();
}
}
layer
}
fn set_offset(&mut self, x: f32, y: f32) {
self.is_dirty = true;
self.offset_x = x * (2.0 / self.width as f32);
self.offset_y = y * (2.0 / self.height as f32);
}
fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32) {
self.is_dirty = true;
self.scale = scale;
self.scale_center = (center_x, center_y);
}
fn get_scale(&self) -> (f32, i32, i32) {
(self.scale, self.scale_center.0, self.scale_center.1)
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn set_clipping(&mut self, clipping: Option<Rect>) {
self.extra_clipping = clipping;
}
fn get_clipping(&self) -> Option<Rect> {
self.extra_clipping
}
fn set_all_fg_alpha(&mut self, alpha: f32) {
self.tiles.iter_mut().for_each(|t| t.fg.a = alpha);
}
fn set_all_bg_alpha(&mut self, alpha: f32) {
self.tiles.iter_mut().for_each(|t| t.bg.a = alpha);
}
fn set_all_alpha(&mut self, fg: f32, bg: f32) {
self.tiles.iter_mut().for_each(|t| {
t.fg.a = fg;
t.bg.a = bg;
});
}
fn set_translation_mode(&mut self, mode: CharacterTranslationMode) {
self.translation = mode;
}
fn set_char_size(&mut self, width: u32, height: u32) {
let num_tiles = (width * height) as usize;
let mut new_tiles: Vec<Tile> = Vec::with_capacity(num_tiles);
for _ in 0..num_tiles {
new_tiles.push(Tile {
glyph: 0,
fg: RGBA::from_u8(255, 255, 255, 255),
bg: RGBA::from_u8(0, 0, 0, 255),
});
}
for y in 0..i32::min(self.height as i32, height as i32) {
for x in 0..i32::min(self.width as i32, width as i32) {
let idx = self.at(x, y);
let new_idx = (((height as i32 - 1 - y) * width as i32) + x) as usize;
new_tiles[new_idx] = self.tiles[idx];
}
}
self.tiles = new_tiles;
self.width = width;
self.height = height;
self.needs_resize_internal = true;
}
fn clear_dirty(&mut self) {
self.is_dirty = false;
}
}