bevy_easy_ui 0.1.1

A declarative, fluent builder-pattern abstraction layer on top of Bevy's UI system and Bevy UI Text Input.
Documentation
use bevy::{
  ecs::{
    bundle::Bundle, event::Event, observer::Observer,
    system::IntoObserverSystem,
  },
  prelude::*,
};

use crate::core::{
  container::WithObservers,
  image_node::EasyImageNode,
  node::EasyNode,
  style::{
    box_style::EasyBoxStyle, box_style::EasyBoxStyleExt,
    stack_style::EasyStackStyle, stack_style::EasyStackStyleExt,
  },
};

//>--------------------- STRUCTURES ---------------------

#[derive(Bundle, Debug)]
pub struct EasyImage {
  pub image_node: ImageNode,
  pub node: Node,
  pub box_style: EasyBoxStyle,
  pub stack_style: EasyStackStyle,
}

pub struct EasyImageBuilder {
  bundle: EasyImage,
  observers: Vec<Observer>,
}

#[derive(Default, Debug)]
pub struct EasyImageStyle {
  pub node: Node,
  pub box_style: EasyBoxStyle,
  pub stack_style: EasyStackStyle,
}

//>--------------------- ACCESSOR IMPLS ---------------------

impl WithObservers for EasyImageBuilder {
  fn take_bundle(&mut self) -> impl Bundle {
    std::mem::replace(&mut self.bundle, EasyImage::default_bundle())
  }
  fn take_observers(&mut self) -> Vec<Observer> {
    std::mem::take(&mut self.observers)
  }
}

impl EasyStackStyleExt for EasyImageBuilder {
  fn easy_stack_style_mut(&mut self) -> &mut EasyStackStyle {
    &mut self.bundle.stack_style
  }
}

impl EasyBoxStyleExt for EasyImageBuilder {
  fn easy_style_mut(&mut self) -> &mut EasyBoxStyle {
    &mut self.bundle.box_style
  }
}

impl EasyImageNode for EasyImageBuilder {
  fn node_mut(&mut self) -> &mut ImageNode {
    &mut self.bundle.image_node
  }
}

impl EasyNode for EasyImageBuilder {
  fn node_mut(&mut self) -> &mut Node {
    &mut self.bundle.node
  }
}

//>--------------------- BUILDER API ---------------------

impl EasyImage {
  #[allow(clippy::new_ret_no_self)]
  pub fn new() -> EasyImageBuilder {
    EasyImageBuilder {
      bundle: EasyImage::default_bundle(),
      observers: Vec::new(),
    }
  }

  pub fn default_bundle() -> Self {
    EasyImage {
      image_node: ImageNode::default(),
      node: Node::default(),
      box_style: EasyBoxStyle::default(),
      stack_style: EasyStackStyle::default(),
    }
  }
}

impl EasyImageBuilder {
  pub fn with_style(mut self, style: EasyImageStyle) -> Self {
    self.bundle.node = style.node;
    self.bundle.box_style = style.box_style;
    self.bundle.stack_style = style.stack_style;
    self
  }

  pub fn with_observer<E, ObsB, M>(
    mut self,
    observer: impl IntoObserverSystem<E, ObsB, M> + 'static,
  ) -> Self
  where
    E: Event,
    ObsB: Bundle,
  {
    self.observers.push(Observer::new(observer));
    self
  }
}