diff --git a/Cargo.lock b/Cargo.lock index fd5004613e29881582d6c631fe27fac3bc42ae51..0fcd11e5d89fb802ad0fee5bcd5b9bd68da76cb4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1841,6 +1841,7 @@ name = "kayak_core" version = "0.1.0" dependencies = [ "as-any", + "bevy", "derivative", "flo_binding", "kayak_render_macros", diff --git a/Cargo.toml b/Cargo.toml index fbbbdafd8bc6bf3a75edbdc8c309c317715e9e3b..379a78a2edc882d414a95c5bb3c21825ff182afd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,12 @@ members = ["bevy_kayak_ui", "kayak_core", "kayak_render_macros", "kayak_font"] [features] default = ["bevy_renderer"] -bevy_renderer = ["bevy_kayak_ui", "kayak_font", "bevy"] +bevy_renderer = [ + "bevy_kayak_ui", + "kayak_font", + "kayak_core/bevy_renderer", + "bevy", +] [dependencies] bevy = { git = "https://github.com/bevyengine/bevy", rev = "9a16a4d01830297987db40b45f03382ed3acad62", optional = true } diff --git a/examples/global_counter.rs b/examples/global_counter.rs index 072b7d83ef96936f91ac230c473db754e157ea22..00395f0ac0db002112cd25a0c07c2c4cc18a4149 100644 --- a/examples/global_counter.rs +++ b/examples/global_counter.rs @@ -12,17 +12,8 @@ struct GlobalCount(pub u32); #[widget] fn Counter(context: &mut KayakContext) { - let global_count = { - if let Ok(world) = context.get_global_state::<World>() { - if let Some(global_count) = world.get_resource::<Binding<GlobalCount>>() { - global_count.clone() - } else { - return; - } - } else { - return; - } - }; + let global_count = context + .query_world::<Res<Binding<GlobalCount>>, _, _>(move |global_count| global_count.clone()); context.bind(&global_count); diff --git a/kayak_core/Cargo.toml b/kayak_core/Cargo.toml index 5baddb04782de7d4218520f0e41e9cfc54f0c90e..468beaf1ade6a3b58778b0cffbfbc4e03a054c81 100644 --- a/kayak_core/Cargo.toml +++ b/kayak_core/Cargo.toml @@ -7,10 +7,12 @@ edition = "2021" [features] default = [] +bevy_renderer = ["bevy"] [dependencies] as-any = "0.2" derivative = "2.2" +bevy = { git = "https://github.com/bevyengine/bevy", rev = "9a16a4d01830297987db40b45f03382ed3acad62", optional = true } flo_binding = { git = "https://github.com/StarArawn/flo_binding.git", rev = "c78431a56df5ec082b7e1c271871e6c0ac75e81e" } kayak_render_macros = { path = "../kayak_render_macros" } morphorm = { git = "https://github.com/geom3trik/morphorm", rev = "1243152d4cebea46fd3e5098df26402c73acae91" } diff --git a/kayak_core/src/context.rs b/kayak_core/src/context.rs index 5bf4b6256b6b281666af864c9e0d31a73625ef97..cf428829d3bfc5f9933cd37c5cf0a7f87a51ce33 100644 --- a/kayak_core/src/context.rs +++ b/kayak_core/src/context.rs @@ -528,4 +528,20 @@ impl KayakContext { self.get_all_parents(*parent, parents); } } + + #[cfg(feature = "bevy_renderer")] + pub fn query_world<T: bevy::ecs::system::SystemParam, F, R>(&mut self, mut f: F) -> R + where + F: FnMut(<T::Fetch as bevy::ecs::system::SystemParamFetch<'_, '_>>::Item) -> R, + { + let mut world = self.get_global_state::<bevy::prelude::World>().unwrap(); + let mut system_state = bevy::ecs::system::SystemState::<T>::new(&mut world); + let r = { + let test = system_state.get_mut(&mut world); + f(test) + }; + system_state.apply(&mut world); + + r + } }