From 12e1d4d4c2fd9dfb7a53ce0f35cd236fda28a6ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Bourdeauducq?= Date: Thu, 12 Sep 2024 22:53:01 +0800 Subject: [PATCH] add slider GUI element --- src/main.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 900a443..665ab17 100644 --- a/src/main.rs +++ b/src/main.rs @@ -489,6 +489,16 @@ struct Cells { cells: Vec, } +static mut CELL_UI: Option<&'static mut egui::Ui> = None; + +#[no_mangle] +pub extern "C" fn __nac3_cells_slider(prev: f64) -> f64 { + let ui = unsafe { CELL_UI.as_mut().unwrap() }; + let mut value = prev; + ui.add(egui::Slider::new(&mut value, 0.0..=100.0)); + value +} + impl Cells { fn new() -> Self { Self { @@ -541,7 +551,18 @@ impl Cells { }); match &cell.result { Ok(bin) => { - ui.label(format!("{}", unsafe { bin.run_fn.unwrap()() })); + let result = { + unsafe { + CELL_UI.replace(std::mem::transmute::< + &mut egui::Ui, + &'static mut egui::Ui, + >(ui)) + }; + let result = unsafe { bin.run_fn.unwrap()() }; + unsafe { CELL_UI.take() }; + result + }; + ui.label(format!("{}", result)); } Err(msg) => { if !msg.is_empty() {