From 2ae6992a3ec0889bac313c48789986429d4f7903 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 16 Mar 2020 12:20:37 -0700 Subject: [PATCH] continue button refactor --- ezgui/src/managed.rs | 33 +++++------------ ezgui/src/widgets/button.rs | 32 +++++++++++++++-- ezgui/src/widgets/wizard.rs | 53 +++++++-------------------- game/src/common/minimap.rs | 24 ++++++------- game/src/common/overlays.rs | 72 ++++++++++++++++--------------------- game/src/common/panels.rs | 18 ++++------ game/src/edit/lanes.rs | 12 +++---- game/src/managed.rs | 21 ++++------- game/src/sandbox/speed.rs | 34 +++++++----------- 9 files changed, 124 insertions(+), 175 deletions(-) diff --git a/ezgui/src/managed.rs b/ezgui/src/managed.rs index 389d381556..9b82c3bade 100644 --- a/ezgui/src/managed.rs +++ b/ezgui/src/managed.rs @@ -1,8 +1,8 @@ use crate::layout::Widget; use crate::widgets::{Checkbox, Dropdown, PopupMenu, TextBox}; use crate::{ - Button, Choice, Color, Drawable, EventCtx, Filler, GeomBatch, GfxCtx, Histogram, - HorizontalAlignment, JustDraw, Line, MultiKey, Plot, RewriteColor, ScreenDims, ScreenPt, + Btn, Button, Choice, Color, Drawable, EventCtx, Filler, GeomBatch, GfxCtx, Histogram, + HorizontalAlignment, JustDraw, MultiKey, Plot, RewriteColor, ScreenDims, ScreenPt, ScreenRectangle, Slider, Text, VerticalAlignment, }; use abstutil::Cloneable; @@ -285,32 +285,17 @@ impl ManagedWidget { hotkey: Option, enabled: bool, ) -> ManagedWidget { - // TODO Just a copy of WrappedComposite::nice_text_button essentially... - fn make_btn( - ctx: &EventCtx, - label: &str, - hotkey: Option, - enabled: bool, - ) -> Button { - let txt = Text::from(Line(format!( - "{} {}", - if enabled { "☑" } else { "☐" }, - label - ))); - Button::text_no_bg( - txt.clone(), - txt.change_fg(Color::ORANGE), - hotkey, - label, - true, - ctx, - ) + fn take_btn(w: ManagedWidget) -> Button { + match w.widget { + WidgetType::Btn(btn) => btn, + _ => unreachable!(), + } } ManagedWidget::custom_checkbox( enabled, - make_btn(ctx, label, hotkey.clone(), false), - make_btn(ctx, label, hotkey, true), + take_btn(Btn::text_fg(format!("☐ {}", label)).build(ctx, label, hotkey.clone())), + take_btn(Btn::text_fg(format!("☑ {}", label)).build(ctx, label, hotkey)), ) .outline(2.0, Color::WHITE) .named(label) diff --git a/ezgui/src/widgets/button.rs b/ezgui/src/widgets/button.rs index 8eb52f3081..4192453907 100644 --- a/ezgui/src/widgets/button.rs +++ b/ezgui/src/widgets/button.rs @@ -265,10 +265,11 @@ impl Button { pub struct Btn {} impl Btn { - pub fn svg(path: &str, hover: RewriteColor) -> BtnBuilder { - BtnBuilder::SVG(path.to_string(), hover) + pub fn svg>(path: I, hover: RewriteColor) -> BtnBuilder { + BtnBuilder::SVG(path.into(), hover) } + // Same as WrappedComposite::text_button pub fn text_fg>(label: I) -> BtnBuilder { BtnBuilder::TextFG(label.into()) } @@ -277,12 +278,18 @@ impl Btn { pub fn text_bg1>(label: I) -> BtnBuilder { BtnBuilder::TextBG1(label.into()) } + + // The white background. WrappedComposite::text_bg_button. + pub fn text_bg2>(label: I) -> BtnBuilder { + BtnBuilder::TextBG2(label.into()) + } } pub enum BtnBuilder { SVG(String, RewriteColor), TextFG(String), TextBG1(String), + TextBG2(String), } impl BtnBuilder { @@ -317,6 +324,27 @@ impl BtnBuilder { &action_tooltip.into(), ctx, )), + BtnBuilder::TextBG2(label) => ManagedWidget::btn(Button::text_bg( + Text::from(Line(label).fg(Color::BLACK)), + Color::WHITE, + Color::ORANGE, + key, + &action_tooltip.into(), + ctx, + )), + } + } + + // Use the text as the action + pub fn build_def(self, ctx: &EventCtx, hotkey: Option) -> ManagedWidget { + match self { + BtnBuilder::SVG(_, _) => panic!("Can't use build_def on an SVG button"), + BtnBuilder::TextFG(ref label) + | BtnBuilder::TextBG1(ref label) + | BtnBuilder::TextBG2(ref label) => { + let copy = label.clone(); + self.build(ctx, copy, hotkey) + } } } } diff --git a/ezgui/src/widgets/wizard.rs b/ezgui/src/widgets/wizard.rs index d9e56adcbd..76382b9650 100644 --- a/ezgui/src/widgets/wizard.rs +++ b/ezgui/src/widgets/wizard.rs @@ -1,7 +1,7 @@ use crate::widgets::PopupMenu; use crate::{ - hotkey, Button, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, InputResult, Key, - Line, ManagedWidget, MultiKey, Outcome, Text, VerticalAlignment, + hotkey, Btn, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, InputResult, Key, Line, + ManagedWidget, MultiKey, Outcome, Text, VerticalAlignment, }; use abstutil::Cloneable; use std::collections::VecDeque; @@ -87,28 +87,14 @@ impl Wizard { ManagedWidget::col(vec![ ManagedWidget::row(vec![ Line(query).roboto_bold().draw(ctx), - // TODO nice text button - ManagedWidget::btn(Button::text_bg( - Text::from(Line("X").fg(Color::BLACK)), - Color::WHITE, - Color::ORANGE, - hotkey(Key::Escape), - "quit", - ctx, - )) - .margin(5) - .align_right(), + Btn::text_fg("X") + .build(ctx, "quit", hotkey(Key::Escape)) + .margin(5) + .align_right(), ]), ManagedWidget::text_entry(ctx, prefilled.unwrap_or_else(String::new), true) .named("input"), - ManagedWidget::btn(Button::text_bg( - Text::from(Line("Done").fg(Color::BLACK)), - Color::WHITE, - Color::ORANGE, - hotkey(Key::Enter), - "done", - ctx, - )), + Btn::text_bg2("Done").build(ctx, "done", hotkey(Key::Enter)), ]) .bg(Color::grey(0.4)) .outline(5.0, Color::WHITE) @@ -272,16 +258,9 @@ impl<'a, 'b> WrappedWizard<'a, 'b> { Composite::new( ManagedWidget::row(vec![ ManagedWidget::col(col), - // TODO nice text button - ManagedWidget::btn(Button::text_bg( - Text::from(Line("X").fg(Color::BLACK)), - Color::WHITE, - Color::ORANGE, - hotkey(Key::Escape), - "quit", - self.ctx, - )) - .margin(5), + Btn::text_fg("X") + .build(self.ctx, "quit", hotkey(Key::Escape)) + .margin(5), ]) .bg(Color::grey(0.4)) .outline(5.0, Color::WHITE) @@ -408,15 +387,9 @@ impl<'a, 'b> WrappedWizard<'a, 'b> { Composite::new( ManagedWidget::col(vec![ txt.draw(self.ctx), - ManagedWidget::btn(Button::text_bg( - Text::from(Line("OK")), - Color::grey(0.6), - Color::ORANGE, - hotkey(Key::Enter), - "OK", - self.ctx, - )) - .margin(5), + Btn::text_bg2("OK") + .build(self.ctx, "OK", hotkey(Key::Enter)) + .margin(5), ]) .bg(Color::grey(0.4)) .outline(10.0, Color::WHITE) diff --git a/game/src/common/minimap.rs b/game/src/common/minimap.rs index 1be8d68fc5..2f81e95a46 100644 --- a/game/src/common/minimap.rs +++ b/game/src/common/minimap.rs @@ -6,7 +6,7 @@ use crate::managed::WrappedComposite; use crate::render::{AgentColorScheme, MIN_ZOOM_FOR_DETAIL}; use abstutil::clamp; use ezgui::{ - hotkey, Button, Choice, Color, Composite, EventCtx, Filler, GeomBatch, GfxCtx, + hotkey, Btn, Button, Choice, Color, Composite, EventCtx, Filler, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, ManagedWidget, Outcome, RewriteColor, ScreenDims, ScreenPt, Text, VerticalAlignment, }; @@ -282,13 +282,11 @@ fn make_minimap_panel(ctx: &mut EventCtx, acs: &AgentColorScheme, zoom_lvl: usiz } let square_len = 0.15 * ctx.canvas.window_width; - let mut zoom_col = vec![ManagedWidget::btn(Button::rectangle_svg( + let mut zoom_col = vec![Btn::svg( "../data/system/assets/speed/speed_up.svg", - "zoom in", - None, RewriteColor::ChangeAll(colors::HOVERING), - ctx, - ))]; + ) + .build(ctx, "zoom in", None)]; for i in (0..=3).rev() { let color = if zoom_lvl < i { Color::grey(0.2) @@ -305,13 +303,13 @@ fn make_minimap_panel(ctx: &mut EventCtx, acs: &AgentColorScheme, zoom_lvl: usiz rect, ))); } - zoom_col.push(ManagedWidget::btn(Button::rectangle_svg( - "../data/system/assets/speed/slow_down.svg", - "zoom out", - None, - RewriteColor::ChangeAll(colors::HOVERING), - ctx, - ))); + zoom_col.push( + Btn::svg( + "../data/system/assets/speed/slow_down.svg", + RewriteColor::ChangeAll(colors::HOVERING), + ) + .build(ctx, "zoom out", None), + ); Composite::new( ManagedWidget::row(vec![ diff --git a/game/src/common/overlays.rs b/game/src/common/overlays.rs index 67ec3877b0..e83b23089e 100644 --- a/game/src/common/overlays.rs +++ b/game/src/common/overlays.rs @@ -7,7 +7,7 @@ use crate::helpers::ID; use crate::managed::{ManagedGUIState, WrappedComposite, WrappedOutcome}; use abstutil::{prettyprint_usize, Counter}; use ezgui::{ - hotkey, Button, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, Histogram, + hotkey, Btn, Button, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, Histogram, HorizontalAlignment, JustDraw, Key, Line, ManagedWidget, Outcome, Plot, PlotOptions, RewriteColor, Series, Text, TextExt, VerticalAlignment, }; @@ -235,45 +235,35 @@ impl Overlays { pub fn change_overlays(ctx: &mut EventCtx, app: &App) -> Option { let mut choices = vec![ - WrappedComposite::text_button(ctx, "None", hotkey(Key::N)), - WrappedComposite::text_button(ctx, "map edits", hotkey(Key::E)), - WrappedComposite::text_button(ctx, "worst traffic jams", hotkey(Key::G)), - WrappedComposite::text_button(ctx, "elevation", hotkey(Key::S)), - ManagedWidget::btn(Button::rectangle_svg( + Btn::text_fg("None").build_def(ctx, hotkey(Key::N)), + Btn::text_fg("map edits").build_def(ctx, hotkey(Key::E)), + Btn::text_fg("worst traffic jams").build_def(ctx, hotkey(Key::G)), + Btn::text_fg("elevation").build_def(ctx, hotkey(Key::S)), + Btn::svg( "../data/system/assets/layers/parking_avail.svg", - "parking availability", - hotkey(Key::P), RewriteColor::Change(Color::hex("#F2F2F2"), colors::HOVERING), - ctx, - )), - ManagedWidget::btn(Button::rectangle_svg( + ) + .build(ctx, "parking availability", hotkey(Key::P)), + Btn::svg( "../data/system/assets/layers/intersection_delay.svg", - "intersection delay", - hotkey(Key::I), RewriteColor::Change(Color::hex("#F2F2F2"), colors::HOVERING), - ctx, - )), - ManagedWidget::btn(Button::rectangle_svg( + ) + .build(ctx, "intersection delay", hotkey(Key::I)), + Btn::svg( "../data/system/assets/layers/throughput.svg", - "throughput", - hotkey(Key::T), RewriteColor::Change(Color::hex("#F2F2F2"), colors::HOVERING), - ctx, - )), - ManagedWidget::btn(Button::rectangle_svg( + ) + .build(ctx, "throughput", hotkey(Key::T)), + Btn::svg( "../data/system/assets/layers/bike_network.svg", - "bike network", - hotkey(Key::B), RewriteColor::Change(Color::hex("#F2F2F2"), colors::HOVERING), - ctx, - )), - ManagedWidget::btn(Button::rectangle_svg( + ) + .build(ctx, "bike network", hotkey(Key::B)), + Btn::svg( "../data/system/assets/layers/bus_network.svg", - "bus network", - hotkey(Key::U), RewriteColor::Change(Color::hex("#F2F2F2"), colors::HOVERING), - ctx, - )), + ) + .build(ctx, "bus network", hotkey(Key::U)), ]; // TODO Grey out the inactive SVGs, and add the green checkmark if let Some((find, replace)) = match app.overlay { @@ -321,7 +311,9 @@ impl Overlays { ManagedWidget::col(vec![ ManagedWidget::row(vec![ "Heat Map Layers".draw_text(ctx), - WrappedComposite::text_button(ctx, "X", hotkey(Key::Escape)).align_right(), + Btn::text_fg("X") + .build(ctx, "close", hotkey(Key::Escape)) + .align_right(), ]), ManagedWidget::row(choices).flex_wrap(ctx, 20), ]) @@ -332,7 +324,7 @@ impl Overlays { .max_size_percent(30, 50) .build(ctx), ) - .cb("X", Box::new(|_, _| Some(Transition::Pop))) + .cb("close", Box::new(|_, _| Some(Transition::Pop))) .maybe_cb( "None", Box::new(|_, app| { @@ -774,13 +766,11 @@ impl Overlays { let col = vec![ ManagedWidget::row(vec![ "intersection demand".draw_text(ctx), - ManagedWidget::btn(Button::rectangle_svg( + Btn::svg( "../data/system/assets/tools/locate.svg", - "intersection demand", - None, RewriteColor::Change(Color::hex("#CC4121"), colors::HOVERING), - ctx, - )), + ) + .build(ctx, "intersection demand", None), WrappedComposite::text_button(ctx, "X", None).align_right(), ]), ColorLegend::row(ctx, Color::RED, "current demand"), @@ -818,13 +808,11 @@ impl Overlays { for idx in 0..route.stops.len() { col.push(ManagedWidget::row(vec![ format!("Stop {}", idx + 1).draw_text(ctx), - ManagedWidget::btn(Button::rectangle_svg( + Btn::svg( "../data/system/assets/tools/locate.svg", - &format!("Stop {}", idx + 1), - None, RewriteColor::Change(Color::hex("#CC4121"), colors::HOVERING), - ctx, - )), + ) + .build(ctx, format!("Stop {}", idx + 1), None), if let Some(hgram) = delay_per_stop.remove(&route.stops[idx]) { format!( ": {} (avg {})", diff --git a/game/src/common/panels.rs b/game/src/common/panels.rs index 78dea2148e..ebfb9e773d 100644 --- a/game/src/common/panels.rs +++ b/game/src/common/panels.rs @@ -3,7 +3,7 @@ use crate::game::Transition; use crate::managed::WrappedComposite; use crate::options; use ezgui::{ - hotkey, Button, Composite, EventCtx, HorizontalAlignment, Key, ManagedWidget, RewriteColor, + hotkey, Btn, Composite, EventCtx, HorizontalAlignment, Key, ManagedWidget, RewriteColor, VerticalAlignment, }; @@ -11,21 +11,17 @@ pub fn tool_panel(ctx: &mut EventCtx) -> WrappedComposite { let row = vec![ // TODO Maybe this is confusing -- it doesn't jump to the title screen necessarily. // Caller has to handle this one - ManagedWidget::btn(Button::rectangle_svg( + Btn::svg( "../data/system/assets/tools/home.svg", - "back", - hotkey(Key::Escape), RewriteColor::ChangeAll(colors::HOVERING), - ctx, - )) + ) + .build(ctx, "back", hotkey(Key::Escape)) .margin(10), - ManagedWidget::btn(Button::rectangle_svg( + Btn::svg( "../data/system/assets/tools/settings.svg", - "settings", - None, RewriteColor::ChangeAll(colors::HOVERING), - ctx, - )) + ) + .build(ctx, "settings", None) .margin(10), ]; WrappedComposite::new( diff --git a/game/src/edit/lanes.rs b/game/src/edit/lanes.rs index 6b1ad202dc..1ea95d2381 100644 --- a/game/src/edit/lanes.rs +++ b/game/src/edit/lanes.rs @@ -7,7 +7,7 @@ use crate::helpers::ID; use crate::managed::WrappedComposite; use crate::render::Renderable; use ezgui::{ - hotkey, Button, Choice, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, + hotkey, Btn, Button, Choice, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, ManagedWidget, Outcome, RewriteColor, TextExt, VerticalAlignment, }; use map_model::{EditCmd, LaneID, LaneType, Map, RoadID}; @@ -57,13 +57,11 @@ impl LaneEditor { ] { row.push( if active { - ManagedWidget::btn(Button::rectangle_svg( - &format!("../data/system/assets/edit/{}.svg", icon), - label, - hotkey(key), + Btn::svg( + format!("../data/system/assets/edit/{}.svg", icon), RewriteColor::ChangeAll(colors::HOVERING), - ctx, - )) + ) + .build(ctx, label, hotkey(key)) } else { ManagedWidget::draw_svg_transform( ctx, diff --git a/game/src/managed.rs b/game/src/managed.rs index 36714a488c..32b1763a73 100644 --- a/game/src/managed.rs +++ b/game/src/managed.rs @@ -2,7 +2,7 @@ use crate::app::App; use crate::colors; use crate::game::{DrawBaselayer, State, Transition}; use ezgui::{ - hotkey, Button, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, + hotkey, Btn, Button, Color, Composite, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, ManagedWidget, MultiKey, Outcome, RewriteColor, Text, VerticalAlignment, }; use std::collections::HashMap; @@ -64,13 +64,11 @@ impl WrappedComposite { tooltip: &str, hotkey: Option, ) -> ManagedWidget { - ManagedWidget::btn(Button::rectangle_svg( + Btn::svg( filename, - tooltip, - hotkey, RewriteColor::Change(Color::WHITE, colors::HOVERING), - ctx, - )) + ) + .build(ctx, tooltip, hotkey) } pub fn nice_text_button( @@ -91,18 +89,11 @@ impl WrappedComposite { } pub fn text_button(ctx: &EventCtx, label: &str, hotkey: Option) -> ManagedWidget { - WrappedComposite::nice_text_button(ctx, Text::from(Line(label)), hotkey, label) + Btn::text_fg(label).build_def(ctx, hotkey) } pub fn text_bg_button(ctx: &EventCtx, label: &str, hotkey: Option) -> ManagedWidget { - ManagedWidget::btn(Button::text_bg( - Text::from(Line(label).fg(Color::BLACK)), - Color::WHITE, - colors::HOVERING, - hotkey, - label, - ctx, - )) + Btn::text_bg2(label).build_def(ctx, hotkey) } // Always includes a built-in "X" quit option diff --git a/game/src/sandbox/speed.rs b/game/src/sandbox/speed.rs index b34024e0ec..d11c55b4ac 100644 --- a/game/src/sandbox/speed.rs +++ b/game/src/sandbox/speed.rs @@ -6,7 +6,7 @@ use crate::helpers::ID; use crate::managed::{WrappedComposite, WrappedOutcome}; use crate::sandbox::{GameplayMode, SandboxMode}; use ezgui::{ - hotkey, Button, Color, Composite, EventCtx, EventLoopMode, GeomBatch, GfxCtx, + hotkey, Btn, Button, Color, Composite, EventCtx, EventLoopMode, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, ManagedWidget, Outcome, Plot, PlotOptions, RewriteColor, Series, Slider, Text, VerticalAlignment, }; @@ -37,23 +37,19 @@ impl SpeedControls { fn make_panel(ctx: &mut EventCtx, paused: bool, setting: SpeedSetting) -> WrappedComposite { let mut row = Vec::new(); row.push( - ManagedWidget::btn(if paused { - Button::rectangle_svg( + if paused { + Btn::svg( "../data/system/assets/speed/triangle.svg", - "play", - hotkey(Key::Space), RewriteColor::ChangeAll(colors::HOVERING), - ctx, ) + .build(ctx, "play", hotkey(Key::Space)) } else { - Button::rectangle_svg( + Btn::svg( "../data/system/assets/speed/pause.svg", - "pause", - hotkey(Key::Space), RewriteColor::ChangeAll(colors::HOVERING), - ctx, ) - }) + .build(ctx, "pause", hotkey(Key::Space)) + } .margin(5) .centered_vert() .bg(colors::SECTION_BG), @@ -117,20 +113,16 @@ impl SpeedControls { false, ctx, )), - ManagedWidget::btn(Button::rectangle_svg( + Btn::svg( "../data/system/assets/speed/jump_to_time.svg", - "jump to specific time", - hotkey(Key::B), RewriteColor::ChangeAll(colors::HOVERING), - ctx, - )), - ManagedWidget::btn(Button::rectangle_svg( + ) + .build(ctx, "jump to specific time", hotkey(Key::B)), + Btn::svg( "../data/system/assets/speed/reset.svg", - "reset to midnight", - hotkey(Key::X), RewriteColor::ChangeAll(colors::HOVERING), - ctx, - )), + ) + .build(ctx, "reset to midnight", hotkey(Key::X)), ] .into_iter() .map(|x| x.margin(5))