fix some double-DPI scaling bugs with Btn::custom. this change makes

some things simpler, some more complex, but it at least fixes some
stuff without breaking anything.
This commit is contained in:
Dustin Carlino 2020-07-29 11:28:05 -07:00
parent 0e5dd551cd
commit 28c3d0ef8e
6 changed files with 30 additions and 26 deletions

View File

@ -195,6 +195,9 @@ impl GeomBatch {
/// Scales the batch by some factor.
pub fn scale(mut self, factor: f64) -> GeomBatch {
if factor == 1.0 {
return self;
}
for (_, poly) in &mut self.list {
*poly = poly.scale(factor);
}

View File

@ -230,12 +230,7 @@ impl Widget {
// TODO These are literally just convenient APIs to avoid importing JustDraw. Do we want this
// or not?
pub fn draw_batch(ctx: &EventCtx, batch: GeomBatch) -> Widget {
let scale = ctx.get_scale_factor();
if scale == 1.0 {
JustDraw::wrap(ctx, batch)
} else {
JustDraw::wrap(ctx, batch.scale(scale))
}
JustDraw::wrap(ctx, batch.scale(ctx.get_scale_factor()))
}
pub fn draw_svg<I: Into<String>>(ctx: &EventCtx, filename: I) -> Widget {
JustDraw::svg(ctx, filename.into())
@ -311,8 +306,9 @@ impl Widget {
// TODO 35 is a sad magic number. By default, Composites have padding of 16, so assuming
// this geometry is going in one of those, it makes sense to subtract 32. But that still
// caused some scrolling in a test, so snip away a few more pixels.
self.layout.style.min_size.width =
Dimension::Points((w * ctx.canvas.window_width) as f32 - 35.0);
self.layout.style.min_size.width = Dimension::Points(
(w * ctx.canvas.window_width * ctx.get_scale_factor()) as f32 - 35.0,
);
}
// Pretend we're in a Composite and basically copy recompute_layout

View File

@ -417,12 +417,12 @@ impl BtnBuilder {
}
BtnBuilder::Custom(normal, hovered, hitbox, maybe_t) => Button::new(
ctx,
normal.scale(ctx.get_scale_factor()),
hovered.scale(ctx.get_scale_factor()),
normal,
hovered,
key,
&action_tooltip.into(),
maybe_t,
hitbox.scale(ctx.get_scale_factor()),
hitbox,
),
}
}

View File

@ -459,7 +459,7 @@ fn make_timeline(
let total_duration_so_far = end_time.unwrap_or_else(|| sim.time()) - trip.departure;
let total_width = 0.22 * ctx.canvas.window_width / ctx.get_scale_factor();
let total_width = 0.22 * ctx.canvas.window_width;
let mut timeline = Vec::new();
let num_phases = phases.len();
let mut elevation = Vec::new();
@ -496,8 +496,12 @@ fn make_timeline(
(100.0 * percent_duration) as usize
)));
// TODO We're manually mixing screenspace_svg, our own geometry, and a centered_on(). Be
// careful about the scale factor. I'm confused about some of the math here, figured it out
// by trial and error.
let scale = ctx.get_scale_factor();
let phase_width = total_width * percent_duration;
let rect = Polygon::rectangle(phase_width, 15.0);
let rect = Polygon::rectangle(phase_width * scale, 15.0 * scale);
let mut normal = GeomBatch::from(vec![(color, rect.clone())]);
if idx == num_phases - 1 {
if let Some(p) = progress_along_path {
@ -506,7 +510,7 @@ fn make_timeline(
ctx.prerender,
"system/assets/timeline/current_pos.svg",
)
.centered_on(Pt2D::new(p * phase_width, 7.5 * ctx.get_scale_factor())),
.centered_on(Pt2D::new(p * phase_width * scale, 7.5 * scale)),
);
}
}
@ -530,7 +534,7 @@ fn make_timeline(
)
.centered_on(
// TODO Hardcoded layouting...
Pt2D::new(0.5 * phase_width, -20.0 * ctx.get_scale_factor()),
Pt2D::new(0.5 * phase_width * scale, -20.0),
),
);

View File

@ -331,6 +331,8 @@ pub fn make_signal_diagram(
ctx,
GeomBatch::from(vec![(
Color::WHITE,
// TODO draw_batch will scale up, but that's inappropriate here, since we're
// depending on window width, which already factors in scale
Polygon::rectangle(0.2 * ctx.canvas.window_width / ctx.get_scale_factor(), 2.0),
)]),
)

View File

@ -448,14 +448,11 @@ pub fn make_table(
rows: Vec<(String, Vec<GeomBatch>)>,
total_width: f64,
) -> Widget {
let total_width = total_width / ctx.get_scale_factor();
let mut width_per_col: Vec<f64> = headers
.iter()
.map(|w| w.get_width_for_forcing() / ctx.get_scale_factor())
.collect();
let total_width = total_width;
let mut width_per_col: Vec<f64> = headers.iter().map(|w| w.get_width_for_forcing()).collect();
for (_, row) in &rows {
for (col, width) in row.iter().zip(width_per_col.iter_mut()) {
*width = width.max(col.get_dims().width / ctx.get_scale_factor());
*width = width.max(col.get_dims().width);
}
}
let extra_margin = ((total_width - width_per_col.clone().into_iter().sum::<f64>())
@ -467,24 +464,26 @@ pub fn make_table(
.into_iter()
.enumerate()
.map(|(idx, w)| {
let margin = extra_margin + width_per_col[idx]
- (w.get_width_for_forcing() / ctx.get_scale_factor());
let margin = extra_margin + width_per_col[idx] - w.get_width_for_forcing();
// TODO margin_right scales up, so we have to cancel that out. Otherwise here we're
// already working in physical pixels. Sigh.
if idx == width_per_col.len() - 1 {
w.margin_right((margin - extra_margin) as usize)
w.margin_right(((margin - extra_margin) / ctx.get_scale_factor()) as usize)
} else {
w.margin_right(margin as usize)
w.margin_right((margin / ctx.get_scale_factor()) as usize)
}
})
.collect(),
)
.bg(app.cs.section_bg)];
// TODO Maybe can do this now simpler with to_geom
for (label, row) in rows {
let mut batch = GeomBatch::new();
batch.autocrop_dims = false;
let mut x1 = 0.0;
for (col, width) in row.into_iter().zip(width_per_col.iter()) {
batch.append(col.scale(1.0 / ctx.get_scale_factor()).translate(x1, 0.0));
batch.append(col.translate(x1, 0.0));
x1 += *width + extra_margin;
}