mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-25 15:33:44 +03:00
make the scrollbar slider work! also, stop wasting github resources
This commit is contained in:
parent
21b8f41a59
commit
ebd405e2ef
2
.github/workflows/main.yml
vendored
2
.github/workflows/main.yml
vendored
@ -18,6 +18,7 @@ jobs:
|
|||||||
# path: target/release/game.exe
|
# path: target/release/game.exe
|
||||||
build-macos:
|
build-macos:
|
||||||
runs-on: macos-latest
|
runs-on: macos-latest
|
||||||
|
if: "contains(github.event.head_commit.message, '[rebuild]')"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@master
|
- uses: actions/checkout@master
|
||||||
- uses: hecrj/setup-rust-action@v1
|
- uses: hecrj/setup-rust-action@v1
|
||||||
@ -32,6 +33,7 @@ jobs:
|
|||||||
path: target/release/game
|
path: target/release/game
|
||||||
build-linux:
|
build-linux:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
if: "contains(github.event.head_commit.message, '[rebuild]')"
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@master
|
- uses: actions/checkout@master
|
||||||
- uses: hecrj/setup-rust-action@v1
|
- uses: hecrj/setup-rust-action@v1
|
||||||
|
@ -295,6 +295,7 @@ impl ManagedWidget {
|
|||||||
nodes: &mut Vec<Node>,
|
nodes: &mut Vec<Node>,
|
||||||
dx: f64,
|
dx: f64,
|
||||||
dy: f64,
|
dy: f64,
|
||||||
|
scroll_y_offset: f64,
|
||||||
ctx: &EventCtx,
|
ctx: &EventCtx,
|
||||||
) {
|
) {
|
||||||
let result = stretch.layout(nodes.pop().unwrap()).unwrap();
|
let result = stretch.layout(nodes.pop().unwrap()).unwrap();
|
||||||
@ -302,10 +303,17 @@ impl ManagedWidget {
|
|||||||
let y: f64 = result.location.y.into();
|
let y: f64 = result.location.y.into();
|
||||||
let width: f64 = result.size.width.into();
|
let width: f64 = result.size.width.into();
|
||||||
let height: f64 = result.size.height.into();
|
let height: f64 = result.size.height.into();
|
||||||
self.rect = ScreenRectangle::top_left(
|
let top_left = match self.widget {
|
||||||
ScreenPt::new(x + dx, y + dy),
|
WidgetType::Slider(ref name) => {
|
||||||
ScreenDims::new(width, height),
|
if name == "scrollbar" {
|
||||||
);
|
ScreenPt::new(x + dx, y + dy)
|
||||||
|
} else {
|
||||||
|
ScreenPt::new(x + dx, y + dy - scroll_y_offset)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => ScreenPt::new(x + dx, y + dy - scroll_y_offset),
|
||||||
|
};
|
||||||
|
self.rect = ScreenRectangle::top_left(top_left, ScreenDims::new(width, height));
|
||||||
if let Some(color) = self.style.bg_color {
|
if let Some(color) = self.style.bg_color {
|
||||||
// Assume widgets don't dynamically change, so we just upload the background once.
|
// Assume widgets don't dynamically change, so we just upload the background once.
|
||||||
if self.bg.is_none() {
|
if self.bg.is_none() {
|
||||||
@ -323,32 +331,45 @@ impl ManagedWidget {
|
|||||||
|
|
||||||
match self.widget {
|
match self.widget {
|
||||||
WidgetType::Draw(ref mut widget) => {
|
WidgetType::Draw(ref mut widget) => {
|
||||||
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
widget.set_pos(top_left);
|
||||||
}
|
}
|
||||||
WidgetType::Btn(ref mut widget) => {
|
WidgetType::Btn(ref mut widget) => {
|
||||||
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
widget.set_pos(top_left);
|
||||||
}
|
}
|
||||||
WidgetType::Slider(ref name) => {
|
WidgetType::Slider(ref name) => {
|
||||||
sliders
|
sliders.get_mut(name).unwrap().set_pos(top_left);
|
||||||
.get_mut(name)
|
|
||||||
.unwrap()
|
|
||||||
.set_pos(ScreenPt::new(x + dx, y + dy));
|
|
||||||
}
|
}
|
||||||
WidgetType::DurationPlot(ref mut widget) => {
|
WidgetType::DurationPlot(ref mut widget) => {
|
||||||
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
widget.set_pos(top_left);
|
||||||
}
|
}
|
||||||
WidgetType::UsizePlot(ref mut widget) => {
|
WidgetType::UsizePlot(ref mut widget) => {
|
||||||
widget.set_pos(ScreenPt::new(x + dx, y + dy));
|
widget.set_pos(top_left);
|
||||||
}
|
}
|
||||||
WidgetType::Row(ref mut widgets) => {
|
WidgetType::Row(ref mut widgets) => {
|
||||||
// layout() doesn't return absolute position; it's relative to the container.
|
// layout() doesn't return absolute position; it's relative to the container.
|
||||||
for widget in widgets {
|
for widget in widgets {
|
||||||
widget.apply_flexbox(sliders, stretch, nodes, x + dx, y + dy, ctx);
|
widget.apply_flexbox(
|
||||||
|
sliders,
|
||||||
|
stretch,
|
||||||
|
nodes,
|
||||||
|
x + dx,
|
||||||
|
y + dy,
|
||||||
|
scroll_y_offset,
|
||||||
|
ctx,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
WidgetType::Column(ref mut widgets) => {
|
WidgetType::Column(ref mut widgets) => {
|
||||||
for widget in widgets {
|
for widget in widgets {
|
||||||
widget.apply_flexbox(sliders, stretch, nodes, x + dx, y + dy, ctx);
|
widget.apply_flexbox(
|
||||||
|
sliders,
|
||||||
|
stretch,
|
||||||
|
nodes,
|
||||||
|
x + dx,
|
||||||
|
y + dy,
|
||||||
|
scroll_y_offset,
|
||||||
|
ctx,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -388,7 +409,6 @@ pub struct Composite {
|
|||||||
// small part of the screen.
|
// small part of the screen.
|
||||||
// TODO Horizontal scrolling?
|
// TODO Horizontal scrolling?
|
||||||
scrollable: bool,
|
scrollable: bool,
|
||||||
scroll_y_offset: f64,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Outcome {
|
pub enum Outcome {
|
||||||
@ -412,7 +432,6 @@ impl Composite {
|
|||||||
sliders: HashMap::new(),
|
sliders: HashMap::new(),
|
||||||
|
|
||||||
scrollable: false,
|
scrollable: false,
|
||||||
scroll_y_offset: 0.0,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -509,17 +528,40 @@ impl Composite {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
let offset = self.scroll_y_offset(ctx);
|
||||||
self.top_level.apply_flexbox(
|
self.top_level.apply_flexbox(
|
||||||
&mut self.sliders,
|
&mut self.sliders,
|
||||||
&stretch,
|
&stretch,
|
||||||
&mut nodes,
|
&mut nodes,
|
||||||
top_left.x,
|
top_left.x,
|
||||||
top_left.y - self.scroll_y_offset,
|
top_left.y,
|
||||||
|
offset,
|
||||||
ctx,
|
ctx,
|
||||||
);
|
);
|
||||||
assert!(nodes.is_empty());
|
assert!(nodes.is_empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn scroll_y_offset(&self, ctx: &EventCtx) -> f64 {
|
||||||
|
if self.scrollable {
|
||||||
|
self.slider("scrollbar").get_percent()
|
||||||
|
* (self.top_level.rect.height() - ctx.canvas.window_height).max(0.0)
|
||||||
|
} else {
|
||||||
|
0.0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_scroll_y_offset(&mut self, ctx: &EventCtx, offset: f64) {
|
||||||
|
assert!(self.scrollable);
|
||||||
|
let max = (self.top_level.rect.height() - ctx.canvas.window_height).max(0.0);
|
||||||
|
if max == 0.0 {
|
||||||
|
assert_eq!(offset, 0.0);
|
||||||
|
self.mut_slider("scrollbar").set_percent(ctx, 0.0);
|
||||||
|
} else {
|
||||||
|
self.mut_slider("scrollbar").set_percent(ctx, offset / max);
|
||||||
|
}
|
||||||
|
self.recompute_layout(ctx);
|
||||||
|
}
|
||||||
|
|
||||||
pub fn event(&mut self, ctx: &mut EventCtx) -> Option<Outcome> {
|
pub fn event(&mut self, ctx: &mut EventCtx) -> Option<Outcome> {
|
||||||
if self.scrollable
|
if self.scrollable
|
||||||
&& self
|
&& self
|
||||||
@ -528,10 +570,10 @@ impl Composite {
|
|||||||
.contains(ctx.canvas.get_cursor_in_screen_space())
|
.contains(ctx.canvas.get_cursor_in_screen_space())
|
||||||
{
|
{
|
||||||
if let Some(scroll) = ctx.input.get_mouse_scroll() {
|
if let Some(scroll) = ctx.input.get_mouse_scroll() {
|
||||||
self.scroll_y_offset -= scroll * SCROLL_SPEED;
|
let offset = self.scroll_y_offset(ctx) - scroll * SCROLL_SPEED;
|
||||||
let max = (self.top_level.rect.height() - ctx.canvas.window_height).max(0.0);
|
let max = (self.top_level.rect.height() - ctx.canvas.window_height).max(0.0);
|
||||||
self.scroll_y_offset = abstutil::clamp(self.scroll_y_offset, 0.0, max);
|
// TODO Do the clamping in there instead
|
||||||
self.recompute_layout(ctx);
|
self.set_scroll_y_offset(ctx, abstutil::clamp(offset, 0.0, max));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -539,7 +581,12 @@ impl Composite {
|
|||||||
self.recompute_layout(ctx);
|
self.recompute_layout(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
self.top_level.event(ctx, &mut self.sliders)
|
let before = self.scroll_y_offset(ctx);
|
||||||
|
let result = self.top_level.event(ctx, &mut self.sliders);
|
||||||
|
if self.scroll_y_offset(ctx) != before {
|
||||||
|
self.recompute_layout(ctx);
|
||||||
|
}
|
||||||
|
result
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw(&self, g: &mut GfxCtx) {
|
pub fn draw(&self, g: &mut GfxCtx) {
|
||||||
@ -553,16 +600,14 @@ impl Composite {
|
|||||||
actions
|
actions
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn preserve_scroll(&self) -> f64 {
|
pub fn preserve_scroll(&self, ctx: &EventCtx) -> f64 {
|
||||||
assert!(self.scrollable);
|
assert!(self.scrollable);
|
||||||
self.scroll_y_offset
|
self.scroll_y_offset(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn restore_scroll(&mut self, ctx: &EventCtx, offset: f64) {
|
pub fn restore_scroll(&mut self, ctx: &EventCtx, offset: f64) {
|
||||||
assert!(self.scrollable);
|
assert!(self.scrollable);
|
||||||
self.scroll_y_offset = offset;
|
self.set_scroll_y_offset(ctx, offset);
|
||||||
// TODO Update the slider!
|
|
||||||
self.recompute_layout(ctx);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn slider(&self, name: &str) -> &Slider {
|
pub fn slider(&self, name: &str) -> &Slider {
|
||||||
|
@ -174,7 +174,7 @@ impl TrafficSignalDiagram {
|
|||||||
|
|
||||||
fn change_phase(&mut self, idx: usize, ui: &UI, ctx: &EventCtx) {
|
fn change_phase(&mut self, idx: usize, ui: &UI, ctx: &EventCtx) {
|
||||||
if self.current_phase != idx {
|
if self.current_phase != idx {
|
||||||
let preserve_scroll = self.composite.preserve_scroll();
|
let preserve_scroll = self.composite.preserve_scroll(ctx);
|
||||||
self.current_phase = idx;
|
self.current_phase = idx;
|
||||||
self.composite = make_diagram(self.i, self.current_phase, ui, ctx);
|
self.composite = make_diagram(self.i, self.current_phase, ui, ctx);
|
||||||
self.composite.restore_scroll(ctx, preserve_scroll);
|
self.composite.restore_scroll(ctx, preserve_scroll);
|
||||||
|
Loading…
Reference in New Issue
Block a user