Force some distance spinners to render in feet

This commit is contained in:
Dustin Carlino 2021-08-19 10:49:10 -07:00
parent a297937ffa
commit 135e85d06d
2 changed files with 36 additions and 3 deletions

View File

@ -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(),
]),

View File

@ -42,6 +42,7 @@ pub struct Spinner<T> {
step_size: T,
pub current: T,
label: String,
render_value: Box<dyn Fn(T) -> String>,
up: Button,
down: Button,
@ -53,12 +54,33 @@ pub struct Spinner<T> {
}
impl<T: 'static + SpinnerValue> Spinner<T> {
/// Creates a spinner using the `SpinnerValue`'s default `to_string` implementation for
/// rendering.
pub fn widget(
ctx: &EventCtx,
label: impl Into<String>,
(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<String>,
(low, high): (T, T),
current: T,
step_size: T,
render_value: Box<dyn Fn(T) -> String>,
) -> Widget {
let label = label.into();
Widget::new(Box::new(Self::new(
@ -67,6 +89,7 @@ impl<T: 'static + SpinnerValue> Spinner<T> {
(low, high),
current,
step_size,
render_value,
)))
.named(label)
}
@ -77,6 +100,7 @@ impl<T: 'static + SpinnerValue> Spinner<T> {
(low, high): (T, T),
mut current: T,
step_size: T,
render_value: Box<dyn Fn(T) -> String>,
) -> Self {
let button_builder = ctx
.style()
@ -136,6 +160,7 @@ impl<T: 'static + SpinnerValue> Spinner<T> {
current,
step_size,
label,
render_value,
up,
down,
@ -169,7 +194,7 @@ impl<T: 'static + SpinnerValue> Spinner<T> {
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)),
);