mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-25 11:44:25 +03:00
bam, variable font size
This commit is contained in:
parent
008b2f9de5
commit
320c91faf7
@ -62,7 +62,6 @@ screenshot tool after each render. Sometimes the quick hack works perfectly.
|
||||
I would consider cleaning up ezgui and publishing it as a generally usable
|
||||
crate, except it's pretty crippled:
|
||||
|
||||
- the text rendering is very primitive; font size is fixed
|
||||
- basic widgets like a scrolling text box, list with radio buttons, and tables
|
||||
are missing
|
||||
- The imperative style makes it quite easy for different parts of the UI to
|
||||
|
@ -199,7 +199,7 @@ impl GUI for GameState {
|
||||
}
|
||||
}
|
||||
|
||||
const SPEED: Speed = Speed::const_meters_per_second(50.0);
|
||||
const SPEED: Speed = Speed::const_meters_per_second(20.0);
|
||||
|
||||
struct Screensaver {
|
||||
line: Line,
|
||||
|
@ -56,7 +56,12 @@ fn panel(ctx: &mut PluginCtx) -> ShowScoreState {
|
||||
}
|
||||
|
||||
fn summarize(txt: &mut Text, summary: ScoreSummary) {
|
||||
txt.add_styled_line("Walking".to_string(), None, Some(Color::RED.alpha(0.8)), None);
|
||||
txt.add_styled_line(
|
||||
"Walking".to_string(),
|
||||
None,
|
||||
Some(Color::RED.alpha(0.8)),
|
||||
None,
|
||||
);
|
||||
txt.add_line(format!(
|
||||
" {}/{} trips done",
|
||||
(summary.total_walking_trips - summary.pending_walking_trips),
|
||||
@ -64,7 +69,12 @@ fn summarize(txt: &mut Text, summary: ScoreSummary) {
|
||||
));
|
||||
txt.add_line(format!(" {} total", summary.total_walking_trip_time));
|
||||
|
||||
txt.add_styled_line("Driving".to_string(), None, Some(Color::BLUE.alpha(0.8)), None);
|
||||
txt.add_styled_line(
|
||||
"Driving".to_string(),
|
||||
None,
|
||||
Some(Color::BLUE.alpha(0.8)),
|
||||
None,
|
||||
);
|
||||
txt.add_line(format!(
|
||||
" {}/{} trips done",
|
||||
(summary.total_driving_trips - summary.pending_driving_trips),
|
||||
|
@ -155,7 +155,7 @@ impl<'a> GfxCtx<'a> {
|
||||
};
|
||||
let y1 = match vert {
|
||||
VerticalAlignment::Top => 0.0,
|
||||
VerticalAlignment::BelowTopMenu => self.canvas.line_height(text::FONT_SIZE),
|
||||
VerticalAlignment::BelowTopMenu => self.canvas.top_menu_height(),
|
||||
VerticalAlignment::Center => (self.canvas.window_height - height) / 2.0,
|
||||
VerticalAlignment::Bottom => self.canvas.window_height - height,
|
||||
};
|
||||
|
@ -120,33 +120,31 @@ impl Text {
|
||||
}
|
||||
|
||||
pub(crate) fn dims(&self, canvas: &Canvas) -> (f64, f64) {
|
||||
// Always use the max height, since other stuff like menus assume a fixed height.
|
||||
let height = (self.lines.len() as f64) * canvas.line_height(FONT_SIZE);
|
||||
let mut max_width = 0;
|
||||
let mut height = 0.0;
|
||||
|
||||
let mut glyphs = canvas.glyphs.borrow_mut();
|
||||
let width = f64::from(
|
||||
self.lines
|
||||
.iter()
|
||||
.map(|(_, l)| {
|
||||
let full_line = l.iter().fold(String::new(), |mut so_far, span| {
|
||||
so_far.push_str(&span.text);
|
||||
so_far
|
||||
});
|
||||
for (_, line) in &self.lines {
|
||||
let mut full_line = String::new();
|
||||
let mut max_size = 0;
|
||||
for span in line {
|
||||
full_line.push_str(&span.text);
|
||||
max_size = max_size.max(span.size);
|
||||
}
|
||||
// Empty lines or whitespace-only lines effectively have 0 width.
|
||||
glyphs
|
||||
let width = canvas
|
||||
.glyphs
|
||||
.borrow_mut()
|
||||
.pixel_bounds(Section {
|
||||
text: &full_line,
|
||||
scale: Scale::uniform(FONT_SIZE as f32),
|
||||
scale: Scale::uniform(max_size as f32),
|
||||
..Section::default()
|
||||
})
|
||||
.map(|rect| rect.width())
|
||||
.unwrap_or(0)
|
||||
})
|
||||
.max()
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
(width, height)
|
||||
.unwrap_or(0);
|
||||
max_width = max_width.max(width);
|
||||
height += canvas.line_height(max_size);
|
||||
}
|
||||
(max_width as f64, height)
|
||||
}
|
||||
}
|
||||
|
||||
@ -173,32 +171,33 @@ pub fn draw_text_bubble(
|
||||
|
||||
let mut y = top_left.y;
|
||||
for (line_color, line) in &txt.lines {
|
||||
let mut max_size = 0;
|
||||
let section = VariedSection {
|
||||
screen_position: (top_left.x as f32, y as f32),
|
||||
text: line
|
||||
.into_iter()
|
||||
.map(|span| SectionText {
|
||||
.map(|span| {
|
||||
max_size = max_size.max(span.size);
|
||||
SectionText {
|
||||
text: &span.text,
|
||||
color: span.fg_color.0,
|
||||
scale: Scale::uniform(FONT_SIZE as f32),
|
||||
scale: Scale::uniform(span.size as f32),
|
||||
..SectionText::default()
|
||||
}
|
||||
})
|
||||
.collect(),
|
||||
..VariedSection::default()
|
||||
};
|
||||
let height = g.canvas.line_height(max_size);
|
||||
|
||||
if let Some(c) = line_color {
|
||||
g.draw_polygon(
|
||||
*c,
|
||||
&Polygon::rectangle_topleft(
|
||||
Pt2D::new(top_left.x, y),
|
||||
total_width,
|
||||
g.canvas.line_height(FONT_SIZE),
|
||||
),
|
||||
&Polygon::rectangle_topleft(Pt2D::new(top_left.x, y), total_width, height),
|
||||
);
|
||||
}
|
||||
|
||||
y += g.canvas.line_height(FONT_SIZE);
|
||||
y += height;
|
||||
g.canvas.glyphs.borrow_mut().queue(section);
|
||||
}
|
||||
|
||||
|
@ -66,7 +66,7 @@ impl<T: Clone> Menu<T> {
|
||||
} else {
|
||||
total_width
|
||||
};
|
||||
ScreenPt::new(canvas.window_width - w, canvas.line_height(text::FONT_SIZE))
|
||||
ScreenPt::new(canvas.window_width - w, canvas.top_menu_height())
|
||||
}
|
||||
};
|
||||
|
||||
@ -221,7 +221,12 @@ impl<T: Clone> Menu<T> {
|
||||
None,
|
||||
);
|
||||
} else {
|
||||
txt.add_styled_line(choice.to_string(), Some(text::INACTIVE_CHOICE_COLOR), bg, None);
|
||||
txt.add_styled_line(
|
||||
choice.to_string(),
|
||||
Some(text::INACTIVE_CHOICE_COLOR),
|
||||
bg,
|
||||
None,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ impl TopMenu {
|
||||
x1: 0.0,
|
||||
y1: 0.0,
|
||||
x2: g.canvas.window_width,
|
||||
y2: g.canvas.line_height(text::FONT_SIZE),
|
||||
y2: g.canvas.top_menu_height(),
|
||||
});
|
||||
|
||||
g.fork_screenspace();
|
||||
@ -135,7 +135,7 @@ impl TopMenu {
|
||||
&Polygon::rectangle_topleft(
|
||||
Pt2D::new(0.0, 0.0),
|
||||
g.canvas.window_width,
|
||||
g.canvas.line_height(text::FONT_SIZE),
|
||||
g.canvas.top_menu_height(),
|
||||
),
|
||||
);
|
||||
g.unfork();
|
||||
|
Loading…
Reference in New Issue
Block a user