mirror of
https://github.com/a-b-street/abstreet.git
synced 2024-12-26 16:02:23 +03:00
implementing GfxCtx forking... aka, reset uniforms
This commit is contained in:
parent
d9d89e93dd
commit
9b14206be0
@ -48,6 +48,7 @@ impl Plugin for Legend {
|
||||
g.fork(
|
||||
Pt2D::new(-self.top_left.x / zoom, -self.top_left.y / zoom),
|
||||
zoom,
|
||||
&ctx.canvas,
|
||||
);
|
||||
|
||||
// Create a fake turn.
|
||||
@ -92,6 +93,6 @@ impl Plugin for Legend {
|
||||
ScreenPt::new(self.top_left.x + 20.0, self.top_left.y + 110.0),
|
||||
);
|
||||
|
||||
g.unfork();
|
||||
g.unfork(&ctx.canvas);
|
||||
}
|
||||
}
|
||||
|
@ -328,7 +328,7 @@ pub fn draw_signal_diagram(
|
||||
let total_screen_width = (intersection_width * zoom) + label_length + 10.0;
|
||||
let x1_screen = ctx.canvas.window_width - total_screen_width;
|
||||
|
||||
g.fork_screenspace();
|
||||
g.fork_screenspace(&ctx.canvas);
|
||||
g.draw_polygon(
|
||||
ctx.cs
|
||||
.get_def("signal editor panel", Color::BLACK.alpha(0.95)),
|
||||
@ -364,6 +364,7 @@ pub fn draw_signal_diagram(
|
||||
- padding * ((idx as f64) + 1.0),
|
||||
),
|
||||
zoom,
|
||||
&ctx.canvas,
|
||||
);
|
||||
draw_signal_cycle(&cycle, g, ctx);
|
||||
|
||||
@ -377,7 +378,7 @@ pub fn draw_signal_diagram(
|
||||
);
|
||||
}
|
||||
|
||||
g.unfork();
|
||||
g.unfork(&ctx.canvas);
|
||||
}
|
||||
|
||||
fn find_pts_between(pts: &Vec<Pt2D>, start: Pt2D, end: Pt2D) -> Option<Vec<Pt2D>> {
|
||||
|
@ -99,7 +99,13 @@ impl Canvas {
|
||||
let x1 = self.cursor_x - (width / 2.0);
|
||||
let y1 = self.cursor_y - (height / 2.0);
|
||||
// No need to cover the tooltip; this tooltip follows the mouse anyway.
|
||||
text::draw_text_bubble(g, glyphs.as_mut().unwrap(), ScreenPt::new(x1, y1), txt);
|
||||
text::draw_text_bubble(
|
||||
g,
|
||||
glyphs.as_mut().unwrap(),
|
||||
ScreenPt::new(x1, y1),
|
||||
txt,
|
||||
self,
|
||||
);
|
||||
}
|
||||
|
||||
// TODO Rename these draw_nonblocking_text_*
|
||||
@ -112,6 +118,7 @@ impl Canvas {
|
||||
glyphs.as_mut().unwrap(),
|
||||
ScreenPt::new(pt.x - (width / 2.0), pt.y - (height / 2.0)),
|
||||
txt,
|
||||
self,
|
||||
);
|
||||
}
|
||||
|
||||
@ -121,11 +128,12 @@ impl Canvas {
|
||||
self.glyphs.borrow_mut().as_mut().unwrap(),
|
||||
self.map_to_screen(pt),
|
||||
txt,
|
||||
self,
|
||||
);
|
||||
}
|
||||
|
||||
pub fn draw_text_at_screenspace_topleft(&self, g: &mut GfxCtx, txt: Text, pt: ScreenPt) {
|
||||
text::draw_text_bubble(g, self.glyphs.borrow_mut().as_mut().unwrap(), pt, txt);
|
||||
text::draw_text_bubble(g, self.glyphs.borrow_mut().as_mut().unwrap(), pt, txt, self);
|
||||
}
|
||||
|
||||
// The text box covers up what's beneath and eats the cursor (for get_cursor_in_map_space).
|
||||
@ -156,6 +164,7 @@ impl Canvas {
|
||||
glyphs.as_mut().unwrap(),
|
||||
ScreenPt::new(x1, y1),
|
||||
txt,
|
||||
self,
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -134,16 +134,22 @@ impl<'a> GfxCtx<'a> {
|
||||
// Up to the caller to call unfork()!
|
||||
// TODO Canvas doesn't understand this change, so things like text drawing that use
|
||||
// map_to_screen will just be confusing.
|
||||
pub fn fork(&mut self, top_left: Pt2D, zoom: f64) {
|
||||
// TODO set uniforms based on values above
|
||||
pub fn fork(&mut self, top_left: Pt2D, zoom: f64, canvas: &Canvas) {
|
||||
self.uniforms = uniform! {
|
||||
transform: [top_left.x() as f32, top_left.y() as f32, zoom as f32],
|
||||
window: [canvas.window_width as f32, canvas.window_height as f32],
|
||||
};
|
||||
}
|
||||
|
||||
pub fn fork_screenspace(&mut self) {
|
||||
self.fork(Pt2D::new(0.0, 0.0), 1.0)
|
||||
pub fn fork_screenspace(&mut self, canvas: &Canvas) {
|
||||
self.fork(Pt2D::new(0.0, 0.0), 1.0, canvas)
|
||||
}
|
||||
|
||||
pub fn unfork(&mut self) {
|
||||
// TODO Reset to canvas?
|
||||
pub fn unfork(&mut self, canvas: &Canvas) {
|
||||
self.uniforms = uniform! {
|
||||
transform: [canvas.cam_x as f32, canvas.cam_y as f32, canvas.cam_zoom as f32],
|
||||
window: [canvas.window_width as f32, canvas.window_height as f32],
|
||||
};
|
||||
}
|
||||
|
||||
pub fn clear(&mut self, color: Color) {
|
||||
|
@ -133,15 +133,19 @@ impl Text {
|
||||
so_far.push_str(&span.text);
|
||||
so_far
|
||||
});
|
||||
let rect = glyphs
|
||||
.pixel_bounds(Section {
|
||||
text: &full_line,
|
||||
scale: Scale::uniform(FONT_SIZE),
|
||||
..Section::default()
|
||||
})
|
||||
.unwrap();
|
||||
widths.push(rect.width());
|
||||
heights.push(rect.height());
|
||||
if let Some(rect) = glyphs.pixel_bounds(Section {
|
||||
text: &full_line,
|
||||
scale: Scale::uniform(FONT_SIZE),
|
||||
..Section::default()
|
||||
}) {
|
||||
widths.push(rect.width());
|
||||
heights.push(rect.height());
|
||||
} else {
|
||||
// TODO Sometimes we want to space something like " ", but no drawn glyphs
|
||||
// means pixel_bounds fails. Hack?
|
||||
widths.push((MAX_CHAR_WIDTH * (full_line.len() as f64)) as i32);
|
||||
heights.push(LINE_HEIGHT as i32);
|
||||
}
|
||||
}
|
||||
|
||||
(
|
||||
@ -156,9 +160,10 @@ pub fn draw_text_bubble(
|
||||
glyphs: &mut GlyphBrush<'static, 'static>,
|
||||
top_left: ScreenPt,
|
||||
txt: Text,
|
||||
canvas: &Canvas,
|
||||
) -> ScreenRectangle {
|
||||
// TODO Is it expensive to constantly change uniforms and the shader program?
|
||||
g.fork_screenspace();
|
||||
g.fork_screenspace(canvas);
|
||||
|
||||
let (total_width, total_height) = txt.dims(glyphs);
|
||||
if let Some(c) = txt.bg_color {
|
||||
@ -212,7 +217,7 @@ pub fn draw_text_bubble(
|
||||
y += LINE_HEIGHT;
|
||||
}
|
||||
|
||||
g.unfork();
|
||||
g.unfork(canvas);
|
||||
|
||||
ScreenRectangle {
|
||||
x1: top_left.x,
|
||||
|
@ -135,7 +135,7 @@ impl TopMenu {
|
||||
y2: TOP_MENU_HEIGHT,
|
||||
});
|
||||
|
||||
g.fork_screenspace();
|
||||
g.fork_screenspace(canvas);
|
||||
g.draw_polygon(
|
||||
text::BG_COLOR,
|
||||
&Polygon::rectangle_topleft(Pt2D::new(0.0, 0.0), canvas.window_width, TOP_MENU_HEIGHT),
|
||||
@ -148,7 +148,7 @@ impl TopMenu {
|
||||
&Polygon::rectangle_topleft(Pt2D::new(r.x1, r.y1), r.x2 - r.x1, r.y2 - r.y1),
|
||||
);
|
||||
}
|
||||
g.unfork();
|
||||
g.unfork(canvas);
|
||||
|
||||
canvas.draw_text_at_screenspace_topleft(g, self.txt.clone(), ScreenPt::new(0.0, 0.0));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user