diff --git a/src/debug.rs b/src/debug.rs index c46dcd9..8705e48 100644 --- a/src/debug.rs +++ b/src/debug.rs @@ -1,7 +1,7 @@ use imgui_glow_renderer::AutoRenderer; use imgui_sdl2_support::SdlPlatform; - +use std::collections::HashMap; use sdl2::event::Event; const MODELS: [&str; 3]= ["resources/models/TestCarModel/CarW4.obj", "resources/models/TestCarModel/CarW1.obj", "resources/models/TestCarModel/CarW0.obj"]; @@ -13,6 +13,8 @@ pub enum dEvent { loadModel(i32), setTest(f32, i32), + addWindow(OpenWindows), + removeWindow(OpenWindows), exit, } @@ -33,7 +35,9 @@ pub struct Debug { imgui: imgui::Context, renderer: AutoRenderer, platform: SdlPlatform, - pub windows: Vec, + pub windows: HashMap, + next_id: i16, + free_ids:Vec } @@ -53,11 +57,16 @@ impl Debug { Ok(ret) => ret, Err(s) => panic!("Failed to initialize Imgui Platform Renderer: {}", s), }; + + let mut windows = HashMap::new(); + windows.insert(0,OpenWindows::TestWind,); Debug { imgui, renderer, platform, - windows: vec![OpenWindows::TestWind, OpenWindows::ModelInfo], + windows, + next_id: 2, + free_ids: Vec::new(), } } @@ -65,16 +74,11 @@ impl Debug { { self.platform.handle_event(&mut self.imgui, event); } - pub fn windows(self) -> Vec - { - let ret = self.windows.clone(); - ret - } pub fn drawImgui(&mut self, events_loop: &sdl2::EventPump, window: &sdl2::video::Window, callback: Vec) -> Vec { - self.platform.prepare_frame(&mut self.imgui, &window, &events_loop ); + self.platform.prepare_frame(&mut self.imgui, &window, &events_loop); let ui = self.imgui.new_frame(); let mut ret = Vec::new(); for call in callback @@ -82,10 +86,10 @@ impl Debug { match call { Callback::ModelInfo(a) => { - Debug::displayModel(ui, &a) + ret.append(&mut Debug::displayModel(ui, &a)) }, Callback::TestWind(mut a) => - ret = Debug::displayTest(ui, &mut a), + ret.append(&mut Debug::displayTest(ui, &mut a)), } } @@ -97,47 +101,74 @@ impl Debug { ret } + pub fn addWindow(&mut self, window: OpenWindows) + { + if (self.free_ids.is_empty()) + { + self.windows.insert(self.next_id, window); + } + else { + self.windows.insert(self.free_ids.pop().unwrap(), window); + } + } + pub fn removeWindow(&mut self, window: OpenWindows) + { + println!("searching for a match..."); + let mut id = 0; + for (hashId, windowType) in &self.windows + { + if let _window = windowType {id = *hashId; println!("found a match!")}; + + } + self.windows.remove(&id); + self.free_ids.push(id); + } fn displayTest(ui: &mut imgui::Ui, testI: &mut info::testI) -> Vec - { - let mut ret = Vec::new(); - let mut selectedModel = testI.selectedModel; - let mut spinSpeed = testI.spinSpeed; - ui.window("POLY I WILL FUCKING") - .size([500.0, 100.0], imgui::Condition::FirstUseEver) - .build(|| { - ui.text("you serve no purpose in life"); - ui.text("Your Purpose in life is to suck my dick"); - ui.slider("The Spin Speed", 0.1, 10.0, &mut spinSpeed); - if ui - .button("Quit") { - ret.push(dEvent::exit); - } - if ui.list_box("Select Model to Load", &mut selectedModel, &MODELS, 5) { - ret.push(dEvent::loadModel(selectedModel)); + { + let mut ret = Vec::new(); + let mut selectedModel = testI.selectedModel; + let mut spinSpeed = testI.spinSpeed; + ui.window("POLY I WILL FUCKING") + .size([500.0, 100.0], imgui::Condition::FirstUseEver) + .build(|| { + ui.text("you serve no purpose in life"); + ui.text("Your Purpose in life is to suck my dick"); + ui.slider("The Spin Speed", 0.1, 10.0, &mut spinSpeed); + if ui + .button("Quit") { + ret.push(dEvent::exit); } - }); - ret.push(dEvent::setTest(spinSpeed, selectedModel - )); - ret - } + if ui.list_box("Select Model to Load", &mut selectedModel, &MODELS, 5) { + ret.push(dEvent::loadModel(selectedModel)); + } + }); + ret.push(dEvent::setTest(spinSpeed, selectedModel + )); + ret + } - fn displayModel(ui: &mut imgui::Ui ,model: &info::ModelI) - { - ui.window("Stats") - .size([300.0, 500.0], imgui::Condition::Always) - .build(|| { - ui.text("Model Information"); - ui.text(format!("count meshes: {}", model.meshesCount)); - ui.separator(); - for i in 0..model.meshesCount - { - ui.text(format!("Mesh Number {}", i)); - ui.text(format!("count Vertices: {}", model.vertCount[i])); - ui.separator(); - } - }); - } + fn displayModel(ui: &mut imgui::Ui ,model: &info::ModelI) -> Vec + { + let mut ret = Vec::new(); + ui.window("Stats") + .size([300.0, 500.0], imgui::Condition::Always) + .build(|| { + ui.text("Model Information"); + ui.text(format!("count meshes: {}", model.meshesCount)); + ui.separator(); + for i in 0..model.meshesCount + { + ui.text(format!("Mesh Number {}", i)); + ui.text(format!("count Vertices: {}", model.vertCount[i])); + ui.separator(); + } + if ui.button("close") { + ret.push(dEvent::removeWindow(OpenWindows::ModelInfo)) + } + }); + ret + } } diff --git a/src/scene.rs b/src/scene.rs index 6e72dd2..4e9a55f 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -120,7 +120,7 @@ impl Scene{ if self.tempData.DebugMode{ let mut Callbacks = Vec::new(); let windows = self.debug.windows.clone(); - for window in windows + for (id, window) in windows { match window { debug::OpenWindows::ModelInfo => Callbacks.push(debug::ModelInfo(&self.Car)), @@ -140,8 +140,9 @@ impl Scene{ dEvent::setTest(a, b) => { self.tempData.spinSpeed = a; self.tempData.selectedModel = b; - } - + }, + dEvent::addWindow(a) => self.debug.addWindow(a), + dEvent::removeWindow(a) => self.debug.removeWindow(a), } } } @@ -159,6 +160,9 @@ impl Scene{ }, Event::KeyDown {keycode: Some(Keycode::D), ..} => { self.tempData.DebugMode = !self.tempData.DebugMode + } + Event::KeyDown {keycode: Some(Keycode::M), ..} => { + self.debug.addWindow(debug::OpenWindows::ModelInfo) } _ => {} }