centralize and adjust constants for text colors. also clean up places

unnecessarilly passing in RGB values for constants
This commit is contained in:
Dustin Carlino 2018-12-22 12:14:51 -08:00
parent dfc08bfe97
commit 0f62da0f63
16 changed files with 53 additions and 76 deletions

View File

@ -121,7 +121,7 @@ impl Plugin for DrawNeighborhoodState {
if pts.len() >= 3 {
g.draw_polygon(
ctx.cs
.get_def("neighborhood polygon", Color::rgba(0, 0, 255, 0.6)),
.get_def("neighborhood polygon", Color::BLUE.alpha(0.6)),
&Polygon::new(&pts),
);
}

View File

@ -58,15 +58,14 @@ impl Plugin for DiffTripState {
}
if let Some(t) = &self.primary_route {
g.draw_polygon(
ctx.cs
.get_def("primary agent route", Color::rgba(255, 0, 0, 0.5)),
ctx.cs.get_def("primary agent route", Color::RED.alpha(0.5)),
&t.make_polygons_blindly(LANE_THICKNESS),
);
}
if let Some(t) = &self.secondary_route {
g.draw_polygon(
ctx.cs
.get_def("secondary agent route", Color::rgba(0, 0, 255, 0.5)),
.get_def("secondary agent route", Color::BLUE.alpha(0.5)),
&t.make_polygons_blindly(LANE_THICKNESS),
);
}

View File

@ -57,8 +57,8 @@ fn panel(ctx: &mut PluginCtx) -> ShowScoreState {
fn summarize(txt: &mut Text, summary: ScoreSummary) {
txt.add_styled_line(
"Walking".to_string(),
Color::BLACK,
Some(Color::rgba(255, 0, 0, 0.8)),
Some(Color::BLACK),
Some(Color::RED.alpha(0.8)),
);
txt.add_line(format!(
" {}/{} trips done",
@ -69,8 +69,8 @@ fn summarize(txt: &mut Text, summary: ScoreSummary) {
txt.add_styled_line(
"Driving".to_string(),
Color::BLACK,
Some(Color::rgba(0, 0, 255, 0.8)),
Some(Color::BLACK),
Some(Color::BLUE.alpha(0.8)),
);
txt.add_line(format!(
" {}/{} trips done",

View File

@ -1,6 +1,6 @@
use crate::objects::{Ctx, ID};
use crate::plugins::{Plugin, PluginCtx};
use ezgui::{Color, GfxCtx, Key, Text, TEXT_FG_COLOR};
use ezgui::{Color, GfxCtx, Key, Text};
use std::collections::BTreeMap;
pub struct DebugObjectsState {
@ -66,7 +66,7 @@ fn tooltip_lines(obj: ID, ctx: &Ctx) -> Text {
.get("name")
.unwrap_or(&"???".to_string())
.to_string(),
Color::BLUE,
Some(Color::BLUE),
None,
);
txt.add_line(format!("From OSM way {}", r.osm_way_id));
@ -139,8 +139,8 @@ fn tooltip_lines(obj: ID, ctx: &Ctx) -> Text {
fn styled_kv(txt: &mut Text, tags: &BTreeMap<String, String>) {
for (k, v) in tags {
txt.add_styled_line(k.to_string(), Color::RED, None);
txt.append(" = ".to_string(), TEXT_FG_COLOR, None);
txt.append(v.to_string(), Color::BLUE, None);
txt.add_styled_line(k.to_string(), Some(Color::RED), None);
txt.append(" = ".to_string(), None, None);
txt.append(v.to_string(), Some(Color::BLUE), None);
}
}

View File

@ -97,7 +97,7 @@ impl Heatmap {
let percent = (self.counts[x][y] as f32) / (self.max as f32);
// TODO Map percent to hot/cold colors. For now, don't ever become totally opaque.
let color = Color::rgba(255, 0, 0, percent * 0.8);
let color = Color::RED.alpha(percent * 0.8);
g.draw_polygon(
color,
&Polygon::rectangle_topleft(

View File

@ -66,7 +66,7 @@ impl Plugin for ShowRouteState {
match self {
ShowRouteState::Active(_, _, Some(ref trace)) => {
g.draw_polygon(
ctx.cs.get_def("route", Color::rgba(255, 0, 0, 0.8)),
ctx.cs.get_def("route", Color::RED.alpha(0.8)),
&trace.make_polygons_blindly(LANE_THICKNESS),
);
}

View File

@ -46,7 +46,7 @@ impl Renderable for DrawBike {
match self.state {
CarState::Debug => ctx
.cs
.get_def("debug bike", Color::rgba(0, 0, 255, 0.8))
.get_def("debug bike", Color::BLUE.alpha(0.8))
.shift(self.id.0),
// TODO Hard to see on the greenish bike lanes? :P
CarState::Moving => ctx.cs.get_def("moving bike", Color::GREEN).shift(self.id.0),
@ -59,7 +59,7 @@ impl Renderable for DrawBike {
if let Some(ref t) = self.stopping_buffer {
g.draw_polygon(
ctx.cs
.get_def("bike stopping buffer", Color::rgba(255, 0, 0, 0.7)),
.get_def("bike stopping buffer", Color::RED.alpha(0.7)),
t,
);
}

View File

@ -120,7 +120,7 @@ impl Renderable for DrawCar {
match self.state {
CarState::Debug => ctx
.cs
.get_def("debug car", Color::rgba(0, 0, 255, 0.8))
.get_def("debug car", Color::BLUE.alpha(0.8))
.shift(self.id.0),
CarState::Moving => ctx.cs.get_def("moving car", Color::CYAN).shift(self.id.0),
CarState::Stuck => ctx.cs.get_def("stuck car", Color::RED).shift(self.id.0),
@ -159,8 +159,7 @@ impl Renderable for DrawCar {
if opts.debug_mode {
if let Some(ref t) = self.stopping_buffer {
g.draw_polygon(
ctx.cs
.get_def("car stopping buffer", Color::rgba(255, 0, 0, 0.7)),
ctx.cs.get_def("car stopping buffer", Color::RED.alpha(0.7)),
t,
);
}

View File

@ -51,7 +51,7 @@ impl Color {
Color([r, g, b, a])
}
pub fn grey(f: f32) -> Color {
pub const fn grey(f: f32) -> Color {
Color([f, f, f, 1.0])
}

View File

@ -24,7 +24,7 @@ pub use crate::log_scroller::LogScroller;
pub use crate::runner::{run, EventLoopMode, GUI};
pub use crate::screen_geom::ScreenPt;
pub use crate::scrolling_menu::ScrollingMenu;
pub use crate::text::{Text, TEXT_FG_COLOR};
pub use crate::text::Text;
pub use crate::text_box::TextBox;
pub use crate::top_menu::{Folder, TopMenu};
pub use crate::wizard::{Wizard, WrappedWizard};

View File

@ -60,11 +60,7 @@ impl LogScroller {
pub fn draw(&self, g: &mut GfxCtx, canvas: &Canvas) {
let mut txt = Text::new();
// TODO Force padding of everything to a fixed 80% of the screen or so
txt.add_styled_line(
"Logs".to_string(),
text::TEXT_FG_COLOR,
Some(text::TEXT_QUERY_COLOR),
);
txt.add_styled_line("Logs".to_string(), None, Some(text::PROMPT_COLOR));
// How many lines can we fit on the screen?
let can_fit = {

View File

@ -1,6 +1,6 @@
use crate::screen_geom::ScreenRectangle;
use crate::text::LINE_HEIGHT;
use crate::{text, Canvas, Color, Event, GfxCtx, InputResult, Key, ScreenPt, Text};
use crate::{text, Canvas, Event, GfxCtx, InputResult, Key, ScreenPt, Text};
// Stores some associated data with each choice
pub struct Menu<T: Clone> {
@ -185,34 +185,30 @@ impl<T: Clone> Menu<T> {
pub fn draw(&self, g: &mut GfxCtx, canvas: &Canvas) {
let mut txt = Text::new();
if let Some(ref line) = self.prompt {
txt.add_styled_line(
line.to_string(),
text::TEXT_FG_COLOR,
Some(text::TEXT_QUERY_COLOR),
);
txt.add_styled_line(line.to_string(), None, Some(text::PROMPT_COLOR));
}
for (idx, (hotkey, choice, active, _)) in self.choices.iter().enumerate() {
let bg = if Some(idx) == self.current_idx {
Some(Color::WHITE)
Some(text::SELECTED_COLOR)
} else {
None
};
if *active {
if let Some(key) = hotkey {
txt.add_styled_line(key.describe(), Color::BLUE, bg);
txt.append(format!(" - {}", choice), text::TEXT_FG_COLOR, bg);
txt.add_styled_line(key.describe(), Some(text::HOTKEY_COLOR), bg);
txt.append(format!(" - {}", choice), None, bg);
} else {
txt.add_styled_line(choice.to_string(), text::TEXT_FG_COLOR, bg);
txt.add_styled_line(choice.to_string(), None, bg);
}
} else {
if let Some(key) = hotkey {
txt.add_styled_line(
format!("{} - {}", key.describe(), choice),
Color::grey(0.8),
Some(text::INACTIVE_CHOICE_COLOR),
bg,
);
} else {
txt.add_styled_line(choice.to_string(), Color::grey(0.8), bg);
txt.add_styled_line(choice.to_string(), Some(text::INACTIVE_CHOICE_COLOR), bg);
}
}
}

View File

@ -49,11 +49,7 @@ impl<T: Clone> ScrollingMenu<T> {
pub fn draw(&self, g: &mut GfxCtx, canvas: &Canvas) {
let mut txt = Text::new();
txt.add_styled_line(
self.prompt.clone(),
text::TEXT_FG_COLOR,
Some(text::TEXT_QUERY_COLOR),
);
txt.add_styled_line(self.prompt.clone(), None, Some(text::PROMPT_COLOR));
// TODO Silly results from doing this:
// - The menu width changes as we scroll
@ -88,11 +84,7 @@ impl<T: Clone> ScrollingMenu<T> {
continue;
}
if self.current_idx == idx {
txt.add_styled_line(
line.clone(),
text::TEXT_FG_COLOR,
Some(text::TEXT_FOCUS_COLOR),
);
txt.add_styled_line(line.clone(), None, Some(text::SELECTED_COLOR));
} else {
txt.add_line(line.clone());
}

View File

@ -8,10 +8,12 @@ use opengl_graphics::GlyphCache;
use ordered_float::NotNaN;
use textwrap;
pub const TEXT_FG_COLOR: Color = Color([0.0, 0.0, 0.0, 1.0]);
pub const TEXT_QUERY_COLOR: Color = Color([0.0, 0.0, 1.0, 0.5]);
pub const TEXT_FOCUS_COLOR: Color = Color([1.0, 0.0, 0.0, 0.5]);
const TEXT_BG_COLOR: Color = Color([0.0, 1.0, 0.0, 0.5]);
const FG_COLOR: Color = Color::WHITE;
pub const PROMPT_COLOR: Color = Color::BLUE;
pub const SELECTED_COLOR: Color = Color::RED;
pub const HOTKEY_COLOR: Color = Color::GREEN;
pub const INACTIVE_CHOICE_COLOR: Color = Color::grey(0.8);
const BG_COLOR: Color = Color::BLACK;
const FONT_SIZE: u32 = 24;
// TODO These are dependent on FONT_SIZE, but hand-tuned. Glyphs all have 0 as their height, and
@ -33,7 +35,7 @@ impl TextSpan {
fn default_style(text: String) -> TextSpan {
TextSpan {
text,
fg_color: TEXT_FG_COLOR,
fg_color: FG_COLOR,
highlight_color: None,
}
}
@ -50,7 +52,7 @@ impl Text {
pub fn new() -> Text {
Text {
lines: Vec::new(),
bg_color: Some(TEXT_BG_COLOR),
bg_color: Some(BG_COLOR),
}
}
@ -88,24 +90,29 @@ impl Text {
pub fn add_styled_line(
&mut self,
line: String,
fg_color: Color,
fg_color: Option<Color>,
highlight_color: Option<Color>,
) {
self.lines.push(vec![TextSpan {
text: line,
fg_color,
fg_color: fg_color.unwrap_or(FG_COLOR),
highlight_color,
}]);
}
pub fn append(&mut self, text: String, fg_color: Color, highlight_color: Option<Color>) {
pub fn append(
&mut self,
text: String,
fg_color: Option<Color>,
highlight_color: Option<Color>,
) {
if self.lines.is_empty() {
self.lines.push(Vec::new());
}
self.lines.last_mut().unwrap().push(TextSpan {
text,
fg_color,
fg_color: fg_color.unwrap_or(FG_COLOR),
highlight_color,
});
}

View File

@ -23,30 +23,18 @@ impl TextBox {
pub fn draw(&self, g: &mut GfxCtx, canvas: &Canvas) {
let mut txt = Text::new();
txt.add_styled_line(
self.prompt.clone(),
text::TEXT_FG_COLOR,
Some(text::TEXT_QUERY_COLOR),
);
txt.add_styled_line(self.prompt.clone(), None, Some(text::PROMPT_COLOR));
txt.add_line(self.line[0..self.cursor_x].to_string());
if self.cursor_x < self.line.len() {
txt.append(
self.line[self.cursor_x..=self.cursor_x].to_string(),
text::TEXT_FG_COLOR,
Some(text::TEXT_FOCUS_COLOR),
);
txt.append(
self.line[self.cursor_x + 1..].to_string(),
text::TEXT_FG_COLOR,
None,
Some(text::SELECTED_COLOR),
);
txt.append(self.line[self.cursor_x + 1..].to_string(), None, None);
} else {
txt.append(
" ".to_string(),
text::TEXT_FG_COLOR,
Some(text::TEXT_FOCUS_COLOR),
);
txt.append(" ".to_string(), None, Some(text::SELECTED_COLOR));
}
canvas.draw_text(g, txt, CENTERED);

View File

@ -37,7 +37,7 @@ impl TopMenu {
let mut txt = Text::with_bg_color(None);
for f in &folders {
txt.append(format!("{} ", f.name), Color::WHITE, None);
txt.append(format!("{} ", f.name), Some(Color::WHITE), None);
}
// Calculate rectangles for the folders