From 6a95321ceb017219173151119ac2cd16c7f4e166 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 15 Sep 2023 10:36:40 +0200 Subject: [PATCH] paint_texture_at --- crates/egui/src/widgets/image.rs | 36 ++++++++++++++++--------------- crates/egui/src/widgets/mod.rs | 2 +- crates/egui_plot/src/items/mod.rs | 4 ++-- 3 files changed, 22 insertions(+), 20 deletions(-) diff --git a/crates/egui/src/widgets/image.rs b/crates/egui/src/widgets/image.rs index f608b735404..ea4729a32b8 100644 --- a/crates/egui/src/widgets/image.rs +++ b/crates/egui/src/widgets/image.rs @@ -332,13 +332,15 @@ impl<'a> Widget for Image<'a> { let ui_size = self.calc_size(ui.available_size(), original_image_size); let (rect, response) = ui.allocate_exact_size(ui_size, self.sense); - paint_texture_load_result( - ui, - &tlr, - rect, - self.show_loading_spinner, - &self.image_options, - ); + if ui.is_rect_visible(rect) { + paint_texture_load_result( + ui, + &tlr, + rect, + self.show_loading_spinner, + &self.image_options, + ); + } texture_load_result_response(&self.source, &tlr, response) } } @@ -564,7 +566,7 @@ pub fn paint_texture_load_result( ) { match tlr { Ok(TexturePoll::Ready { texture }) => { - paint_image_at(ui, rect, options, texture); + paint_texture_at(ui.painter(), rect, options, texture); } Ok(TexturePoll::Pending { .. }) => { let show_loading_spinner = @@ -710,16 +712,16 @@ impl Default for ImageOptions { } } -/// Paint a `SizedTexture` as an image according to some `ImageOptions` at a given `rect`. -pub fn paint_image_at(ui: &Ui, rect: Rect, options: &ImageOptions, texture: &SizedTexture) { - if !ui.is_rect_visible(rect) { - return; - } - +pub fn paint_texture_at( + painter: &Painter, + rect: Rect, + options: &ImageOptions, + texture: &SizedTexture, +) { if options.bg_fill != Default::default() { let mut mesh = Mesh::default(); mesh.add_colored_rect(rect, options.bg_fill); - ui.painter().add(Shape::mesh(mesh)); + painter.add(Shape::mesh(mesh)); } match options.rotation { @@ -734,10 +736,10 @@ pub fn paint_image_at(ui: &Ui, rect: Rect, options: &ImageOptions, texture: &Siz let mut mesh = Mesh::with_texture(texture.id); mesh.add_rect_with_uv(rect, options.uv, options.tint); mesh.rotate(rot, rect.min + origin * rect.size()); - ui.painter().add(Shape::mesh(mesh)); + painter.add(Shape::mesh(mesh)); } None => { - ui.painter().add(RectShape { + painter.add(RectShape { rect, rounding: options.rounding, fill: options.tint, diff --git a/crates/egui/src/widgets/mod.rs b/crates/egui/src/widgets/mod.rs index eff7a2515a5..d589e386ac7 100644 --- a/crates/egui/src/widgets/mod.rs +++ b/crates/egui/src/widgets/mod.rs @@ -22,7 +22,7 @@ pub mod text_edit; pub use button::*; pub use drag_value::DragValue; pub use hyperlink::*; -pub use image::{paint_image_at, Image, ImageFit, ImageOptions, ImageSize, ImageSource}; +pub use image::{paint_texture_at, Image, ImageFit, ImageOptions, ImageSize, ImageSource}; pub use label::*; pub use progress_bar::ProgressBar; pub use selected_label::SelectableLabel; diff --git a/crates/egui_plot/src/items/mod.rs b/crates/egui_plot/src/items/mod.rs index 7e71de92b40..644a2c14e79 100644 --- a/crates/egui_plot/src/items/mod.rs +++ b/crates/egui_plot/src/items/mod.rs @@ -1235,8 +1235,8 @@ impl PlotItem for PlotImage { }; let screen_rotation = -*rotation as f32; - egui::paint_image_at( - ui, + egui::paint_texture_at( + ui.painter(), image_screen_rect, &ImageOptions { uv: *uv,