improve checkbox API, use it for colored minimap checkboxes

This commit is contained in:
Dustin Carlino 2020-04-06 11:12:23 -07:00
parent 696077a460
commit fadae3aa85
9 changed files with 89 additions and 93 deletions

View File

@ -9,8 +9,8 @@
// --features wasm-backend --example demo
use ezgui::{
hotkey, lctrl, Btn, Color, Composite, Drawable, EventCtx, EventLoopMode, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, Plot, PlotOptions, Series, Text, TextExt,
hotkey, lctrl, Btn, Checkbox, Color, Composite, Drawable, EventCtx, EventLoopMode, GeomBatch,
GfxCtx, HorizontalAlignment, Key, Line, Outcome, Plot, PlotOptions, Series, Text, TextExt,
VerticalAlignment, Widget, GUI,
};
use geom::{Angle, Duration, Polygon, Pt2D, Time};
@ -259,7 +259,7 @@ fn make_controls(ctx: &mut EventCtx) -> Composite {
},
Widget::row(vec![
// This just cycles between two arbitrary buttons
Widget::custom_checkbox(
Checkbox::new(
false,
Btn::text_bg1("Pause").build(ctx, "pause the stopwatch", hotkey(Key::Space)),
Btn::text_bg1("Resume").build(ctx, "resume the stopwatch", hotkey(Key::Space)),
@ -269,8 +269,8 @@ fn make_controls(ctx: &mut EventCtx) -> Composite {
Btn::text_fg("Reset")
.build(ctx, "reset the stopwatch", None)
.margin(5),
Widget::checkbox(ctx, "Draw scrollable canvas", None, true).margin(5),
Widget::checkbox(ctx, "Show timeseries", lctrl(Key::T), false).margin(5),
Checkbox::new(ctx, "Draw scrollable canvas", None, true).margin(5),
Checkbox::new(ctx, "Show timeseries", lctrl(Key::T), false).margin(5),
])
.evenly_spaced(),
"Stopwatch: ...".draw_text(ctx).named("stopwatch"),

View File

@ -1,9 +1,8 @@
use crate::widgets::containers::{Container, Nothing};
use crate::{
Autocomplete, Btn, Button, Checkbox, Choice, Color, Drawable, Dropdown, EventCtx, Filler,
GeomBatch, GfxCtx, HorizontalAlignment, JustDraw, Menu, MultiKey, PersistentSplit,
RewriteColor, ScreenDims, ScreenPt, ScreenRectangle, Slider, Spinner, TextBox,
VerticalAlignment, WidgetImpl,
Autocomplete, Button, Checkbox, Choice, Color, Drawable, Dropdown, EventCtx, Filler, GeomBatch,
GfxCtx, HorizontalAlignment, JustDraw, Menu, PersistentSplit, RewriteColor, ScreenDims,
ScreenPt, ScreenRectangle, Slider, Spinner, TextBox, VerticalAlignment, WidgetImpl,
};
use geom::{Distance, Polygon};
use std::collections::HashSet;
@ -217,30 +216,6 @@ impl Widget {
JustDraw::svg_transform(ctx, filename, rewrite)
}
// TODO Convenient constructors here, but shouldn't do it like this
pub fn checkbox(
ctx: &EventCtx,
label: &str,
hotkey: Option<MultiKey>,
enabled: bool,
) -> Widget {
Widget::custom_checkbox(
enabled,
Btn::text_fg(format!("[ ] {}", label)).build(ctx, label, hotkey.clone()),
Btn::text_fg(format!("[X] {}", label)).build(ctx, label, hotkey),
)
.outline(ctx.style().outline_thickness, ctx.style().outline_color)
.named(label)
}
// TODO Not typesafe! Gotta pass a button.
pub fn custom_checkbox(enabled: bool, false_btn: Widget, true_btn: Widget) -> Widget {
Widget::new(Box::new(Checkbox::new(
enabled,
false_btn.take_btn(),
true_btn.take_btn(),
)))
}
// TODO Likewise
pub fn text_entry(ctx: &EventCtx, prefilled: String, exclusive_focus: bool) -> Widget {
// TODO Hardcoded style, max chars

View File

@ -1,4 +1,6 @@
use crate::{Button, EventCtx, GfxCtx, Outcome, ScreenDims, ScreenPt, WidgetImpl};
use crate::{
Btn, Button, EventCtx, GfxCtx, MultiKey, Outcome, ScreenDims, ScreenPt, Widget, WidgetImpl,
};
pub struct Checkbox {
pub(crate) enabled: bool,
@ -7,21 +9,32 @@ pub struct Checkbox {
}
impl Checkbox {
pub fn new(enabled: bool, false_btn: Button, true_btn: Button) -> Checkbox {
// TODO Not typesafe! Gotta pass a button.
pub fn new(enabled: bool, false_btn: Widget, true_btn: Widget) -> Widget {
if enabled {
Checkbox {
Widget::new(Box::new(Checkbox {
enabled,
btn: true_btn,
other_btn: false_btn,
}
btn: true_btn.take_btn(),
other_btn: false_btn.take_btn(),
}))
} else {
Checkbox {
Widget::new(Box::new(Checkbox {
enabled,
btn: false_btn,
other_btn: true_btn,
}
btn: false_btn.take_btn(),
other_btn: true_btn.take_btn(),
}))
}
}
pub fn text(ctx: &EventCtx, label: &str, hotkey: Option<MultiKey>, enabled: bool) -> Widget {
Checkbox::new(
enabled,
Btn::text_fg(format!("[ ] {}", label)).build(ctx, label, hotkey.clone()),
Btn::text_fg(format!("[X] {}", label)).build(ctx, label, hotkey),
)
.outline(ctx.style().outline_thickness, ctx.style().outline_color)
.named(label)
}
}
impl WidgetImpl for Checkbox {

View File

@ -4,8 +4,9 @@ use crate::game::Transition;
use crate::render::MIN_ZOOM_FOR_DETAIL;
use abstutil::clamp;
use ezgui::{
hotkey, Btn, Color, Composite, EventCtx, Filler, GeomBatch, GfxCtx, HorizontalAlignment, Key,
Line, Outcome, RewriteColor, ScreenDims, ScreenPt, VerticalAlignment, Widget,
hotkey, Btn, Checkbox, Color, Composite, EventCtx, Filler, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, RewriteColor, ScreenDims, ScreenPt, VerticalAlignment,
Widget,
};
use geom::{Distance, Polygon, Pt2D, Ring};
@ -119,15 +120,22 @@ impl Minimap {
x if x == "change overlay" => {
return Overlays::change_overlays(ctx, app);
}
x => {
// Handles both "show {}" and "hide {}"
let key = x[5..].to_string();
app.agent_cs.toggle(key);
self.composite = make_minimap_panel(ctx, app, self.zoom_lvl);
}
_ => unreachable!(),
},
None => {}
}
// TODO an outcome for a checkbox flipping state could be useful
let mut toggle = None;
for (label, _, enabled) in &app.agent_cs.rows {
if *enabled != self.composite.is_checked(label) {
toggle = Some(label.clone());
break;
}
}
if let Some(label) = toggle {
app.agent_cs.toggle(label);
self.composite = make_minimap_panel(ctx, app, self.zoom_lvl);
}
if self.zoomed {
let inner_rect = self.composite.filler_rect("minimap");
@ -380,26 +388,26 @@ fn make_vert_viz_panel(ctx: &mut EventCtx, app: &App) -> Widget {
}
fn colored_checkbox(ctx: &EventCtx, app: &App, label: &str, color: Color, enabled: bool) -> Widget {
if enabled {
Btn::svg(
"../data/system/assets/tools/checkmark.svg",
RewriteColor::ChangeMore(vec![(Color::BLACK, color), (Color::WHITE, app.cs.hovering)]),
)
.normal_color(RewriteColor::Change(Color::BLACK, color))
.build(ctx, format!("hide {}", label), None)
} else {
// Fancy way of saying a circle ;)
Btn::svg(
"../data/system/assets/tools/checkmark.svg",
RewriteColor::ChangeMore(vec![
(Color::BLACK, color),
(Color::WHITE, Color::INVISIBLE),
]),
)
.normal_color(RewriteColor::ChangeMore(vec![
(Color::BLACK, color.alpha(0.3)),
let true_btn = Btn::svg(
"../data/system/assets/tools/checkmark.svg",
RewriteColor::ChangeMore(vec![(Color::BLACK, color), (Color::WHITE, app.cs.hovering)]),
)
.normal_color(RewriteColor::Change(Color::BLACK, color))
.build(ctx, format!("hide {}", label), None);
// Fancy way of saying a circle ;)
let false_btn = Btn::svg(
"../data/system/assets/tools/checkmark.svg",
RewriteColor::ChangeMore(vec![
(Color::BLACK, color),
(Color::WHITE, Color::INVISIBLE),
]))
.build(ctx, format!("show {}", label), None)
}
]),
)
.normal_color(RewriteColor::ChangeMore(vec![
(Color::BLACK, color.alpha(0.3)),
(Color::WHITE, Color::INVISIBLE),
]))
.build(ctx, format!("show {}", label), None);
Checkbox::new(enabled, false_btn, true_btn).named(label)
}

View File

@ -8,7 +8,7 @@ use crate::managed::{ManagedGUIState, WrappedComposite};
use crate::render::MIN_ZOOM_FOR_DETAIL;
use abstutil::{prettyprint_usize, Counter};
use ezgui::{
hotkey, Btn, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, Histogram,
hotkey, Btn, Checkbox, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, Histogram,
HorizontalAlignment, Key, Line, Outcome, Spinner, Text, TextExt, VerticalAlignment, Widget,
};
use geom::{Circle, Distance, Duration, PolyLine, Pt2D, Time};
@ -937,7 +937,7 @@ fn population_controls(
])
.centered(),
if app.primary.sim.get_pandemic_model().is_some() {
Widget::checkbox(ctx, "Run pandemic model", None, opts.pandemic)
Checkbox::text(ctx, "Run pandemic model", None, opts.pandemic)
} else {
Widget::nothing()
},
@ -962,7 +962,7 @@ fn population_controls(
assert_eq!(total_ppl, model.count_total());
}
col.push(Widget::checkbox(
col.push(Checkbox::text(
ctx,
"Show heatmap",
None,

View File

@ -10,8 +10,8 @@ use crate::managed::{WrappedComposite, WrappedOutcome};
use crate::render::{calculate_corners, DrawOptions};
use abstutil::Timer;
use ezgui::{
hotkey, lctrl, Btn, Color, Composite, Drawable, EventCtx, EventLoopMode, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, Text, VerticalAlignment, Widget, Wizard,
hotkey, lctrl, Btn, Checkbox, Color, Composite, Drawable, EventCtx, EventLoopMode, GeomBatch,
GfxCtx, HorizontalAlignment, Key, Line, Outcome, Text, VerticalAlignment, Widget, Wizard,
};
use geom::{Duration, Pt2D};
use map_model::{IntersectionID, NORMAL_LANE_THICKNESS};
@ -43,13 +43,13 @@ impl DebugMode {
.align_right(),
]),
Text::new().draw(ctx).named("current info"),
Widget::checkbox(ctx, "show buildings", hotkey(Key::Num1), true),
Widget::checkbox(ctx, "show intersections", hotkey(Key::Num2), true),
Widget::checkbox(ctx, "show lanes", hotkey(Key::Num3), true),
Widget::checkbox(ctx, "show areas", hotkey(Key::Num4), true),
Widget::checkbox(ctx, "show extra shapes", hotkey(Key::Num5), true),
Widget::checkbox(ctx, "show labels", hotkey(Key::Num6), false),
Widget::checkbox(ctx, "show route for all agents", hotkey(Key::R), false),
Checkbox::text(ctx, "show buildings", hotkey(Key::Num1), true),
Checkbox::text(ctx, "show intersections", hotkey(Key::Num2), true),
Checkbox::text(ctx, "show lanes", hotkey(Key::Num3), true),
Checkbox::text(ctx, "show areas", hotkey(Key::Num4), true),
Checkbox::text(ctx, "show extra shapes", hotkey(Key::Num5), true),
Checkbox::text(ctx, "show labels", hotkey(Key::Num6), false),
Checkbox::text(ctx, "show route for all agents", hotkey(Key::R), false),
Widget::col(
vec![
(lctrl(Key::H), "unhide everything"),

View File

@ -12,7 +12,7 @@ use crate::game::Transition;
use crate::helpers::ID;
use crate::render::{ExtraShapeID, MIN_ZOOM_FOR_DETAIL};
use ezgui::{
hotkey, Btn, Choice, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx,
hotkey, Btn, Checkbox, Choice, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, Plot, PlotOptions, Series, Text, TextExt,
VerticalAlignment, Widget,
};
@ -563,7 +563,7 @@ impl DataOptions {
]),
if app.has_prebaked().is_some() {
// TODO Change the wording of this
Widget::checkbox(ctx, "Show baseline data", None, self.show_baseline)
Checkbox::text(ctx, "Show baseline data", None, self.show_baseline)
} else {
Widget::nothing()
},

View File

@ -2,7 +2,7 @@ use crate::app::App;
use crate::colors::ColorSchemeChoice;
use crate::game::{State, Transition};
use ezgui::{
hotkey, Btn, Choice, Composite, EventCtx, GfxCtx, Key, Line, Outcome, TextExt, Widget,
hotkey, Btn, Checkbox, Choice, Composite, EventCtx, GfxCtx, Key, Line, Outcome, TextExt, Widget,
};
use geom::Duration;
@ -49,15 +49,15 @@ impl OptionsPanel {
.build_def(ctx, hotkey(Key::Escape))
.align_right(),
]),
Widget::checkbox(ctx, "Enable developer mode", None, app.opts.dev).margin(5),
Widget::checkbox(
Checkbox::text(ctx, "Enable developer mode", None, app.opts.dev).margin(5),
Checkbox::text(
ctx,
"Invert direction of vertical scrolling",
None,
ctx.canvas.invert_scroll,
)
.margin(5),
Widget::checkbox(
Checkbox::text(
ctx,
"Use touchpad to pan and hold Control to zoom",
None,

View File

@ -4,7 +4,7 @@ use crate::game::{msg, State, Transition};
use crate::helpers::ID;
use crate::sandbox::{GameplayMode, SandboxMode};
use ezgui::{
hotkey, Btn, Choice, Color, Composite, EventCtx, EventLoopMode, GeomBatch, GfxCtx,
hotkey, Btn, Checkbox, Choice, Color, Composite, EventCtx, EventLoopMode, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, PersistentSplit, RewriteColor, Slider, Text,
VerticalAlignment, Widget,
};
@ -31,7 +31,7 @@ enum SpeedSetting {
}
impl SpeedControls {
// TODO Could use custom_checkbox here, but not sure it'll make things that much simpler.
// TODO Could use checkbox here, but not sure it'll make things that much simpler.
fn make_panel(ctx: &mut EventCtx, app: &App, paused: bool, setting: SpeedSetting) -> Composite {
let mut row = Vec::new();
row.push(
@ -345,7 +345,7 @@ impl JumpToTime {
)
.named("time slider")
.margin(10),
Widget::checkbox(ctx, "Stop when there's a traffic jam", None, false)
Checkbox::text(ctx, "Stop when there's a traffic jam", None, false)
.padding(10)
.margin(10),
Btn::text_bg2("Go!")