use bevy::prelude::*;
use crate::{
core::container::{Container, WithObservers},
widgets::{
containers::{
button::EasyButtonContainer,
horizontal_layout::EasyHorizontalLayoutContainer,
rich_text::EasyRichTextContainer,
vertical_layout::EasyVerticalLayoutContainer,
},
image::EasyImageBuilder,
label::EasyLabelBuilder,
span::EasySpanBuilder,
text::EasyTextBuilder,
text_input::EasyTextInputBuilder,
},
};
pub enum EasyElement {
ButtonContainer(EasyButtonContainer),
RichTextContainer(EasyRichTextContainer),
VerticalContainer(EasyVerticalLayoutContainer),
HorizontalContainer(EasyHorizontalLayoutContainer),
Image(EasyImageBuilder),
Text(EasyTextBuilder),
Label(EasyLabelBuilder),
Span(EasySpanBuilder),
TextInput(EasyTextInputBuilder),
}
impl From<EasyButtonContainer> for EasyElement {
fn from(b: EasyButtonContainer) -> Self {
EasyElement::ButtonContainer(b)
}
}
impl From<EasyRichTextContainer> for EasyElement {
fn from(t: EasyRichTextContainer) -> Self {
EasyElement::RichTextContainer(t)
}
}
impl From<EasyVerticalLayoutContainer> for EasyElement {
fn from(c: EasyVerticalLayoutContainer) -> Self {
EasyElement::VerticalContainer(c)
}
}
impl From<EasyHorizontalLayoutContainer> for EasyElement {
fn from(c: EasyHorizontalLayoutContainer) -> Self {
EasyElement::HorizontalContainer(c)
}
}
impl From<EasyImageBuilder> for EasyElement {
fn from(i: EasyImageBuilder) -> Self {
EasyElement::Image(i)
}
}
impl From<EasyTextBuilder> for EasyElement {
fn from(t: EasyTextBuilder) -> Self {
EasyElement::Text(t)
}
}
impl From<EasyLabelBuilder> for EasyElement {
fn from(l: EasyLabelBuilder) -> Self {
EasyElement::Label(l)
}
}
impl From<EasySpanBuilder> for EasyElement {
fn from(s: EasySpanBuilder) -> Self {
EasyElement::Span(s)
}
}
impl From<EasyTextInputBuilder> for EasyElement {
fn from(t: EasyTextInputBuilder) -> Self {
EasyElement::TextInput(t)
}
}
impl EasyElement {
pub fn spawn_in(self, p: &mut ChildSpawnerCommands) {
match self {
EasyElement::ButtonContainer(c) => spawn_container(c, p),
EasyElement::RichTextContainer(c) => spawn_container_special(c, p),
EasyElement::VerticalContainer(c) => spawn_container(c, p),
EasyElement::HorizontalContainer(c) => spawn_container(c, p),
EasyElement::Image(i) => spawn(i, p),
EasyElement::Text(t) => spawn(t, p),
EasyElement::Label(l) => spawn(l, p),
EasyElement::Span(s) => spawn(s, p),
EasyElement::TextInput(t) => spawn(t, p),
}
}
}
fn spawn(mut e: impl WithObservers<EasyElement>, p: &mut ChildSpawnerCommands) {
let entity = p.spawn(e.take_bundle()).id();
for observer in e.take_observers() {
p.commands().spawn(observer.with_entity(entity));
}
}
fn spawn_container(
mut c: impl Container<EasyElement>,
p: &mut ChildSpawnerCommands,
) {
let entity = p.spawn(c.take_bundle()).id();
let kids = c.take_children();
p.commands().entity(entity).with_children(|sub| {
for child in kids {
child.spawn_in(sub);
}
});
for observer in c.take_observers() {
p.commands().spawn(observer.with_entity(entity));
}
}
fn spawn_container_special(
mut t: EasyRichTextContainer,
p: &mut ChildSpawnerCommands,
) {
let entity = p.spawn(t.take_bundle()).id();
let kids = t.take_children();
for observer in t.take_observers() {
p.commands().spawn(observer.with_entity(entity));
}
p.commands().entity(entity).with_children(|sub| {
for child in kids {
spawn(child, sub);
}
});
}