Skip to content

Commit dd54423

Browse files
committed
Remove IntoImTexture
Functions should directly return texture type used by backend. No automatic conversion. This simplify uses of API. Everything is more straightforward.
1 parent 57578bf commit dd54423

File tree

6 files changed

+116
-48
lines changed

6 files changed

+116
-48
lines changed

imgui-examples/examples/custom_textures.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ fn main() {
3131

3232
// Constant texture (define once)
3333
ui.text("Constant texture");
34-
let constant_texture = ui.make_texture::<_, _, Texture>(im_str!("#Constant"), || {
34+
let constant_texture = ui.make_texture(im_str!("#Constant"), || {
3535
let mut image_data: Vec<Vec<(f32, f32, f32, f32)>> = Vec::new();
3636
for i in 0..100 {
3737
let mut row: Vec<(f32, f32, f32, f32)> = Vec::new();
@@ -40,15 +40,15 @@ fn main() {
4040
}
4141
image_data.push(row);
4242
}
43-
Texture2d::new(&gl_ctx, image_data).unwrap()
43+
Texture::from_data(&gl_ctx, image_data).unwrap()
4444
});
4545
let size = constant_texture.get_size();
4646
ui.image(&constant_texture, (size.0 as f32, size.1 as f32))
4747
.build();
4848

4949
// Changing texture (re-defined and swap texture for each frame)
5050
ui.text("Variable texture");
51-
let changing_texture = ui.replace_texture::<_, Texture>(im_str!("#Changing"), {
51+
let changing_texture = ui.replace_texture(im_str!("#Changing"), {
5252
let mut image_data: Vec<Vec<(f32, f32, f32, f32)>> = Vec::new();
5353
for i in 0..100 {
5454
let mut row: Vec<(f32, f32, f32, f32)> = Vec::new();
@@ -61,16 +61,16 @@ fn main() {
6161
if t > 1.0 {
6262
t = 0.0;
6363
}
64-
Texture2d::new(&gl_ctx, image_data).unwrap()
64+
Texture::from_data(&gl_ctx, image_data).unwrap()
6565
});
6666
let size = changing_texture.get_size();
6767
ui.image(&changing_texture, (size.0 as f32, size.1 as f32))
6868
.build();
6969

7070
// Texture defined only once, however, you can dynamically draw on it.
7171
ui.text("Draw on texture");
72-
let draw_texture = ui.make_texture::<_, _, Texture>(im_str!("#Draw"), || {
73-
Texture2d::empty(&gl_ctx, 100, 100).unwrap()
72+
let draw_texture = ui.make_texture(im_str!("#Draw"), || {
73+
Texture::from_texture_2d(Texture2d::empty(&gl_ctx, 100, 100).unwrap())
7474
});
7575
// Get the texture as a surface. It must first be converted to a
7676
// `glium::Texture2d` object by using `Texture::from`.

imgui-examples/examples/support_gfx/mod.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use imgui::{FontGlyphRange, FrameSize, ImFontConfig, ImGui, ImGuiMouseCursor, ImVec4, Ui};
2-
use imgui_gfx_renderer::{Renderer, Shaders};
2+
use imgui_gfx_renderer::{Renderer, Shaders, Texture};
33
use std::time::Instant;
44

55
#[derive(Copy, Clone, PartialEq, Debug, Default)]
@@ -202,6 +202,12 @@ pub fn run<F: FnMut(&Ui) -> bool>(title: String, clear_color: [f32; 4], mut run_
202202
};
203203

204204
let ui = imgui.frame(frame_size, delta_s);
205+
206+
let texture = ui.make_texture(im_str!("#STANDARD"), || gfx_load_texture(&mut factory));
207+
ui.image(&texture, (100.0, 100.0)).build();
208+
let texture = ui.replace_texture(im_str!("#CHANGE"), gfx_load_texture_change(&mut factory));
209+
ui.image(&texture, (100.0, 100.0)).build();
210+
205211
if !run_ui(&ui) {
206212
break;
207213
}
@@ -252,3 +258,43 @@ fn update_mouse(imgui: &mut ImGui, mouse_state: &mut MouseState) {
252258
imgui.set_mouse_wheel(mouse_state.wheel);
253259
mouse_state.wheel = 0.0;
254260
}
261+
262+
use gfx;
263+
264+
fn gfx_load_texture<F, R>(factory: &mut F) -> Texture<R>
265+
where
266+
F: gfx::Factory<R>,
267+
R: gfx::Resources,
268+
{
269+
let mut data = Vec::new();
270+
let (width, height) = (100, 100);
271+
for i in 0..width {
272+
for j in 0..height {
273+
data.push(i as u8);
274+
data.push(j as u8);
275+
data.push(255u8);
276+
data.push(255u8);
277+
}
278+
}
279+
Texture::from_raw(factory, width, height, &data).unwrap()
280+
}
281+
282+
fn gfx_load_texture_change<F, R>(factory: &mut F) -> Texture<R>
283+
where
284+
F: gfx::Factory<R>,
285+
R: gfx::Resources,
286+
{
287+
static mut COUNT: u8 = 0;
288+
let mut data = Vec::new();
289+
let (width, height) = (100, 100);
290+
for i in 0..width {
291+
for j in 0..height {
292+
data.push(i as u8);
293+
data.push(j as u8);
294+
data.push(unsafe { COUNT });
295+
data.push(255u8);
296+
}
297+
}
298+
unsafe { if COUNT == 255 { COUNT = 0 } else { COUNT += 1 } };
299+
Texture::from_raw(factory, width, height, &data).unwrap()
300+
}

imgui-glium-renderer/src/im_texture.rs

+39-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
use std::borrow::Cow;
12
use std::mem;
23
use std::ops::Deref;
34

5+
use glium::backend::Facade;
6+
use glium::texture::{PixelValue, RawImage2d, Texture2dDataSource, TextureCreationError};
47
use glium::Texture2d;
5-
use imgui::{FromImTexture, ImTexture, ImTextureID, IntoImTexture};
8+
use imgui::{FromImTexture, ImTexture, ImTextureID};
69

710
/// Handle to a glium texture
811
///
@@ -19,12 +22,6 @@ impl ImTexture for Texture {
1922
}
2023
}
2124

22-
impl IntoImTexture<Texture2d> for Texture {
23-
fn into_texture(texture: Texture2d) -> Texture {
24-
Texture(texture)
25-
}
26-
}
27-
2825
impl Deref for Texture {
2926
type Target = Texture2d;
3027
fn deref(&self) -> &Self::Target {
@@ -37,3 +34,38 @@ impl FromImTexture for Texture {
3734
unsafe { mem::transmute::<_, &Texture>(texture_id) }
3835
}
3936
}
37+
38+
impl Texture {
39+
pub fn from_texture_2d(texture: Texture2d) -> Self {
40+
Texture(texture)
41+
}
42+
43+
pub fn from_data<'a, F, T>(facade: &F, data: T) -> Result<Self, TextureCreationError>
44+
where
45+
T: Texture2dDataSource<'a>,
46+
F: Facade,
47+
{
48+
Texture2d::new(facade, data).map(Texture)
49+
}
50+
51+
pub fn from_raw<F, P>(
52+
facade: &F,
53+
width: u32,
54+
height: u32,
55+
data: &[P],
56+
) -> Result<Self, TextureCreationError>
57+
where
58+
F: Facade,
59+
P: PixelValue,
60+
{
61+
Self::from_data(
62+
facade,
63+
RawImage2d {
64+
data: Cow::Borrowed(data),
65+
width,
66+
height,
67+
format: <P as PixelValue>::get_format(),
68+
},
69+
)
70+
}
71+
}

imgui-glium-renderer/src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ impl DeviceObjects {
221221
let index_buffer = IndexBuffer::empty_dynamic(ctx, PrimitiveType::TrianglesList, 0)?;
222222

223223
let program = compile_default_program(ctx)?;
224-
let texture = im_gui.register_font_texture::<_, _, Texture, _>(|handle| {
224+
let texture = im_gui.register_font_texture(|handle| {
225225
let data = RawImage2d {
226226
data: Cow::Borrowed(handle.pixels),
227227
width: handle.width,
228228
height: handle.height,
229229
format: ClientFormat::U8U8U8U8,
230230
};
231-
Texture2d::new(ctx, data)
231+
Texture2d::new(ctx, data).map(Texture::from_texture_2d)
232232
})?;
233233
im_gui.set_texture_id(texture.get_id() as usize);
234234

src/lib.rs

+22-23
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub use sys::{
3939
ImGuiTreeNodeFlags, ImGuiWindowFlags, ImTextureID, ImVec2, ImVec4,
4040
};
4141
use texture::TextureCache;
42-
pub use texture::{AnyTexture, FromImTexture, ImTexture, IntoImTexture};
42+
pub use texture::{AnyTexture, FromImTexture, ImTexture};
4343
pub use trees::{CollapsingHeader, TreeNode};
4444
pub use window::Window;
4545
pub use window_draw_list::{ChannelsSplit, ImColor, WindowDrawList};
@@ -178,29 +178,29 @@ impl ImGui {
178178
}
179179
}
180180
/// Register font texture returned by closure to [`ImGui`] instance.
181-
pub fn register_font_texture<'a, F, T, U, E>(&mut self, f: F) -> Result<AnyTexture, E>
181+
pub fn register_font_texture<'a, F, T, E>(&mut self, f: F) -> Result<AnyTexture, E>
182182
where
183183
F: FnOnce(FontTextureHandle<'a>) -> Result<T, E>,
184-
U: 'static + IntoImTexture<T> + ImTexture,
184+
T: 'static + ImTexture,
185185
{
186186
let io = self.io();
187187
let mut pixels: *mut c_uchar = ptr::null_mut();
188188
let mut width: c_int = 0;
189189
let mut height: c_int = 0;
190190
let mut bytes_per_pixel: c_int = 0;
191-
let texture: U = unsafe {
191+
let texture = unsafe {
192192
sys::ImFontAtlas_GetTexDataAsRGBA32(
193193
io.fonts,
194194
&mut pixels,
195195
&mut width,
196196
&mut height,
197197
&mut bytes_per_pixel,
198198
);
199-
IntoImTexture::into_texture(f(FontTextureHandle {
199+
f(FontTextureHandle {
200200
width: width as u32,
201201
height: height as u32,
202202
pixels: slice::from_raw_parts(pixels, (width * height * bytes_per_pixel) as usize),
203-
})?)
203+
})?
204204
};
205205
self.textures.register_texture(im_str!("#FONT"), texture);
206206
Ok(self.textures.get_texture(im_str!("#FONT")).unwrap())
@@ -1425,13 +1425,13 @@ impl<'ui> Ui<'ui> {
14251425
/// extern crate imgui_glium_renderer;
14261426
///
14271427
/// use imgui::*;
1428-
/// use glium::Texture2d;
1428+
/// use imgui_glium_renderer::Texture;
14291429
/// use glium::backend::Facade;
14301430
///
14311431
/// fn make_a_texture<F: Facade>(ui: &Ui, facade: &F, data: Vec<Vec<(u8, u8, u8, u8)>>) {
1432-
/// let texture_handle = ui.replace_texture::<_, imgui_glium_renderer::Texture>(
1432+
/// let texture_handle = ui.replace_texture(
14331433
/// im_str!("#Texture Name ID"),
1434-
/// Texture2d::new(facade, data).unwrap(),
1434+
/// Texture::from_data(facade, data).unwrap(),
14351435
/// );
14361436
/// ui.image(&texture_handle, [100.0, 100.0]).build();
14371437
/// }
@@ -1687,29 +1687,29 @@ impl<'ui> Ui<'ui> {
16871687
/// extern crate imgui_glium_renderer;
16881688
///
16891689
/// use imgui::*;
1690-
/// use glium::Texture2d;
1690+
/// use imgui_glium_renderer::Texture;
16911691
/// use glium::backend::Facade;
1692+
/// use glium::Texture2d;
16921693
///
16931694
/// fn make_a_texture<F: Facade>(ui: &Ui, facade: &F) {
1694-
/// let texture_handle = ui.make_texture::<_, _, imgui_glium_renderer::Texture>(im_str!("#Texture Name ID"), || {
1695-
/// Texture2d::empty(facade, 100, 100).unwrap()
1695+
/// let texture_handle = ui.make_texture(im_str!("#Texture Name ID"), || {
1696+
/// Texture::from_texture_2d(Texture2d::empty(facade, 100, 100).unwrap())
16961697
/// });
16971698
/// // ... Do something with `texture_handle`
16981699
/// }
16991700
///
17001701
/// # fn main() {}
17011702
/// ```
1702-
pub fn make_texture<F, T, U>(&self, name: &ImStr, f: F) -> AnyTexture
1703+
pub fn make_texture<F, T>(&self, name: &ImStr, f: F) -> AnyTexture
17031704
where
17041705
F: FnOnce() -> T,
1705-
U: 'static + IntoImTexture<T> + ImTexture,
1706+
T: 'static + ImTexture,
17061707
{
17071708
let imgui = self.imgui();
17081709
if let Some(texture) = imgui.textures.get_texture(name) {
17091710
texture
17101711
} else {
1711-
let texture: U = IntoImTexture::into_texture(f());
1712-
imgui.textures.register_texture(name, texture);
1712+
imgui.textures.register_texture(name, f());
17131713
imgui.textures.get_texture(name).unwrap()
17141714
}
17151715
}
@@ -1730,26 +1730,25 @@ impl<'ui> Ui<'ui> {
17301730
/// extern crate imgui_glium_renderer;
17311731
///
17321732
/// use imgui::*;
1733-
/// use glium::Texture2d;
1733+
/// use imgui_glium_renderer::Texture;
17341734
/// use glium::backend::Facade;
17351735
///
17361736
/// fn make_a_texture<F: Facade>(ui: &Ui, facade: &F, data: Vec<Vec<(u8, u8, u8, u8)>>) {
1737-
/// let texture_handle = ui.replace_texture::<_, imgui_glium_renderer::Texture>(
1737+
/// let texture_handle = ui.replace_texture(
17381738
/// im_str!("#Texture Name ID"),
1739-
/// Texture2d::new(facade, data).unwrap(),
1739+
/// Texture::from_data(facade, data).unwrap(),
17401740
/// );
17411741
/// // ... Do something with `texture_handle`
17421742
/// }
17431743
///
17441744
/// # fn main() {}
17451745
/// ```
1746-
pub fn replace_texture<T, U>(&self, name: &ImStr, t: T) -> AnyTexture
1746+
pub fn replace_texture<T>(&self, name: &ImStr, t: T) -> AnyTexture
17471747
where
1748-
U: 'static + IntoImTexture<T> + ImTexture,
1748+
T: 'static + ImTexture,
17491749
{
17501750
let imgui = self.imgui();
1751-
let texture: U = IntoImTexture::into_texture(t);
1752-
imgui.textures.register_texture(name, texture);
1751+
imgui.textures.register_texture(name, t);
17531752
imgui.textures.get_texture(name).unwrap()
17541753
}
17551754
}

src/texture.rs

-9
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ impl Deref for AnyTexture {
6262
}
6363
}
6464

65-
/// Trait defining how an external type can be converted into a type implementing
66-
/// [`ImTexture`].
67-
///
68-
/// Typically implemented to convert a native type (e.g. Texture2d from the
69-
/// glium crate) to a type defined in the back-end implemnting [`ImTexture`].
70-
pub trait IntoImTexture<T>: ImTexture {
71-
fn into_texture(t: T) -> Self;
72-
}
73-
7465
/// Trait defining how an object implementing [`ImTexture`] should be converted
7566
/// back to the native texture type used by the back-end.
7667
pub trait FromImTexture {

0 commit comments

Comments
 (0)