-
Notifications
You must be signed in to change notification settings - Fork 125
Expand file tree
/
Copy pathcontext.rs
More file actions
544 lines (500 loc) · 17.7 KB
/
context.rs
File metadata and controls
544 lines (500 loc) · 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
use crate::{
consoles::{ConsoleFrontEnd, DrawBatch, DrawCommand, ScreenScaler},
fonts::FontStore,
FontCharType, TerminalScalingMode,
};
use bevy::{prelude::Resource, sprite::Mesh2dHandle, utils::HashMap};
use bracket_color::prelude::RGBA;
use bracket_geometry::prelude::{Point, Rect};
use parking_lot::Mutex;
#[derive(Resource)]
pub struct BracketContext {
pub(crate) fonts: Vec<FontStore>,
pub(crate) terminals: Mutex<Vec<Box<dyn ConsoleFrontEnd>>>,
pub(crate) current_layer: Mutex<usize>,
pub(crate) color_palette: HashMap<String, RGBA>,
pub fps: f64,
pub frame_time_ms: f64,
pub(crate) mesh_replacement: Vec<(Mesh2dHandle, Mesh2dHandle, bool)>,
pub(crate) scaling_mode: TerminalScalingMode,
command_buffers: Mutex<Vec<(usize, DrawBatch)>>,
mouse_pixels: (f32, f32),
}
impl BracketContext {
pub(crate) fn new(color_palette: HashMap<String, RGBA>) -> Self {
Self {
fonts: Vec::new(),
terminals: Mutex::new(Vec::new()),
current_layer: Mutex::new(0),
color_palette,
fps: 0.0,
frame_time_ms: 0.0,
mesh_replacement: Vec::new(),
scaling_mode: TerminalScalingMode::Stretch,
command_buffers: Mutex::new(Vec::new()),
mouse_pixels: (0.0, 0.0),
}
}
#[inline(always)]
fn current_layer(&self) -> usize {
*self.current_layer.lock()
}
/// Retrieve the current console size, in characters.
/// Applies to the currently active layer.
pub fn get_char_size(&self) -> (i32, i32) {
self.terminals.lock()[self.current_layer()].get_char_size()
}
/// Retrieve the largest natural pixel size from all layers.
/// This is useful when scaling.
pub fn get_pixel_size(&self) -> (f32, f32) {
let mut pixel_size = (0.0, 0.0);
self.terminals.lock().iter().for_each(|t| {
let ts = t.get_pixel_size();
pixel_size.0 = f32::max(pixel_size.0, ts.0);
pixel_size.1 = f32::max(pixel_size.1, ts.1);
});
pixel_size
}
/// Retrieve the pixel size of the largest active font, across all layers.
pub fn largest_font(&self) -> (f32, f32) {
let mut result = (1.0, 1.0);
self.fonts.iter().for_each(|fs| {
result.0 = f32::max(result.0, fs.font_height_pixels.0);
result.1 = f32::max(result.1, fs.font_height_pixels.1);
});
result
}
/// Retrieve the index of a character in the backing array at x/y.
/// WARNING: this may return an invalid/non-existent index.
pub fn at<POS: Into<i32>>(&self, x: POS, y: POS) -> usize {
self.terminals.lock()[self.current_layer()].at(x.into(), y.into())
}
/// Try to obtain the index of a character in the terminal backing array at x/y.
/// If the character is out-of-bounds or clipped out of usefulness, returns None.
/// Otherwise, it returns Some(index)
pub fn try_at<POS: Into<i32>>(&self, x: POS, y: POS) -> Option<usize> {
self.terminals.lock()[self.current_layer()].try_at(x.into(), y.into())
}
/// Get the current clipping rectangle, or None if there isn't one.
pub fn get_clipping(&self) -> Option<Rect> {
self.terminals.lock()[self.current_layer()].get_clipping()
}
/// Set the current clipping rectange. Set to None if you don't want one.
pub fn set_clipping(&self, clipping: Option<Rect>) {
self.terminals.lock()[self.current_layer()].set_clipping(clipping);
}
/// Set the current layer index.
pub fn set_active_console(&self, layer: usize) {
*self.current_layer.lock() = layer;
}
/// Remove all entries from the current layer.
pub fn cls(&self) {
self.terminals.lock()[self.current_layer()].cls();
}
/// Set the background on the current layer to a uniform color.
/// Only useful for Simple consoles.
pub fn cls_bg<C: Into<RGBA>>(&self, color: C) {
self.terminals.lock()[self.current_layer()].cls_bg(color.into());
}
/// Set a character at (x,y) to a specified foreground, background and glyph.
pub fn set<POS: Into<i32>, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
fg: C,
bg: C,
glyph: FontCharType,
) {
self.terminals.lock()[self.current_layer()].set(
x.into(),
y.into(),
fg.into(),
bg.into(),
glyph,
);
}
/// Set just the background color of a terminal cell.
pub fn set_bg<POS: Into<i32>, C: Into<RGBA>>(&self, x: POS, y: POS, bg: C) {
self.terminals.lock()[self.current_layer()].set_bg(x.into(), y.into(), bg.into());
}
/// Print some text to the currently active terminal.
pub fn print<POS: Into<i32>, S: ToString>(&self, x: POS, y: POS, text: S) {
self.terminals.lock()[self.current_layer()].print(x.into(), y.into(), &text.to_string());
}
/// Print some text, centered along the `y` line, to the current terminal.
pub fn print_centered<POS: Into<i32>, S: ToString>(&self, y: POS, text: S) {
self.terminals.lock()[self.current_layer()].print_centered(y.into(), &text.to_string());
}
/// Print some text, centered along the `y` line, to the current terminal in the specified color.
pub fn print_color_centered<POS: Into<i32>, S: ToString, C: Into<RGBA>>(
&self,
y: POS,
fg: C,
bg: C,
text: S,
) {
self.terminals.lock()[self.current_layer()].print_color_centered(
y.into(),
fg.into(),
bg.into(),
&text.to_string(),
);
}
/// Print some text, centered around (x, y) to the current terminal.
pub fn print_centered_at<POS: Into<i32>, S: ToString>(&self, x: POS, y: POS, text: S) {
self.terminals.lock()[self.current_layer()].print_centered_at(
x.into(),
y.into(),
&text.to_string(),
);
}
/// Print some text, cenetered around (x,y) to the current terminal in the specified colors.
pub fn print_color_centered_at<POS: Into<i32>, S: ToString, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
fg: C,
bg: C,
text: S,
) {
self.terminals.lock()[self.current_layer()].print_color_centered_at(
x.into(),
y.into(),
fg.into(),
bg.into(),
&text.to_string(),
)
}
/// Print some text, right justified around (x,y), to the current terminal layer.
pub fn print_right<POS: Into<i32>, S: ToString>(&self, x: POS, y: POS, text: S) {
self.terminals.lock()[self.current_layer()].print_right(
x.into(),
y.into(),
&text.to_string(),
);
}
/// Print some text, right justified at (x,y), to the current terminal layer in the specified colors.
pub fn print_color_right<POS: Into<i32>, S: ToString, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
fg: C,
bg: C,
text: S,
) {
self.terminals.lock()[self.current_layer()].print_color_right(
x.into(),
y.into(),
fg.into(),
bg.into(),
&text.to_string(),
);
}
/// Print some text in color at the specified (x,y) coordinates.
pub fn print_color<POS: Into<i32>, S: ToString, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
text: S,
foreground: C,
background: C,
) {
self.terminals.lock()[self.current_layer()].print_color(
x.into(),
y.into(),
&text.to_string(),
foreground.into(),
background.into(),
)
}
/// Use the pretty-printer to format text for the screen.
pub fn printer<POS: Into<i32>, S: ToString>(
&self,
x: POS,
y: POS,
output: S,
align: crate::consoles::TextAlign,
background: Option<RGBA>,
) {
self.terminals.lock()[self.current_layer()].printer(
self,
x.into(),
y.into(),
&output.to_string(),
align,
background,
);
}
/// Draws a filled box, with single line characters.
pub fn draw_box<POS: Into<i32>, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
width: POS,
height: POS,
fg: C,
bg: C,
) {
self.terminals.lock()[self.current_layer()].draw_box(
x.into(),
y.into(),
width.into(),
height.into(),
fg.into(),
bg.into(),
);
}
/// Draw a hollow box with single line characters.
pub fn draw_hollow_box<POS: Into<i32>, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
width: POS,
height: POS,
fg: C,
bg: C,
) {
self.terminals.lock()[self.current_layer()].draw_hollow_box(
x.into(),
y.into(),
width.into(),
height.into(),
fg.into(),
bg.into(),
);
}
/// Draw a filled box with double-line characters.
pub fn draw_box_double<POS: Into<i32>, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
width: POS,
height: POS,
fg: C,
bg: C,
) {
self.terminals.lock()[self.current_layer()].draw_box_double(
x.into(),
y.into(),
width.into(),
height.into(),
fg.into(),
bg.into(),
);
}
/// Draw an empty box with double-line characters.
pub fn draw_hollow_box_double<POS: Into<i32>, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
width: POS,
height: POS,
fg: C,
bg: C,
) {
self.terminals.lock()[self.current_layer()].draw_hollow_box_double(
x.into(),
y.into(),
width.into(),
height.into(),
fg.into(),
bg.into(),
);
}
/// Fill a region specified by a rectangle with a specified glyph, and colors.
pub fn fill_region<C: Into<RGBA>>(&self, target: Rect, glyph: FontCharType, fg: C, bg: C) {
self.terminals.lock()[self.current_layer()].fill_region(
target,
glyph,
fg.into(),
bg.into(),
);
}
/// Draw a horizontal progress bar.
#[allow(clippy::too_many_arguments)]
pub fn draw_bar_horizontal<POS: Into<i32>, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
width: POS,
n: POS,
max: POS,
fg: C,
bg: C,
) {
self.terminals.lock()[self.current_layer()].draw_bar_horizontal(
x.into(),
y.into(),
width.into(),
n.into(),
max.into(),
fg.into(),
bg.into(),
);
}
/// Draw a vertical progress bar.
#[allow(clippy::too_many_arguments)]
pub fn draw_bar_vertical<POS: Into<i32>, C: Into<RGBA>>(
&self,
x: POS,
y: POS,
height: POS,
n: POS,
max: POS,
fg: C,
bg: C,
) {
self.terminals.lock()[self.current_layer()].draw_bar_vertical(
x.into(),
y.into(),
height.into(),
n.into(),
max.into(),
fg.into(),
bg.into(),
);
}
/// Sets the alpha level on all foreground characters on the current layer.
pub fn set_all_fg_alpha(&self, alpha: f32) {
self.terminals.lock()[self.current_layer()].set_all_bg_alpha(alpha);
}
/// Sets the alpha level on all background characters on the current layer.
pub fn set_all_bg_alpha(&self, alpha: f32) {
self.terminals.lock()[self.current_layer()].set_all_bg_alpha(alpha);
}
/// Sets foreground and background alpha on the current player.
pub fn set_all_alpha(&self, fg: f32, bg: f32) {
self.terminals.lock()[self.current_layer()].set_all_alpha(fg, bg);
}
/// Retrieve a named color from the palette.
/// Note that this replaces the `bracket_color` palette; there were performance problems
/// using it on Bevy.
pub fn get_named_color(&self, color: &str) -> Option<&RGBA> {
self.color_palette.get(color)
}
pub(crate) fn resize_terminals(&mut self, scaler: &ScreenScaler) {
let available_size = scaler.available_size();
let mut lock = self.terminals.lock();
lock.iter_mut().for_each(|t| t.resize(&available_size));
}
/// Create a new draw batch. Note that this is now a context variable,
/// since contexts are `Res` not `ResMut` - and consequently don't
/// affect scheduling.
pub fn new_draw_batch(&self) -> DrawBatch {
DrawBatch::new()
}
/// Submit a batch for rendering.
pub fn submit_batch(&self, z_order: usize, mut batch: DrawBatch) {
if batch.needs_sort {
batch.batch.sort_by(|a, b| a.0.cmp(&b.0));
}
self.command_buffers.lock().push((z_order, batch));
}
/// Submit all draw batches for rendering.
pub fn render_all_batches(&mut self) {
let mut batches = self.command_buffers.lock();
batches.sort_unstable_by(|a, b| a.0.cmp(&b.0));
batches.iter().for_each(|(_, batch)| {
batch.batch.iter().for_each(|(_, cmd)| match cmd {
DrawCommand::ClearScreen => self.cls(),
DrawCommand::ClearToColor { color } => self.cls_bg(*color),
DrawCommand::SetTarget { console } => self.set_active_console(*console),
DrawCommand::Set { pos, color, glyph } => {
self.set(pos.x, pos.y, color.fg, color.bg, *glyph)
}
DrawCommand::SetBackground { pos, bg } => self.set_bg(pos.x, pos.y, *bg),
DrawCommand::Print { pos, text } => self.print(pos.x, pos.y, &text),
DrawCommand::PrintColor { pos, text, color } => {
self.print_color(pos.x, pos.y, &text, color.fg, color.bg)
}
DrawCommand::PrintCentered { y, text } => self.print_centered(*y, &text),
DrawCommand::PrintColorCentered { y, text, color } => {
self.print_color_centered(*y, color.fg, color.bg, &text)
}
DrawCommand::PrintCenteredAt { pos, text } => {
self.print_centered_at(pos.x, pos.y, &text)
}
DrawCommand::PrintColorCenteredAt { pos, text, color } => {
self.print_color_centered_at(pos.x, pos.y, color.fg, color.bg, &text)
}
DrawCommand::PrintRight { pos, text } => self.print_right(pos.x, pos.y, text),
DrawCommand::PrintColorRight { pos, text, color } => {
self.print_color_right(pos.x, pos.y, color.fg, color.bg, text)
}
DrawCommand::Printer {
pos,
text,
align,
background,
} => self.printer(pos.x, pos.y, text, *align, *background),
DrawCommand::Box { pos, color } => self.draw_box(
pos.x1,
pos.y1,
pos.width(),
pos.height(),
color.fg,
color.bg,
),
DrawCommand::HollowBox { pos, color } => self.draw_hollow_box(
pos.x1,
pos.y1,
pos.width(),
pos.height(),
color.fg,
color.bg,
),
DrawCommand::DoubleBox { pos, color } => self.draw_box_double(
pos.x1,
pos.y1,
pos.width(),
pos.height(),
color.fg,
color.bg,
),
DrawCommand::HollowDoubleBox { pos, color } => self.draw_hollow_box_double(
pos.x1,
pos.y1,
pos.width(),
pos.height(),
color.fg,
color.bg,
),
DrawCommand::FillRegion { pos, color, glyph } => {
self.fill_region::<RGBA>(*pos, *glyph, color.fg, color.bg)
}
DrawCommand::BarHorizontal {
pos,
width,
n,
max,
color,
} => self.draw_bar_horizontal(pos.x, pos.y, *width, *n, *max, color.fg, color.bg),
DrawCommand::BarVertical {
pos,
height,
n,
max,
color,
} => self.draw_bar_vertical(pos.x, pos.y, *height, *n, *max, color.fg, color.bg),
DrawCommand::SetClipping { clip } => self.set_clipping(*clip),
DrawCommand::SetFgAlpha { alpha } => self.set_all_fg_alpha(*alpha),
DrawCommand::SetBgAlpha { alpha } => self.set_all_fg_alpha(*alpha),
DrawCommand::SetAllAlpha { fg, bg } => self.set_all_alpha(*fg, *bg),
})
});
batches.clear();
}
pub(crate) fn set_mouse_pixel_position(&mut self, pos: (f32, f32), scaler: &ScreenScaler) {
self.mouse_pixels = pos;
self.terminals
.lock()
.iter_mut()
.for_each(|t| t.set_mouse_position(pos, scaler));
}
pub fn get_mouse_position_in_pixels(&self) -> (f32, f32) {
self.mouse_pixels
}
pub fn get_mouse_position_for_current_layer(&self) -> Point {
self.terminals.lock()[self.current_layer()].get_mouse_position_for_current_layer()
}
}