use crate::app::{App, Plugin, Stage};
use crate::ecs::world::World;
use nightshade_ui::{UiSystemFn, UiSystemPhase};
pub fn run_retained_ui_schedule(world: &mut World) {
let systems = std::mem::take(
&mut world
.res_mut::<crate::schedule::RetainedUiSchedule>()
.systems,
);
for system in &systems {
system(&mut world.ecs);
}
world
.res_mut::<crate::schedule::RetainedUiSchedule>()
.systems = systems;
}
pub fn install(world: &mut World, systems: &[(UiSystemPhase, UiSystemFn)]) {
world
.res_mut::<crate::ui::resources::RetainedUiRuntime>()
.visible = true;
world
.res_mut::<crate::schedule::RetainedUiSchedule>()
.systems = nightshade_ui::ui_systems_with(systems);
}
pub fn register_frame_systems(stages: &mut nightshade_ecs::Stages<World>) {
stages
.stage_mut(Stage::FramePostUpdate.name())
.insert_after(
std::any::type_name_of_val(&crate::ecs::transform::systems::run_systems),
std::any::type_name_of_val(&run_retained_ui_schedule),
run_retained_ui_schedule,
);
}
#[derive(Default)]
pub struct UiPlugin {
systems: Vec<(UiSystemPhase, UiSystemFn)>,
}
impl UiPlugin {
pub fn new() -> Self {
Self::default()
}
pub fn with_system(mut self, phase: UiSystemPhase, system: UiSystemFn) -> Self {
self.systems.push((phase, system));
self
}
}
impl Plugin for UiPlugin {
fn build(&self, app: &mut App) {
install(&mut app.world, &self.systems);
register_frame_systems(&mut app.stages);
}
}