Batch render tooltips, completing section 5.30

This commit is contained in:
Timothy Warren 2022-02-02 14:43:09 -05:00
parent 0b82f12bd8
commit 9fa0b46a16
2 changed files with 22 additions and 15 deletions

View File

@ -12,7 +12,7 @@ pub enum ItemMenuResult {
pub fn menu_box<T: ToString>(draw_batch: &mut DrawBatch, x: i32, y: i32, width: i32, title: T) {
draw_batch.draw_box(
Rect::with_size(x, y - 2, 31, width),
Rect::with_size(x, y - 2, 51, width),
ColorPair::new(colors::WHITE, colors::BLACK),
);
draw_batch.print_color(

View File

@ -1,6 +1,7 @@
use ::rltk::{Point, Rltk};
use ::rltk::prelude::*;
use ::specs::prelude::*;
use super::get_item_display_name;
use crate::components::{Attributes, Duration, Hidden, Name, Pools, StatusEffect};
use crate::{camera, colors, Map};
@ -32,14 +33,10 @@ impl Tooltip {
self.lines.len() as i32 + 2
}
fn render(&self, ctx: &mut Rltk, x: i32, y: i32) {
ctx.draw_box(
x,
y,
self.width() - 1,
self.height() - 1,
colors::WHITE,
colors::BOX_GRAY,
fn render(&self, draw_batch: &mut DrawBatch, x: i32, y: i32) {
draw_batch.draw_box(
Rect::with_size(x, y, self.width() - 1, self.height() - 1),
ColorPair::new(colors::WHITE, colors::BOX_GRAY),
);
for (i, s) in self.lines.iter().enumerate() {
@ -48,13 +45,17 @@ impl Tooltip {
} else {
colors::LIGHT_GRAY
};
ctx.print_color(x + 1, y + i as i32 + 1, col, colors::BLACK, &s);
draw_batch.print_color(
Point::new(x + 1, y + i as i32 + 1),
&s,
ColorPair::new(col, colors::BLACK),
);
}
}
}
pub fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
use ::rltk::{to_cp437, Algorithm2D};
let mut draw_batch = DrawBatch::new();
let (min_x, _max_x, min_y, _max_y) = camera::get_screen_bounds(ecs, ctx);
let map = ecs.fetch::<Map>();
@ -94,7 +95,7 @@ pub fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
}
let mut tip = Tooltip::new();
tip.add(super::get_item_display_name(ecs, entity));
tip.add(get_item_display_name(ecs, entity));
// Comment on attributes
if let Some(attr) = attributes.get(entity) {
@ -164,7 +165,11 @@ pub fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
arrow = to_cp437('←');
arrow_x = mouse_pos.0 + 1;
}
ctx.set(arrow_x, arrow_y, colors::WHITE, colors::BOX_GRAY, arrow);
draw_batch.set(
Point::new(arrow_x, arrow_y),
ColorPair::new(colors::WHITE, colors::BOX_GRAY),
arrow,
);
let mut total_height = 0;
for tt in tip_boxes.iter() {
@ -182,7 +187,9 @@ pub fn draw_tooltips(ecs: &World, ctx: &mut Rltk) {
} else {
mouse_pos.0 + (1 + tt.width())
};
tt.render(ctx, x, y);
tt.render(&mut draw_batch, x, y);
y += tt.height();
}
draw_batch.submit(7000).expect("Failed to render tooltip");
}