bam, variable font size

This commit is contained in:
Dustin Carlino 2019-04-22 20:09:08 -07:00
parent 008b2f9de5
commit 320c91faf7
7 changed files with 58 additions and 45 deletions

View File

@ -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 I would consider cleaning up ezgui and publishing it as a generally usable
crate, except it's pretty crippled: 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 - basic widgets like a scrolling text box, list with radio buttons, and tables
are missing are missing
- The imperative style makes it quite easy for different parts of the UI to - The imperative style makes it quite easy for different parts of the UI to

View File

@ -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 { struct Screensaver {
line: Line, line: Line,

View File

@ -56,7 +56,12 @@ fn panel(ctx: &mut PluginCtx) -> ShowScoreState {
} }
fn summarize(txt: &mut Text, summary: ScoreSummary) { 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!( txt.add_line(format!(
" {}/{} trips done", " {}/{} trips done",
(summary.total_walking_trips - summary.pending_walking_trips), (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_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!( txt.add_line(format!(
" {}/{} trips done", " {}/{} trips done",
(summary.total_driving_trips - summary.pending_driving_trips), (summary.total_driving_trips - summary.pending_driving_trips),

View File

@ -155,7 +155,7 @@ impl<'a> GfxCtx<'a> {
}; };
let y1 = match vert { let y1 = match vert {
VerticalAlignment::Top => 0.0, 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::Center => (self.canvas.window_height - height) / 2.0,
VerticalAlignment::Bottom => self.canvas.window_height - height, VerticalAlignment::Bottom => self.canvas.window_height - height,
}; };

View File

@ -120,33 +120,31 @@ impl Text {
} }
pub(crate) fn dims(&self, canvas: &Canvas) -> (f64, f64) { pub(crate) fn dims(&self, canvas: &Canvas) -> (f64, f64) {
// Always use the max height, since other stuff like menus assume a fixed height. let mut max_width = 0;
let height = (self.lines.len() as f64) * canvas.line_height(FONT_SIZE); let mut height = 0.0;
let mut glyphs = canvas.glyphs.borrow_mut(); for (_, line) in &self.lines {
let width = f64::from( let mut full_line = String::new();
self.lines let mut max_size = 0;
.iter() for span in line {
.map(|(_, l)| { full_line.push_str(&span.text);
let full_line = l.iter().fold(String::new(), |mut so_far, span| { max_size = max_size.max(span.size);
so_far.push_str(&span.text); }
so_far
});
// Empty lines or whitespace-only lines effectively have 0 width. // Empty lines or whitespace-only lines effectively have 0 width.
glyphs let width = canvas
.glyphs
.borrow_mut()
.pixel_bounds(Section { .pixel_bounds(Section {
text: &full_line, text: &full_line,
scale: Scale::uniform(FONT_SIZE as f32), scale: Scale::uniform(max_size as f32),
..Section::default() ..Section::default()
}) })
.map(|rect| rect.width()) .map(|rect| rect.width())
.unwrap_or(0) .unwrap_or(0);
}) max_width = max_width.max(width);
.max() height += canvas.line_height(max_size);
.unwrap(), }
); (max_width as f64, height)
(width, height)
} }
} }
@ -173,32 +171,33 @@ pub fn draw_text_bubble(
let mut y = top_left.y; let mut y = top_left.y;
for (line_color, line) in &txt.lines { for (line_color, line) in &txt.lines {
let mut max_size = 0;
let section = VariedSection { let section = VariedSection {
screen_position: (top_left.x as f32, y as f32), screen_position: (top_left.x as f32, y as f32),
text: line text: line
.into_iter() .into_iter()
.map(|span| SectionText { .map(|span| {
max_size = max_size.max(span.size);
SectionText {
text: &span.text, text: &span.text,
color: span.fg_color.0, color: span.fg_color.0,
scale: Scale::uniform(FONT_SIZE as f32), scale: Scale::uniform(span.size as f32),
..SectionText::default() ..SectionText::default()
}
}) })
.collect(), .collect(),
..VariedSection::default() ..VariedSection::default()
}; };
let height = g.canvas.line_height(max_size);
if let Some(c) = line_color { if let Some(c) = line_color {
g.draw_polygon( g.draw_polygon(
*c, *c,
&Polygon::rectangle_topleft( &Polygon::rectangle_topleft(Pt2D::new(top_left.x, y), total_width, height),
Pt2D::new(top_left.x, y),
total_width,
g.canvas.line_height(FONT_SIZE),
),
); );
} }
y += g.canvas.line_height(FONT_SIZE); y += height;
g.canvas.glyphs.borrow_mut().queue(section); g.canvas.glyphs.borrow_mut().queue(section);
} }

View File

@ -66,7 +66,7 @@ impl<T: Clone> Menu<T> {
} else { } else {
total_width 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, None,
); );
} else { } 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,
);
} }
} }
} }

View File

@ -126,7 +126,7 @@ impl TopMenu {
x1: 0.0, x1: 0.0,
y1: 0.0, y1: 0.0,
x2: g.canvas.window_width, x2: g.canvas.window_width,
y2: g.canvas.line_height(text::FONT_SIZE), y2: g.canvas.top_menu_height(),
}); });
g.fork_screenspace(); g.fork_screenspace();
@ -135,7 +135,7 @@ impl TopMenu {
&Polygon::rectangle_topleft( &Polygon::rectangle_topleft(
Pt2D::new(0.0, 0.0), Pt2D::new(0.0, 0.0),
g.canvas.window_width, g.canvas.window_width,
g.canvas.line_height(text::FONT_SIZE), g.canvas.top_menu_height(),
), ),
); );
g.unfork(); g.unfork();