mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-29 17:34:58 +03:00
refactor: make text_dims return ScreenDims. also, update the logo image
This commit is contained in:
parent
a4d5aa766b
commit
dfd036300b
@ -1,4 +1,4 @@
|
||||
use crate::{Color, ScreenPt, ScreenRectangle, Text, UserInput};
|
||||
use crate::{Color, ScreenDims, ScreenPt, ScreenRectangle, Text, UserInput};
|
||||
use abstutil::Timer;
|
||||
use geom::{Bounds, Circle, Pt2D};
|
||||
use glium::texture::Texture2dArray;
|
||||
@ -181,8 +181,7 @@ impl Canvas {
|
||||
b
|
||||
}
|
||||
|
||||
// TODO Maybe return ScreenDims
|
||||
pub fn text_dims(&self, txt: &Text) -> (f64, f64) {
|
||||
pub fn text_dims(&self, txt: &Text) -> ScreenDims {
|
||||
txt.dims(self)
|
||||
}
|
||||
|
||||
|
@ -211,26 +211,26 @@ impl<'a> GfxCtx<'a> {
|
||||
txt: &Text,
|
||||
(horiz, vert): (HorizontalAlignment, VerticalAlignment),
|
||||
) {
|
||||
let (mut width, height) = self.text_dims(&txt);
|
||||
let mut dims = self.text_dims(&txt);
|
||||
let x1 = match horiz {
|
||||
HorizontalAlignment::Left => 0.0,
|
||||
HorizontalAlignment::Center => (self.canvas.window_width - width) / 2.0,
|
||||
HorizontalAlignment::Right => self.canvas.window_width - width,
|
||||
HorizontalAlignment::Center => (self.canvas.window_width - dims.width) / 2.0,
|
||||
HorizontalAlignment::Right => self.canvas.window_width - dims.width,
|
||||
HorizontalAlignment::FillScreen => {
|
||||
width = self.canvas.window_width;
|
||||
dims.width = self.canvas.window_width;
|
||||
0.0
|
||||
}
|
||||
};
|
||||
let y1 = match vert {
|
||||
VerticalAlignment::Top => 0.0,
|
||||
VerticalAlignment::Center => (self.canvas.window_height - height) / 2.0,
|
||||
VerticalAlignment::Bottom => self.canvas.window_height - height,
|
||||
VerticalAlignment::Center => (self.canvas.window_height - dims.height) / 2.0,
|
||||
VerticalAlignment::Bottom => self.canvas.window_height - dims.height,
|
||||
};
|
||||
self.canvas.mark_covered_area(text::draw_text_bubble(
|
||||
self,
|
||||
ScreenPt::new(x1, y1),
|
||||
txt,
|
||||
(width, height),
|
||||
dims,
|
||||
));
|
||||
}
|
||||
|
||||
@ -240,30 +240,30 @@ impl<'a> GfxCtx<'a> {
|
||||
|
||||
// TODO Rename these draw_nonblocking_text_*
|
||||
pub fn draw_text_at(&mut self, txt: &Text, map_pt: Pt2D) {
|
||||
let (width, height) = self.text_dims(&txt);
|
||||
let dims = self.text_dims(&txt);
|
||||
let pt = self.canvas.map_to_screen(map_pt);
|
||||
text::draw_text_bubble(
|
||||
self,
|
||||
ScreenPt::new(pt.x - (width / 2.0), pt.y - (height / 2.0)),
|
||||
ScreenPt::new(pt.x - (dims.width / 2.0), pt.y - (dims.height / 2.0)),
|
||||
txt,
|
||||
(width, height),
|
||||
dims,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn draw_text_at_mapspace(&mut self, txt: &Text, map_pt: Pt2D) {
|
||||
let (width, height) = self.text_dims(&txt);
|
||||
let dims = self.text_dims(&txt);
|
||||
text::draw_text_bubble_mapspace(
|
||||
self,
|
||||
Pt2D::new(
|
||||
map_pt.x() - (width / (2.0 * text::SCALE_DOWN)),
|
||||
map_pt.y() - (height / (2.0 * text::SCALE_DOWN)),
|
||||
map_pt.x() - (dims.width / (2.0 * text::SCALE_DOWN)),
|
||||
map_pt.y() - (dims.height / (2.0 * text::SCALE_DOWN)),
|
||||
),
|
||||
txt,
|
||||
(width, height),
|
||||
dims,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn text_dims(&self, txt: &Text) -> (f64, f64) {
|
||||
pub fn text_dims(&self, txt: &Text) -> ScreenDims {
|
||||
txt.dims(&self.canvas)
|
||||
}
|
||||
|
||||
@ -274,15 +274,15 @@ impl<'a> GfxCtx<'a> {
|
||||
}
|
||||
|
||||
pub fn draw_mouse_tooltip(&mut self, txt: &Text) {
|
||||
let (width, height) = self.text_dims(&txt);
|
||||
let dims = self.text_dims(&txt);
|
||||
// TODO Maybe also consider the cursor as a valid center. After context menus go away, this
|
||||
// makes even more sense.
|
||||
let pt = ScreenDims::new(width, height).top_left_for_corner(
|
||||
let pt = dims.top_left_for_corner(
|
||||
ScreenPt::new(self.canvas.cursor_x, self.canvas.cursor_y),
|
||||
&self.canvas,
|
||||
);
|
||||
// No need to cover the tooltip; this tooltip follows the mouse anyway.
|
||||
text::draw_text_bubble(self, pt, txt, (width, height));
|
||||
text::draw_text_bubble(self, pt, txt, dims);
|
||||
}
|
||||
|
||||
pub fn screen_to_map(&self, pt: ScreenPt) -> Pt2D {
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{Canvas, Color, GfxCtx, ScreenPt, ScreenRectangle};
|
||||
use crate::{Canvas, Color, GfxCtx, ScreenDims, ScreenPt, ScreenRectangle};
|
||||
use geom::{Distance, Polygon, Pt2D};
|
||||
use glium_glyph::glyph_brush::rusttype::Scale;
|
||||
use glium_glyph::glyph_brush::GlyphCruncher;
|
||||
@ -155,7 +155,7 @@ impl Text {
|
||||
self.lines.extend(other.lines.clone())
|
||||
}
|
||||
|
||||
pub(crate) fn dims(&self, canvas: &Canvas) -> (f64, f64) {
|
||||
pub(crate) fn dims(&self, canvas: &Canvas) -> ScreenDims {
|
||||
let mut max_width = 0;
|
||||
let mut height = 0.0;
|
||||
|
||||
@ -180,7 +180,7 @@ impl Text {
|
||||
max_width = max_width.max(width);
|
||||
height += canvas.line_height(max_size);
|
||||
}
|
||||
(
|
||||
ScreenDims::new(
|
||||
self.override_width.unwrap_or_else(|| f64::from(max_width)),
|
||||
self.override_height.unwrap_or_else(|| height),
|
||||
)
|
||||
@ -192,7 +192,7 @@ pub fn draw_text_bubble(
|
||||
top_left: ScreenPt,
|
||||
txt: &Text,
|
||||
// Callers almost always calculate this anyway
|
||||
(total_width, total_height): (f64, f64),
|
||||
total_dims: ScreenDims,
|
||||
) -> ScreenRectangle {
|
||||
// TODO Is it expensive to constantly change uniforms and the shader program?
|
||||
g.fork_screenspace();
|
||||
@ -202,8 +202,8 @@ pub fn draw_text_bubble(
|
||||
c,
|
||||
&Polygon::rectangle_topleft(
|
||||
top_left.to_pt(),
|
||||
Distance::meters(total_width),
|
||||
Distance::meters(total_height),
|
||||
Distance::meters(total_dims.width),
|
||||
Distance::meters(total_dims.height),
|
||||
),
|
||||
);
|
||||
}
|
||||
@ -238,7 +238,7 @@ pub fn draw_text_bubble(
|
||||
*c,
|
||||
&Polygon::rectangle_topleft(
|
||||
Pt2D::new(top_left.x, y),
|
||||
Distance::meters(total_width),
|
||||
Distance::meters(total_dims.width),
|
||||
Distance::meters(height),
|
||||
),
|
||||
);
|
||||
@ -250,12 +250,7 @@ pub fn draw_text_bubble(
|
||||
|
||||
g.unfork();
|
||||
|
||||
ScreenRectangle {
|
||||
x1: top_left.x,
|
||||
y1: top_left.y,
|
||||
x2: top_left.x + total_width,
|
||||
y2: top_left.y + total_height,
|
||||
}
|
||||
ScreenRectangle::top_left(top_left, total_dims)
|
||||
}
|
||||
|
||||
pub fn draw_text_bubble_mapspace(
|
||||
@ -263,15 +258,15 @@ pub fn draw_text_bubble_mapspace(
|
||||
top_left: Pt2D,
|
||||
txt: &Text,
|
||||
// Callers almost always calculate this anyway
|
||||
(total_width, total_height): (f64, f64),
|
||||
total_dims: ScreenDims,
|
||||
) {
|
||||
if let Some(c) = txt.bg_color {
|
||||
g.draw_polygon(
|
||||
c,
|
||||
&Polygon::rectangle_topleft(
|
||||
top_left,
|
||||
Distance::meters(total_width / SCALE_DOWN),
|
||||
Distance::meters(total_height / SCALE_DOWN),
|
||||
Distance::meters(total_dims.width / SCALE_DOWN),
|
||||
Distance::meters(total_dims.height / SCALE_DOWN),
|
||||
),
|
||||
);
|
||||
}
|
||||
@ -308,7 +303,7 @@ pub fn draw_text_bubble_mapspace(
|
||||
*c,
|
||||
&Polygon::rectangle_topleft(
|
||||
Pt2D::new(top_left.x(), y),
|
||||
Distance::meters(total_width / SCALE_DOWN),
|
||||
Distance::meters(total_dims.width / SCALE_DOWN),
|
||||
Distance::meters(height),
|
||||
),
|
||||
);
|
||||
|
@ -205,10 +205,10 @@ impl TextButton {
|
||||
selected_bg_color: Color,
|
||||
ctx: &EventCtx,
|
||||
) -> TextButton {
|
||||
let (w, h) = ctx.canvas.text_dims(&text);
|
||||
let dims = ctx.canvas.text_dims(&text);
|
||||
let geom = Polygon::rounded_rectangle(
|
||||
Distance::meters(w + 2.0 * HORIZ_PADDING),
|
||||
Distance::meters(h + 2.0 * VERT_PADDING),
|
||||
Distance::meters(dims.width + 2.0 * HORIZ_PADDING),
|
||||
Distance::meters(dims.height + 2.0 * VERT_PADDING),
|
||||
Distance::meters(VERT_PADDING),
|
||||
);
|
||||
|
||||
@ -218,7 +218,10 @@ impl TextButton {
|
||||
text: text.no_bg(),
|
||||
rect: ScreenRectangle::top_left(
|
||||
ScreenPt::new(0.0, 0.0),
|
||||
ScreenDims::new(w + 2.0 * HORIZ_PADDING, h + 2.0 * VERT_PADDING),
|
||||
ScreenDims::new(
|
||||
dims.width + 2.0 * HORIZ_PADDING,
|
||||
dims.height + 2.0 * VERT_PADDING,
|
||||
),
|
||||
),
|
||||
|
||||
hovering: false,
|
||||
|
@ -54,8 +54,8 @@ impl ContextMenuImpl {
|
||||
top_left: ScreenPt::new(0.0, 0.0),
|
||||
dims: ScreenDims::new(0.0, 0.0),
|
||||
};
|
||||
let (w, h) = canvas.text_dims(&m.calculate_txt());
|
||||
m.dims = ScreenDims::new(w, h);
|
||||
let dims = canvas.text_dims(&m.calculate_txt());
|
||||
m.dims = dims;
|
||||
m.top_left = m.dims.top_left_for_corner(corner, canvas);
|
||||
|
||||
m
|
||||
|
@ -93,7 +93,7 @@ impl ModalMenu {
|
||||
let cursor = ctx.canvas.get_cursor_in_screen_space();
|
||||
self.hovering_idx = None;
|
||||
let mut top_left = self.top_left;
|
||||
top_left.y += ctx.canvas.line_height + ctx.canvas.text_dims(&self.info).1;
|
||||
top_left.y += ctx.canvas.line_height + ctx.canvas.text_dims(&self.info).height;
|
||||
for idx in 0..self.choices.len() {
|
||||
let rect = ScreenRectangle {
|
||||
x1: top_left.x,
|
||||
@ -269,8 +269,7 @@ impl ModalMenu {
|
||||
}
|
||||
|
||||
fn recalculate_dims(&mut self, ctx: &EventCtx) {
|
||||
let (w, h) = ctx.canvas.text_dims(&self.calculate_txt());
|
||||
self.dims = ScreenDims::new(w, h);
|
||||
self.dims = ctx.canvas.text_dims(&self.calculate_txt());
|
||||
}
|
||||
|
||||
fn calculate_txt(&self) -> Text {
|
||||
|
@ -32,11 +32,9 @@ impl JustDraw {
|
||||
|
||||
// TODO I wish this wasn't a separate type...
|
||||
pub fn text(text: Text, ctx: &EventCtx) -> JustDrawText {
|
||||
let (w, h) = ctx.canvas.text_dims(&text);
|
||||
JustDrawText {
|
||||
dims: ctx.canvas.text_dims(&text),
|
||||
text,
|
||||
|
||||
dims: ScreenDims::new(w, h),
|
||||
top_left: ScreenPt::new(0.0, 0.0),
|
||||
}
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ impl<T: Clone> PopupMenu<T> {
|
||||
if ctx.redo_mouseover() {
|
||||
let cursor = ctx.canvas.get_cursor_in_screen_space();
|
||||
let mut top_left = self.top_left;
|
||||
top_left.y += ctx.canvas.text_dims(&self.prompt).1;
|
||||
top_left.y += ctx.canvas.text_dims(&self.prompt).height;
|
||||
for idx in 0..self.choices.len() {
|
||||
let rect = ScreenRectangle {
|
||||
x1: top_left.x,
|
||||
@ -76,7 +76,7 @@ impl<T: Clone> PopupMenu<T> {
|
||||
if ctx.input.left_mouse_button_pressed() {
|
||||
// Did we actually click the entry?
|
||||
let mut top_left = self.top_left;
|
||||
top_left.y += ctx.canvas.text_dims(&self.prompt).1;
|
||||
top_left.y += ctx.canvas.text_dims(&self.prompt).height;
|
||||
top_left.y += ctx.canvas.line_height * (self.current_idx as f64);
|
||||
let rect = ScreenRectangle {
|
||||
x1: top_left.x,
|
||||
@ -138,8 +138,7 @@ impl<T: Clone> PopupMenu<T> {
|
||||
}
|
||||
|
||||
fn recalculate_dims(&mut self, ctx: &EventCtx) {
|
||||
let (w, h) = ctx.canvas.text_dims(&self.calculate_txt());
|
||||
self.dims = ScreenDims::new(w, h);
|
||||
self.dims = ctx.canvas.text_dims(&self.calculate_txt());
|
||||
}
|
||||
|
||||
fn calculate_txt(&self) -> Text {
|
||||
|
@ -287,14 +287,14 @@ impl NewScroller {
|
||||
pub fn new(geom: GeomBatch, multi_txt: MultiText, zoom: f64, ctx: &EventCtx) -> NewScroller {
|
||||
let mut total_dims = geom.get_dims();
|
||||
for (txt, top_left) in &multi_txt.list {
|
||||
let (mut w, mut h) = ctx.canvas.text_dims(txt);
|
||||
w += top_left.x;
|
||||
h += top_left.y;
|
||||
if w > total_dims.width {
|
||||
total_dims.width = w;
|
||||
let mut dims = ctx.canvas.text_dims(txt);
|
||||
dims.width += top_left.x;
|
||||
dims.height += top_left.y;
|
||||
if dims.width > total_dims.width {
|
||||
total_dims.width = dims.width;
|
||||
}
|
||||
if h > total_dims.height {
|
||||
total_dims.height = h;
|
||||
if dims.height > total_dims.height {
|
||||
total_dims.height = dims.height;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -29,9 +29,7 @@ impl TextBox {
|
||||
dims: ScreenDims::new(0.0, 0.0),
|
||||
};
|
||||
// TODO Assume the dims never exceed the prompt width?
|
||||
// TODO Return dims directly
|
||||
let (w, h) = canvas.text_dims(&tb.get_text());
|
||||
tb.dims = ScreenDims::new(w, h);
|
||||
tb.dims = canvas.text_dims(&tb.get_text());
|
||||
tb
|
||||
}
|
||||
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 16 KiB After Width: | Height: | Size: 12 KiB |
@ -254,8 +254,8 @@ fn bar_chart(g: &mut GfxCtx, data: &BTreeMap<String, Estimate>) {
|
||||
Line(")"),
|
||||
]);
|
||||
}
|
||||
let (txt_width, total_height) = g.text_dims(&labels);
|
||||
let line_height = total_height / ((data.len() as f64) - 1.0);
|
||||
let txt_dims = g.text_dims(&labels);
|
||||
let line_height = txt_dims.height / ((data.len() as f64) - 1.0);
|
||||
labels.add(Line(format!("{} samples", prettyprint_usize(sum))).size(40));
|
||||
|
||||
// This is, uh, pixels. :P
|
||||
@ -266,8 +266,8 @@ fn bar_chart(g: &mut GfxCtx, data: &BTreeMap<String, Estimate>) {
|
||||
Color::grey(0.3),
|
||||
&Polygon::rectangle_topleft(
|
||||
Pt2D::new(0.0, 0.0),
|
||||
Distance::meters(txt_width + 1.2 * max_bar_width),
|
||||
Distance::meters(total_height + line_height),
|
||||
Distance::meters(txt_dims.width + 1.2 * max_bar_width),
|
||||
Distance::meters(txt_dims.height + line_height),
|
||||
),
|
||||
);
|
||||
g.draw_blocking_text(&labels, (HorizontalAlignment::Left, VerticalAlignment::Top));
|
||||
@ -282,7 +282,7 @@ fn bar_chart(g: &mut GfxCtx, data: &BTreeMap<String, Estimate>) {
|
||||
g.draw_polygon(
|
||||
rotating_color_total(idx, data.len() - 1),
|
||||
&Polygon::rectangle_topleft(
|
||||
Pt2D::new(txt_width, (0.1 + (idx as f64)) * line_height),
|
||||
Pt2D::new(txt_dims.width, (0.1 + (idx as f64)) * line_height),
|
||||
Distance::meters(this_width),
|
||||
Distance::meters(0.8 * line_height),
|
||||
),
|
||||
@ -295,7 +295,7 @@ fn bar_chart(g: &mut GfxCtx, data: &BTreeMap<String, Estimate>) {
|
||||
Color::BLACK,
|
||||
&Polygon::rectangle_topleft(
|
||||
Pt2D::new(
|
||||
txt_width + this_width - half_moe_width,
|
||||
txt_dims.width + this_width - half_moe_width,
|
||||
(0.4 + (idx as f64)) * line_height,
|
||||
),
|
||||
2.0 * Distance::meters(half_moe_width),
|
||||
|
Loading…
Reference in New Issue
Block a user