can now add and remove windows

pull/3/head
Milk.H 2023-03-23 16:21:05 +01:00
parent 0c39d1b1e4
commit 542223ddd9
No known key found for this signature in database
GPG Key ID: 3D9DAE46AAC37BD8
2 changed files with 86 additions and 51 deletions

View File

@ -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<OpenWindows>,
pub windows: HashMap<i16, OpenWindows>,
next_id: i16,
free_ids:Vec<i16>
}
@ -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,11 +74,6 @@ impl Debug {
{
self.platform.handle_event(&mut self.imgui, event);
}
pub fn windows(self) -> Vec<OpenWindows>
{
let ret = self.windows.clone();
ret
}
pub fn drawImgui(&mut self, events_loop: &sdl2::EventPump, window: &sdl2::video::Window,
callback: Vec<Callback>) -> Vec<dEvent>
@ -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,6 +101,28 @@ 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<dEvent>
{
@ -122,8 +148,9 @@ impl Debug {
ret
}
fn displayModel(ui: &mut imgui::Ui ,model: &info::ModelI)
fn displayModel(ui: &mut imgui::Ui ,model: &info::ModelI) -> Vec<dEvent>
{
let mut ret = Vec::new();
ui.window("Stats")
.size([300.0, 500.0], imgui::Condition::Always)
.build(|| {
@ -136,7 +163,11 @@ impl Debug {
ui.text(format!("count Vertices: {}", model.vertCount[i]));
ui.separator();
}
if ui.button("close") {
ret.push(dEvent::removeWindow(OpenWindows::ModelInfo))
}
});
ret
}
}

View File

@ -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)
}
_ => {}
}