Add imgui slider command to the debug ui buffer implementation

This commit is contained in:
reo 2025-09-25 13:01:02 +03:00
parent 84ab3a26b1
commit f34a9b01a0
2 changed files with 17 additions and 2 deletions

View file

@ -1,6 +1,7 @@
pub enum UICommand { pub enum UICommand {
Text(String), Text(String),
Separator, Separator,
SliderF32 { label: String, min: f32, max: f32, value: std::rc::Rc<std::cell::RefCell<f32>> },
} }
pub struct DebugUIBuffer { pub struct DebugUIBuffer {
@ -21,6 +22,11 @@ impl DebugUIBuffer {
self.cmds.push(UICommand::Separator); self.cmds.push(UICommand::Separator);
} }
pub fn slider_f32<T: Into<String>>(&mut self, label: T, min: f32, max: f32, value: std::rc::Rc<std::cell::RefCell<f32>>) {
self.cmds.push(UICommand::SliderF32 { label: label.into(), min, max, value });
}
// End of commands
pub fn write_buffer(&self, ui: &imgui::Ui) { pub fn write_buffer(&self, ui: &imgui::Ui) {
for cmd in &self.cmds { for cmd in &self.cmds {
match cmd { match cmd {
@ -30,6 +36,10 @@ impl DebugUIBuffer {
UICommand::Separator => { UICommand::Separator => {
ui.separator(); ui.separator();
} }
UICommand::SliderF32 { label, min, max, value } => {
let mut v = value.borrow_mut();
ui.slider_config(label.as_str(), *min, *max).build(&mut *v);
}
} }
} }
} }

View file

@ -34,10 +34,14 @@ impl System for UpdateAspectRatioSystem {
} }
#[derive(Default)] #[derive(Default)]
struct RenderingTestSystem; struct RenderingTestSystem {
rotation_speed: std::rc::Rc<std::cell::RefCell<f32>>,
}
impl System for RenderingTestSystem { impl System for RenderingTestSystem {
fn load_world(&mut self, ctx: &mut SystemContext) { fn load_world(&mut self, ctx: &mut SystemContext) {
self.rotation_speed = std::rc::Rc::new(std::cell::RefCell::new(5.0));
ctx.scene.world.spawn((Camera { ctx.scene.world.spawn((Camera {
eye: Vec3::new(0.0, 0.0, 2.0), eye: Vec3::new(0.0, 0.0, 2.0),
center: Vec3::ZERO, center: Vec3::ZERO,
@ -67,11 +71,12 @@ impl System for RenderingTestSystem {
dbg_ui.text("Hello World!".to_owned()); dbg_ui.text("Hello World!".to_owned());
dbg_ui.text(format!("Frame Delta: {}", ctx.platform_context.time_ctx.frame_dt)); dbg_ui.text(format!("Frame Delta: {}", ctx.platform_context.time_ctx.frame_dt));
dbg_ui.text(format!("Fixed Delta: {}", ctx.platform_context.time_ctx.fixed_dt)); dbg_ui.text(format!("Fixed Delta: {}", ctx.platform_context.time_ctx.fixed_dt));
dbg_ui.slider_f32("Rotation Speed", -10.0, 10.0, self.rotation_speed.clone());
} }
fn fixed_update(&mut self, ctx: &mut SystemContext) { fn fixed_update(&mut self, ctx: &mut SystemContext) {
ctx.scene.world.query_mut::<(&mut Transform, &ModelHandle)>().into_iter().for_each(|(_, (t, _))| { ctx.scene.world.query_mut::<(&mut Transform, &ModelHandle)>().into_iter().for_each(|(_, (t, _))| {
t.rotation *= Quat::from_rotation_y(10.0 * ctx.platform_context.time_ctx.fixed_dt); t.rotation *= Quat::from_rotation_y(*self.rotation_speed.borrow() * ctx.platform_context.time_ctx.fixed_dt);
}); });
} }