From bef4b17d7f5c20b55a48bf847442a1261c1b299d Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sun, 21 Jan 2024 22:00:43 +0100 Subject: fix font clipping --- src/ui/graph.rs | 83 ++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 52 insertions(+), 31 deletions(-) diff --git a/src/ui/graph.rs b/src/ui/graph.rs index 198c020..725c0dc 100644 --- a/src/ui/graph.rs +++ b/src/ui/graph.rs @@ -85,8 +85,14 @@ impl GraphState { self.map_to_step((frame.height() / self.scale) + 2.0 * step as f32, step), ); let p = Point::new( - self.map_to_step(-self.center.x / self.scale - s.width / 2.0 - step as f32, step), - self.map_to_step(-self.center.y / self.scale - s.height / 2.0 - step as f32, step), + self.map_to_step( + -self.center.x / self.scale - s.width / 2.0 - step as f32, + step, + ), + self.map_to_step( + -self.center.y / self.scale - s.height / 2.0 - step as f32, + step, + ), ); Rectangle::new(p, s) } @@ -192,35 +198,50 @@ impl canvas::Program for GraphCanvas { ); frame.with_save(|frame| { - for x in (rect.x as i64 + 2 * step as i64..x_end).step_by(step) { - let p = Point::new(x as f32, 0.0); - frame.fill(&Path::circle(p, 10.0 / state.scale), zcolor); - frame.fill_text(Text { - content: x.to_string(), - position: p, - horizontal_alignment: iced::alignment::Horizontal::Center, - vertical_alignment: iced::alignment::Vertical::Center, - color: Color::WHITE, - ..Text::default() - }); - } - - for y in (rect.y as i64..y_end).step_by(step) { - if y == 0 { - continue; - } + frame.with_clip( + Rectangle { + x: 0.0, + y: 0.0, + width: frame.width(), + height: frame.height(), + }, + |frame| { + frame.translate(Vector { + x: frame.center().x + state.center.x, + y: frame.center().y + state.center.y, + }); + frame.scale(state.scale); + for x in (rect.x as i64..x_end).step_by(step) { + let p = Point::new(x as f32, 0.0); + frame.fill(&Path::circle(p, 10.0 / state.scale), zcolor); + frame.fill_text(Text { + content: x.to_string(), + position: p, + horizontal_alignment: iced::alignment::Horizontal::Center, + vertical_alignment: iced::alignment::Vertical::Center, + color: Color::WHITE, + ..Text::default() + }); + } - let p = Point::new(0.0, y as f32); - frame.fill(&Path::circle(p, 10.0 / state.scale), zcolor); - frame.fill_text(Text { - content: (-y).to_string(), - position: p, - horizontal_alignment: iced::alignment::Horizontal::Center, - vertical_alignment: iced::alignment::Vertical::Center, - color: Color::WHITE, - ..Text::default() - }); - } + for y in (rect.y as i64..y_end).step_by(step) { + if y == 0 { + continue; + } + + let p = Point::new(0.0, y as f32); + frame.fill(&Path::circle(p, 10.0 / state.scale), zcolor); + frame.fill_text(Text { + content: (-y).to_string(), + position: p, + horizontal_alignment: iced::alignment::Horizontal::Center, + vertical_alignment: iced::alignment::Vertical::Center, + color: Color::WHITE, + ..Text::default() + }); + } + }, + ) }); let step = (step / 10).max(1); @@ -294,7 +315,7 @@ impl canvas::Program for GraphCanvas { } else { state.scale *= 1.1; } - state.scale = state.scale.max(0.000001); + state.scale = state.scale.max(0.0000000001); return (event::Status::Captured, Some(Message::RefreshGraphCanvas)); } _ => {} -- cgit v1.2.3-70-g09d2