Painter
extend accepts IntoIter
(#2249)
* `Painter` extend accepts `IntoIter` * Update painter.rs
This commit is contained in:
parent
e48602059d
commit
22a917c00a
4 changed files with 41 additions and 36 deletions
|
@ -130,9 +130,12 @@ impl PaintList {
|
|||
idx
|
||||
}
|
||||
|
||||
pub fn extend(&mut self, clip_rect: Rect, mut shapes: Vec<Shape>) {
|
||||
self.0
|
||||
.extend(shapes.drain(..).map(|shape| ClippedShape(clip_rect, shape)));
|
||||
pub fn extend<I: IntoIterator<Item = Shape>>(&mut self, clip_rect: Rect, shapes: I) {
|
||||
self.0.extend(
|
||||
shapes
|
||||
.into_iter()
|
||||
.map(|shape| ClippedShape(clip_rect, shape)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Modify an existing [`Shape`].
|
||||
|
|
|
@ -178,19 +178,19 @@ impl Painter {
|
|||
/// Add many shapes at once.
|
||||
///
|
||||
/// Calling this once is generally faster than calling [`Self::add`] multiple times.
|
||||
pub fn extend(&self, mut shapes: Vec<Shape>) {
|
||||
pub fn extend<I: IntoIterator<Item = Shape>>(&self, shapes: I) {
|
||||
if self.fade_to_color == Some(Color32::TRANSPARENT) {
|
||||
return;
|
||||
}
|
||||
if !shapes.is_empty() {
|
||||
if self.fade_to_color.is_some() {
|
||||
for shape in &mut shapes {
|
||||
self.transform_shape(shape);
|
||||
}
|
||||
}
|
||||
|
||||
if self.fade_to_color.is_some() {
|
||||
let shapes = shapes.into_iter().map(|mut shape| {
|
||||
self.transform_shape(&mut shape);
|
||||
shape
|
||||
});
|
||||
self.paint_list().extend(self.clip_rect, shapes);
|
||||
}
|
||||
} else {
|
||||
self.paint_list().extend(self.clip_rect, shapes);
|
||||
};
|
||||
}
|
||||
|
||||
/// Modify an existing [`Shape`].
|
||||
|
|
|
@ -77,28 +77,28 @@ impl PaintBezier {
|
|||
|
||||
let control_point_radius = 8.0;
|
||||
|
||||
let mut control_point_shapes = vec![];
|
||||
let control_point_shapes: Vec<Shape> = self
|
||||
.control_points
|
||||
.iter_mut()
|
||||
.enumerate()
|
||||
.take(self.degree)
|
||||
.map(|(i, point)| {
|
||||
let size = Vec2::splat(2.0 * control_point_radius);
|
||||
|
||||
for (i, point) in self.control_points.iter_mut().enumerate().take(self.degree) {
|
||||
let size = Vec2::splat(2.0 * control_point_radius);
|
||||
let point_in_screen = to_screen.transform_pos(*point);
|
||||
let point_rect = Rect::from_center_size(point_in_screen, size);
|
||||
let point_id = response.id.with(i);
|
||||
let point_response = ui.interact(point_rect, point_id, Sense::drag());
|
||||
|
||||
let point_in_screen = to_screen.transform_pos(*point);
|
||||
let point_rect = Rect::from_center_size(point_in_screen, size);
|
||||
let point_id = response.id.with(i);
|
||||
let point_response = ui.interact(point_rect, point_id, Sense::drag());
|
||||
*point += point_response.drag_delta();
|
||||
*point = to_screen.from().clamp(*point);
|
||||
|
||||
*point += point_response.drag_delta();
|
||||
*point = to_screen.from().clamp(*point);
|
||||
let point_in_screen = to_screen.transform_pos(*point);
|
||||
let stroke = ui.style().interact(&point_response).fg_stroke;
|
||||
|
||||
let point_in_screen = to_screen.transform_pos(*point);
|
||||
let stroke = ui.style().interact(&point_response).fg_stroke;
|
||||
|
||||
control_point_shapes.push(Shape::circle_stroke(
|
||||
point_in_screen,
|
||||
control_point_radius,
|
||||
stroke,
|
||||
));
|
||||
}
|
||||
Shape::circle_stroke(point_in_screen, control_point_radius, stroke)
|
||||
})
|
||||
.collect();
|
||||
|
||||
let points_in_screen: Vec<Pos2> = self
|
||||
.control_points
|
||||
|
|
|
@ -56,13 +56,15 @@ impl Painting {
|
|||
response.mark_changed();
|
||||
}
|
||||
|
||||
let mut shapes = vec![];
|
||||
for line in &self.lines {
|
||||
if line.len() >= 2 {
|
||||
let shapes = self
|
||||
.lines
|
||||
.iter()
|
||||
.filter(|line| line.len() >= 2)
|
||||
.map(|line| {
|
||||
let points: Vec<Pos2> = line.iter().map(|p| to_screen * *p).collect();
|
||||
shapes.push(egui::Shape::line(points, self.stroke));
|
||||
}
|
||||
}
|
||||
egui::Shape::line(points, self.stroke)
|
||||
});
|
||||
|
||||
painter.extend(shapes);
|
||||
|
||||
response
|
||||
|
|
Loading…
Reference in a new issue