mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-11-28 20:29:04 +03:00
revamp time warp screen
This commit is contained in:
parent
74a95093bd
commit
1268890b75
@ -429,6 +429,7 @@ impl ManagedWidget {
|
||||
dy: f64,
|
||||
scroll_offset: (f64, f64),
|
||||
ctx: &EventCtx,
|
||||
recompute_layout: bool,
|
||||
) {
|
||||
let result = stretch.layout(nodes.pop().unwrap()).unwrap();
|
||||
let x: f64 = result.location.x.into();
|
||||
@ -449,7 +450,9 @@ impl ManagedWidget {
|
||||
self.rect = ScreenRectangle::top_left(top_left, ScreenDims::new(width, height));
|
||||
|
||||
// Assume widgets don't dynamically change, so we just upload the background once.
|
||||
if self.bg.is_none() && (self.style.bg_color.is_some() || self.style.outline.is_some()) {
|
||||
if (self.bg.is_none() || recompute_layout)
|
||||
&& (self.style.bg_color.is_some() || self.style.outline.is_some())
|
||||
{
|
||||
let mut batch = GeomBatch::new();
|
||||
if let Some(c) = self.style.bg_color {
|
||||
batch.push(c, Polygon::rounded_rectangle(width, height, 5.0));
|
||||
@ -502,6 +505,7 @@ impl ManagedWidget {
|
||||
y + dy,
|
||||
scroll_offset,
|
||||
ctx,
|
||||
recompute_layout,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -517,6 +521,7 @@ impl ManagedWidget {
|
||||
y + dy,
|
||||
scroll_offset,
|
||||
ctx,
|
||||
recompute_layout,
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -657,7 +662,7 @@ impl Composite {
|
||||
}
|
||||
}
|
||||
|
||||
fn recompute_layout(&mut self, ctx: &EventCtx) {
|
||||
fn recompute_layout(&mut self, ctx: &EventCtx, recompute_bg: bool) {
|
||||
let mut stretch = Stretch::new();
|
||||
let root = stretch
|
||||
.new_node(
|
||||
@ -707,6 +712,7 @@ impl Composite {
|
||||
top_left.y,
|
||||
offset,
|
||||
ctx,
|
||||
recompute_bg,
|
||||
);
|
||||
assert!(nodes.is_empty());
|
||||
}
|
||||
@ -750,7 +756,7 @@ impl Composite {
|
||||
}
|
||||
}
|
||||
if changed {
|
||||
self.recompute_layout(ctx);
|
||||
self.recompute_layout(ctx, false);
|
||||
}
|
||||
}
|
||||
|
||||
@ -778,7 +784,7 @@ impl Composite {
|
||||
}
|
||||
|
||||
if ctx.input.is_window_resized() {
|
||||
self.recompute_layout(ctx);
|
||||
self.recompute_layout(ctx, false);
|
||||
}
|
||||
|
||||
let before = self.scroll_offset();
|
||||
@ -786,7 +792,7 @@ impl Composite {
|
||||
.top_level
|
||||
.event(ctx, &mut self.sliders, &mut self.menus);
|
||||
if self.scroll_offset() != before {
|
||||
self.recompute_layout(ctx);
|
||||
self.recompute_layout(ctx, false);
|
||||
}
|
||||
result
|
||||
}
|
||||
@ -888,7 +894,7 @@ impl Composite {
|
||||
pub fn align_above(&mut self, ctx: &mut EventCtx, other: &Composite) {
|
||||
// Small padding
|
||||
self.vert = VerticalAlignment::Above(other.top_level.rect.y1 - 5.0);
|
||||
self.recompute_layout(ctx);
|
||||
self.recompute_layout(ctx, false);
|
||||
}
|
||||
|
||||
pub fn replace(&mut self, ctx: &mut EventCtx, id: &str, new: ManagedWidget) {
|
||||
@ -896,7 +902,7 @@ impl Composite {
|
||||
panic!("Can't replace widget {}", id);
|
||||
}
|
||||
|
||||
self.recompute_layout(ctx);
|
||||
self.recompute_layout(ctx, true);
|
||||
}
|
||||
}
|
||||
|
||||
@ -924,7 +930,7 @@ impl CompositeBuilder {
|
||||
height: Dimension::Points((h * ctx.canvas.window_height) as f32),
|
||||
});
|
||||
}
|
||||
c.recompute_layout(ctx);
|
||||
c.recompute_layout(ctx, false);
|
||||
|
||||
c.contents_dims = ScreenDims::new(c.top_level.rect.width(), c.top_level.rect.height());
|
||||
c.container_dims = match c.dims {
|
||||
@ -972,7 +978,7 @@ impl CompositeBuilder {
|
||||
]);
|
||||
}
|
||||
if c.scrollable_x || c.scrollable_y {
|
||||
c.recompute_layout(ctx);
|
||||
c.recompute_layout(ctx, false);
|
||||
c.clip_rect = Some(ScreenRectangle::top_left(top_left, c.container_dims));
|
||||
}
|
||||
|
||||
|
@ -162,12 +162,10 @@ impl SpeedControls {
|
||||
.cb(
|
||||
"step forwards 1 hour",
|
||||
Box::new(|ctx, ui| {
|
||||
Some(Transition::Push(Box::new(TimeWarpScreen {
|
||||
target: ui.primary.sim.time() + Duration::hours(1),
|
||||
started: Instant::now(),
|
||||
composite: Composite::new(ManagedWidget::draw_text(ctx, Text::new()))
|
||||
.build(ctx),
|
||||
})))
|
||||
Some(Transition::Push(Box::new(TimeWarpScreen::new(
|
||||
ctx,
|
||||
ui.primary.sim.time() + Duration::hours(1),
|
||||
))))
|
||||
}),
|
||||
)
|
||||
}
|
||||
@ -416,15 +414,7 @@ impl State for JumpToTime {
|
||||
ui.primary.clear_sim();
|
||||
return Transition::ReplaceThenPush(
|
||||
Box::new(SandboxMode::new(ctx, ui, mode)),
|
||||
Box::new(TimeWarpScreen {
|
||||
target: self.target,
|
||||
started: Instant::now(),
|
||||
composite: Composite::new(ManagedWidget::draw_text(
|
||||
ctx,
|
||||
Text::new(),
|
||||
))
|
||||
.build(ctx),
|
||||
}),
|
||||
Box::new(TimeWarpScreen::new(ctx, self.target)),
|
||||
);
|
||||
} else {
|
||||
return Transition::Replace(msg(
|
||||
@ -433,12 +423,7 @@ impl State for JumpToTime {
|
||||
));
|
||||
}
|
||||
}
|
||||
return Transition::Replace(Box::new(TimeWarpScreen {
|
||||
target: self.target,
|
||||
started: Instant::now(),
|
||||
composite: Composite::new(ManagedWidget::draw_text(ctx, Text::new()))
|
||||
.build(ctx),
|
||||
}));
|
||||
return Transition::Replace(Box::new(TimeWarpScreen::new(ctx, self.target)));
|
||||
}
|
||||
_ => unreachable!(),
|
||||
},
|
||||
@ -480,11 +465,26 @@ pub struct TimeWarpScreen {
|
||||
composite: Composite,
|
||||
}
|
||||
|
||||
impl TimeWarpScreen {
|
||||
fn new(ctx: &mut EventCtx, target: Time) -> TimeWarpScreen {
|
||||
TimeWarpScreen {
|
||||
target,
|
||||
started: Instant::now(),
|
||||
composite: Composite::new(
|
||||
ManagedWidget::col(vec![
|
||||
ManagedWidget::draw_text(ctx, Text::new()).named("text"),
|
||||
WrappedComposite::text_bg_button(ctx, "stop now", hotkey(Key::Escape))
|
||||
.centered_horiz(),
|
||||
])
|
||||
.bg(colors::PANEL_BG),
|
||||
)
|
||||
.build(ctx),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl State for TimeWarpScreen {
|
||||
fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition {
|
||||
if ctx.input.new_was_pressed(hotkey(Key::Escape).unwrap()) {
|
||||
return Transition::Pop;
|
||||
}
|
||||
if ctx.input.nonblocking_is_update_event().is_some() {
|
||||
ctx.input.use_update_event();
|
||||
ui.primary.sim.time_limited_step(
|
||||
@ -494,8 +494,6 @@ impl State for TimeWarpScreen {
|
||||
);
|
||||
// TODO secondary for a/b test mode
|
||||
|
||||
// TODO Instead display base speed controls, some indication of target time and ability
|
||||
// to cancel
|
||||
let mut txt = Text::from(Line("Warping through time...").roboto_bold());
|
||||
txt.add(Line(format!(
|
||||
"Simulating until it's {}",
|
||||
@ -509,21 +507,27 @@ impl State for TimeWarpScreen {
|
||||
"Have been simulating for {}",
|
||||
Duration::realtime_elapsed(self.started)
|
||||
)));
|
||||
txt.add(Line(""));
|
||||
txt.add(Line(format!("Press ESCAPE to stop now")));
|
||||
|
||||
self.composite = Composite::new(
|
||||
ManagedWidget::draw_text(ctx, txt)
|
||||
.padding(10)
|
||||
.bg(colors::PANEL_BG)
|
||||
.outline(5.0, Color::WHITE),
|
||||
)
|
||||
.build(ctx);
|
||||
self.composite.replace(
|
||||
ctx,
|
||||
"text",
|
||||
ManagedWidget::draw_text(ctx, txt).named("text"),
|
||||
);
|
||||
}
|
||||
if ui.primary.sim.time() == self.target {
|
||||
return Transition::Pop;
|
||||
}
|
||||
|
||||
match self.composite.event(ctx) {
|
||||
Some(Outcome::Clicked(x)) => match x.as_ref() {
|
||||
"stop now" => {
|
||||
return Transition::Pop;
|
||||
}
|
||||
_ => unreachable!(),
|
||||
},
|
||||
None => {}
|
||||
}
|
||||
|
||||
Transition::KeepWithMode(EventLoopMode::Animation)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user