invert the default for text background. none by default, should be an exception to use it. planning to support background more generally through some kind of widget layouting.

This commit is contained in:
Dustin Carlino 2019-12-13 15:45:23 -08:00
parent e307e1f56d
commit 442a0509dd
26 changed files with 67 additions and 74 deletions

View File

@ -74,7 +74,7 @@ impl Text {
pub fn new() -> Text {
Text {
lines: Vec::new(),
bg_color: Some(BG_COLOR),
bg_color: None,
override_width: None,
override_height: None,
}
@ -86,20 +86,15 @@ impl Text {
txt
}
// TODO nope
pub fn prompt(line: &str) -> Text {
let mut txt = Text::new();
let mut txt = Text::new().with_bg();
txt.add_highlighted(Line(line), PROMPT_COLOR);
txt
}
pub fn bg(mut self, color: Color) -> Text {
self.bg_color = Some(color);
self
}
pub fn no_bg(mut self) -> Text {
self.bg_color = None;
pub fn with_bg(mut self) -> Text {
assert!(self.bg_color.is_none());
self.bg_color = Some(BG_COLOR);
self
}

View File

@ -39,11 +39,11 @@ impl Button {
draw_hovered,
hotkey,
tooltip: if let Some(key) = hotkey {
let mut txt = Text::from(Line(key.describe()).fg(text::HOTKEY_COLOR));
let mut txt = Text::from(Line(key.describe()).fg(text::HOTKEY_COLOR)).with_bg();
txt.append(Line(format!(" - {}", tooltip)));
txt
} else {
Text::from(Line(tooltip))
Text::from(Line(tooltip)).with_bg()
},
hitbox,
@ -247,14 +247,13 @@ impl Button {
}
pub fn text(
mut text: Text,
text: Text,
unselected_bg_color: Color,
selected_bg_color: Color,
hotkey: Option<MultiKey>,
tooltip: &str,
ctx: &EventCtx,
) -> Button {
text = text.no_bg();
let dims = ctx.text_dims(&text);
let geom = Polygon::rounded_rectangle(
Distance::meters(dims.width + 2.0 * HORIZ_PADDING),

View File

@ -9,7 +9,7 @@ pub struct LogScroller {
impl LogScroller {
pub fn new(title: String, lines: Vec<String>) -> LogScroller {
let mut text = Text::new();
let mut text = Text::new().with_bg();
text.add_highlighted(Line(title).size(50), text::PROMPT_COLOR);
for line in lines {
text.add(Line(line));

View File

@ -41,7 +41,7 @@ impl ModalMenu {
) -> ModalMenu {
let mut m = ModalMenu {
title: title.into(),
info: Text::new(),
info: Text::new().with_bg(),
chosen_action: None,
choices: raw_choices
.into_iter()
@ -74,7 +74,7 @@ impl ModalMenu {
}
pub fn set_info(&mut self, ctx: &EventCtx, info: Text) {
self.info = info;
self.info = info.with_bg();
self.recalculate_dims(ctx);
}

View File

@ -207,7 +207,7 @@ impl<T: Clone + Copy> Scroller<T> {
Item::UpButton => {
// TODO center the text inside the rectangle. and actually, g should have a
// method for that.
let mut txt = Text::new().no_bg();
let mut txt = Text::new();
if self.top_idx == 0 {
// TODO text::INACTIVE_CHOICE_COLOR
txt.add(Line("scroll up").fg(Color::grey(0.4)));
@ -217,7 +217,7 @@ impl<T: Clone + Copy> Scroller<T> {
g.draw_text_at_screenspace_topleft(&txt, ScreenPt::new(rect.x1, rect.y1));
}
Item::DownButton => {
let mut txt = Text::new().no_bg();
let mut txt = Text::new();
let num_items = self.num_items_hidden_below(g.canvas);
if num_items == 0 {
txt.add(Line("scroll down").fg(Color::grey(0.4)));

View File

@ -118,17 +118,17 @@ pub fn challenges_picker(ctx: &EventCtx) -> Box<dyn State> {
hotkey(Key::Escape),
Box::new(|_, _| Some(Transition::Pop)),
),
ManagedWidget::draw_text(ctx, Text::from(Line("A/B STREET").size(50)).no_bg()),
ManagedWidget::draw_text(ctx, Text::from(Line("A/B STREET").size(50))),
],
));
col.push(ManagedWidget::draw_text(
ctx,
Text::from(Line("CHALLENGES")).no_bg(),
Text::from(Line("CHALLENGES")),
));
col.push(ManagedWidget::draw_text(
ctx,
Text::from(Line("Make changes to achieve a goal")).no_bg(),
Text::from(Line("Make changes to achieve a goal")),
));
let mut flex_row = Vec::new();
@ -155,7 +155,7 @@ pub fn challenges_picker(ctx: &EventCtx) -> Box<dyn State> {
let edits = abstutil::list_all_objects(abstutil::path_all_edits(
&abstutil::basename(&challenge.map_path),
));
let mut summary = Text::new();
let mut summary = Text::new().with_bg();
for l in &challenge.description {
summary.add(Line(l));
}

View File

@ -66,7 +66,7 @@ impl State for InfoPanel {
fn info_for(id: ID, ui: &UI) -> Text {
let (map, sim, draw_map) = (&ui.primary.map, &ui.primary.sim, &ui.primary.draw_map);
let mut txt = Text::new();
let mut txt = Text::new().with_bg();
txt.extend(&CommonState::default_osd(id.clone(), ui));
txt.highlight_last_line(Color::BLUE);

View File

@ -100,7 +100,7 @@ impl CommonState {
let map = &ui.primary.map;
let id_color = ui.cs.get_def("OSD ID color", Color::RED);
let name_color = ui.cs.get_def("OSD name color", Color::CYAN);
let mut osd = Text::new();
let mut osd = Text::new().with_bg();
match id {
ID::Lane(l) => {
osd.append_all(vec![
@ -181,7 +181,7 @@ impl CommonState {
} else if let Some(button) = g.button_tooltip() {
button
} else {
Text::from(Line("..."))
Text::from(Line("...")).with_bg()
};
CommonState::draw_custom_osd(ui, g, osd);
}

View File

@ -64,7 +64,7 @@ impl<T: 'static + Ord + PartialEq + Copy + core::fmt::Debug + Yvalue<T>> Plot<T>
let percent_x = (i as f64) / ((num_x_labels - 1) as f64);
let t = max_x.percent_of(percent_x);
labels.push((
Text::from(Line(t.to_string())),
Text::from(Line(t.to_string())).with_bg(),
ScreenPt::new(x1 + percent_x * (x2 - x1), y2),
));
}
@ -73,7 +73,7 @@ impl<T: 'static + Ord + PartialEq + Copy + core::fmt::Debug + Yvalue<T>> Plot<T>
for i in 0..num_y_labels {
let percent_y = (i as f64) / ((num_y_labels - 1) as f64);
labels.push((
Text::from(Line(max_y.from_percent(percent_y).prettyprint())),
Text::from(Line(max_y.from_percent(percent_y).prettyprint())).with_bg(),
ScreenPt::new(x1, y2 - percent_y * (y2 - y1)),
));
}
@ -127,7 +127,7 @@ impl<T: 'static + Ord + PartialEq + Copy + core::fmt::Debug + Yvalue<T>> Plot<T>
let cursor = g.canvas.get_cursor_in_screen_space();
if self.rect.contains(cursor) {
let radius = Distance::meters(5.0);
let mut txt = Text::new();
let mut txt = Text::new().with_bg();
for (label, pt, _) in self.closest.all_close_pts(cursor.to_pt(), radius) {
let t = self
.max_x
@ -287,7 +287,8 @@ impl Histogram {
min,
max,
prettyprint_usize(cnt)
))),
)))
.with_bg(),
));
}
}
@ -298,7 +299,7 @@ impl Histogram {
let percent_x = (i as f64) / ((num_x_labels - 1) as f64);
let dt = min_x + (max_x - min_x) * percent_x;
labels.push((
Text::from(Line(dt.to_string())),
Text::from(Line(dt.to_string())).with_bg(),
ScreenPt::new(x1 + percent_x * (x2 - x1), y2),
));
}
@ -311,7 +312,8 @@ impl Histogram {
labels.push((
Text::from(Line(prettyprint_usize(
((max_y as f64) * percent_y) as usize,
))),
)))
.with_bg(),
ScreenPt::new(x1 - left_px, y2 - percent_y * (y2 - y1)),
));
}

View File

@ -41,7 +41,7 @@ impl ObjectDebugger {
if self.debug_tooltip_key_held {
if let Some(pt) = g.canvas.get_cursor_in_map_space() {
if let Some(gps) = pt.to_gps(ui.primary.map.get_gps_bounds()) {
let mut txt = Text::new();
let mut txt = Text::new().with_bg();
txt.add(Line(pt.to_string()));
txt.add(Line(gps.to_string()));
txt.add(Line(format!("{:?}", g.canvas.get_cursor_in_screen_space())));

View File

@ -173,21 +173,21 @@ impl State for PolygonDebugger {
match item {
Item::Point(pt) => {
g.draw_text_at(&Text::from(Line(idx.to_string())), *pt);
g.draw_text_at(&Text::from(Line(idx.to_string())).with_bg(), *pt);
}
Item::Triangle(ref tri) => {
for pt in &[tri.pt1, tri.pt2, tri.pt3] {
g.draw_text_at(&Text::from(Line(idx.to_string())), *pt);
g.draw_text_at(&Text::from(Line(idx.to_string())).with_bg(), *pt);
}
g.draw_polygon(ui.cs.get("selected"), &Polygon::from_triangle(tri));
}
Item::Polygon(ref poly) => {
g.draw_polygon(ui.cs.get("selected"), poly);
g.draw_text_at(&Text::from(Line(idx.to_string())), poly.center());
g.draw_text_at(&Text::from(Line(idx.to_string())).with_bg(), poly.center());
}
}
if let Some(pt) = self.center {
g.draw_text_at(&Text::from(Line("c")), pt);
g.draw_text_at(&Text::from(Line("c")).with_bg(), pt);
}
self.slider.draw(g);

View File

@ -125,7 +125,7 @@ impl State for StopSignEditor {
self.menu.draw(g);
if let Some(r) = self.selected_sign {
let mut osd = Text::new();
let mut osd = Text::new().with_bg();
osd.add_appended(vec![
Line("Stop sign for "),
Line(ui.primary.map.get_r(r).get_name()).fg(ui.cs.get("OSD name color")),

View File

@ -289,7 +289,7 @@ impl State for TrafficSignalEditor {
ui.primary.map.get_r(id.to).get_name()
)))
};
CommonState::draw_custom_osd(ui, g, osd);
CommonState::draw_custom_osd(ui, g, osd.with_bg());
} else {
CommonState::draw_osd(g, ui, &None);
}

View File

@ -122,8 +122,10 @@ impl GUI for Game {
state.draw(g, &self.ui);
if self.ui.opts.dev {
let mut txt = Text::from(Line("DEV"));
txt.highlight_last_line(Color::RED);
g.draw_blocking_text(
&Text::from(Line("DEV")).bg(Color::RED),
&txt,
(HorizontalAlignment::Right, VerticalAlignment::Bottom),
);
}

View File

@ -86,17 +86,17 @@ pub fn main_menu(ctx: &EventCtx, ui: &UI) -> Box<dyn State> {
std::process::exit(0);
}),
),
ManagedWidget::draw_text(ctx, Text::from(Line("A/B STREET").size(50)).no_bg()),
ManagedWidget::draw_text(ctx, Text::from(Line("A/B STREET").size(50))),
],
));
col.push(ManagedWidget::draw_text(
ctx,
Text::from(Line("Created by Dustin Carlino")).no_bg(),
Text::from(Line("Created by Dustin Carlino")),
));
col.push(ManagedWidget::draw_text(
ctx,
Text::from(Line("Choose your game")).no_bg(),
Text::from(Line("Choose your game")),
));
col.push(ManagedWidget::Row(
@ -169,7 +169,7 @@ fn about(ctx: &EventCtx) -> Box<dyn State> {
Box::new(|_, _| Some(Transition::Pop)),
));
let mut txt = Text::new().no_bg();
let mut txt = Text::new();
txt.add(Line("A/B STREET").size(50));
txt.add(Line("Created by Dustin Carlino"));
txt.add(Line(""));

View File

@ -42,7 +42,7 @@ impl DrawBuilding {
let label = bldg
.osm_tags
.get("addr:housenumber")
.map(|num| Text::from(Line(num.to_string()).fg(Color::BLACK).size(50)).no_bg());
.map(|num| Text::from(Line(num.to_string()).fg(Color::BLACK).size(50)));
if bldg.parking.is_some() {
let center = bldg.label_center;

View File

@ -142,7 +142,7 @@ impl DrawCar {
zorder: input.on.get_zorder(map),
label: input
.label
.map(|line| Text::from(Line(line).fg(Color::rgb(249, 206, 24)).size(20)).no_bg()),
.map(|line| Text::from(Line(line).fg(Color::rgb(249, 206, 24)).size(20))),
draw_default: prerender.upload(draw_default),
}
}

View File

@ -216,8 +216,7 @@ impl DrawPedCrowd {
Line(format!("{}", input.members.len()))
.fg(Color::BLACK)
.size(15),
)
.no_bg();
);
DrawPedCrowd {
members: input.members,

View File

@ -43,7 +43,7 @@ impl DrawRoad {
);
}
let mut label = Text::new();
let mut label = Text::new().with_bg();
label.add(Line(r.get_name()).size(50));
DrawRoad {

View File

@ -182,11 +182,8 @@ impl TrafficSignalDiagram {
);
let mut labels = Vec::new();
for (idx, phase) in phases.iter().enumerate() {
labels.push(Text::from(Line(format!(
"Phase {}: {}",
idx + 1,
phase.duration
))));
labels
.push(Text::from(Line(format!("Phase {}: {}", idx + 1, phase.duration))).with_bg());
}
TrafficSignalDiagram {
@ -261,7 +258,7 @@ fn make_new_scroller(i: IntersectionID, draw_ctx: &DrawCtx, ctx: &EventCtx) -> N
);
}
txt.push((
Text::from(Line(format!("Phase {}: {}", idx + 1, phase.duration))),
Text::from(Line(format!("Phase {}: {}", idx + 1, phase.duration))).with_bg(),
ScreenPt::new(10.0 + (bounds.max_x - bounds.min_x) * zoom, y_offset * zoom),
));
y_offset += bounds.max_y - bounds.min_y;

View File

@ -61,7 +61,7 @@ impl ShowBusRoute {
let mut labels = Vec::new();
for (idx, bs) in route.stops.iter().enumerate() {
labels.push((
Text::from(Line(format!("{}", idx + 1))),
Text::from(Line(format!("{}", idx + 1))).with_bg(),
map.get_bs(*bs).sidewalk_pos.pt(map),
));
}

View File

@ -72,7 +72,7 @@ impl GameplayState for Freeform {
if let Some(ID::Intersection(i)) = ui.primary.current_selection {
if self.spawn_pts.contains(&i) {
let cnt = ui.primary.sim.count_trips_involving_border(i);
let mut txt = Text::new();
let mut txt = Text::new().with_bg();
for line in cnt.describe() {
txt.add(Line(line));
}

View File

@ -287,7 +287,7 @@ impl AgentMeter {
pub fn new(ctx: &EventCtx, ui: &UI) -> AgentMeter {
let (active, unfinished, by_mode) = ui.primary.sim.num_trips();
let mut row1_txt = Text::new().no_bg();
let mut row1_txt = Text::new();
row1_txt.add(Line(format!("Active trips: {}", active)));
row1_txt.add(Line(format!("Unfinished trips: {}", unfinished)));
@ -306,13 +306,13 @@ impl AgentMeter {
JustDraw::wrap(DrawBoth::new(ctx, rect_bg, Vec::new())),
JustDraw::text(row1_txt, ctx),
JustDraw::svg("assets/meters/pedestrian.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Walk])).no_bg(), ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Walk])), ctx),
JustDraw::svg("assets/meters/bike.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Bike])).no_bg(), ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Bike])), ctx),
JustDraw::svg("assets/meters/car.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Drive])).no_bg(), ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Drive])), ctx),
JustDraw::svg("assets/meters/bus.svg", ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Transit])).no_bg(), ctx),
JustDraw::text(Text::from(Line(&by_mode[&TripMode::Transit])), ctx),
];
// TODO A horrible experiment in manual layouting

View File

@ -37,7 +37,7 @@ impl Scoreboard {
prebaked.all_finished_trips(ui.primary.sim.time());
// TODO Include unfinished count
let mut txt = Text::new();
let mut txt = Text::new().with_bg();
txt.add_appended(vec![
Line("Finished trips as of "),
Line(ui.primary.sim.time().ampm_tostring()).fg(Color::CYAN),

View File

@ -77,7 +77,7 @@ impl SpeedControls {
.translate(330.0, 10.0),
);
txt.push((
Text::from(Line("speed").size(14).roboto()).no_bg(),
Text::from(Line("speed").size(14).roboto()),
ScreenPt::new(330.0, 15.0),
));
@ -333,8 +333,7 @@ impl SpeedControls {
Line(format!("{:.1}x", self.desired_speed()))
.size(14)
.roboto(),
)
.no_bg(),
),
ScreenPt::new(self.top_left.x + 530.0, self.top_left.y + 10.0),
);
@ -446,17 +445,17 @@ impl TimePanel {
);
txt.push((
Text::from(Line("00:00").size(12).roboto()).no_bg(),
Text::from(Line("00:00").size(12).roboto()),
ScreenPt::new(25.0, 80.0),
));
batch.add_svg("assets/speed/sunrise.svg", 94.0, 80.0);
txt.push((
Text::from(Line("12:00").size(12).roboto()).no_bg(),
Text::from(Line("12:00").size(12).roboto()),
ScreenPt::new(153.0, 80.0),
));
batch.add_svg("assets/speed/sunset.svg", 220.0, 80.0);
txt.push((
Text::from(Line("24:00").size(12).roboto()).no_bg(),
Text::from(Line("24:00").size(12).roboto()),
ScreenPt::new(280.0, 80.0),
));
@ -475,7 +474,7 @@ impl TimePanel {
});
g.draw_text_at_screenspace_topleft(
&Text::from(Line(ui.primary.sim.time().ampm_tostring()).size(30)).no_bg(),
&Text::from(Line(ui.primary.sim.time().ampm_tostring()).size(30)),
ScreenPt::new(24.0, 10.0),
);

View File

@ -94,7 +94,7 @@ impl UI {
],
ctx,
),
sidebar: Text::new(),
sidebar: Text::new().with_bg(),
last_id: None,
};
@ -562,7 +562,7 @@ impl GUI for UI {
}
}
self.sidebar = Text::new();
self.sidebar = Text::new().with_bg();
self.sidebar.override_width = Some(0.3 * ctx.canvas.window_width);
self.sidebar.override_height = Some(ctx.canvas.window_height);
if let Some(id) = self.model.world.get_selection() {
@ -669,7 +669,7 @@ impl GUI for UI {
if show_tooltip {
// TODO Argh, covers up mouseover tooltip.
if let Some(cursor) = g.canvas.get_cursor_in_map_space() {
g.draw_mouse_tooltip(&Text::from(Line(cursor.to_string())));
g.draw_mouse_tooltip(&Text::from(Line(cursor.to_string())).with_bg());
}
}
}
@ -709,7 +709,7 @@ fn preview_intersection(
batch.push(Color::GREEN.alpha(0.5), r);
}
for (label, poly) in debug {
labels.push((Text::from(Line(label)), poly.center()));
labels.push((Text::from(Line(label)).with_bg(), poly.center()));
batch.push(Color::RED.alpha(0.5), poly);
}
(batch.upload(ctx), labels)