starting a refactor of buttons. very modest start. [rebuild]

This commit is contained in:
Dustin Carlino 2020-03-15 19:58:03 -07:00
parent fa013f70d4
commit 122aa1e25c
4 changed files with 89 additions and 53 deletions

View File

@ -31,7 +31,7 @@ pub use crate::runner::{run, EventLoopMode, Settings, GUI};
pub use crate::screen_geom::{ScreenDims, ScreenPt, ScreenRectangle};
pub use crate::text::{Line, Text, TextExt, TextSpan, HOTKEY_COLOR};
pub use crate::widgets::{
Autocomplete, Button, Choice, Filler, Histogram, ItemSlider, JustDraw, ModalMenu, Plot,
Autocomplete, Btn, Button, Choice, Filler, Histogram, ItemSlider, JustDraw, ModalMenu, Plot,
PlotOptions, Series, Slider, Warper, WarpingItemSlider, Wizard, WrappedWizard,
};

View File

@ -259,3 +259,64 @@ impl Button {
JustDraw::wrap(ctx, batch)
}
}
// TODO Experimental new refactoring
pub struct Btn {}
impl Btn {
pub fn svg(path: &str, hover: RewriteColor) -> BtnBuilder {
BtnBuilder::SVG(path.to_string(), hover)
}
pub fn text_fg<I: Into<String>>(label: I) -> BtnBuilder {
BtnBuilder::TextFG(label.into())
}
// The info panel style with the lighter background color
pub fn text_bg1<I: Into<String>>(label: I) -> BtnBuilder {
BtnBuilder::TextBG1(label.into())
}
}
pub enum BtnBuilder {
SVG(String, RewriteColor),
TextFG(String),
TextBG1(String),
}
impl BtnBuilder {
pub fn build<I: Into<String>>(
self,
ctx: &EventCtx,
action_tooltip: I,
key: Option<MultiKey>,
) -> ManagedWidget {
match self {
BtnBuilder::SVG(path, hover) => ManagedWidget::btn(Button::rectangle_svg(
&path,
&action_tooltip.into(),
key,
hover,
ctx,
)),
BtnBuilder::TextFG(label) => ManagedWidget::btn(Button::text_no_bg(
Text::from(Line(&label)),
Text::from(Line(label).fg(Color::ORANGE)),
key,
&action_tooltip.into(),
true,
ctx,
))
.outline(2.0, Color::WHITE),
BtnBuilder::TextBG1(label) => ManagedWidget::btn(Button::text_bg(
Text::from(Line(label)),
Color::grey(0.5),
Color::ORANGE,
key,
&action_tooltip.into(),
ctx,
)),
}
}
}

View File

@ -15,7 +15,7 @@ mod warper;
mod wizard;
pub use self::autocomplete::Autocomplete;
pub use self::button::Button;
pub use self::button::{Btn, Button};
pub use self::checkbox::Checkbox;
pub(crate) use self::dropdown::Dropdown;
pub use self::filler::Filler;

View File

@ -3,14 +3,13 @@ use crate::colors;
use crate::common::Warping;
use crate::game::{msg, State, Transition, WizardState};
use crate::helpers::{rotating_color_map, ID};
use crate::managed::WrappedComposite;
use crate::render::{dashed_lines, Renderable, MIN_ZOOM_FOR_DETAIL};
use crate::sandbox::{SandboxMode, SpeedControls};
use abstutil::prettyprint_usize;
use ezgui::{
hotkey, Button, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment,
Key, Line, ManagedWidget, Outcome, Plot, PlotOptions, RewriteColor, Series, Text, TextExt,
VerticalAlignment,
hotkey, Btn, Button, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx,
HorizontalAlignment, Key, Line, ManagedWidget, Outcome, Plot, PlotOptions, RewriteColor,
Series, Text, TextExt, VerticalAlignment,
};
use geom::{Angle, Circle, Distance, Duration, Polygon, Pt2D, Statistic, Time};
use map_model::{BuildingID, IntersectionID, IntersectionType, Map, Path, PathStep};
@ -291,7 +290,7 @@ impl InfoPanel {
match self.composite.event(ctx) {
Some(Outcome::Clicked(action)) => {
if action == "X" {
if action == "close info" {
(true, None)
} else if action == "jump to object" {
(
@ -377,14 +376,12 @@ fn info_for(
) -> Vec<ManagedWidget> {
let (map, sim, draw_map) = (&app.primary.map, &app.primary.sim, &app.primary.draw_map);
let header_btns = ManagedWidget::row(vec![
ManagedWidget::btn(Button::rectangle_svg(
Btn::svg(
"../data/system/assets/tools/locate.svg",
"jump to object",
hotkey(Key::J),
RewriteColor::Change(Color::hex("#CC4121"), colors::HOVERING),
ctx,
)),
WrappedComposite::text_button(ctx, "X", hotkey(Key::Escape)),
)
.build(ctx, "jump to object", hotkey(Key::J)),
Btn::text_fg("X").build(ctx, "close info", hotkey(Key::Escape)),
])
.align_right();
@ -652,15 +649,9 @@ fn info_for(
// TODO Show buttons to examine first 3, or a ...More button
for p in people {
rows.push(
ManagedWidget::btn(Button::text_bg(
Text::from(Line(format!("Person #{}", p.0))),
colors::SECTION_BG,
colors::HOVERING,
None,
&format!("examine Person #{}", p.0),
ctx,
))
.margin(5),
Btn::text_bg1(format!("Person #{}", p.0))
.build(ctx, format!("examine Person #{}", p.0), None)
.margin(5),
);
}
}
@ -809,7 +800,9 @@ fn info_for(
rows.push(ManagedWidget::row(vec![
Line(format!("Trip #{}", id.0)).roboto_bold().draw(ctx),
// No jump-to-object button; this is probably a finished trip.
WrappedComposite::text_button(ctx, "X", hotkey(Key::Escape)).align_right(),
Btn::text_fg("X")
.build(ctx, "close info", hotkey(Key::Escape))
.align_right(),
]));
}
rows.extend(action_btns);
@ -828,18 +821,12 @@ fn info_for(
// TODO Point out where the person is now, relative to schedule...
rows.push(match person.state {
PersonState::Inside(b) => ManagedWidget::btn(Button::text_bg(
Text::from(Line(format!(
"Currently inside {}",
map.get_b(b).just_address(map)
))),
colors::SECTION_BG,
colors::HOVERING,
None,
// TODO not the best tooltip, but easy to parse :(
&format!("examine Building #{}", b.0),
ctx,
)),
// TODO not the best tooltip, but easy to parse :(
PersonState::Inside(b) => Btn::text_bg1(format!(
"Currently inside {}",
map.get_b(b).just_address(map)
))
.build(ctx, format!("examine Building #{}", b.0), None),
PersonState::Trip(t) => format!("Currently doing Trip #{}", t.0).draw_text(ctx),
PersonState::OffMap => "Currently outside the map boundaries".draw_text(ctx),
PersonState::Limbo => "Currently in limbo -- they broke out of the Matrix! Woops. \
@ -860,15 +847,9 @@ fn info_for(
} else {
rows.push(ManagedWidget::row(vec![
format!("{}: ", start_time.ampm_tostring()).draw_text(ctx),
ManagedWidget::btn(Button::text_bg(
Text::from(Line(format!("Trip #{}", t.0))),
colors::SECTION_BG,
colors::HOVERING,
None,
&format!("examine Trip #{}", t.0),
ctx,
))
.margin(5),
Btn::text_bg1(format!("Trip #{}", t.0))
.build(ctx, format!("examine Trip #{}", t.0), None)
.margin(5),
]));
}
}
@ -1273,15 +1254,9 @@ fn trip_details(
col.extend(elevation);
if let Some(p) = app.primary.sim.trip_to_person(trip) {
col.push(
ManagedWidget::btn(Button::text_bg(
Text::from(Line(format!("Trip by Person #{}", p.0))),
colors::SECTION_BG,
colors::HOVERING,
None,
&format!("examine Person #{}", p.0),
ctx,
))
.margin(5),
Btn::text_bg1(format!("Trip by Person #{}", p.0))
.build(ctx, format!("examine Person #{}", p.0), None)
.margin(5),
);
}