From 9b14206be01b56b3f19f55ae3d16fa55f95e753b Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 23 Jan 2019 12:13:29 -0800 Subject: [PATCH] implementing GfxCtx forking... aka, reset uniforms --- editor/src/plugins/view/legend.rs | 3 ++- editor/src/render/intersection.rs | 5 +++-- ezgui/src/canvas.rs | 13 +++++++++++-- ezgui/src/lib.rs | 18 ++++++++++++------ ezgui/src/text.rs | 27 ++++++++++++++++----------- ezgui/src/top_menu.rs | 4 ++-- 6 files changed, 46 insertions(+), 24 deletions(-) diff --git a/editor/src/plugins/view/legend.rs b/editor/src/plugins/view/legend.rs index bdbfbca5a3..34e389dcd7 100644 --- a/editor/src/plugins/view/legend.rs +++ b/editor/src/plugins/view/legend.rs @@ -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); } } diff --git a/editor/src/render/intersection.rs b/editor/src/render/intersection.rs index 457c97b683..f122c18809 100644 --- a/editor/src/render/intersection.rs +++ b/editor/src/render/intersection.rs @@ -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, start: Pt2D, end: Pt2D) -> Option> { diff --git a/ezgui/src/canvas.rs b/ezgui/src/canvas.rs index 69a8637bc8..cc5a101700 100644 --- a/ezgui/src/canvas.rs +++ b/ezgui/src/canvas.rs @@ -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, )); } diff --git a/ezgui/src/lib.rs b/ezgui/src/lib.rs index ce2a0efd90..5e67a45286 100644 --- a/ezgui/src/lib.rs +++ b/ezgui/src/lib.rs @@ -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) { diff --git a/ezgui/src/text.rs b/ezgui/src/text.rs index 37ca335f3f..d32df668a8 100644 --- a/ezgui/src/text.rs +++ b/ezgui/src/text.rs @@ -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, diff --git a/ezgui/src/top_menu.rs b/ezgui/src/top_menu.rs index 8c3452cf0f..952456607d 100644 --- a/ezgui/src/top_menu.rs +++ b/ezgui/src/top_menu.rs @@ -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));