From 9644ff24be301fc312a37c0d8f78b02a3309274a Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 20 Mar 2020 15:40:43 -0700 Subject: [PATCH] remove the last button constructor, except for the inactive ones --- ezgui/src/managed.rs | 20 ++++++------- ezgui/src/widgets/button.rs | 56 ++++++++++++----------------------- ezgui/src/widgets/dropdown.rs | 16 ++++------ game/src/sandbox/speed.rs | 47 +++++++++++++++++------------ 4 files changed, 62 insertions(+), 77 deletions(-) diff --git a/ezgui/src/managed.rs b/ezgui/src/managed.rs index 8cec7c886a..028a90347e 100644 --- a/ezgui/src/managed.rs +++ b/ezgui/src/managed.rs @@ -263,7 +263,7 @@ impl ManagedWidget { JustDraw::svg_transform(ctx, filename, rewrite) } - pub fn btn(btn: Button) -> ManagedWidget { + pub(crate) fn btn(btn: Button) -> ManagedWidget { ManagedWidget::new(WidgetType::Btn(btn)) } @@ -299,17 +299,10 @@ impl ManagedWidget { false_btn: ManagedWidget, true_btn: ManagedWidget, ) -> ManagedWidget { - fn take_btn(w: ManagedWidget) -> Button { - match w.widget { - WidgetType::Btn(btn) => btn, - _ => unreachable!(), - } - } - ManagedWidget::new(WidgetType::Checkbox(Checkbox::new( enabled, - take_btn(false_btn), - take_btn(true_btn), + false_btn.take_btn(), + true_btn.take_btn(), ))) } @@ -752,6 +745,13 @@ impl ManagedWidget { None } } + + pub(crate) fn take_btn(self) -> Button { + match self.widget { + WidgetType::Btn(btn) => btn, + _ => unreachable!(), + } + } } enum Dims { diff --git a/ezgui/src/widgets/button.rs b/ezgui/src/widgets/button.rs index 654973cf8f..4790954f68 100644 --- a/ezgui/src/widgets/button.rs +++ b/ezgui/src/widgets/button.rs @@ -122,36 +122,6 @@ impl Widget for Button { // TODO Simplify all of these APIs! impl Button { - pub fn text_no_bg( - unselected_text: Text, - selected_text: Text, - hotkey: Option, - tooltip: &str, - padding: bool, - ctx: &EventCtx, - ) -> Button { - // TODO Padding here is unfortunate, but I don't understand when the flexbox padding - // actually works. - let horiz_padding = if padding { 15.0 } else { 0.0 }; - let vert_padding = if padding { 8.0 } else { 0.0 }; - - let unselected_batch = unselected_text.render_ctx(ctx); - let dims = unselected_batch.get_dims(); - let selected_batch = selected_text.render_ctx(ctx); - assert_eq!(dims, selected_batch.get_dims()); - let geom = Polygon::rectangle( - dims.width + 2.0 * horiz_padding, - dims.height + 2.0 * vert_padding, - ); - - let mut normal = GeomBatch::new(); - normal.add_translated(unselected_batch, horiz_padding, vert_padding); - let mut hovered = GeomBatch::new(); - hovered.add_translated(selected_batch, horiz_padding, vert_padding); - - Button::new(ctx, normal, hovered, hotkey, tooltip, geom) - } - // TODO Extreme wackiness. pub fn inactive_button>(ctx: &mut EventCtx, label: S) -> ManagedWidget { let txt_batch = Text::from(Line(label).fg(Color::grey(0.5))).render_ctx(ctx); @@ -339,14 +309,26 @@ impl BtnBuilder { ManagedWidget::btn(btn) } BtnBuilder::TextFG(_, normal_txt, maybe_t) => { - let mut btn = Button::text_no_bg( - normal_txt.clone(), - normal_txt.change_fg(Color::ORANGE), - key, - &action_tooltip.into(), - true, - ctx, + // TODO Padding here is unfortunate, but I don't understand when the flexbox padding + // actually works. + let horiz_padding = 15.0; + let vert_padding = 8.0; + + let unselected_batch = normal_txt.clone().render_ctx(ctx); + let dims = unselected_batch.get_dims(); + let selected_batch = normal_txt.change_fg(Color::ORANGE).render_ctx(ctx); + assert_eq!(dims, selected_batch.get_dims()); + let geom = Polygon::rectangle( + dims.width + 2.0 * horiz_padding, + dims.height + 2.0 * vert_padding, ); + + let mut normal = GeomBatch::new(); + normal.add_translated(unselected_batch, horiz_padding, vert_padding); + let mut hovered = GeomBatch::new(); + hovered.add_translated(selected_batch, horiz_padding, vert_padding); + + let mut btn = Button::new(ctx, normal, hovered, key, &action_tooltip.into(), geom); if let Some(t) = maybe_t { btn.tooltip = t; } diff --git a/ezgui/src/widgets/dropdown.rs b/ezgui/src/widgets/dropdown.rs index 7e5b551eee..de48597ed2 100644 --- a/ezgui/src/widgets/dropdown.rs +++ b/ezgui/src/widgets/dropdown.rs @@ -1,8 +1,8 @@ use crate::layout::Widget; use crate::widgets::PopupMenu; use crate::{ - Button, Choice, Color, EventCtx, GfxCtx, InputResult, Line, ScreenDims, ScreenPt, - ScreenRectangle, Text, + Btn, Button, Choice, Color, EventCtx, GfxCtx, InputResult, ScreenDims, ScreenPt, + ScreenRectangle, }; use geom::{Polygon, Pt2D}; use std::any::Any; @@ -126,13 +126,7 @@ impl Widget for Dropdown { } fn make_btn(ctx: &EventCtx, name: &str, label: &str) -> Button { - let txt = Text::from(Line(format!("{} ▼", name))); - Button::text_no_bg( - txt.clone(), - txt.change_fg(Color::ORANGE), - None, - label, - true, - ctx, - ) + Btn::text_fg(format!("{} ▼", name)) + .build(ctx, label, None) + .take_btn() } diff --git a/game/src/sandbox/speed.rs b/game/src/sandbox/speed.rs index ef0809e9ef..55d7c0946d 100644 --- a/game/src/sandbox/speed.rs +++ b/game/src/sandbox/speed.rs @@ -6,9 +6,9 @@ use crate::helpers::ID; use crate::managed::{WrappedComposite, WrappedOutcome}; use crate::sandbox::{GameplayMode, SandboxMode}; use ezgui::{ - hotkey, Btn, Button, Color, Composite, EventCtx, EventLoopMode, GeomBatch, GfxCtx, - HorizontalAlignment, Key, Line, ManagedWidget, Outcome, Plot, PlotOptions, RewriteColor, - Series, Slider, Text, VerticalAlignment, + hotkey, Btn, Color, Composite, EventCtx, EventLoopMode, GeomBatch, GfxCtx, HorizontalAlignment, + Key, Line, ManagedWidget, Outcome, Plot, PlotOptions, RewriteColor, Series, Slider, Text, + VerticalAlignment, }; use geom::{Duration, Polygon, Time}; use instant::Instant; @@ -97,22 +97,31 @@ impl SpeedControls { row.push( ManagedWidget::row( vec![ - ManagedWidget::btn(Button::text_no_bg( - Text::from(Line("+1h").fg(Color::WHITE).size(21).roboto()), - Text::from(Line("+1h").fg(colors::HOVERING).size(21).roboto()), - hotkey(Key::N), - "step forwards 1 hour", - false, - ctx, - )), - ManagedWidget::btn(Button::text_no_bg( - Text::from(Line("+0.1s").fg(Color::WHITE).size(21).roboto()), - Text::from(Line("+0.1s").fg(colors::HOVERING).size(21).roboto()), - hotkey(Key::M), - "step forwards 0.1 seconds", - false, - ctx, - )), + Btn::custom( + Text::from(Line("+1h").fg(Color::WHITE).size(21).roboto()).render_ctx(ctx), + Text::from(Line("+1h").fg(colors::HOVERING).size(21).roboto()) + .render_ctx(ctx), + { + let dims = Text::from(Line("+1h").size(21).roboto()) + .render_ctx(ctx) + .get_dims(); + Polygon::rectangle(dims.width, dims.height) + }, + ) + .build(ctx, "step forwards 1 hour", hotkey(Key::N)), + Btn::custom( + Text::from(Line("+0.1s").fg(Color::WHITE).size(21).roboto()) + .render_ctx(ctx), + Text::from(Line("+0.1s").fg(colors::HOVERING).size(21).roboto()) + .render_ctx(ctx), + { + let dims = Text::from(Line("+0.1s").size(21).roboto()) + .render_ctx(ctx) + .get_dims(); + Polygon::rectangle(dims.width, dims.height) + }, + ) + .build(ctx, "step forwards 0.1 seconds", hotkey(Key::M)), Btn::svg_def("../data/system/assets/speed/jump_to_time.svg").build( ctx, "jump to specific time",