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. /// Scales the batch by some factor.
pub fn scale(mut self, factor: f64) -> GeomBatch { pub fn scale(mut self, factor: f64) -> GeomBatch {
if factor == 1.0 {
return self;
}
for (_, poly) in &mut self.list { for (_, poly) in &mut self.list {
*poly = poly.scale(factor); *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 // TODO These are literally just convenient APIs to avoid importing JustDraw. Do we want this
// or not? // or not?
pub fn draw_batch(ctx: &EventCtx, batch: GeomBatch) -> Widget { pub fn draw_batch(ctx: &EventCtx, batch: GeomBatch) -> Widget {
let scale = ctx.get_scale_factor(); JustDraw::wrap(ctx, batch.scale(ctx.get_scale_factor()))
if scale == 1.0 {
JustDraw::wrap(ctx, batch)
} else {
JustDraw::wrap(ctx, batch.scale(scale))
}
} }
pub fn draw_svg<I: Into<String>>(ctx: &EventCtx, filename: I) -> Widget { pub fn draw_svg<I: Into<String>>(ctx: &EventCtx, filename: I) -> Widget {
JustDraw::svg(ctx, filename.into()) 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 // 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 // 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. // caused some scrolling in a test, so snip away a few more pixels.
self.layout.style.min_size.width = self.layout.style.min_size.width = Dimension::Points(
Dimension::Points((w * ctx.canvas.window_width) as f32 - 35.0); (w * ctx.canvas.window_width * ctx.get_scale_factor()) as f32 - 35.0,
);
} }
// Pretend we're in a Composite and basically copy recompute_layout // 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( BtnBuilder::Custom(normal, hovered, hitbox, maybe_t) => Button::new(
ctx, ctx,
normal.scale(ctx.get_scale_factor()), normal,
hovered.scale(ctx.get_scale_factor()), hovered,
key, key,
&action_tooltip.into(), &action_tooltip.into(),
maybe_t, 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_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 mut timeline = Vec::new();
let num_phases = phases.len(); let num_phases = phases.len();
let mut elevation = Vec::new(); let mut elevation = Vec::new();
@ -496,8 +496,12 @@ fn make_timeline(
(100.0 * percent_duration) as usize (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 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())]); let mut normal = GeomBatch::from(vec![(color, rect.clone())]);
if idx == num_phases - 1 { if idx == num_phases - 1 {
if let Some(p) = progress_along_path { if let Some(p) = progress_along_path {
@ -506,7 +510,7 @@ fn make_timeline(
ctx.prerender, ctx.prerender,
"system/assets/timeline/current_pos.svg", "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( .centered_on(
// TODO Hardcoded layouting... // 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, ctx,
GeomBatch::from(vec![( GeomBatch::from(vec![(
Color::WHITE, 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), 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>)>, rows: Vec<(String, Vec<GeomBatch>)>,
total_width: f64, total_width: f64,
) -> Widget { ) -> Widget {
let total_width = total_width / ctx.get_scale_factor(); let total_width = total_width;
let mut width_per_col: Vec<f64> = headers let mut width_per_col: Vec<f64> = headers.iter().map(|w| w.get_width_for_forcing()).collect();
.iter()
.map(|w| w.get_width_for_forcing() / ctx.get_scale_factor())
.collect();
for (_, row) in &rows { for (_, row) in &rows {
for (col, width) in row.iter().zip(width_per_col.iter_mut()) { 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>()) let extra_margin = ((total_width - width_per_col.clone().into_iter().sum::<f64>())
@ -467,24 +464,26 @@ pub fn make_table(
.into_iter() .into_iter()
.enumerate() .enumerate()
.map(|(idx, w)| { .map(|(idx, w)| {
let margin = extra_margin + width_per_col[idx] let margin = extra_margin + width_per_col[idx] - w.get_width_for_forcing();
- (w.get_width_for_forcing() / ctx.get_scale_factor()); // 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 { 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 { } else {
w.margin_right(margin as usize) w.margin_right((margin / ctx.get_scale_factor()) as usize)
} }
}) })
.collect(), .collect(),
) )
.bg(app.cs.section_bg)]; .bg(app.cs.section_bg)];
// TODO Maybe can do this now simpler with to_geom
for (label, row) in rows { for (label, row) in rows {
let mut batch = GeomBatch::new(); let mut batch = GeomBatch::new();
batch.autocrop_dims = false; batch.autocrop_dims = false;
let mut x1 = 0.0; let mut x1 = 0.0;
for (col, width) in row.into_iter().zip(width_per_col.iter()) { 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; x1 += *width + extra_margin;
} }