Skip to content

Commit

Permalink
fixed egui image api
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazariglez committed Oct 15, 2023
1 parent 3acc6ce commit 8bdf4ae
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
18 changes: 10 additions & 8 deletions crates/notan_egui/src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::epaint::Primitive;
use crate::plugin::Output;
use crate::TextureId;
use egui::load::SizedTexture;
use egui::{PaintCallbackInfo, Rect};
use notan_app::{
BlendFactor, BlendMode, Buffer, CullMode, Device, Graphics, Pipeline, RenderTexture,
Expand Down Expand Up @@ -149,14 +150,15 @@ impl EguiExtension {
})
}

pub fn add_texture(&mut self, texture: &Texture) -> egui::TextureId {
pub fn add_texture(&mut self, texture: &Texture) -> SizedTexture {
let id = egui::TextureId::User(texture.id());
let size: egui::Vec2 = texture.size().into();
self.textures.insert(id, texture.clone());
id
SizedTexture { id, size }
}

pub fn remove_texture(&mut self, id: egui::TextureId) {
self.free_texture(id);
pub fn remove_texture(&mut self, id: impl Into<TextureId>) {
self.free_texture(id.into());
}

fn set_texture(
Expand Down Expand Up @@ -388,18 +390,18 @@ impl EguiExtension {
}

pub trait EguiRegisterTexture {
fn egui_register_texture(&mut self, texture: &Texture) -> egui::TextureId;
fn egui_remove_texture(&mut self, id: egui::TextureId);
fn egui_register_texture(&mut self, texture: &Texture) -> egui::load::SizedTexture;
fn egui_remove_texture(&mut self, id: impl Into<egui::TextureId>);
}

impl EguiRegisterTexture for Graphics {
fn egui_register_texture(&mut self, texture: &Texture) -> TextureId {
fn egui_register_texture(&mut self, texture: &Texture) -> SizedTexture {
self.extension_mut::<Output, EguiExtension>()
.unwrap()
.add_texture(texture)
}

fn egui_remove_texture(&mut self, id: TextureId) {
fn egui_remove_texture(&mut self, id: impl Into<TextureId>) {
self.extension_mut::<Output, EguiExtension>()
.unwrap()
.remove_texture(id);
Expand Down
1 change: 1 addition & 0 deletions crates/notan_egui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ pub use config::EguiConfig;
pub use extension::{EguiCallbackFn, EguiExtension, EguiRegisterTexture};
pub use plugin::{EguiPlugin, EguiPluginSugar};

pub use egui::load::SizedTexture;
pub use egui::*;
11 changes: 4 additions & 7 deletions examples/egui_render_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ use notan::prelude::*;
struct State {
cube: Cube,
render_texture: RenderTexture,
tex_id: egui::TextureId,
img_size: egui::Vec2,
sized_texture: SizedTexture,
}

impl State {
Expand All @@ -20,14 +19,12 @@ impl State {
.build()
.unwrap();

let img_size = render_texture.size().into();
let tex_id = gfx.egui_register_texture(&render_texture);
let sized_texture = gfx.egui_register_texture(&render_texture);

Self {
img_size,
tex_id,
cube,
render_texture,
sized_texture,
}
}
}
Expand All @@ -47,7 +44,7 @@ fn draw(app: &mut App, gfx: &mut Graphics, plugins: &mut Plugins, state: &mut St

let mut output = plugins.egui(|ctx| {
egui::Window::new("Notan Render Texture").show(ctx, |ui| {
ui.image(state.tex_id, state.img_size);
ui.image(state.sized_texture);
});
});
output.clear_color(Color::BLACK);
Expand Down
10 changes: 4 additions & 6 deletions examples/egui_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use notan::prelude::*;

#[derive(AppState)]
struct State {
tex_id: egui::TextureId,
img_size: egui::Vec2,
sized_texture: egui::SizedTexture,
}

impl State {
Expand All @@ -16,10 +15,9 @@ impl State {
.build()
.unwrap();

let img_size: egui::Vec2 = texture.size().into();
let tex_id = gfx.egui_register_texture(&texture);
let sized_texture = gfx.egui_register_texture(&texture);

Self { img_size, tex_id }
Self { sized_texture }
}
}

Expand All @@ -34,7 +32,7 @@ fn main() -> Result<(), String> {
fn draw(gfx: &mut Graphics, plugins: &mut Plugins, state: &mut State) {
let mut output = plugins.egui(|ctx| {
egui::Window::new("Notan Texture").show(ctx, |ui| {
ui.image(state.tex_id, state.img_size);
ui.image(state.sized_texture);
});
});

Expand Down

0 comments on commit 8bdf4ae

Please sign in to comment.