From 9f148a36496f5743e7185179d91f920bf368cdd6 Mon Sep 17 00:00:00 2001 From: StarArawn <toasterthegamer@gmail.com> Date: Tue, 21 Dec 2021 11:34:05 -0500 Subject: [PATCH] Added query_world function that behaves a bit like a system. --- Cargo.lock | 1 + Cargo.toml | 7 ++++++- examples/global_counter.rs | 13 ++----------- kayak_core/Cargo.toml | 2 ++ kayak_core/src/context.rs | 16 ++++++++++++++++ 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fd50046..0fcd11e 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 fbbbdaf..379a78a 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 072b7d8..00395f0 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 5baddb0..468beaf 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 5bf4b62..cf42882 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 + } } -- GitLab