use bevy::ecs::system::EntityCommands;
use bevy::prelude::*;
use bevy_cobweb::prelude::*;
use crate::prelude::*;
pub trait UiBuilderReactExt
{
fn add_reactor<T: EntityWorldReactor>(&mut self, data: T::Local);
fn insert_reactive<T: ReactComponent>(&mut self, component: T) -> &mut Self;
fn react(&mut self) -> ReactCommands<'_, '_>;
fn on_event<T: Send + Sync + 'static>(&mut self) -> OnEventExt<'_, T>;
fn despawn_on_event<T: Send + Sync + 'static>(&mut self) -> &mut Self;
fn despawn_on_broadcast<T: Send + Sync + 'static>(&mut self) -> &mut Self;
fn reactor<M, C, T, R>(&mut self, triggers: T, reactor: C) -> &mut Self
where
T: ReactionTriggerBundle,
R: CobwebResult,
C: IntoSystem<TargetId, R, M> + Send + Sync + 'static;
fn update<M, R: CobwebResult, C: IntoSystem<TargetId, R, M> + Send + Sync + 'static>(
&mut self,
reactor: C,
) -> &mut Self;
fn update_on<M, C, T, R>(&mut self, triggers: T, reactor: C) -> &mut Self
where
T: ReactionTriggerBundle,
R: CobwebResult,
C: IntoSystem<TargetId, R, M> + Send + Sync + 'static;
fn update_text(&mut self, text: impl Into<String>) -> &mut Self;
fn modify(&mut self, callback: impl FnMut(EntityCommands) + Send + Sync + 'static) -> &mut Self;
}
impl UiBuilderReactExt for UiBuilder<'_, Entity>
{
fn add_reactor<T: EntityWorldReactor>(&mut self, data: T::Local)
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.add_world_reactor::<T>(data);
}
}
fn insert_reactive<T: ReactComponent>(&mut self, component: T) -> &mut Self
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.insert_reactive(component);
}
self
}
fn react(&mut self) -> ReactCommands<'_, '_>
{
self.commands().react()
}
fn on_event<T: Send + Sync + 'static>(&mut self) -> OnEventExt<'_, T>
{
let id = self.id();
OnEventExt::new(self.commands().reborrow(), id)
}
fn despawn_on_event<T: Send + Sync + 'static>(&mut self) -> &mut Self
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.despawn_on_event::<T>();
}
self
}
fn despawn_on_broadcast<T: Send + Sync + 'static>(&mut self) -> &mut Self
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.despawn_on_broadcast::<T>();
}
self
}
fn reactor<M, C, T, R>(&mut self, triggers: T, reactor: C) -> &mut Self
where
T: ReactionTriggerBundle,
R: CobwebResult,
C: IntoSystem<TargetId, R, M> + Send + Sync + 'static,
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.reactor(triggers, reactor);
}
self
}
fn update<M, R: CobwebResult, C: IntoSystem<TargetId, R, M> + Send + Sync + 'static>(
&mut self,
reactor: C,
) -> &mut Self
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.update_on((), reactor);
}
self
}
fn update_on<M, C, T, R>(&mut self, triggers: T, reactor: C) -> &mut Self
where
T: ReactionTriggerBundle,
R: CobwebResult,
C: IntoSystem<TargetId, R, M> + Send + Sync + 'static,
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.update_on(triggers, reactor);
}
self
}
fn update_text(&mut self, text: impl Into<String>) -> &mut Self
{
let text = text.into();
self.update(move |id: TargetId, mut e: TextEditor| {
write_text!(e, id, "{}", text.as_str());
})
}
fn modify(&mut self, callback: impl FnMut(EntityCommands) + Send + Sync + 'static) -> &mut Self
{
let id = self.id();
if let Ok(mut emut) = self.commands().get_entity(id) {
emut.modify(callback);
}
self
}
}