Skip to content

Commit

Permalink
paint_texture_at
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Sep 15, 2023
1 parent 0fe5f8a commit 6a95321
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
36 changes: 19 additions & 17 deletions crates/egui/src/widgets/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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 =
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/widgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions crates/egui_plot/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 6a95321

Please sign in to comment.