From 135e85d06d5cb301649476ee96a7164ffdd2136f Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 19 Aug 2021 10:49:10 -0700 Subject: [PATCH] Force some distance spinners to render in feet --- game/src/edit/roads.rs | 12 ++++++++++-- widgetry/src/widgets/spinner.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/game/src/edit/roads.rs b/game/src/edit/roads.rs index d8d4c7a682..d232eccf4f 100644 --- a/game/src/edit/roads.rs +++ b/game/src/edit/roads.rs @@ -1,5 +1,5 @@ use abstutil::Tags; -use geom::{CornerRadii, Distance}; +use geom::{CornerRadii, Distance, UnitFmt}; use map_gui::render::{Renderable, OUTLINE_THICKNESS}; use map_gui::tools::PopupMsg; use map_gui::ID; @@ -488,12 +488,20 @@ fn make_main_panel( Line("Width").secondary().into_widget(ctx).centered_vert(), Widget::col(vec![ Widget::dropdown(ctx, "width preset", lane.width, width_choices(app, l)), - Spinner::widget( + Spinner::widget_with_custom_rendering( ctx, "width custom", (Distance::feet(1.0), Distance::feet(20.0)), lane.width, Distance::feet(0.5), + // Even if the user's settings are set to meters, our step size is in feet, so + // just render in feet. + Box::new(|x| { + x.to_string(&UnitFmt { + round_durations: false, + metric: false, + }) + }), ) .centered_horiz(), ]), diff --git a/widgetry/src/widgets/spinner.rs b/widgetry/src/widgets/spinner.rs index 394d819c1d..4d335523d3 100644 --- a/widgetry/src/widgets/spinner.rs +++ b/widgetry/src/widgets/spinner.rs @@ -42,6 +42,7 @@ pub struct Spinner { step_size: T, pub current: T, label: String, + render_value: Box String>, up: Button, down: Button, @@ -53,12 +54,33 @@ pub struct Spinner { } impl Spinner { + /// Creates a spinner using the `SpinnerValue`'s default `to_string` implementation for + /// rendering. pub fn widget( ctx: &EventCtx, label: impl Into, (low, high): (T, T), current: T, step_size: T, + ) -> Widget { + Spinner::widget_with_custom_rendering( + ctx, + label, + (low, high), + current, + step_size, + Box::new(|x| x.to_string()), + ) + } + + /// Creates a spinner using a custom method for rendering the value as text. + pub fn widget_with_custom_rendering( + ctx: &EventCtx, + label: impl Into, + (low, high): (T, T), + current: T, + step_size: T, + render_value: Box String>, ) -> Widget { let label = label.into(); Widget::new(Box::new(Self::new( @@ -67,6 +89,7 @@ impl Spinner { (low, high), current, step_size, + render_value, ))) .named(label) } @@ -77,6 +100,7 @@ impl Spinner { (low, high): (T, T), mut current: T, step_size: T, + render_value: Box String>, ) -> Self { let button_builder = ctx .style() @@ -136,6 +160,7 @@ impl Spinner { current, step_size, label, + render_value, up, down, @@ -169,7 +194,7 @@ impl Spinner { Polygon::rounded_rectangle(self.dims.width, self.dims.height, 5.0), )]); batch.append( - Text::from(self.current.to_string()) + Text::from((self.render_value)(self.current)) .render_autocropped(prerender) .centered_on(Pt2D::new(TEXT_WIDTH / 2.0, self.dims.height / 2.0)), );