From fb7ea044e204e9cc8a700720b91ea8438adefc63 Mon Sep 17 00:00:00 2001 From: Tad Hardesty Date: Mon, 10 Sep 2018 00:54:00 -0700 Subject: [PATCH] Split the ImTexture allocator into a generic struct --- imgui-gfx-renderer/src/lib.rs | 24 ++++++++--------------- src/image.rs | 36 +++++++++++++++++++++++++++++++++++ src/lib.rs | 2 +- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/imgui-gfx-renderer/src/lib.rs b/imgui-gfx-renderer/src/lib.rs index c6c135e..d3aa98a 100644 --- a/imgui-gfx-renderer/src/lib.rs +++ b/imgui-gfx-renderer/src/lib.rs @@ -7,9 +7,7 @@ use gfx::memory::Bind; use gfx::texture::{FilterMethod, SamplerInfo, WrapMode}; use gfx::traits::FactoryExt; use gfx::{Bundle, CommandBuffer, Encoder, Factory, IntoIndexBuffer, Rect, Resources, Slice}; -use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, ImTexture}; - -use std::collections::HashMap; +use imgui::{DrawList, FrameSize, ImDrawIdx, ImDrawVert, ImGui, Ui, Textures}; pub type RendererResult = Result; @@ -101,8 +99,7 @@ pub type Texture = (gfx::handle::ShaderResourceView, gfx::handle pub struct Renderer { bundle: Bundle>, index_buffer: Buffer, - textures: HashMap>, - next_texture: usize, + textures: Textures>, } impl Renderer { @@ -140,9 +137,8 @@ impl Renderer { let sampler = factory.create_sampler(SamplerInfo::new(FilterMethod::Trilinear, WrapMode::Clamp)); let pair = (texture, sampler); - let mut textures = HashMap::new(); - textures.insert(0, pair.clone()); - imgui.set_texture_id(0); + let mut textures = Textures::new(); + imgui.set_texture_id(textures.insert(pair.clone())); let data = pipe::Data { vertex_buffer: vertex_buffer, @@ -171,8 +167,7 @@ impl Renderer { Ok(Renderer { bundle: Bundle::new(slice, pso, data), index_buffer: index_buffer, - textures, - next_texture: 1, + textures: textures, }) } @@ -180,11 +175,8 @@ impl Renderer { self.bundle.data.out = out; } - pub fn add_texture(&mut self, texture: Texture) -> ImTexture { - let id = self.next_texture; - self.textures.insert(id, texture); - self.next_texture += 1; - id + pub fn textures(&mut self) -> &mut Textures> { + &mut self.textures } pub fn render<'a, F: Factory, C: CommandBuffer>( @@ -235,7 +227,7 @@ impl Renderer { self.bundle.slice.start = 0; for cmd in draw_list.cmd_buffer { - if let Some(tex) = self.textures.get(&(cmd.texture_id as usize)) { + if let Some(tex) = self.textures.get(cmd.texture_id as usize) { // cloning handles is okay, since they're Arcs internally self.bundle.data.tex = tex.clone(); } diff --git a/src/image.rs b/src/image.rs index b8cf978..2326ef4 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,5 +1,6 @@ use super::{ImVec2, ImVec4, Ui}; use std::marker::PhantomData; +use std::collections::HashMap; use std::os::raw::c_void; use sys; @@ -88,3 +89,38 @@ impl<'ui> Image<'ui> { } } } + +/// Generic texture mapping for use by renderers. +pub struct Textures { + textures: HashMap, + next: usize, +} + +impl Textures { + pub fn new() -> Self { + Textures { + textures: HashMap::new(), + next: 0, + } + } + + pub fn insert(&mut self, texture: T) -> ImTexture { + let id = self.next; + self.textures.insert(id, texture); + self.next += 1; + id + } + + pub fn replace(&mut self, id: ImTexture, texture: T) { + assert!(self.textures.contains_key(&id)); + self.textures.insert(id, texture); + } + + pub fn remove(&mut self, id: ImTexture) { + self.textures.remove(&id); + } + + pub fn get(&self, id: ImTexture) -> Option<&T> { + self.textures.get(&id) + } +} diff --git a/src/lib.rs b/src/lib.rs index 4e9688f..55aa8e8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,7 +18,7 @@ pub use drag::{ DragInt4, DragIntRange2, }; pub use fonts::{FontGlyphRange, ImFont, ImFontAtlas, ImFontConfig}; -pub use image::{ImTexture, Image}; +pub use image::{ImTexture, Image, Textures}; pub use input::{ InputFloat, InputFloat2, InputFloat3, InputFloat4, InputInt, InputInt2, InputInt3, InputInt4, InputText, InputTextMultiline,