don't do sketchy egui Id stuff

This commit is contained in:
Simon Gardling 2025-12-03 03:42:51 -05:00
parent d5bc1bed11
commit d592c74654
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D
2 changed files with 9 additions and 26 deletions

View File

@ -1,8 +1,5 @@
use crate::{ use crate::{
consts::COLORS, consts::COLORS, function_entry::FunctionEntry, misc::random_u64, widgets::widgets_ontop,
function_entry::FunctionEntry,
misc::{create_id, get_u64_id, random_u64},
widgets::widgets_ontop,
}; };
use egui::{Button, Id, Key, Modifiers, Popup, TextEdit, WidgetText}; use egui::{Button, Id, Key, Modifiers, Popup, TextEdit, WidgetText};
use emath::vec2; use emath::vec2;
@ -22,7 +19,7 @@ impl Default for FunctionManager {
fn default() -> Self { fn default() -> Self {
let mut vec: Functions = Vec::with_capacity(COLORS.len()); let mut vec: Functions = Vec::with_capacity(COLORS.len());
vec.push(( vec.push((
create_id(11414819524356497634), // Random number here to avoid call to crate::misc::random_u64() Id::NULL.with(11414819524356497634_u64), // Random number here to avoid call to crate::misc::random_u64()
FunctionEntry::default(), FunctionEntry::default(),
)); ));
Self { functions: vec } Self { functions: vec }
@ -40,8 +37,8 @@ impl Serialize for FunctionManager {
&self &self
.functions .functions
.iter() .iter()
.map(|(id, func)| (get_u64_id(*id), func.clone())) .map(|(id, func)| (*id, func.clone()))
.collect::<Vec<(u64, FunctionEntry)>>(), .collect::<Vec<(Id, FunctionEntry)>>(),
)?; )?;
s.end() s.end()
} }
@ -53,17 +50,12 @@ impl<'de> Deserialize<'de> for FunctionManager {
D: Deserializer<'de>, D: Deserializer<'de>,
{ {
#[derive(Deserialize)] #[derive(Deserialize)]
struct Helper(Vec<(u64, FunctionEntry)>); struct Helper(Vec<(Id, FunctionEntry)>);
let helper = Helper::deserialize(deserializer)?; let helper = Helper::deserialize(deserializer)?;
Ok(FunctionManager { Ok(FunctionManager {
functions: helper functions: helper.0.to_vec(),
.0
.iter()
.cloned()
.map(|(id, func)| (create_id(id), func))
.collect::<Vec<(Id, FunctionEntry)>>(),
}) })
} }
} }
@ -204,7 +196,7 @@ impl FunctionManager {
const BUTTONS_Y_OFFSET: f32 = 1.32; const BUTTONS_Y_OFFSET: f32 = 1.32;
const Y_OFFSET: f32 = crate::consts::FONT_SIZE * BUTTONS_Y_OFFSET; const Y_OFFSET: f32 = crate::consts::FONT_SIZE * BUTTONS_Y_OFFSET;
widgets_ontop(ui, create_id(i as u64), &re, Y_OFFSET, |ui| { widgets_ontop(ui, Id::NULL.with(i), &re, Y_OFFSET, |ui| {
ui.horizontal(|ui| { ui.horizontal(|ui| {
// There's more than 1 function! Functions can now be deleted // There's more than 1 function! Functions can now be deleted
if ui if ui
@ -327,7 +319,7 @@ impl FunctionManager {
/// Create and push new empty function entry /// Create and push new empty function entry
pub fn push_empty(&mut self) { pub fn push_empty(&mut self) {
self.functions.push(( self.functions.push((
create_id(random_u64().expect("unable to generate random id")), Id::NULL.with(random_u64().expect("unable to generate random id")),
FunctionEntry::default(), FunctionEntry::default(),
)); ));
} }
@ -337,7 +329,7 @@ impl FunctionManager {
// Reset settings_opened so the cloned function doesn't have settings open // Reset settings_opened so the cloned function doesn't have settings open
entry.settings_opened = false; entry.settings_opened = false;
self.functions.push(( self.functions.push((
create_id(random_u64().expect("unable to generate random id")), Id::NULL.with(random_u64().expect("unable to generate random id")),
entry, entry,
)); ));
} }

View File

@ -1,4 +1,3 @@
use egui::Id;
use egui_plot::{Line, PlotPoint, PlotPoints, Points}; use egui_plot::{Line, PlotPoint, PlotPoints, Points};
use emath::Pos2; use emath::Pos2;
use itertools::Itertools; use itertools::Itertools;
@ -62,14 +61,6 @@ impl Offset for Pos2 {
} }
} }
pub const fn create_id(x: u64) -> Id {
unsafe { std::mem::transmute::<u64, Id>(x) }
}
pub const fn get_u64_id(id: Id) -> u64 {
unsafe { std::mem::transmute::<Id, u64>(id) }
}
/* /*
/// Rounds f64 to `n` decimal places /// Rounds f64 to `n` decimal places
pub fn decimal_round(x: f64, n: usize) -> f64 { pub fn decimal_round(x: f64, n: usize) -> f64 {