diff --git a/framework_crates/bones_framework/src/render/color.rs b/framework_crates/bones_framework/src/render/color.rs index da4e653e37..f054cad132 100644 --- a/framework_crates/bones_framework/src/render/color.rs +++ b/framework_crates/bones_framework/src/render/color.rs @@ -23,6 +23,20 @@ pub enum Color { }, } +#[cfg(feature = "ui")] +impl From for egui::Color32 { + fn from(value: Color) -> Self { + match value { + Color::Rgba { + red, + green, + blue, + alpha, + } => egui::Rgba::from_rgba_unmultiplied(red, green, blue, alpha).into(), + } + } +} + impl<'de> serde::Deserialize<'de> for Color { fn deserialize(deserializer: D) -> Result where diff --git a/framework_crates/bones_framework/src/render/ui.rs b/framework_crates/bones_framework/src/render/ui.rs index 59531e7575..6fc26ecac5 100644 --- a/framework_crates/bones_framework/src/render/ui.rs +++ b/framework_crates/bones_framework/src/render/ui.rs @@ -119,6 +119,15 @@ impl FontMeta { .color(self.color.into_egui()) .font(self.id()) } + + /// Clone the font and set a new color. + pub fn with_color(&self, color: Color) -> Self { + Self { + family: self.family.clone(), + size: self.size, + color, + } + } } fn deserialize_arc_str<'de, D: serde::Deserializer<'de>>(d: D) -> Result, D::Error> { @@ -172,12 +181,31 @@ impl Default for EguiSettings { pub trait EguiContextExt { /// Clear the UI focus fn clear_focus(self); + + /// Get a global runtime state from the EGUI context, returning the default value if it is not + /// present. + /// + /// This is just a convenience wrapper around Egui's built in temporary data store. + /// + /// The value will be cloned to get it out of the store without holding a lock. + fn get_state(self) -> T; + + /// Set a global runtime state from the EGUI context. + /// + /// This is just a convenience wrapper around Egui's built in temporary data store. + fn set_state(self, value: T); } impl EguiContextExt for &egui::Context { fn clear_focus(self) { self.memory_mut(|r| r.request_focus(egui::Id::null())); } + fn get_state(self) -> T { + self.data_mut(|data| data.get_temp_mut_or_default::(egui::Id::null()).clone()) + } + fn set_state(self, value: T) { + self.data_mut(|data| *data.get_temp_mut_or_default::(egui::Id::null()) = value); + } } /// Extension trait with helpers for egui responses