bracket_terminal/consoles/
console.rs

1use crate::prelude::{FontCharType, XpLayer};
2use bracket_color::prelude::RGBA;
3use bracket_geometry::prelude::{Point, Rect};
4use std::any::Any;
5
6/// The internal storage type for tiles in a simple console.
7#[derive(PartialEq, Copy, Clone, Debug)]
8pub struct Tile {
9    pub glyph: FontCharType,
10    pub fg: RGBA,
11    pub bg: RGBA,
12}
13
14#[derive(PartialEq, Copy, Clone, Debug)]
15pub enum TextAlign {
16    Left,
17    Center,
18    Right,
19}
20
21#[derive(PartialEq, Copy, Clone, Debug)]
22pub enum CharacterTranslationMode {
23    Codepage437,
24    Unicode,
25}
26
27/// Trait that must be implemented by console types.
28pub trait Console {
29    /// Gets the dimensions of the console in characters
30    fn get_char_size(&self) -> (u32, u32);
31
32    // Resizes the viewport
33    fn resize_pixels(&mut self, width: u32, height: u32);
34
35    /// Converts an x/y coordinate to a console index number.
36    fn at(&self, x: i32, y: i32) -> usize;
37
38    /// Clear the console.
39    fn cls(&mut self);
40
41    /// Clear the console to a set background color, if supported.
42    fn cls_bg(&mut self, background: RGBA);
43
44    /// Print a string at the specified x/y coordinate.
45    fn print(&mut self, x: i32, y: i32, output: &str);
46
47    /// Print a string in color at the specified x/y coordinate, with specified foreground and background.
48    fn print_color(&mut self, x: i32, y: i32, fg: RGBA, bg: RGBA, output: &str);
49
50    /// Print a colorized string with the color encoding defined inline.
51    /// For example: printer(1, 1, "#[blue]This blue text contains a #[pink]pink#[] word")
52    /// You can get the same effect with a TextBlock, but this can be easier.
53    /// Thanks to doryen_rs for the idea.
54    fn printer(&mut self, x: i32, y: i32, output: &str, align: TextAlign, background: Option<RGBA>);
55
56    /// Sets a single cell to a color/glyph combination.
57    fn set(&mut self, x: i32, y: i32, fg: RGBA, bg: RGBA, glyph: FontCharType);
58
59    /// Sets a single cell's background color.
60    fn set_bg(&mut self, x: i32, y: i32, bg: RGBA);
61
62    /// Draws a box, starting at x/y with the extents width/height using CP437 line characters
63    fn draw_box(&mut self, x: i32, y: i32, width: i32, height: i32, fg: RGBA, bg: RGBA);
64
65    /// Draws a box, starting at x/y with the extents width/height using CP437 line characters,
66    /// without filling in the middle
67    fn draw_hollow_box(&mut self, x: i32, y: i32, width: i32, height: i32, fg: RGBA, bg: RGBA);
68
69    /// Draws a box, starting at x/y with the extents width/height using CP437 double line characters
70    fn draw_box_double(&mut self, x: i32, y: i32, width: i32, height: i32, fg: RGBA, bg: RGBA);
71
72    /// Draws a box, starting at x/y with the extents width/height using CP437 double line characters,
73    /// without filling in the middle
74    fn draw_hollow_box_double(
75        &mut self,
76        x: i32,
77        y: i32,
78        width: i32,
79        height: i32,
80        fg: RGBA,
81        bg: RGBA,
82    );
83
84    /// Fills a rectangle-defined region with a given glyph
85    fn fill_region(&mut self, target: Rect, glyph: FontCharType, fg: RGBA, bg: RGBA);
86
87    /// Draws a horizontal progress bar.
88    #[allow(clippy::too_many_arguments)]
89    fn draw_bar_horizontal(
90        &mut self,
91        x: i32,
92        y: i32,
93        width: i32,
94        n: i32,
95        max: i32,
96        fg: RGBA,
97        bg: RGBA,
98    );
99
100    /// Draws a vertical progress bar.
101    #[allow(clippy::too_many_arguments)]
102    fn draw_bar_vertical(
103        &mut self,
104        x: i32,
105        y: i32,
106        height: i32,
107        n: i32,
108        max: i32,
109        fg: RGBA,
110        bg: RGBA,
111    );
112
113    /// Prints text, centered to the whole console width, at vertical location y.
114    fn print_centered(&mut self, y: i32, text: &str);
115
116    /// Prints text in color, centered to the whole console width, at vertical location y.
117    fn print_color_centered(&mut self, y: i32, fg: RGBA, bg: RGBA, text: &str);
118
119    /// Prints text, centered on an arbitrary point
120    fn print_centered_at(&mut self, x: i32, y: i32, text: &str);
121
122    /// Prints colored text, centered on an arbitrary point
123    fn print_color_centered_at(&mut self, x: i32, y: i32, fg: RGBA, bg: RGBA, text: &str);
124
125    /// Prints text right-aligned
126    fn print_right(&mut self, x: i32, y: i32, text: &str);
127
128    /// Prints colored text right-aligned
129    fn print_color_right(&mut self, x: i32, y: i32, fg: RGBA, bg: RGBA, text: &str);
130
131    /// Serializes the console layer to an XpFile
132    fn to_xp_layer(&self) -> XpLayer;
133
134    /// Specify a global offset (by character count, so 0.5 is half a character). Useful for
135    /// drawing walls between tiles.
136    fn set_offset(&mut self, x: f32, y: f32);
137
138    /// Specify a scale and center of the console.
139    /// A scale above 1.0 will make the text larger.
140    /// The center of the scale is at character position (center_x, center_y).
141    fn set_scale(&mut self, scale: f32, center_x: i32, center_y: i32);
142
143    /// Get the scale & center of the console.
144    /// Returns (scale, center_x, center_y).
145    fn get_scale(&self) -> (f32, i32, i32);
146
147    /// Produces the implementor as an Any that can be matched to determine type and access
148    /// natively.
149    fn as_any(&self) -> &dyn Any;
150
151    /// Produces the implementor as an Any that can be matched to determine type and access
152    /// natively.
153    fn as_any_mut(&mut self) -> &mut dyn Any;
154
155    /// Permits the creation of an arbitrary clipping rectangle. It's a really good idea
156    /// to make sure that this rectangle is entirely valid.
157    fn set_clipping(&mut self, clipping: Option<Rect>);
158
159    /// Returns the current arbitrary clipping rectangle, None if there isn't one.
160    fn get_clipping(&self) -> Option<Rect>;
161
162    /// Returns true if an x/y coordinate is within the console bounds
163    fn in_bounds(&self, x: i32, y: i32) -> bool {
164        let bounds = self.get_char_size();
165        if let Some(clip) = self.get_clipping() {
166            clip.point_in_rect(Point::new(x, y))
167                && x >= 0
168                && x < bounds.0 as i32
169                && y >= 0
170                && y < bounds.1 as i32
171        } else {
172            x >= 0 && x < bounds.0 as i32 && y >= 0 && y < bounds.1 as i32
173        }
174    }
175
176    /// Try to use a coordinate: return Some(the coordinate) if it is valid,
177    /// None if it isn't.
178    fn try_at(&self, x: i32, y: i32) -> Option<usize> {
179        if self.in_bounds(x, y) {
180            Some(self.at(x, y))
181        } else {
182            None
183        }
184    }
185
186    /// Sets ALL tiles foreground alpha (only tiles that exist, in sparse consoles).
187    fn set_all_fg_alpha(&mut self, alpha: f32);
188
189    /// Sets ALL tiles background alpha (only tiles that exist, in sparse consoles).
190    fn set_all_bg_alpha(&mut self, alpha: f32);
191
192    /// Sets ALL tiles foreground alpha (only tiles that exist, in sparse consoles).
193    fn set_all_alpha(&mut self, fg: f32, bg: f32);
194
195    /// Sets the character translation mode
196    fn set_translation_mode(&mut self, mode: CharacterTranslationMode);
197
198    /// Sets the character size of the layer. Be really careful with this.
199    fn set_char_size(&mut self, width: u32, height: u32);
200
201    // Clears the dirty bit
202    fn clear_dirty(&mut self);
203}
204
205pub fn log<S: ToString>(message: S) {
206    crate::hal::log(&message.to_string());
207}