ui/src/guihandler/elements.rs
2023-01-16 10:53:52 +01:00

175 lines
5.1 KiB
Rust

use std::{slice::Iter, sync::Arc};
use crate::prelude::*;
#[derive(Default)]
pub struct Elements {
framables: Vec<Arc<Framable>>,
hoverables: Vec<Arc<Hoverable>>,
clickables: Vec<Arc<Clickable>>,
displayables: Vec<Arc<Displayable>>,
selectables: Vec<Arc<Selectable>>,
textables: Vec<Arc<Textable>>,
writeables: Vec<Arc<Writeable>>,
iconizables: Vec<Arc<Iconizable>>,
colorables: Vec<Arc<Colorable>>,
}
impl Elements {
#[inline]
fn push_element<T>(vector: &mut Vec<Arc<T>>, element: Arc<T>) {
if cfg!(debug_assertions) && vector.iter().any(|e| Arc::ptr_eq(e, &element)) {
panic!("element is pushed twice, actually an error");
}
vector.push(element);
}
#[inline]
fn erase_element<T>(vector: &mut Vec<Arc<T>>, element: &Arc<T>) -> bool {
match vector.iter().position(|t| Arc::ptr_eq(t, element)) {
Some(i) => {
vector.remove(i);
true
}
None => false,
}
}
// pub fn is_empty(&self) -> bool {
// self.framables.is_empty()
// && self.hoverables.is_empty()
// && self.clickables.is_empty()
// && self.displayables.is_empty()
// && self.selectables.is_empty()
// && self.textables.is_empty()
// && self.writeables.is_empty()
// && self.iconizables.is_empty()
// && self.colorables.is_empty()
// }
// framable
pub fn add_framable(&mut self, framable: Arc<Framable>) {
Self::push_element(&mut self.framables, framable);
}
pub fn delete_framable(&mut self, framable: &Arc<Framable>) -> bool {
Self::erase_element(&mut self.framables, framable)
}
pub fn iter_framables(&self) -> Iter<'_, Arc<Framable>> {
self.framables.iter()
}
// hoverable
pub fn add_hoverable(&mut self, hoverable: Arc<Hoverable>) {
Self::push_element(&mut self.hoverables, hoverable);
}
pub fn delete_hoverable(&mut self, hoverable: &Arc<Hoverable>) -> bool {
Self::erase_element(&mut self.hoverables, hoverable)
}
pub fn iter_hoverables(&self) -> Iter<'_, Arc<Hoverable>> {
self.hoverables.iter()
}
// selectable
pub fn add_selectable(&mut self, selectable: Arc<Selectable>) {
Self::push_element(&mut self.selectables, selectable);
}
pub fn delete_selectable(&mut self, selectable: &Arc<Selectable>) -> bool {
Self::erase_element(&mut self.selectables, selectable)
}
// displayable
pub fn add_displayable(&mut self, displayable: Arc<Displayable>) {
Self::push_element(&mut self.displayables, displayable);
}
pub fn delete_displayable(&mut self, displayable: &Arc<Displayable>) -> bool {
Self::erase_element(&mut self.displayables, displayable)
}
pub fn iter_displayables(&self) -> Iter<'_, Arc<Displayable>> {
self.displayables.iter()
}
pub fn is_displayables_empty(&self) -> bool {
self.displayables.is_empty()
}
// clickable
pub fn add_clickable(&mut self, clickable: Arc<Clickable>) {
Self::push_element(&mut self.clickables, clickable);
}
pub fn delete_clickable(&mut self, clickable: &Arc<Clickable>) -> bool {
Self::erase_element(&mut self.clickables, clickable)
}
pub fn iter_clickables(&self) -> Iter<'_, Arc<Clickable>> {
self.clickables.iter()
}
// textable
pub fn add_textable(&mut self, textable: Arc<Textable>) {
Self::push_element(&mut self.textables, textable);
}
pub fn delete_textable(&mut self, textable: &Arc<Textable>) -> bool {
Self::erase_element(&mut self.textables, textable)
}
pub fn iter_textables(&self) -> Iter<'_, Arc<Textable>> {
self.textables.iter()
}
pub fn is_textables_empty(&self) -> bool {
self.textables.is_empty()
}
// writable
pub fn add_writeable(&mut self, writeable: Arc<Writeable>) {
Self::push_element(&mut self.writeables, writeable);
}
pub fn delete_writeable(&mut self, writeable: &Arc<Writeable>) -> bool {
Self::erase_element(&mut self.writeables, writeable)
}
// iconizable
pub fn add_iconizable(&mut self, iconizable: Arc<Iconizable>) {
Self::push_element(&mut self.iconizables, iconizable);
}
pub fn delete_iconizable(&mut self, iconizable: &Arc<Iconizable>) -> bool {
Self::erase_element(&mut self.iconizables, iconizable)
}
pub fn iter_iconizables(&self) -> Iter<'_, Arc<Iconizable>> {
self.iconizables.iter()
}
pub fn is_iconizables_empty(&self) -> bool {
self.iconizables.is_empty()
}
// colorable
pub fn add_colorable(&mut self, colorable: Arc<Colorable>) {
Self::push_element(&mut self.colorables, colorable);
}
pub fn delete_colorable(&mut self, colorable: &Arc<Colorable>) -> bool {
Self::erase_element(&mut self.colorables, colorable)
}
pub fn iter_colorables(&self) -> Iter<'_, Arc<Colorable>> {
self.colorables.iter()
}
pub fn is_colorables_empty(&self) -> bool {
self.colorables.is_empty()
}
}