diff --git a/egui_demo_lib/src/apps/color_test.rs b/egui_demo_lib/src/apps/color_test.rs index 4772e090..c1f2e2ab 100644 --- a/egui_demo_lib/src/apps/color_test.rs +++ b/egui_demo_lib/src/apps/color_test.rs @@ -394,9 +394,7 @@ impl TextureManager { let pixels = gradient.to_pixel_row(); let width = pixels.len(); let height = 1; - let id = tex_allocator.alloc(); - tex_allocator.set_srgba_premultiplied(id, (width, height), &pixels); - id + tex_allocator.alloc_srgba_premultiplied((width, height), &pixels) }) } } diff --git a/egui_demo_lib/src/apps/http_app.rs b/egui_demo_lib/src/apps/http_app.rs index aaf2f815..1f3365ba 100644 --- a/egui_demo_lib/src/apps/http_app.rs +++ b/egui_demo_lib/src/apps/http_app.rs @@ -265,13 +265,16 @@ impl TexMngr { image: &Image, ) -> Option { let tex_allocator = frame.tex_allocator().as_mut()?; - let texture_id = self.texture_id.unwrap_or_else(|| tex_allocator.alloc()); - self.texture_id = Some(texture_id); if self.loaded_url != url { + if let Some(texture_id) = self.texture_id.take() { + tex_allocator.free(texture_id); + } + + self.texture_id = + Some(tex_allocator.alloc_srgba_premultiplied(image.size, &image.pixels)); self.loaded_url = url.to_owned(); - tex_allocator.set_srgba_premultiplied(texture_id, image.size, &image.pixels); } - Some(texture_id) + self.texture_id } } diff --git a/egui_glium/src/backend.rs b/egui_glium/src/backend.rs index 4c1ec502..d7fcdcff 100644 --- a/egui_glium/src/backend.rs +++ b/egui_glium/src/backend.rs @@ -28,17 +28,14 @@ fn deserialize_memory(_: &Option>) -> Option } impl epi::TextureAllocator for Painter { - fn alloc(&mut self) -> egui::TextureId { - self.alloc_user_texture() - } - - fn set_srgba_premultiplied( + fn alloc_srgba_premultiplied( &mut self, - id: egui::TextureId, size: (usize, usize), srgba_pixels: &[Color32], - ) { + ) -> egui::TextureId { + let id = self.alloc_user_texture(); self.set_user_texture(id, size, srgba_pixels); + id } fn free(&mut self, id: egui::TextureId) { diff --git a/egui_web/src/backend.rs b/egui_web/src/backend.rs index 8a1bcdea..69ba32b1 100644 --- a/egui_web/src/backend.rs +++ b/egui_web/src/backend.rs @@ -66,17 +66,14 @@ impl WebBackend { } impl epi::TextureAllocator for webgl::Painter { - fn alloc(&mut self) -> egui::TextureId { - self.alloc_user_texture() - } - - fn set_srgba_premultiplied( + fn alloc_srgba_premultiplied( &mut self, - id: egui::TextureId, size: (usize, usize), srgba_pixels: &[Color32], - ) { + ) -> egui::TextureId { + let id = self.alloc_user_texture(); self.set_user_texture(id, size, srgba_pixels); + id } fn free(&mut self, id: egui::TextureId) { diff --git a/epi/CHANGELOG.md b/epi/CHANGELOG.md index f17e7e82..dc51ada0 100644 --- a/epi/CHANGELOG.md +++ b/epi/CHANGELOG.md @@ -7,6 +7,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). ## Unreleased +* Simplify `TextureAllocator` interface. + ## 0.7.0 - 2021-01-04 diff --git a/epi/src/lib.rs b/epi/src/lib.rs index 71d14832..38475d5b 100644 --- a/epi/src/lib.rs +++ b/epi/src/lib.rs @@ -190,15 +190,11 @@ pub struct IntegrationInfo { /// How to allocate textures (images) to use in [`egui`]. pub trait TextureAllocator { /// Allocate a new user texture. - fn alloc(&mut self) -> egui::TextureId; - - /// Set or change the pixels of a user texture. - fn set_srgba_premultiplied( + fn alloc_srgba_premultiplied( &mut self, - id: egui::TextureId, size: (usize, usize), srgba_pixels: &[egui::Color32], - ); + ) -> egui::TextureId; /// Free the given texture. fn free(&mut self, id: egui::TextureId);