Revert from lifetimes

This commit is contained in:
Michael Hübner 2025-03-04 12:25:02 +01:00
parent ac733ecdc7
commit 2a77856cb8
26 changed files with 403 additions and 446 deletions

View file

@ -116,7 +116,7 @@ impl ButtonBuilder {
self self
} }
pub fn build<'a>(self, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<Button<'a>>> { pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<Button>> {
let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?; let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?;
let normal = FillType::new( let normal = FillType::new(
@ -256,10 +256,10 @@ pub enum ButtonSelectMode {
Bigger, Bigger,
} }
pub struct Button<'a> { pub struct Button {
clickable: Arc<Clickable>, clickable: Arc<Clickable>,
hoverable: Arc<Hoverable>, hoverable: Arc<Hoverable>,
selectable: Arc<Selectable<'a>>, selectable: Arc<Selectable>,
framable: Arc<Framable>, framable: Arc<Framable>,
iconizable: IconizableWrapper, iconizable: IconizableWrapper,
textable: TextableWrapper, textable: TextableWrapper,
@ -271,7 +271,7 @@ pub struct Button<'a> {
_hover_sound: Option<Arc<Audible>>, _hover_sound: Option<Arc<Audible>>,
click_executable: Arc<Executable<()>>, click_executable: Arc<Executable<()>>,
select_executable: Arc<Executable<(&'a mut GuiHandler<'a>, bool)>>, select_executable: Arc<Executable<bool>>,
on_select_executable: Arc<Executable<bool>>, on_select_executable: Arc<Executable<bool>>,
normal: FillType, normal: FillType,
@ -283,7 +283,7 @@ pub struct Button<'a> {
visible: AtomicBool, visible: AtomicBool,
} }
impl<'a> Button<'a> { impl Button {
pub fn builder() -> ButtonBuilder { pub fn builder() -> ButtonBuilder {
ButtonBuilder { ButtonBuilder {
icon: None, icon: None,
@ -308,7 +308,7 @@ impl<'a> Button<'a> {
} }
} }
pub fn select(&self, gui_handler: &mut GuiHandler<'a>) -> Result<()> { pub fn select(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.selectable.select(gui_handler) self.selectable.select(gui_handler)
} }
@ -316,14 +316,15 @@ impl<'a> Button<'a> {
where where
F: Fn() -> Result<()> + Send + Sync + 'static, F: Fn() -> Result<()> + Send + Sync + 'static,
{ {
self.click_executable.set_callback(move |_| callback()); self.click_executable.set_callback(move |_, _| callback());
} }
pub fn set_select_callback<F>(&self, callback: F) pub fn set_select_callback<F>(&self, callback: F)
where where
F: Fn(bool) -> Result<()> + Send + Sync + 'static, F: Fn(bool) -> Result<()> + Send + Sync + 'static,
{ {
self.on_select_executable.set_callback(callback); self.on_select_executable
.set_callback(|_m, select| callback(select));
} }
pub fn set_custom_callback<F>(&self, callback: F) pub fn set_custom_callback<F>(&self, callback: F)
@ -333,19 +334,19 @@ impl<'a> Button<'a> {
self.selectable.set_custom_callback(callback); self.selectable.set_custom_callback(callback);
} }
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
self.textable.set_text(gui_handler, text, self.visible()) self.textable.set_text(gui_handler, text, self.visible())
} }
pub fn set_icon(&self, gui_handler: &mut GuiHandler<'_>, icon: &Arc<Image>) -> Result<()> { pub fn set_icon(&self, gui_handler: &mut GuiHandler, icon: &Arc<Image>) -> Result<()> {
self.iconizable.set_icon(gui_handler, icon, self.visible()) self.iconizable.set_icon(gui_handler, icon, self.visible())
} }
pub fn set_icon_margon(&self, gui_handler: &mut GuiHandler<'_>, margin: u32) -> Result<()> { pub fn set_icon_margon(&self, gui_handler: &mut GuiHandler, margin: u32) -> Result<()> {
self.iconizable.set_margin(gui_handler, margin) self.iconizable.set_margin(gui_handler, margin)
} }
pub fn clear_icon(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn clear_icon(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.iconizable.clear_icon(gui_handler, self.visible()) self.iconizable.clear_icon(gui_handler, self.visible())
} }
@ -353,15 +354,11 @@ impl<'a> Button<'a> {
self.iconizable.icon() self.iconizable.icon()
} }
pub fn set_info_icon( pub fn set_info_icon(&self, gui_handler: &mut GuiHandler, button: &Arc<Image>) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
button: &Arc<Image>,
) -> Result<()> {
self.info_icon.set_icon(gui_handler, button, self.visible()) self.info_icon.set_icon(gui_handler, button, self.visible())
} }
pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.info_icon.clear_icon(gui_handler, self.visible()) self.info_icon.clear_icon(gui_handler, self.visible())
} }
@ -369,18 +366,11 @@ impl<'a> Button<'a> {
self.textable.text() self.textable.text()
} }
pub fn set_text_color( pub fn set_text_color(&self, gui_handler: &mut GuiHandler, text_color: Color) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
text_color: Color,
) -> Result<()> {
self.textable.set_text_color(gui_handler, text_color) self.textable.set_text_color(gui_handler, text_color)
} }
pub fn try_from( pub fn try_from(gui_handler: &mut GuiHandler, button_info: &ButtonInfo) -> Result<Arc<Self>> {
gui_handler: &mut GuiHandler<'_>,
button_info: &ButtonInfo,
) -> Result<Arc<Self>> {
let mut button_builder = Button::builder() let mut button_builder = Button::builder()
.set_select_mode(button_info.select_mode) .set_select_mode(button_info.select_mode)
.set_isolate(button_info.isolate); .set_isolate(button_info.isolate);
@ -437,8 +427,8 @@ impl<'a> Button<'a> {
} }
} }
impl<'a> GuiElementTraits<'a> for Button<'a> { impl GuiElementTraits for Button {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -446,17 +436,17 @@ impl<'a> GuiElementTraits<'a> for Button<'a> {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::Button(self)) Some(GuiElement::Button(self))
} }
} }
impl<'a> Visibility for Button<'a> { impl Visibility for Button {
fn visible(&self) -> bool { fn visible(&self) -> bool {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
if visibility != self.visible.load(SeqCst) { if visibility != self.visible.load(SeqCst) {
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
@ -483,10 +473,10 @@ impl<'a> Visibility for Button<'a> {
} }
} }
impl<'a> Gridable<'a> for Button<'a> { impl Gridable for Button {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -510,7 +500,7 @@ impl<'a> Gridable<'a> for Button<'a> {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
Some(&self.selectable) Some(&self.selectable)
} }
@ -539,7 +529,7 @@ impl<'a> Gridable<'a> for Button<'a> {
} }
// private // private
impl<'a> Button<'a> { impl Button {
// fn create_hovered_changed_callback(button: Arc<Button>) -> Result<()> { // fn create_hovered_changed_callback(button: Arc<Button>) -> Result<()> {
// let button_weak = Arc::downgrade(&button); // let button_weak = Arc::downgrade(&button);
@ -562,7 +552,7 @@ impl<'a> Button<'a> {
// .set_hovered_changed_callback(Some(hovered_changed)) // .set_hovered_changed_callback(Some(hovered_changed))
// } // }
fn create_clicked_changed_callback(button: Arc<Button<'a>>) { fn create_clicked_changed_callback(button: Arc<Button>) {
let button_weak = Arc::downgrade(&button); let button_weak = Arc::downgrade(&button);
let clicked_changed = Box::new(move |gui_handler| { let clicked_changed = Box::new(move |gui_handler| {
@ -584,7 +574,7 @@ impl<'a> Button<'a> {
.set_clicked_changed_callback(Some(clicked_changed)); .set_clicked_changed_callback(Some(clicked_changed));
} }
fn create_selected_changed_callback(button: Arc<Button<'a>>) { fn create_selected_changed_callback(button: Arc<Button>) {
let button_weak = Arc::downgrade(&button); let button_weak = Arc::downgrade(&button);
let selected_changed = move |(gui_handler, selected)| { let selected_changed = move |(gui_handler, selected)| {
@ -602,7 +592,7 @@ impl<'a> Button<'a> {
button.select_executable.set_callback(selected_changed); button.select_executable.set_callback(selected_changed);
} }
fn modify_hovered_vbo(&self, gui_handler: &GuiHandler<'_>) -> Result<()> { fn modify_hovered_vbo(&self, gui_handler: &GuiHandler) -> Result<()> {
assert!( assert!(
self.framable.is_framed(), self.framable.is_framed(),
"button frame needs to be defined before hovering can be calculated" "button frame needs to be defined before hovering can be calculated"
@ -657,7 +647,7 @@ impl<'a> Button<'a> {
Ok(()) Ok(())
} }
fn disable_base(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn disable_base(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.delete(gui_handler)?; self.framable.delete(gui_handler)?;
self.selectable.delete(gui_handler)?; self.selectable.delete(gui_handler)?;
@ -677,7 +667,7 @@ impl<'a> Button<'a> {
#[inline] #[inline]
fn set_button_state( fn set_button_state(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
button_state: ButtonState, button_state: ButtonState,
) -> Result<()> { ) -> Result<()> {
let mut current_button_state = self.button_state.lock().unwrap(); let mut current_button_state = self.button_state.lock().unwrap();
@ -709,8 +699,8 @@ impl<'a> Button<'a> {
} }
} }
impl<'a> Deref for Button<'a> { impl Deref for Button {
type Target = Arc<Selectable<'a>>; type Target = Arc<Selectable>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.selectable &self.selectable

View file

@ -61,7 +61,7 @@ pub(crate) enum InnerFillType {
impl InnerFillType { impl InnerFillType {
pub(crate) fn new( pub(crate) fn new(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
framable: Arc<Framable>, framable: Arc<Framable>,
fill_type_info: FillTypeInfo, fill_type_info: FillTypeInfo,
) -> Result<Self> { ) -> Result<Self> {
@ -84,7 +84,7 @@ impl InnerFillType {
} }
} }
pub(crate) fn enable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
match self { match self {
Self::Image(displayable) => displayable.add(gui_handler)?, Self::Image(displayable) => displayable.add(gui_handler)?,
Self::Color(colorable) => colorable.add(gui_handler)?, Self::Color(colorable) => colorable.add(gui_handler)?,
@ -93,7 +93,7 @@ impl InnerFillType {
Ok(()) Ok(())
} }
pub(crate) fn disable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
match self { match self {
Self::Image(displayable) => displayable.delete(gui_handler)?, Self::Image(displayable) => displayable.delete(gui_handler)?,
Self::Color(colorable) => colorable.delete(gui_handler)?, Self::Color(colorable) => colorable.delete(gui_handler)?,
@ -102,7 +102,7 @@ impl InnerFillType {
Ok(()) Ok(())
} }
pub(crate) fn update_frame(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn update_frame(&self, gui_handler: &mut GuiHandler) -> Result<()> {
match self { match self {
Self::Image(displayable) => displayable.update_frame(gui_handler)?, Self::Image(displayable) => displayable.update_frame(gui_handler)?,
Self::Color(colorable) => colorable.update_frame(gui_handler)?, Self::Color(colorable) => colorable.update_frame(gui_handler)?,
@ -128,7 +128,7 @@ pub(crate) struct FillType {
impl FillType { impl FillType {
pub(crate) fn new( pub(crate) fn new(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
framable: Arc<Framable>, framable: Arc<Framable>,
fill_type_info: FillTypeInfo, fill_type_info: FillTypeInfo,
) -> Result<Self> { ) -> Result<Self> {
@ -142,7 +142,7 @@ impl FillType {
Ok(me) Ok(me)
} }
pub(crate) fn enable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if !self.visible.load(SeqCst) { if !self.visible.load(SeqCst) {
self.visible.store(true, SeqCst); self.visible.store(true, SeqCst);
self.inner.enable(gui_handler)?; self.inner.enable(gui_handler)?;
@ -151,7 +151,7 @@ impl FillType {
Ok(()) Ok(())
} }
pub(crate) fn disable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if self.visible.load(SeqCst) { if self.visible.load(SeqCst) {
self.visible.store(false, SeqCst); self.visible.store(false, SeqCst);
self.inner.disable(gui_handler)?; self.inner.disable(gui_handler)?;
@ -160,7 +160,7 @@ impl FillType {
Ok(()) Ok(())
} }
pub(crate) fn update_frame(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn update_frame(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.inner.update_frame(gui_handler) self.inner.update_frame(gui_handler)
} }

View file

@ -19,21 +19,21 @@ pub enum ConnectDirection {
} }
#[derive(Clone)] #[derive(Clone)]
enum ChildState<'a> { enum ChildState {
Some { Some {
child: Arc<dyn GuiElementTraits<'a>>, child: Arc<dyn GuiElementTraits>,
x: u32, x: u32,
y: u32, y: u32,
}, },
Extend { Extend {
weak_child: Weak<dyn GuiElementTraits<'a>>, weak_child: Weak<dyn GuiElementTraits>,
x: u32, x: u32,
y: u32, y: u32,
}, },
None, None,
} }
impl<'a> ChildState<'a> { impl ChildState {
fn downgrade(&self) -> Self { fn downgrade(&self) -> Self {
match self { match self {
Self::Some { child, x, y } => Self::Extend { Self::Some { child, x, y } => Self::Extend {
@ -62,7 +62,7 @@ impl<'a> ChildState<'a> {
} }
} }
fn get_some(&self) -> Option<(&Arc<dyn GuiElementTraits<'a>>, u32, u32)> { fn get_some(&self) -> Option<(&Arc<dyn GuiElementTraits>, u32, u32)> {
match self { match self {
ChildState::Some { child, x, y } => Some((child, *x, *y)), ChildState::Some { child, x, y } => Some((child, *x, *y)),
ChildState::Extend { .. } => None, ChildState::Extend { .. } => None,
@ -70,7 +70,7 @@ impl<'a> ChildState<'a> {
} }
} }
fn get(&self) -> Option<(Arc<dyn GuiElementTraits<'a>>, u32, u32)> { fn get(&self) -> Option<(Arc<dyn GuiElementTraits>, u32, u32)> {
match self { match self {
ChildState::Some { child, x, y } => Some((child.clone(), *x, *y)), ChildState::Some { child, x, y } => Some((child.clone(), *x, *y)),
ChildState::Extend { weak_child, x, y } => { ChildState::Extend { weak_child, x, y } => {
@ -82,7 +82,7 @@ impl<'a> ChildState<'a> {
fn map<C, R>(&self, f: C) -> Option<R> fn map<C, R>(&self, f: C) -> Option<R>
where where
C: FnOnce(&Arc<dyn GuiElementTraits<'a>>) -> R, C: FnOnce(&Arc<dyn GuiElementTraits>) -> R,
{ {
match self { match self {
ChildState::Some { child, .. } => Some(f(child)), ChildState::Some { child, .. } => Some(f(child)),
@ -92,11 +92,11 @@ impl<'a> ChildState<'a> {
} }
} }
pub struct Grid<'a> { pub struct Grid {
pub(crate) framable: Arc<Framable>, pub(crate) framable: Arc<Framable>,
background: RwLock<Option<FillType>>, background: RwLock<Option<FillType>>,
children: RwLock<Vec<Vec<ChildState<'a>>>>, children: RwLock<Vec<Vec<ChildState>>>,
dim_x: usize, dim_x: usize,
dim_y: usize, dim_y: usize,
@ -107,9 +107,9 @@ pub struct Grid<'a> {
visible: AtomicBool, visible: AtomicBool,
} }
impl<'a> Grid<'a> { impl Grid {
pub fn new( pub fn new(
gui_handler: &GuiHandler<'a>, gui_handler: &GuiHandler,
dim_x: usize, dim_x: usize,
dim_y: usize, dim_y: usize,
top_level: bool, top_level: bool,
@ -129,20 +129,20 @@ impl<'a> Grid<'a> {
visible: AtomicBool::new(false), visible: AtomicBool::new(false),
}); });
if top_level { // if top_level {
let weak_grid = Arc::downgrade(&grid); // let weak_grid = Arc::downgrade(&grid);
grid.framable.add_callback( // grid.framable.add_callback(
weak_grid.clone(), // weak_grid.clone(),
Box::new(move |gui_handler| { // Box::new(move |gui_handler| {
if let Some(grid) = weak_grid.upgrade() { // if let Some(grid) = weak_grid.upgrade() {
grid.calculate_child_positions(gui_handler)?; // grid.calculate_child_positions(gui_handler)?;
} // }
Ok(()) // Ok(())
}), // }),
); // );
} // }
Ok(grid) Ok(grid)
} }
@ -153,7 +153,7 @@ impl<'a> Grid<'a> {
pub fn set_background( pub fn set_background(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
background: impl Into<FillTypeInfo>, background: impl Into<FillTypeInfo>,
) -> Result<()> { ) -> Result<()> {
super::set_background( super::set_background(
@ -177,7 +177,7 @@ impl<'a> Grid<'a> {
self.padding.store(padding, SeqCst); self.padding.store(padding, SeqCst);
} }
pub fn child_at(&self, x: usize, y: usize) -> Result<Option<Arc<dyn GuiElementTraits<'a>>>> { pub fn child_at(&self, x: usize, y: usize) -> Result<Option<Arc<dyn GuiElementTraits>>> {
if x >= self.dim_x { if x >= self.dim_x {
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(
"Tried to access Grid at {} while only being {} wide", "Tried to access Grid at {} while only being {} wide",
@ -199,10 +199,10 @@ impl<'a> Grid<'a> {
pub fn detach( pub fn detach(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
pos_x: usize, pos_x: usize,
pos_y: usize, pos_y: usize,
) -> Result<Option<Arc<dyn GuiElementTraits<'a>>>> { ) -> Result<Option<Arc<dyn GuiElementTraits>>> {
if cfg!(debug_assertions) { if cfg!(debug_assertions) {
if pos_x >= self.dim_x as usize { if pos_x >= self.dim_x as usize {
panic!( panic!(
@ -244,8 +244,8 @@ impl<'a> Grid<'a> {
/// Returns `true` if item got detached /// Returns `true` if item got detached
pub fn detach_item( pub fn detach_item(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
item: Arc<dyn GuiElementTraits<'a>>, item: Arc<dyn GuiElementTraits>,
) -> Result<bool> { ) -> Result<bool> {
let mut grid = self.children.write().unwrap(); let mut grid = self.children.write().unwrap();
let mut removed = false; let mut removed = false;
@ -288,7 +288,7 @@ impl<'a> Grid<'a> {
Ok(removed) Ok(removed)
} }
pub fn set_position(&self, gui_handler: &mut GuiHandler<'_>, x: i32, y: i32) -> Result<()> { pub fn set_position(&self, gui_handler: &mut GuiHandler, x: i32, y: i32) -> Result<()> {
// update own position // update own position
self.framable.change_position(gui_handler, x, y)?; self.framable.change_position(gui_handler, x, y)?;
@ -301,7 +301,7 @@ impl<'a> Grid<'a> {
Ok(()) Ok(())
} }
fn calculate_child_positions(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn calculate_child_positions(&self, gui_handler: &mut GuiHandler) -> Result<()> {
// recalculate positions of the children // recalculate positions of the children
let children = self.children.read().unwrap(); let children = self.children.read().unwrap();
@ -329,18 +329,18 @@ impl<'a> Grid<'a> {
(width, height) (width, height)
} }
pub fn disallow_position_scale(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn disallow_position_scale(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.allow_position_scale(gui_handler, false) self.framable.allow_position_scale(gui_handler, false)
} }
pub fn disallow_size_scale(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn disallow_size_scale(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.allow_size_scale(gui_handler, false) self.framable.allow_size_scale(gui_handler, false)
} }
pub fn connect( pub fn connect(
&self, &self,
direction: ConnectDirection, direction: ConnectDirection,
elements: impl IntoIterator<Item = (usize, &'a Arc<Selectable<'a>>)>, elements: impl IntoIterator<Item = (usize, &Arc<Selectable>)>,
) -> Result<()> { ) -> Result<()> {
for (index, selectable) in elements { for (index, selectable) in elements {
match direction { match direction {
@ -408,8 +408,8 @@ impl<'a> Grid<'a> {
pub fn attach( pub fn attach(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
child: Arc<dyn GuiElementTraits<'a>>, child: Arc<dyn GuiElementTraits>,
pos_x: usize, pos_x: usize,
pos_y: usize, pos_y: usize,
dim_x: u32, dim_x: u32,
@ -500,7 +500,7 @@ impl<'a> Grid<'a> {
pub fn try_from( pub fn try_from(
grid_info: &GridInfo, grid_info: &GridInfo,
gui_handler: &mut GuiHandler<'a>, gui_handler: &mut GuiHandler,
top_level: bool, top_level: bool,
) -> Result<Arc<Self>> { ) -> Result<Arc<Self>> {
let grid = Grid::new( let grid = Grid::new(
@ -522,7 +522,7 @@ impl<'a> Grid<'a> {
pub fn change_position_unscaled( pub fn change_position_unscaled(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
) -> Result<()> { ) -> Result<()> {
@ -539,8 +539,8 @@ impl<'a> Grid<'a> {
fn child_position( fn child_position(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
child: &dyn Gridable<'_>, child: &dyn Gridable,
pos_x: usize, pos_x: usize,
pos_y: usize, pos_y: usize,
dim_x: u32, dim_x: u32,
@ -605,7 +605,7 @@ impl<'a> Grid<'a> {
Ok(()) Ok(())
} }
pub fn select(&self, gui_handler: &mut GuiHandler<'_>, x: u32, y: u32) -> Result<()> { pub fn select(&self, gui_handler: &mut GuiHandler, x: u32, y: u32) -> Result<()> {
match self.children.read().unwrap()[x as usize][y as usize].get_some() { match self.children.read().unwrap()[x as usize][y as usize].get_some() {
Some((child, ..)) => match child.gridable() { Some((child, ..)) => match child.gridable() {
Some(gridable) => match gridable.selectable() { Some(gridable) => match gridable.selectable() {
@ -622,7 +622,7 @@ impl<'a> Grid<'a> {
} }
} }
fn disable_tree(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn disable_tree(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.delete(gui_handler)?; self.framable.delete(gui_handler)?;
if let Some(background) = self.background.read().unwrap().as_ref() { if let Some(background) = self.background.read().unwrap().as_ref() {
@ -634,7 +634,7 @@ impl<'a> Grid<'a> {
Ok(()) Ok(())
} }
fn set_tree_visibility(&self, gui_handler: &mut GuiHandler<'_>, visible: bool) -> Result<()> { fn set_tree_visibility(&self, gui_handler: &mut GuiHandler, visible: bool) -> Result<()> {
let tree = self.children.read().unwrap(); let tree = self.children.read().unwrap();
for row in tree.deref() { for row in tree.deref() {
@ -652,8 +652,8 @@ impl<'a> Grid<'a> {
#[inline] #[inline]
fn set_neighbours( fn set_neighbours(
grid: &Vec<Vec<ChildState<'_>>>, grid: &Vec<Vec<ChildState>>,
current_child: &Arc<Selectable<'a>>, current_child: &Arc<Selectable>,
pos: (usize, usize), pos: (usize, usize),
dim: (usize, usize), dim: (usize, usize),
) -> Result<()> { ) -> Result<()> {
@ -674,8 +674,8 @@ impl<'a> Grid<'a> {
#[inline] #[inline]
fn set_north_neighbour( fn set_north_neighbour(
grid: &Vec<Vec<ChildState<'_>>>, grid: &Vec<Vec<ChildState>>,
current_child: &Arc<Selectable<'a>>, current_child: &Arc<Selectable>,
pos: (usize, usize), pos: (usize, usize),
dim: (usize, usize), dim: (usize, usize),
) -> Result<bool> { ) -> Result<bool> {
@ -692,8 +692,8 @@ impl<'a> Grid<'a> {
#[inline] #[inline]
fn set_south_neighbour( fn set_south_neighbour(
grid: &Vec<Vec<ChildState<'_>>>, grid: &Vec<Vec<ChildState>>,
current_child: &Arc<Selectable<'a>>, current_child: &Arc<Selectable>,
pos: (usize, usize), pos: (usize, usize),
dim: (usize, usize), dim: (usize, usize),
) -> Result<bool> { ) -> Result<bool> {
@ -710,8 +710,8 @@ impl<'a> Grid<'a> {
#[inline] #[inline]
fn set_east_neighbour( fn set_east_neighbour(
grid: &Vec<Vec<ChildState<'_>>>, grid: &Vec<Vec<ChildState>>,
current_child: &Arc<Selectable<'a>>, current_child: &Arc<Selectable>,
pos: (usize, usize), pos: (usize, usize),
dim: (usize, usize), dim: (usize, usize),
) -> Result<bool> { ) -> Result<bool> {
@ -728,8 +728,8 @@ impl<'a> Grid<'a> {
#[inline] #[inline]
fn set_west_neighbour( fn set_west_neighbour(
grid: &Vec<Vec<ChildState<'_>>>, grid: &Vec<Vec<ChildState>>,
current_child: &Arc<Selectable<'a>>, current_child: &Arc<Selectable>,
pos: (usize, usize), pos: (usize, usize),
dim: (usize, usize), dim: (usize, usize),
) -> Result<bool> { ) -> Result<bool> {
@ -746,11 +746,11 @@ impl<'a> Grid<'a> {
#[inline] #[inline]
fn search_neighbour_in_direction( fn search_neighbour_in_direction(
grid: &Vec<Vec<ChildState<'_>>>, grid: &Vec<Vec<ChildState>>,
pos: (usize, usize), pos: (usize, usize),
dir: (i32, i32), dir: (i32, i32),
dim: (usize, usize), dim: (usize, usize),
) -> Option<Arc<Selectable<'a>>> { ) -> Option<Arc<Selectable>> {
let (mut x, mut y) = pos; let (mut x, mut y) = pos;
let (x_step, y_step) = dir; let (x_step, y_step) = dir;
let (dim_x, dim_y) = dim; let (dim_x, dim_y) = dim;
@ -774,8 +774,8 @@ impl<'a> Grid<'a> {
} }
} }
impl<'a> GuiElementTraits<'a> for Grid<'a> { impl GuiElementTraits for Grid {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -783,17 +783,17 @@ impl<'a> GuiElementTraits<'a> for Grid<'a> {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::Grid(self)) Some(GuiElement::Grid(self))
} }
} }
impl<'a> Visibility for Grid<'a> { impl Visibility for Grid {
fn visible(&self) -> bool { fn visible(&self) -> bool {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
if visibility != self.visible.load(SeqCst) { if visibility != self.visible.load(SeqCst) {
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
@ -814,10 +814,10 @@ impl<'a> Visibility for Grid<'a> {
} }
} }
impl<'a> Gridable<'a> for Grid<'a> { impl Gridable for Grid {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -837,7 +837,7 @@ impl<'a> Gridable<'a> for Grid<'a> {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
None None
} }

View file

@ -65,7 +65,7 @@ impl IconBuilder {
self self
} }
pub fn build(self, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<Icon>> { pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<Icon>> {
let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?; let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?;
let iconizable_wrapper = IconizableWrapper::new( let iconizable_wrapper = IconizableWrapper::new(
@ -141,32 +141,32 @@ impl Icon {
} }
} }
pub fn clear_icon(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn clear_icon(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.iconizable_wrapper self.iconizable_wrapper
.clear_icon(gui_handler, self.visible()) .clear_icon(gui_handler, self.visible())
} }
pub fn set_icon(&self, gui_handler: &mut GuiHandler<'_>, icon: &Arc<Image>) -> Result<()> { pub fn set_icon(&self, gui_handler: &mut GuiHandler, icon: &Arc<Image>) -> Result<()> {
self.iconizable_wrapper self.iconizable_wrapper
.set_icon(gui_handler, icon, self.visible()) .set_icon(gui_handler, icon, self.visible())
} }
pub fn set_icon_from_path( pub fn set_icon_from_path(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
asset_path: AssetPath, asset_path: AssetPath,
) -> Result<()> { ) -> Result<()> {
self.iconizable_wrapper self.iconizable_wrapper
.set_icon(gui_handler, asset_path, self.visible()) .set_icon(gui_handler, asset_path, self.visible())
} }
pub fn set_margin(&self, gui_handler: &mut GuiHandler<'_>, margin: u32) -> Result<()> { pub fn set_margin(&self, gui_handler: &mut GuiHandler, margin: u32) -> Result<()> {
self.iconizable_wrapper.set_margin(gui_handler, margin) self.iconizable_wrapper.set_margin(gui_handler, margin)
} }
pub fn set_background( pub fn set_background(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
background: impl Into<FillTypeInfo>, background: impl Into<FillTypeInfo>,
) -> Result<()> { ) -> Result<()> {
super::set_background( super::set_background(
@ -182,33 +182,29 @@ impl Icon {
*self.background.write().unwrap() = None; *self.background.write().unwrap() = None;
} }
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
self.textable_wrapper self.textable_wrapper
.set_text(gui_handler, text, self.visible()) .set_text(gui_handler, text, self.visible())
} }
pub fn clear_text(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn clear_text(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.textable_wrapper self.textable_wrapper
.set_text(gui_handler, "", self.visible()) .set_text(gui_handler, "", self.visible())
} }
pub fn set_text_color(&self, gui_handler: &mut GuiHandler<'_>, color: Color) -> Result<()> { pub fn set_text_color(&self, gui_handler: &mut GuiHandler, color: Color) -> Result<()> {
self.textable_wrapper.set_text_color(gui_handler, color) self.textable_wrapper.set_text_color(gui_handler, color)
} }
pub fn set_info_icon( pub fn set_info_icon(&self, gui_handler: &mut GuiHandler, button: &Arc<Image>) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
button: &Arc<Image>,
) -> Result<()> {
self.info_icon.set_icon(gui_handler, button, self.visible()) self.info_icon.set_icon(gui_handler, button, self.visible())
} }
pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.info_icon.clear_icon(gui_handler, self.visible()) self.info_icon.clear_icon(gui_handler, self.visible())
} }
pub fn try_from(icon_info: &IconInfo, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<Self>> { pub fn try_from(icon_info: &IconInfo, gui_handler: &mut GuiHandler) -> Result<Arc<Self>> {
let mut icon_builder = Icon::builder().set_text_color(icon_info.text_color); let mut icon_builder = Icon::builder().set_text_color(icon_info.text_color);
if let Some(text_ratio) = icon_info.text_ratio { if let Some(text_ratio) = icon_info.text_ratio {
@ -250,7 +246,7 @@ impl Icon {
) )
} }
fn disable_base(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn disable_base(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.delete(gui_handler)?; self.framable.delete(gui_handler)?;
self.iconizable_wrapper.disable(gui_handler)?; self.iconizable_wrapper.disable(gui_handler)?;
@ -265,8 +261,8 @@ impl Icon {
} }
} }
impl<'a> GuiElementTraits<'a> for Icon { impl GuiElementTraits for Icon {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -274,7 +270,7 @@ impl<'a> GuiElementTraits<'a> for Icon {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::Icon(self)) Some(GuiElement::Icon(self))
} }
} }
@ -284,7 +280,7 @@ impl Visibility for Icon {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
if visibility != self.visible.load(SeqCst) { if visibility != self.visible.load(SeqCst) {
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
@ -307,10 +303,10 @@ impl Visibility for Icon {
} }
} }
impl<'a> Gridable<'a> for Icon { impl Gridable for Icon {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -332,7 +328,7 @@ impl<'a> Gridable<'a> for Icon {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
None None
} }

View file

@ -57,7 +57,7 @@ impl LabelBuilder {
self self
} }
pub fn build(self, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<Label>> { pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<Label>> {
let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?; let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?;
let textable_wrapper = TextableWrapper::new( let textable_wrapper = TextableWrapper::new(
@ -121,7 +121,7 @@ impl Label {
} }
} }
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
self.textable_wrapper self.textable_wrapper
.set_text(gui_handler, text, self.visible()) .set_text(gui_handler, text, self.visible())
} }
@ -130,24 +130,20 @@ impl Label {
self.textable_wrapper.text() self.textable_wrapper.text()
} }
pub fn set_text_color( pub fn set_text_color(&self, gui_handler: &mut GuiHandler, text_color: Color) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
text_color: Color,
) -> Result<()> {
self.textable_wrapper self.textable_wrapper
.set_text_color(gui_handler, text_color) .set_text_color(gui_handler, text_color)
} }
pub fn set_alignment( pub fn set_alignment(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
alignment: TextAlignment, alignment: TextAlignment,
) -> Result<()> { ) -> Result<()> {
self.textable_wrapper.set_alignment(gui_handler, alignment) self.textable_wrapper.set_alignment(gui_handler, alignment)
} }
pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler<'_>, ratio: f32) -> Result<()> { pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler, ratio: f32) -> Result<()> {
self.textable_wrapper.set_height_ratio(gui_handler, ratio) self.textable_wrapper.set_height_ratio(gui_handler, ratio)
} }
@ -155,21 +151,17 @@ impl Label {
self.textable_wrapper.height_ratio() self.textable_wrapper.height_ratio()
} }
pub fn set_info_icon( pub fn set_info_icon(&self, gui_handler: &mut GuiHandler, button: &Arc<Image>) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
button: &Arc<Image>,
) -> Result<()> {
self.info_icon.set_icon(gui_handler, button, self.visible()) self.info_icon.set_icon(gui_handler, button, self.visible())
} }
pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn clear_info_icon(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.info_icon.clear_icon(gui_handler, self.visible()) self.info_icon.clear_icon(gui_handler, self.visible())
} }
pub fn set_background( pub fn set_background(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
background: impl Into<FillTypeInfo>, background: impl Into<FillTypeInfo>,
) -> Result<()> { ) -> Result<()> {
super::set_background( super::set_background(
@ -181,7 +173,7 @@ impl Label {
) )
} }
pub fn try_from(label_info: &LabelInfo, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<Self>> { pub fn try_from(label_info: &LabelInfo, gui_handler: &mut GuiHandler) -> Result<Arc<Self>> {
let text = label_info.text.read().unwrap().clone(); let text = label_info.text.read().unwrap().clone();
let color = label_info.text_color; let color = label_info.text_color;
@ -204,7 +196,7 @@ impl Label {
label_builder.build(gui_handler) label_builder.build(gui_handler)
} }
fn disable_base(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn disable_base(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.delete(gui_handler)?; self.framable.delete(gui_handler)?;
self.textable_wrapper.disable(gui_handler)?; self.textable_wrapper.disable(gui_handler)?;
@ -218,8 +210,8 @@ impl Label {
} }
} }
impl<'a> GuiElementTraits<'a> for Label { impl GuiElementTraits for Label {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -227,7 +219,7 @@ impl<'a> GuiElementTraits<'a> for Label {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::Label(self)) Some(GuiElement::Label(self))
} }
} }
@ -237,7 +229,7 @@ impl Visibility for Label {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
@ -259,10 +251,10 @@ impl Visibility for Label {
} }
} }
impl<'a> Gridable<'a> for Label { impl Gridable for Label {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -283,7 +275,7 @@ impl<'a> Gridable<'a> for Label {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
None None
} }

View file

@ -22,7 +22,7 @@ pub mod prelude;
mod wrapper; mod wrapper;
pub(crate) fn set_background( pub(crate) fn set_background(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
visible: bool, visible: bool,
framable: &Arc<Framable>, framable: &Arc<Framable>,
current: &RwLock<Option<FillType>>, current: &RwLock<Option<FillType>>,

View file

@ -56,7 +56,7 @@ impl MultiLineLabelBuilder {
self self
} }
pub fn build<'a>(self, gui_handler: &mut GuiHandler<'a>) -> Result<Arc<MultiLineLabel<'a>>> { pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<MultiLineLabel>> {
let base_grid = Grid::new(gui_handler, 1, self.line_count as usize, false)?; let base_grid = Grid::new(gui_handler, 1, self.line_count as usize, false)?;
base_grid.set_margin(0); base_grid.set_margin(0);
base_grid.set_padding(0); base_grid.set_padding(0);
@ -85,15 +85,15 @@ impl MultiLineLabelBuilder {
} }
} }
pub struct MultiLineLabel<'a> { pub struct MultiLineLabel {
grid: Arc<Grid<'a>>, grid: Arc<Grid>,
characters_per_line: AtomicU32, characters_per_line: AtomicU32,
text: Mutex<String>, text: Mutex<String>,
splits: Mutex<Vec<String>>, splits: Mutex<Vec<String>>,
} }
impl<'a> MultiLineLabel<'a> { impl MultiLineLabel {
pub fn builder() -> MultiLineLabelBuilder { pub fn builder() -> MultiLineLabelBuilder {
MultiLineLabelBuilder { MultiLineLabelBuilder {
text_alignment: TextAlignment::Center, text_alignment: TextAlignment::Center,
@ -107,13 +107,13 @@ impl<'a> MultiLineLabel<'a> {
} }
} }
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
*self.text.lock().unwrap() = text.to_string(); *self.text.lock().unwrap() = text.to_string();
self.update_text(gui_handler) self.update_text(gui_handler)
} }
fn update_text(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn update_text(&self, gui_handler: &mut GuiHandler) -> Result<()> {
let text = self.text.lock().unwrap(); let text = self.text.lock().unwrap();
let splits = text.split(' '); let splits = text.split(' ');
let character_count = self.characters_per_line.load(SeqCst) as usize; let character_count = self.characters_per_line.load(SeqCst) as usize;
@ -158,29 +158,25 @@ impl<'a> MultiLineLabel<'a> {
self.text.lock().unwrap().clone() self.text.lock().unwrap().clone()
} }
pub fn set_text_color( pub fn set_text_color(&self, gui_handler: &mut GuiHandler, text_color: Color) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
text_color: Color,
) -> Result<()> {
self.iter_label(|label, _| label.set_text_color(gui_handler, text_color)) self.iter_label(|label, _| label.set_text_color(gui_handler, text_color))
} }
pub fn set_alignment( pub fn set_alignment(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
alignment: TextAlignment, alignment: TextAlignment,
) -> Result<()> { ) -> Result<()> {
self.iter_label(|label, _| label.set_alignment(gui_handler, alignment)) self.iter_label(|label, _| label.set_alignment(gui_handler, alignment))
} }
pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler<'_>, ratio: f32) -> Result<()> { pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler, ratio: f32) -> Result<()> {
self.iter_label(|label, _| label.set_text_ratio(gui_handler, ratio)) self.iter_label(|label, _| label.set_text_ratio(gui_handler, ratio))
} }
pub fn set_background( pub fn set_background(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
background: impl Into<FillTypeInfo>, background: impl Into<FillTypeInfo>,
) -> Result<()> { ) -> Result<()> {
self.grid.set_background(gui_handler, background) self.grid.set_background(gui_handler, background)
@ -188,7 +184,7 @@ impl<'a> MultiLineLabel<'a> {
pub fn try_from( pub fn try_from(
multi_line_label_info: &MultiLineLabelInfo, multi_line_label_info: &MultiLineLabelInfo,
gui_handler: &mut GuiHandler<'a>, gui_handler: &mut GuiHandler,
) -> Result<Arc<Self>> { ) -> Result<Arc<Self>> {
let text = multi_line_label_info.text.read().unwrap().clone(); let text = multi_line_label_info.text.read().unwrap().clone();
let color = multi_line_label_info.text_color; let color = multi_line_label_info.text_color;
@ -235,8 +231,8 @@ impl<'a> MultiLineLabel<'a> {
} }
} }
impl<'a> GuiElementTraits<'a> for MultiLineLabel<'a> { impl GuiElementTraits for MultiLineLabel {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -244,25 +240,25 @@ impl<'a> GuiElementTraits<'a> for MultiLineLabel<'a> {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::MultiLineLabel(self)) Some(GuiElement::MultiLineLabel(self))
} }
} }
impl<'a> Visibility for MultiLineLabel<'a> { impl Visibility for MultiLineLabel {
fn visible(&self) -> bool { fn visible(&self) -> bool {
self.grid.visible() self.grid.visible()
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
self.grid.set_visibility(gui_handler, visibility) self.grid.set_visibility(gui_handler, visibility)
} }
} }
impl<'a> Gridable<'a> for MultiLineLabel<'a> { impl Gridable for MultiLineLabel {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -299,7 +295,7 @@ impl<'a> Gridable<'a> for MultiLineLabel<'a> {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
None None
} }

View file

@ -62,10 +62,7 @@ impl MultiLineTextFieldBuilder {
self self
} }
pub fn build<'a>( pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<MultiLineTextField>> {
self,
gui_handler: &mut GuiHandler<'a>,
) -> Result<Arc<MultiLineTextField<'a>>> {
let base_grid = Grid::new(gui_handler, 1, self.line_count as usize, false)?; let base_grid = Grid::new(gui_handler, 1, self.line_count as usize, false)?;
base_grid.set_margin(0); base_grid.set_margin(0);
base_grid.set_padding(0); base_grid.set_padding(0);
@ -99,7 +96,7 @@ impl MultiLineTextFieldBuilder {
multi_line_text_field.text_changed_exec.set_callback({ multi_line_text_field.text_changed_exec.set_callback({
let weak_tf = Arc::downgrade(&multi_line_text_field); let weak_tf = Arc::downgrade(&multi_line_text_field);
move |(gui_handler, _text)| { move |gui_handler: &mut GuiHandler, _text| {
if let Some(tf) = weak_tf.upgrade() { if let Some(tf) = weak_tf.upgrade() {
tf.update_text(gui_handler)?; tf.update_text(gui_handler)?;
} }
@ -176,13 +173,13 @@ impl SplittedText {
} }
impl ModifyText for SplittedText { impl ModifyText for SplittedText {
fn set_text(&self, _gui_handler: &mut GuiHandler<'_>, text: String) -> Result<()> { fn set_text(&self, _gui_handler: &mut GuiHandler, text: String) -> Result<()> {
*self.text.lock().unwrap() = text; *self.text.lock().unwrap() = text;
self.update_text() self.update_text()
} }
fn add_letter(&self, _gui_handler: &mut GuiHandler<'_>, letter: char) -> Result<String> { fn add_letter(&self, _gui_handler: &mut GuiHandler, letter: char) -> Result<String> {
*self.text.lock().unwrap() += &format!("{}", letter); *self.text.lock().unwrap() += &format!("{}", letter);
self.update_text()?; self.update_text()?;
@ -190,7 +187,7 @@ impl ModifyText for SplittedText {
Ok(self.text.lock().unwrap().clone()) Ok(self.text.lock().unwrap().clone())
} }
fn remove_last(&self, _gui_handler: &mut GuiHandler<'_>) -> Result<Option<String>> { fn remove_last(&self, _gui_handler: &mut GuiHandler) -> Result<Option<String>> {
self.text.lock().unwrap().pop(); self.text.lock().unwrap().pop();
self.update_text()?; self.update_text()?;
@ -200,15 +197,15 @@ impl ModifyText for SplittedText {
} }
} }
pub struct MultiLineTextField<'a> { pub struct MultiLineTextField {
grid: Arc<Grid<'a>>, grid: Arc<Grid>,
text: Arc<SplittedText>, text: Arc<SplittedText>,
text_changed_exec: Arc<Executable<(&'a mut GuiHandler<'a>, Option<String>)>>, text_changed_exec: Arc<Executable<Option<String>>>,
writeable: Arc<Writeable>, writeable: Arc<Writeable>,
} }
impl<'a> MultiLineTextField<'a> { impl MultiLineTextField {
pub fn builder() -> MultiLineTextFieldBuilder { pub fn builder() -> MultiLineTextFieldBuilder {
MultiLineTextFieldBuilder { MultiLineTextFieldBuilder {
text_alignment: TextAlignment::Center, text_alignment: TextAlignment::Center,
@ -222,11 +219,11 @@ impl<'a> MultiLineTextField<'a> {
} }
} }
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
self.text.set_text(gui_handler, text.to_string()) self.text.set_text(gui_handler, text.to_string())
} }
fn update_text(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn update_text(&self, gui_handler: &mut GuiHandler) -> Result<()> {
let lines = self.text.lines(); let lines = self.text.lines();
self.iter_label(|label, y| { self.iter_label(|label, y| {
@ -240,7 +237,7 @@ impl<'a> MultiLineTextField<'a> {
}) })
} }
fn update_color_change(self: &Arc<Self>) { fn update_color_change(self: &Arc<MultiLineTextField>) {
if let Some(background) = &*self.grid.background() { if let Some(background) = &*self.grid.background() {
if let InnerFillType::Color(colorable) = &background.inner { if let InnerFillType::Color(colorable) = &background.inner {
let normal_color = colorable.color(); let normal_color = colorable.color();
@ -274,29 +271,25 @@ impl<'a> MultiLineTextField<'a> {
self.text.text() self.text.text()
} }
pub fn set_text_color( pub fn set_text_color(&self, gui_handler: &mut GuiHandler, text_color: Color) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
text_color: Color,
) -> Result<()> {
self.iter_label(|label, _| label.set_text_color(gui_handler, text_color)) self.iter_label(|label, _| label.set_text_color(gui_handler, text_color))
} }
pub fn set_alignment( pub fn set_alignment(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
alignment: TextAlignment, alignment: TextAlignment,
) -> Result<()> { ) -> Result<()> {
self.iter_label(|label, _| label.set_alignment(gui_handler, alignment)) self.iter_label(|label, _| label.set_alignment(gui_handler, alignment))
} }
pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler<'_>, ratio: f32) -> Result<()> { pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler, ratio: f32) -> Result<()> {
self.iter_label(|label, _| label.set_text_ratio(gui_handler, ratio)) self.iter_label(|label, _| label.set_text_ratio(gui_handler, ratio))
} }
pub fn set_background( pub fn set_background(
self: &Arc<Self>, self: &Arc<Self>,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
background: impl Into<FillTypeInfo>, background: impl Into<FillTypeInfo>,
) -> Result<()> { ) -> Result<()> {
self.grid.set_background(gui_handler, background)?; self.grid.set_background(gui_handler, background)?;
@ -305,13 +298,13 @@ impl<'a> MultiLineTextField<'a> {
Ok(()) Ok(())
} }
pub fn focus_input(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn focus_input(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.writeable.set_active(gui_handler) self.writeable.set_active(gui_handler)
} }
pub fn try_from( pub fn try_from(
multi_line_text_field_info: &MultiLineTextFieldInfo, multi_line_text_field_info: &MultiLineTextFieldInfo,
gui_handler: &mut GuiHandler<'a>, gui_handler: &mut GuiHandler,
) -> Result<Arc<Self>> { ) -> Result<Arc<Self>> {
let text = multi_line_text_field_info.text.read().unwrap().clone(); let text = multi_line_text_field_info.text.read().unwrap().clone();
let color = multi_line_text_field_info.text_color; let color = multi_line_text_field_info.text_color;
@ -358,8 +351,8 @@ impl<'a> MultiLineTextField<'a> {
} }
} }
impl<'a> GuiElementTraits<'a> for MultiLineTextField<'a> { impl GuiElementTraits for MultiLineTextField {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -367,17 +360,17 @@ impl<'a> GuiElementTraits<'a> for MultiLineTextField<'a> {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::MultiLineTextField(self)) Some(GuiElement::MultiLineTextField(self))
} }
} }
impl<'a> Visibility for MultiLineTextField<'a> { impl Visibility for MultiLineTextField {
fn visible(&self) -> bool { fn visible(&self) -> bool {
self.grid.visible() self.grid.visible()
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
if visibility { if visibility {
self.writeable.add(gui_handler)?; self.writeable.add(gui_handler)?;
@ -390,10 +383,10 @@ impl<'a> Visibility for MultiLineTextField<'a> {
} }
} }
impl<'a> Gridable<'a> for MultiLineTextField<'a> { impl Gridable for MultiLineTextField {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -429,7 +422,7 @@ impl<'a> Gridable<'a> for MultiLineTextField<'a> {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
None None
} }

View file

@ -74,7 +74,7 @@ impl ProgressBarBuilder {
self self
} }
pub fn build(self, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<ProgressBar>> { pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<ProgressBar>> {
let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?; let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?;
let background = self let background = self
@ -140,7 +140,7 @@ impl ProgressBar {
} }
} }
pub fn set_progress(&self, gui_handler: &mut GuiHandler<'_>, mut progress: f32) -> Result<()> { pub fn set_progress(&self, gui_handler: &mut GuiHandler, mut progress: f32) -> Result<()> {
if progress < 0.0 { if progress < 0.0 {
progress = 0.0; progress = 0.0;
} else if progress > 1.0 { } else if progress > 1.0 {
@ -181,27 +181,23 @@ impl ProgressBar {
Ok(()) Ok(())
} }
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
self.textable_wrapper self.textable_wrapper
.set_text(gui_handler, text, self.visible()) .set_text(gui_handler, text, self.visible())
} }
pub fn set_text_color( pub fn set_text_color(&self, gui_handler: &mut GuiHandler, text_color: Color) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
text_color: Color,
) -> Result<()> {
self.textable_wrapper self.textable_wrapper
.set_text_color(gui_handler, text_color) .set_text_color(gui_handler, text_color)
} }
pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler<'_>, ratio: f32) -> Result<()> { pub fn set_text_ratio(&self, gui_handler: &mut GuiHandler, ratio: f32) -> Result<()> {
self.textable_wrapper.set_height_ratio(gui_handler, ratio) self.textable_wrapper.set_height_ratio(gui_handler, ratio)
} }
pub fn set_text_alignment( pub fn set_text_alignment(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
text_alignment: TextAlignment, text_alignment: TextAlignment,
) -> Result<()> { ) -> Result<()> {
self.textable_wrapper self.textable_wrapper
@ -210,7 +206,7 @@ impl ProgressBar {
pub fn try_from( pub fn try_from(
progress_bar_info: &ProgressBarInfo, progress_bar_info: &ProgressBarInfo,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
) -> Result<Arc<Self>> { ) -> Result<Arc<Self>> {
let mut progress_bar_builder = ProgressBar::builder() let mut progress_bar_builder = ProgressBar::builder()
.set_text_alignment(progress_bar_info.text_alignment) .set_text_alignment(progress_bar_info.text_alignment)
@ -246,7 +242,7 @@ impl ProgressBar {
*self.progress.lock().unwrap() *self.progress.lock().unwrap()
} }
fn disable_base(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn disable_base(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.delete(gui_handler)?; self.framable.delete(gui_handler)?;
if let Some(background) = &self.background { if let Some(background) = &self.background {
@ -263,8 +259,8 @@ impl ProgressBar {
} }
} }
impl<'a> GuiElementTraits<'a> for ProgressBar { impl GuiElementTraits for ProgressBar {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -272,7 +268,7 @@ impl<'a> GuiElementTraits<'a> for ProgressBar {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::ProgressBar(self)) Some(GuiElement::ProgressBar(self))
} }
} }
@ -282,7 +278,7 @@ impl Visibility for ProgressBar {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
@ -307,10 +303,10 @@ impl Visibility for ProgressBar {
} }
} }
impl<'a> Gridable<'a> for ProgressBar { impl Gridable for ProgressBar {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -334,7 +330,7 @@ impl<'a> Gridable<'a> for ProgressBar {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
None None
} }

View file

@ -48,7 +48,7 @@ impl TextFieldBuilder {
self self
} }
pub fn build(self, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<TextField>> { pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<TextField>> {
let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?; let framable = Framable::new(gui_handler.width(), gui_handler.height(), false)?;
let background = RwLock::new( let background = RwLock::new(
@ -114,7 +114,7 @@ impl TextField {
} }
} }
pub fn try_from(info: &TextFieldInfo, gui_handler: &mut GuiHandler<'_>) -> Result<Arc<Self>> { pub fn try_from(info: &TextFieldInfo, gui_handler: &mut GuiHandler) -> Result<Arc<Self>> {
let text = info.text.read().unwrap().clone(); let text = info.text.read().unwrap().clone();
let color = info.text_color; let color = info.text_color;
@ -169,20 +169,16 @@ impl TextField {
pub fn set_text_changed_callback<F>(&self, f: F) pub fn set_text_changed_callback<F>(&self, f: F)
where where
F: Fn(Option<String>) -> Result<()> + Send + Sync + 'static, F: Fn(&mut GuiHandler, Option<String>) -> Result<()> + Send + Sync + 'static,
{ {
self.text_changed_executable.set_callback(f); self.text_changed_executable.set_callback(f);
} }
pub fn set_text_color( pub fn set_text_color(&self, gui_handler: &mut GuiHandler, text_color: Color) -> Result<()> {
&self,
gui_handler: &mut GuiHandler<'_>,
text_color: Color,
) -> Result<()> {
self.textable.set_text_color(gui_handler, text_color) self.textable.set_text_color(gui_handler, text_color)
} }
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
self.textable.set_text(gui_handler, text) self.textable.set_text(gui_handler, text)
} }
@ -194,7 +190,7 @@ impl TextField {
pub fn set_background( pub fn set_background(
self: &Arc<Self>, self: &Arc<Self>,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
background: impl Into<FillTypeInfo>, background: impl Into<FillTypeInfo>,
) -> Result<()> { ) -> Result<()> {
super::set_background( super::set_background(
@ -220,19 +216,19 @@ impl TextField {
None None
} }
pub fn add_letter(&self, gui_handler: &mut GuiHandler<'_>, letter: char) -> Result<()> { pub fn add_letter(&self, gui_handler: &mut GuiHandler, letter: char) -> Result<()> {
self.writeable.add_letter(gui_handler, letter) self.writeable.add_letter(gui_handler, letter)
} }
pub fn remove_last(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn remove_last(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.writeable.remove_last(gui_handler) self.writeable.remove_last(gui_handler)
} }
pub fn focus_input(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn focus_input(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.writeable.set_active(gui_handler) self.writeable.set_active(gui_handler)
} }
fn disable_base(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn disable_base(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.framable.delete(gui_handler)?; self.framable.delete(gui_handler)?;
self.textable.delete(gui_handler)?; self.textable.delete(gui_handler)?;
self.writeable.delete(gui_handler)?; self.writeable.delete(gui_handler)?;
@ -245,8 +241,8 @@ impl TextField {
} }
} }
impl<'a> GuiElementTraits<'a> for TextField { impl GuiElementTraits for TextField {
fn gridable(&self) -> Option<&dyn Gridable<'a>> { fn gridable(&self) -> Option<&dyn Gridable> {
Some(self) Some(self)
} }
@ -254,15 +250,15 @@ impl<'a> GuiElementTraits<'a> for TextField {
Some(self) Some(self)
} }
fn downcast(&'a self) -> Option<GuiElement<'a>> { fn downcast<'a>(&'a self) -> Option<GuiElement<'a>> {
Some(GuiElement::TextField(self)) Some(GuiElement::TextField(self))
} }
} }
impl<'a> Gridable<'a> for TextField { impl Gridable for TextField {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -282,7 +278,7 @@ impl<'a> Gridable<'a> for TextField {
Ok(()) Ok(())
} }
fn selectable(&self) -> Option<&Arc<Selectable<'a>>> { fn selectable(&self) -> Option<&Arc<Selectable>> {
None None
} }
@ -314,7 +310,7 @@ impl Visibility for TextField {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()> { fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);

View file

@ -4,10 +4,10 @@ use anyhow::Result;
use std::{any::Any, collections::HashMap, sync::Arc}; use std::{any::Any, collections::HashMap, sync::Arc};
// simulate trait upcasting // simulate trait upcasting
pub trait TopLevelGui<'a>: Send + Sync { pub trait TopLevelGui: Send + Sync {
fn gui_traits(&self) -> &dyn GuiElementTraits<'a>; fn gui_traits(&self) -> &dyn GuiElementTraits;
fn top_gui(&self) -> Option<&dyn TopGui>; fn top_gui(&self) -> Option<&dyn TopGui>;
fn elements(&self) -> Option<&HashMap<String, UiElement<'a>>>; fn elements(&self) -> Option<&HashMap<String, UiElement>>;
fn functionality(&self) -> Option<&dyn Functionality>; fn functionality(&self) -> Option<&dyn Functionality>;
fn enable(&self) -> Result<()>; fn enable(&self) -> Result<()>;
fn disable(&self) -> Result<()>; fn disable(&self) -> Result<()>;
@ -45,16 +45,16 @@ pub trait Functionality {
) -> Result<()>; ) -> Result<()>;
} }
pub trait GuiElementTraits<'a>: Send + Sync { pub trait GuiElementTraits: Send + Sync {
fn gridable(&self) -> Option<&dyn Gridable<'a>>; fn gridable(&self) -> Option<&dyn Gridable>;
fn visibility(&self) -> Option<&dyn Visibility>; fn visibility(&self) -> Option<&dyn Visibility>;
fn downcast(&'a self) -> Option<GuiElement<'a>>; fn downcast<'a>(&'a self) -> Option<GuiElement<'a>>;
} }
pub trait Gridable<'a> { pub trait Gridable {
fn set_frame( fn set_frame(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
w: u32, w: u32,
@ -63,7 +63,7 @@ pub trait Gridable<'a> {
hori_align: HorizontalAlign, hori_align: HorizontalAlign,
) -> Result<()>; ) -> Result<()>;
fn selectable(&self) -> Option<&Arc<Selectable<'a>>>; fn selectable(&self) -> Option<&Arc<Selectable>>;
fn type_name(&self) -> &str; fn type_name(&self) -> &str;
@ -74,15 +74,15 @@ pub trait Gridable<'a> {
pub trait Visibility { pub trait Visibility {
fn visible(&self) -> bool; fn visible(&self) -> bool;
fn set_visibility(&self, gui_handler: &mut GuiHandler<'_>, visibility: bool) -> Result<()>; fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()>;
} }
pub enum GuiElement<'a> { pub enum GuiElement<'a> {
Button(&'a Button<'a>), Button(&'a Button),
Grid(&'a Grid<'a>), Grid(&'a Grid),
Label(&'a Label), Label(&'a Label),
MultiLineLabel(&'a MultiLineLabel<'a>), MultiLineLabel(&'a MultiLineLabel),
MultiLineTextField(&'a MultiLineTextField<'a>), MultiLineTextField(&'a MultiLineTextField),
TextField(&'a TextField), TextField(&'a TextField),
Icon(&'a Icon), Icon(&'a Icon),
ProgressBar(&'a ProgressBar), ProgressBar(&'a ProgressBar),
@ -92,14 +92,14 @@ pub enum GuiElement<'a> {
} }
impl<'a> GuiElement<'a> { impl<'a> GuiElement<'a> {
pub fn button(&self) -> Option<&Button<'a>> { pub fn button(&self) -> Option<&Button> {
match self { match self {
Self::Button(button) => Some(button), Self::Button(button) => Some(button),
_ => None, _ => None,
} }
} }
pub fn grid(&self) -> Option<&Grid<'a>> { pub fn grid(&self) -> Option<&Grid> {
match self { match self {
Self::Grid(grid) => Some(grid), Self::Grid(grid) => Some(grid),
_ => None, _ => None,
@ -113,14 +113,14 @@ impl<'a> GuiElement<'a> {
} }
} }
pub fn multi_line_label(&self) -> Option<&MultiLineLabel<'a>> { pub fn multi_line_label(&self) -> Option<&MultiLineLabel> {
match self { match self {
Self::MultiLineLabel(multi_line_label) => Some(multi_line_label), Self::MultiLineLabel(multi_line_label) => Some(multi_line_label),
_ => None, _ => None,
} }
} }
pub fn multi_line_text_field(&self) -> Option<&MultiLineTextField<'a>> { pub fn multi_line_text_field(&self) -> Option<&MultiLineTextField> {
match self { match self {
Self::MultiLineTextField(multi_line_text_field) => Some(multi_line_text_field), Self::MultiLineTextField(multi_line_text_field) => Some(multi_line_text_field),
_ => None, _ => None,

View file

@ -5,19 +5,19 @@ use std::collections::HashMap;
use std::fmt::{Debug, Formatter}; use std::fmt::{Debug, Formatter};
use std::sync::{Arc, Weak}; use std::sync::{Arc, Weak};
pub enum UiElement<'a> { pub enum UiElement {
Button(Weak<Button<'a>>), Button(Weak<Button>),
Grid(Weak<Grid<'a>>), Grid(Weak<Grid>),
Label(Weak<Label>), Label(Weak<Label>),
MultiLineLabel(Weak<MultiLineLabel<'a>>), MultiLineLabel(Weak<MultiLineLabel>),
MultiLineTextField(Weak<MultiLineTextField<'a>>), MultiLineTextField(Weak<MultiLineTextField>),
TextField(Weak<TextField>), TextField(Weak<TextField>),
Icon(Weak<Icon>), Icon(Weak<Icon>),
ProgressBar(Weak<ProgressBar>), ProgressBar(Weak<ProgressBar>),
GuiSnippet(Weak<GuiSnippet>), GuiSnippet(Weak<GuiSnippet>),
} }
impl<'a> Debug for UiElement<'a> { impl Debug for UiElement {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self { match self {
UiElement::Button(_) => write!(f, "Button"), UiElement::Button(_) => write!(f, "Button"),
@ -35,7 +35,7 @@ impl<'a> Debug for UiElement<'a> {
macro_rules! impl_from { macro_rules! impl_from {
($data_type:ident $(< $($lt:lifetime),+ >)? ) => { ($data_type:ident $(< $($lt:lifetime),+ >)? ) => {
impl<'a> From<&Arc<$data_type$(<$($lt,)+>)?>> for UiElement<'a> { impl<'a> From<&Arc<$data_type$(<$($lt,)+>)?>> for UiElement {
fn from(v: &Arc<$data_type$(<$($lt,)+>)?>) -> Self { fn from(v: &Arc<$data_type$(<$($lt,)+>)?>) -> Self {
UiElement::$data_type(Arc::downgrade(v)) UiElement::$data_type(Arc::downgrade(v))
} }
@ -43,11 +43,11 @@ macro_rules! impl_from {
}; };
} }
impl_from!(Button<'a>); impl_from!(Button);
impl_from!(Grid<'a>); impl_from!(Grid);
impl_from!(Label); impl_from!(Label);
impl_from!(MultiLineLabel<'a>); impl_from!(MultiLineLabel);
impl_from!(MultiLineTextField<'a>); impl_from!(MultiLineTextField);
impl_from!(TextField); impl_from!(TextField);
impl_from!(Icon); impl_from!(Icon);
impl_from!(ProgressBar); impl_from!(ProgressBar);
@ -59,7 +59,7 @@ pub trait GetElement<T> {
macro_rules! impl_element { macro_rules! impl_element {
($data_type:ident $(< $($lt:lifetime),+ >)? ) => { ($data_type:ident $(< $($lt:lifetime),+ >)? ) => {
impl<'a> GetElement<$data_type$(<$($lt,)+>)?> for HashMap<String, UiElement<'a>> { impl<'a> GetElement<$data_type$(<$($lt,)+>)?> for HashMap<String, UiElement> {
fn element(&self, id: &str) -> Result<Arc<$data_type$(<$($lt,)+>)?>> { fn element(&self, id: &str) -> Result<Arc<$data_type$(<$($lt,)+>)?>> {
match self.get(id) { match self.get(id) {
Some(element) => match element { Some(element) => match element {
@ -83,11 +83,11 @@ macro_rules! impl_element {
}; };
} }
impl_element!(Button<'a>); impl_element!(Button);
impl_element!(Grid<'a>); impl_element!(Grid);
impl_element!(Label); impl_element!(Label);
impl_element!(MultiLineLabel<'a>); impl_element!(MultiLineLabel);
impl_element!(MultiLineTextField<'a>); impl_element!(MultiLineTextField);
impl_element!(TextField); impl_element!(TextField);
impl_element!(Icon); impl_element!(Icon);
impl_element!(ProgressBar); impl_element!(ProgressBar);

View file

@ -50,7 +50,7 @@ impl TextableWrapper {
pub(crate) fn set_text_color( pub(crate) fn set_text_color(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
text_color: Color, text_color: Color,
) -> Result<()> { ) -> Result<()> {
*self.color.lock().unwrap() = text_color; *self.color.lock().unwrap() = text_color;
@ -64,7 +64,7 @@ impl TextableWrapper {
pub(crate) fn set_text( pub(crate) fn set_text(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
text: impl ToString, text: impl ToString,
is_visible: bool, is_visible: bool,
) -> Result<()> { ) -> Result<()> {
@ -125,7 +125,7 @@ impl TextableWrapper {
pub(crate) fn set_height_ratio( pub(crate) fn set_height_ratio(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
height_ratio: f32, height_ratio: f32,
) -> Result<()> { ) -> Result<()> {
*self.ratio.lock().unwrap() = height_ratio; *self.ratio.lock().unwrap() = height_ratio;
@ -143,7 +143,7 @@ impl TextableWrapper {
pub(crate) fn set_alignment( pub(crate) fn set_alignment(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
alignment: TextAlignment, alignment: TextAlignment,
) -> Result<()> { ) -> Result<()> {
*self.alignment.lock().unwrap() = alignment; *self.alignment.lock().unwrap() = alignment;
@ -155,7 +155,7 @@ impl TextableWrapper {
Ok(()) Ok(())
} }
pub(crate) fn update(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn update(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(textable) = self.textable.lock().unwrap().as_ref() { if let Some(textable) = self.textable.lock().unwrap().as_ref() {
textable.update_text(gui_handler)?; textable.update_text(gui_handler)?;
} }
@ -163,7 +163,7 @@ impl TextableWrapper {
Ok(()) Ok(())
} }
pub(crate) fn enable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(textable) = self.textable.lock().unwrap().as_ref() { if let Some(textable) = self.textable.lock().unwrap().as_ref() {
textable.add(gui_handler)?; textable.add(gui_handler)?;
} }
@ -171,7 +171,7 @@ impl TextableWrapper {
Ok(()) Ok(())
} }
pub(crate) fn disable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(textable) = self.textable.lock().unwrap().as_ref() { if let Some(textable) = self.textable.lock().unwrap().as_ref() {
textable.delete(gui_handler)?; textable.delete(gui_handler)?;
} }
@ -198,7 +198,7 @@ pub(crate) struct IconizableWrapper {
impl IconizableWrapper { impl IconizableWrapper {
pub(crate) fn new( pub(crate) fn new(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
framable: Arc<Framable>, framable: Arc<Framable>,
builder: Option<IconBuilderType>, builder: Option<IconBuilderType>,
positioning: Option<IconizablePositioning>, positioning: Option<IconizablePositioning>,
@ -236,7 +236,7 @@ impl IconizableWrapper {
pub(crate) fn clear_icon( pub(crate) fn clear_icon(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
is_visible: bool, is_visible: bool,
) -> Result<()> { ) -> Result<()> {
let mut iconizable = self.iconizable.lock().unwrap(); let mut iconizable = self.iconizable.lock().unwrap();
@ -256,7 +256,7 @@ impl IconizableWrapper {
pub(crate) fn set_icon( pub(crate) fn set_icon(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
icon_builder: impl Into<IconBuilderType>, icon_builder: impl Into<IconBuilderType>,
is_visible: bool, is_visible: bool,
) -> Result<()> { ) -> Result<()> {
@ -291,7 +291,7 @@ impl IconizableWrapper {
Ok(()) Ok(())
} }
pub(crate) fn set_margin(&self, gui_handler: &mut GuiHandler<'_>, margin: u32) -> Result<()> { pub(crate) fn set_margin(&self, gui_handler: &mut GuiHandler, margin: u32) -> Result<()> {
if self.framable.is_framed() { if self.framable.is_framed() {
if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() { if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() {
iconizable.set_margin(margin)?; iconizable.set_margin(margin)?;
@ -311,7 +311,7 @@ impl IconizableWrapper {
} }
} }
pub(crate) fn enable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn enable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() { if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() {
iconizable.add(gui_handler)?; iconizable.add(gui_handler)?;
} }
@ -319,7 +319,7 @@ impl IconizableWrapper {
Ok(()) Ok(())
} }
pub(crate) fn disable(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn disable(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() { if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() {
iconizable.delete(gui_handler)?; iconizable.delete(gui_handler)?;
} }
@ -327,7 +327,7 @@ impl IconizableWrapper {
Ok(()) Ok(())
} }
pub(crate) fn update_frame(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn update_frame(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() { if let Some(iconizable) = self.iconizable.lock().unwrap().as_ref() {
iconizable.update_frame(gui_handler)?; iconizable.update_frame(gui_handler)?;
} }

View file

@ -3,19 +3,19 @@ use std::{slice::Iter, sync::Arc};
use crate::prelude::*; use crate::prelude::*;
#[derive(Default)] #[derive(Default)]
pub struct Elements<'a> { pub struct Elements {
framables: Vec<Arc<Framable>>, framables: Vec<Arc<Framable>>,
hoverables: Vec<Arc<Hoverable>>, hoverables: Vec<Arc<Hoverable>>,
clickables: Vec<Arc<Clickable>>, clickables: Vec<Arc<Clickable>>,
displayables: Vec<Arc<Displayable>>, displayables: Vec<Arc<Displayable>>,
selectables: Vec<Arc<Selectable<'a>>>, selectables: Vec<Arc<Selectable>>,
textables: Vec<Arc<Textable>>, textables: Vec<Arc<Textable>>,
writeables: Vec<Arc<Writeable>>, writeables: Vec<Arc<Writeable>>,
iconizables: Vec<Arc<Iconizable>>, iconizables: Vec<Arc<Iconizable>>,
colorables: Vec<Arc<Colorable>>, colorables: Vec<Arc<Colorable>>,
} }
impl<'a> Elements<'a> { impl Elements {
#[inline] #[inline]
fn push_element<T>(vector: &mut Vec<Arc<T>>, element: Arc<T>) { 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)) { if cfg!(debug_assertions) && vector.iter().any(|e| Arc::ptr_eq(e, &element)) {
@ -75,11 +75,11 @@ impl<'a> Elements<'a> {
} }
// selectable // selectable
pub fn add_selectable(&mut self, selectable: Arc<Selectable<'a>>) { pub fn add_selectable(&mut self, selectable: Arc<Selectable>) {
Self::push_element(&mut self.selectables, selectable); Self::push_element(&mut self.selectables, selectable);
} }
pub fn delete_selectable(&mut self, selectable: &Arc<Selectable<'a>>) -> bool { pub fn delete_selectable(&mut self, selectable: &Arc<Selectable>) -> bool {
Self::erase_element(&mut self.selectables, selectable) Self::erase_element(&mut self.selectables, selectable)
} }

View file

@ -26,7 +26,7 @@ pub struct Clickable {
executable: Arc<Executable<()>>, executable: Arc<Executable<()>>,
clicked_changed_callback: clicked_changed_callback:
RwLock<Option<Box<dyn Fn(&mut GuiHandler<'_>) -> Result<()> + Send + Sync>>>, RwLock<Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>>,
} }
impl Clickable { impl Clickable {
@ -56,7 +56,7 @@ impl Clickable {
/// # Arguments /// # Arguments
/// ///
/// * `clickable` is a `&Arc<Clickable>` instance that is going to be added /// * `clickable` is a `&Arc<Clickable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_clickable(self.ui_layer.load(SeqCst), self.clone()) gui_handler.add_clickable(self.ui_layer.load(SeqCst), self.clone())
} }
@ -65,7 +65,7 @@ impl Clickable {
/// # Arguments /// # Arguments
/// ///
/// * `clickable` is a `&Arc<Clickable>` instance that is going to be deleted /// * `clickable` is a `&Arc<Clickable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_clickable(self.ui_layer.load(SeqCst), self)?; gui_handler.delete_clickable(self.ui_layer.load(SeqCst), self)?;
self.set_clicked(gui_handler, false)?; self.set_clicked(gui_handler, false)?;
@ -90,9 +90,7 @@ impl Clickable {
/// * `clicked_changed_callback` is a `Option<Box<Callback>>` /// * `clicked_changed_callback` is a `Option<Box<Callback>>`
pub fn set_clicked_changed_callback( pub fn set_clicked_changed_callback(
&self, &self,
clicked_changed_callback: Option< clicked_changed_callback: Option<Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>>,
Box<dyn Fn(&mut GuiHandler<'_>) -> Result<()> + Send + Sync>,
>,
) { ) {
*self.clicked_changed_callback.write().unwrap() = clicked_changed_callback; *self.clicked_changed_callback.write().unwrap() = clicked_changed_callback;
} }
@ -102,7 +100,7 @@ impl Clickable {
*self.audible.write().unwrap() = audible; *self.audible.write().unwrap() = audible;
} }
pub fn set_clicked(&self, gui_handler: &mut GuiHandler<'_>, clicked: bool) -> Result<bool> { pub fn set_clicked(&self, gui_handler: &mut GuiHandler, clicked: bool) -> Result<bool> {
if self.clicked() != clicked { if self.clicked() != clicked {
self.clicked.store(clicked, SeqCst); self.clicked.store(clicked, SeqCst);

View file

@ -34,7 +34,7 @@ pub struct Colorable {
impl Colorable { impl Colorable {
/// Factory method for `Colorable`, returns `Arc<Colorable>`. /// Factory method for `Colorable`, returns `Arc<Colorable>`.
pub fn new( pub fn new(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
framable: Arc<Framable>, framable: Arc<Framable>,
color: Color, color: Color,
fill_type: DisplayableFillType, fill_type: DisplayableFillType,
@ -80,7 +80,7 @@ impl Colorable {
/// # Arguments /// # Arguments
/// ///
/// * `colorable` is a `&Arc<Colorable>` instance that is going to be added /// * `colorable` is a `&Arc<Colorable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_colorable(self.ui_layer.load(SeqCst), self.clone()) gui_handler.add_colorable(self.ui_layer.load(SeqCst), self.clone())
} }
@ -89,7 +89,7 @@ impl Colorable {
/// # Arguments /// # Arguments
/// ///
/// * `colorable` is a `&Arc<Colorable>` instance that is going to be deleted /// * `colorable` is a `&Arc<Colorable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_colorable(self.ui_layer.load(SeqCst), self) gui_handler.delete_colorable(self.ui_layer.load(SeqCst), self)
} }
@ -107,7 +107,7 @@ impl Colorable {
/// # Arguments /// # Arguments
/// ///
/// * `color` defines the color /// * `color` defines the color
pub fn set_color(&self, gui_handler: &mut GuiHandler<'_>, color: Color) -> Result<()> { pub fn set_color(&self, gui_handler: &mut GuiHandler, color: Color) -> Result<()> {
let set = gui_handler.color_descriptor(color)?; let set = gui_handler.color_descriptor(color)?;
*self.color.write().unwrap() = color; *self.color.write().unwrap() = color;
@ -147,7 +147,7 @@ impl Colorable {
} }
/// Update frame method if the original frame is invalidated /// Update frame method if the original frame is invalidated
pub fn update_frame(&self, gui_handler: &GuiHandler<'_>) -> Result<()> { pub fn update_frame(&self, gui_handler: &GuiHandler) -> Result<()> {
let mut frame = self.buffer.map_complete()?; let mut frame = self.buffer.map_complete()?;
let mut x_start = self.framable.left() as f32; let mut x_start = self.framable.left() as f32;

View file

@ -71,7 +71,7 @@ impl Displayable {
/// * `framable` is a `Arc<Framable>` instance /// * `framable` is a `Arc<Framable>` instance
/// * `name` is the name for a png /// * `name` is the name for a png
pub fn new( pub fn new(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
framable: Arc<Framable>, framable: Arc<Framable>,
displayable_type: impl Into<DisplayableType>, displayable_type: impl Into<DisplayableType>,
fill_type: DisplayableFillType, fill_type: DisplayableFillType,
@ -128,7 +128,7 @@ impl Displayable {
/// # Arguments /// # Arguments
/// ///
/// * `displayable` is a `&Arc<Displayable>` instance that is going to be added /// * `displayable` is a `&Arc<Displayable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_displayable(self.ui_layer.load(SeqCst), self.clone()) gui_handler.add_displayable(self.ui_layer.load(SeqCst), self.clone())
} }
@ -137,7 +137,7 @@ impl Displayable {
/// # Arguments /// # Arguments
/// ///
/// * `displayable` is a `&Arc<Displayable>` instance that is going to be deleted /// * `displayable` is a `&Arc<Displayable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_displayable(self.ui_layer.load(SeqCst), self) gui_handler.delete_displayable(self.ui_layer.load(SeqCst), self)
} }
@ -161,7 +161,7 @@ impl Displayable {
/// * `name` is the name of the texture in `gui/` without `.png` suffix /// * `name` is the name of the texture in `gui/` without `.png` suffix
pub fn set_image( pub fn set_image(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
displayable_type: impl Into<DisplayableType>, displayable_type: impl Into<DisplayableType>,
) -> Result<()> { ) -> Result<()> {
let displayable_type = displayable_type.into(); let displayable_type = displayable_type.into();
@ -233,7 +233,7 @@ impl Displayable {
} }
/// Update frame method if the original frame is invalidated /// Update frame method if the original frame is invalidated
pub fn update_frame(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn update_frame(&self, gui_handler: &mut GuiHandler) -> Result<()> {
let mut frame = self.buffer.map_complete()?; let mut frame = self.buffer.map_complete()?;
let mut x_start = self.framable.left() as f32; let mut x_start = self.framable.left() as f32;

View file

@ -7,8 +7,8 @@ use std::sync::{Arc, RwLock};
use crate::prelude::*; use crate::prelude::*;
/// `Executable` holds a closure which can be executed /// `Executable` holds a closure which can be executed
pub struct Executable<I: Send + Sync + 'static> { pub struct Executable<I: Send + Sync> {
callback: RwLock<Option<Arc<dyn Fn(I) -> Result<()> + Send + Sync>>>, callback: RwLock<Option<Arc<dyn Fn(&mut GuiHandler, I) -> Result<()> + Send + Sync>>>,
} }
impl<I: Send + Sync + 'static> Executable<I> { impl<I: Send + Sync + 'static> Executable<I> {
@ -26,7 +26,7 @@ impl<I: Send + Sync + 'static> Executable<I> {
/// * `callback` is a `Option<Callback>` closure /// * `callback` is a `Option<Callback>` closure
pub fn set_callback<F>(&self, callback: impl Into<Option<F>>) pub fn set_callback<F>(&self, callback: impl Into<Option<F>>)
where where
F: Fn(I) -> Result<()> + Send + Sync + 'static, F: Fn(&mut GuiHandler, I) -> Result<()> + Send + Sync + 'static,
{ {
let mut function = self.callback.write().unwrap(); let mut function = self.callback.write().unwrap();
@ -37,11 +37,11 @@ impl<I: Send + Sync + 'static> Executable<I> {
} }
/// Execute the callback closure if possible /// Execute the callback closure if possible
pub fn execute(&self, gui_handler: &mut GuiHandler<'_>, input: I) -> Result<()> { pub fn execute(&self, gui_handler: &mut GuiHandler, input: I) -> Result<()> {
if let Some(callback) = self.callback.read().unwrap().as_ref() { if let Some(callback) = self.callback.read().unwrap().as_ref() {
let callback = callback.clone(); let callback = callback.clone();
gui_handler.add_callback(move || (callback)(input)); gui_handler.add_callback(move |gui_handler| (callback)(gui_handler, input));
} }
Ok(()) Ok(())

View file

@ -86,7 +86,7 @@ pub struct Framable {
resize_callbacks: RwLock< resize_callbacks: RwLock<
Vec<( Vec<(
Handle, Handle,
Box<dyn Fn(&mut GuiHandler<'_>) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>,
)>, )>,
>, >,
@ -149,7 +149,7 @@ impl Framable {
/// # Arguments /// # Arguments
/// ///
/// * `framable` - is a `&Arc<Framable>` instance that is going to be added /// * `framable` - is a `&Arc<Framable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
// check if window size is the same as last time // check if window size is the same as last time
if gui_handler.width() != self.window_width.load(SeqCst) if gui_handler.width() != self.window_width.load(SeqCst)
|| gui_handler.height() != self.window_height.load(SeqCst) || gui_handler.height() != self.window_height.load(SeqCst)
@ -174,7 +174,7 @@ impl Framable {
/// # Arguments /// # Arguments
/// ///
/// * `framable` - is a `&Arc<Framable>` instance that is going to be deleted /// * `framable` - is a `&Arc<Framable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
if self.resize_allowed() { if self.resize_allowed() {
gui_handler.delete_framable(self.ui_layer.load(SeqCst), self)?; gui_handler.delete_framable(self.ui_layer.load(SeqCst), self)?;
} }
@ -192,7 +192,7 @@ impl Framable {
pub(crate) fn set_reference_size( pub(crate) fn set_reference_size(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
width: u32, width: u32,
height: u32, height: u32,
) -> Result<()> { ) -> Result<()> {
@ -209,7 +209,7 @@ impl Framable {
pub(crate) fn allow_position_scale( pub(crate) fn allow_position_scale(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
allowed: bool, allowed: bool,
) -> Result<()> { ) -> Result<()> {
if allowed != self.reference_scale_position.load(SeqCst) { if allowed != self.reference_scale_position.load(SeqCst) {
@ -225,7 +225,7 @@ impl Framable {
pub(crate) fn allow_size_scale( pub(crate) fn allow_size_scale(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
allowed: bool, allowed: bool,
) -> Result<()> { ) -> Result<()> {
if allowed != self.reference_scale_size.load(SeqCst) { if allowed != self.reference_scale_size.load(SeqCst) {
@ -247,7 +247,7 @@ impl Framable {
/// width and a certain alignment /// width and a certain alignment
pub fn set_frame( pub fn set_frame(
&self, &self,
gui_handler: &GuiHandler<'_>, gui_handler: &GuiHandler,
x_off: i32, x_off: i32,
y_off: i32, y_off: i32,
w: u32, w: u32,
@ -311,7 +311,7 @@ impl Framable {
pub fn add_callback( pub fn add_callback(
&self, &self,
handle: impl Into<Handle>, handle: impl Into<Handle>,
callback: Box<dyn Fn(&mut GuiHandler<'_>) -> Result<()> + Send + Sync>, callback: Box<dyn Fn(&mut GuiHandler) -> Result<()> + Send + Sync>,
) { ) {
let handle = handle.into(); let handle = handle.into();
@ -354,7 +354,7 @@ impl Framable {
*self.horizontal_alignment.read().unwrap() *self.horizontal_alignment.read().unwrap()
} }
fn calculate_frame(&self, gui_handler: &GuiHandler<'_>) { fn calculate_frame(&self, gui_handler: &GuiHandler) {
let width = gui_handler.width(); let width = gui_handler.width();
let height = gui_handler.height(); let height = gui_handler.height();
@ -428,7 +428,7 @@ impl Framable {
pub fn change_position( pub fn change_position(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x_offset: i32, x_offset: i32,
y_offset: i32, y_offset: i32,
) -> Result<()> { ) -> Result<()> {
@ -443,7 +443,7 @@ impl Framable {
} }
pub fn change_position_unscaled( pub fn change_position_unscaled(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
x: i32, x: i32,
y: i32, y: i32,
) -> Result<()> { ) -> Result<()> {
@ -469,7 +469,7 @@ impl Framable {
Ok(()) Ok(())
} }
pub fn resize(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn resize(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.calculate_frame(gui_handler); self.calculate_frame(gui_handler);
for (_, callback) in self.resize_callbacks.read().unwrap().iter() { for (_, callback) in self.resize_callbacks.read().unwrap().iter() {

View file

@ -56,7 +56,7 @@ impl Hoverable {
/// # Arguments /// # Arguments
/// ///
/// * `hoverable` is a `&Arc<Hoverable>` instance that is going to be added /// * `hoverable` is a `&Arc<Hoverable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_hoverable(self.ui_layer.load(SeqCst), self.clone())?; gui_handler.add_hoverable(self.ui_layer.load(SeqCst), self.clone())?;
Ok(()) Ok(())
@ -67,7 +67,7 @@ impl Hoverable {
/// # Arguments /// # Arguments
/// ///
/// * `hoverable` is a `&Arc<Hoverable>` instance that is going to be deleted /// * `hoverable` is a `&Arc<Hoverable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_hoverable(self.ui_layer.load(SeqCst), self)?; gui_handler.delete_hoverable(self.ui_layer.load(SeqCst), self)?;
self.set_hovered(gui_handler, false)?; self.set_hovered(gui_handler, false)?;
@ -103,7 +103,7 @@ impl Hoverable {
/// # Arguments /// # Arguments
/// ///
/// * `hovered` is the new hovered state /// * `hovered` is the new hovered state
pub fn set_hovered(&self, gui_handler: &mut GuiHandler<'_>, hovered: bool) -> Result<()> { pub fn set_hovered(&self, gui_handler: &mut GuiHandler, hovered: bool) -> Result<()> {
if self.hovered() != hovered { if self.hovered() != hovered {
self.hovered.store(hovered, SeqCst); self.hovered.store(hovered, SeqCst);

View file

@ -85,7 +85,7 @@ impl Iconizable {
/// * `framable` is a `Arc<Framable>` instance /// * `framable` is a `Arc<Framable>` instance
/// * `icon` is a reference to an `Arc<Image>` /// * `icon` is a reference to an `Arc<Image>`
pub fn new( pub fn new(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
framable: Arc<Framable>, framable: Arc<Framable>,
icon_builder: impl Into<IconBuilderType>, icon_builder: impl Into<IconBuilderType>,
positioning: impl Into<Option<IconizablePositioning>>, positioning: impl Into<Option<IconizablePositioning>>,
@ -137,7 +137,7 @@ impl Iconizable {
/// # Arguments /// # Arguments
/// ///
/// * `iconizable` is a `&Arc<Iconizable>` instance that is going to be added /// * `iconizable` is a `&Arc<Iconizable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_iconizable(self.ui_layer.load(SeqCst), self.clone()) gui_handler.add_iconizable(self.ui_layer.load(SeqCst), self.clone())
} }
@ -146,7 +146,7 @@ impl Iconizable {
/// # Arguments /// # Arguments
/// ///
/// * `iconizable` is a `&Arc<Iconizable>` instance that is going to be deleted /// * `iconizable` is a `&Arc<Iconizable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_iconizable(self.ui_layer.load(SeqCst), self) gui_handler.delete_iconizable(self.ui_layer.load(SeqCst), self)
} }
@ -172,7 +172,7 @@ impl Iconizable {
} }
/// Updates the frame /// Updates the frame
pub fn update_frame(&self, gui_handler: &GuiHandler<'_>) -> Result<()> { pub fn update_frame(&self, gui_handler: &GuiHandler) -> Result<()> {
assert!(self.framable.is_framed(), "framable is not framed yet"); assert!(self.framable.is_framed(), "framable is not framed yet");
// frame parameter // frame parameter
@ -269,7 +269,7 @@ impl Iconizable {
/// * `icon` the new icon /// * `icon` the new icon
pub fn set_icon( pub fn set_icon(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
icon_builder: impl Into<IconBuilderType>, icon_builder: impl Into<IconBuilderType>,
) -> Result<()> { ) -> Result<()> {
let icon = gui_handler.request_icon(icon_builder)?; let icon = gui_handler.request_icon(icon_builder)?;

View file

@ -12,13 +12,13 @@ use std::sync::{
/// `Selectable` gives the ability to navigate per button or controller to /// `Selectable` gives the ability to navigate per button or controller to
/// optionally adjacent neighbour Selectables and to execute a closure /// optionally adjacent neighbour Selectables and to execute a closure
/// when the current Selectable is pressed /// when the current Selectable is pressed
pub struct Selectable<'a> { pub struct Selectable {
selected: AtomicBool, selected: AtomicBool,
east_neighbour: RwLock<Option<Weak<Selectable<'a>>>>, east_neighbour: RwLock<Option<Weak<Selectable>>>,
west_neighbour: RwLock<Option<Weak<Selectable<'a>>>>, west_neighbour: RwLock<Option<Weak<Selectable>>>,
north_neighbour: RwLock<Option<Weak<Selectable<'a>>>>, north_neighbour: RwLock<Option<Weak<Selectable>>>,
south_neighbour: RwLock<Option<Weak<Selectable<'a>>>>, south_neighbour: RwLock<Option<Weak<Selectable>>>,
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
select_audible: RwLock<Option<Arc<Audible>>>, select_audible: RwLock<Option<Arc<Audible>>>,
@ -30,29 +30,28 @@ pub struct Selectable<'a> {
isolate: bool, isolate: bool,
// used when clicked // used when clicked
executable: Arc<Executable<&'a mut World>>, executable: Arc<Executable<()>>,
// used internally by button // used internally by button
selected_changed_executable: Arc<Executable<(&'a mut World, bool)>>, selected_changed_executable: Arc<Executable<bool>>,
// exposed externally for event // exposed externally for event
on_select_executable: Arc<Executable<(&'a mut World, bool)>>, on_select_executable: Arc<Executable<bool>>,
// used for custom buttons // used for custom buttons
custom_callback: custom_callback: RwLock<Option<Box<dyn Fn(ControllerButton) -> Result<bool> + Send + Sync>>>,
RwLock<Option<Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>>>,
} }
impl<'a> Selectable<'a> { impl Selectable {
/// Factory method for `Selectable`, returns `Arc<Selectable>`. /// Factory method for `Selectable`, returns `Arc<Selectable>`.
/// ///
/// # Arguments /// # Arguments
/// ///
/// * `executable` is a `Arc<Executable>` instance /// * `executable` is a `Arc<Executable>` instance
pub fn new( pub fn new(
executable: Arc<Executable<&'a mut World>>, executable: Arc<Executable<()>>,
selected_changed_executable: Arc<Executable<(&'a mut World, bool)>>, selected_changed_executable: Arc<Executable<bool>>,
on_select_executable: Arc<Executable<(&'a mut World, bool)>>, on_select_executable: Arc<Executable<bool>>,
isolate: bool, isolate: bool,
) -> Arc<Self> { ) -> Arc<Self> {
Arc::new(Selectable { Arc::new(Selectable {
@ -85,7 +84,7 @@ impl<'a> Selectable<'a> {
/// # Arguments /// # Arguments
/// ///
/// * `selectable` is a `&Arc<Selectable>` instance that is going to be added /// * `selectable` is a `&Arc<Selectable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_selectable(self.ui_layer.load(SeqCst), self.clone()) gui_handler.add_selectable(self.ui_layer.load(SeqCst), self.clone())
} }
@ -94,7 +93,7 @@ impl<'a> Selectable<'a> {
/// # Arguments /// # Arguments
/// ///
/// * `selectable` is a `&Arc<Selectable>` instance that is going to be deleted /// * `selectable` is a `&Arc<Selectable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_selectable(self.ui_layer.load(SeqCst), self)?; gui_handler.delete_selectable(self.ui_layer.load(SeqCst), self)?;
self.set_selected(gui_handler, false)?; self.set_selected(gui_handler, false)?;
@ -110,7 +109,7 @@ impl<'a> Selectable<'a> {
/// # Argument /// # Argument
/// ///
/// * `selectable` is a `Arc<Selectable>` instance /// * `selectable` is a `Arc<Selectable>` instance
pub fn select(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn select(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.set_selectable(Some(self.clone())) gui_handler.set_selectable(Some(self.clone()))
} }
@ -137,7 +136,7 @@ impl<'a> Selectable<'a> {
/// # Arguments /// # Arguments
/// ///
/// * `selected` is the new selected state /// * `selected` is the new selected state
pub fn set_selected(&self, gui_handler: &mut GuiHandler<'_>, selected: bool) -> Result<()> { pub fn set_selected(&self, gui_handler: &mut GuiHandler, selected: bool) -> Result<()> {
if self.selected() != selected { if self.selected() != selected {
self.selected.store(selected, SeqCst); self.selected.store(selected, SeqCst);
@ -164,7 +163,7 @@ impl<'a> Selectable<'a> {
} }
/// Executes the `Executable`'s callback /// Executes the `Executable`'s callback
pub(crate) fn click_event(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub(crate) fn click_event(&self, gui_handler: &mut GuiHandler) -> Result<()> {
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
{ {
if let Some(audible) = self.click_audible.read().unwrap().as_ref() { if let Some(audible) = self.click_audible.read().unwrap().as_ref() {
@ -195,7 +194,7 @@ impl<'a> Selectable<'a> {
} }
/// Returns the current east neighbour, if possible /// Returns the current east neighbour, if possible
pub fn east_neighbour(&self) -> Option<Arc<Selectable<'a>>> { pub fn east_neighbour(&self) -> Option<Arc<Selectable>> {
if let Some(weak_neighbour) = self.east_neighbour.read().unwrap().as_ref() { if let Some(weak_neighbour) = self.east_neighbour.read().unwrap().as_ref() {
if let Some(neighbour) = weak_neighbour.upgrade() { if let Some(neighbour) = weak_neighbour.upgrade() {
return Some(neighbour); return Some(neighbour);
@ -210,7 +209,7 @@ impl<'a> Selectable<'a> {
/// # Arguments /// # Arguments
/// ///
/// * `selectable` the new east neighbour /// * `selectable` the new east neighbour
pub fn set_east_neighbour(&self, selectable: Option<&Arc<Selectable<'a>>>) { pub fn set_east_neighbour(&self, selectable: Option<&Arc<Selectable>>) {
if self.isolate { if self.isolate {
return; return;
} }
@ -224,7 +223,7 @@ impl<'a> Selectable<'a> {
} }
/// Returns the current west neighbour, if possible /// Returns the current west neighbour, if possible
pub fn west_neighbour(&self) -> Option<Arc<Selectable<'a>>> { pub fn west_neighbour(&self) -> Option<Arc<Selectable>> {
if let Some(weak_neighbour) = self.west_neighbour.read().unwrap().as_ref() { if let Some(weak_neighbour) = self.west_neighbour.read().unwrap().as_ref() {
if let Some(neighbour) = weak_neighbour.upgrade() { if let Some(neighbour) = weak_neighbour.upgrade() {
return Some(neighbour); return Some(neighbour);
@ -239,7 +238,7 @@ impl<'a> Selectable<'a> {
/// # Arguments /// # Arguments
/// ///
/// * `selectable` the new west neighbour /// * `selectable` the new west neighbour
pub fn set_west_neighbour(&self, selectable: Option<&Arc<Selectable<'a>>>) { pub fn set_west_neighbour(&self, selectable: Option<&Arc<Selectable>>) {
if self.isolate { if self.isolate {
return; return;
} }
@ -253,7 +252,7 @@ impl<'a> Selectable<'a> {
} }
/// Returns the current north neighbour, if possible /// Returns the current north neighbour, if possible
pub fn north_neighbour(&self) -> Option<Arc<Selectable<'a>>> { pub fn north_neighbour(&self) -> Option<Arc<Selectable>> {
if let Some(weak_neighbour) = self.north_neighbour.read().unwrap().as_ref() { if let Some(weak_neighbour) = self.north_neighbour.read().unwrap().as_ref() {
if let Some(neighbour) = weak_neighbour.upgrade() { if let Some(neighbour) = weak_neighbour.upgrade() {
return Some(neighbour); return Some(neighbour);
@ -268,7 +267,7 @@ impl<'a> Selectable<'a> {
/// # Argumnents /// # Argumnents
/// ///
/// * `selectable` the new north neighbour /// * `selectable` the new north neighbour
pub fn set_north_neighbour(&self, selectable: Option<&Arc<Selectable<'a>>>) { pub fn set_north_neighbour(&self, selectable: Option<&Arc<Selectable>>) {
if self.isolate { if self.isolate {
return; return;
} }
@ -282,7 +281,7 @@ impl<'a> Selectable<'a> {
} }
/// Returns the current south neighbour, if possible /// Returns the current south neighbour, if possible
pub fn south_neighbour(&self) -> Option<Arc<Selectable<'a>>> { pub fn south_neighbour(&self) -> Option<Arc<Selectable>> {
if let Some(weak_neighbour) = self.south_neighbour.read().unwrap().as_ref() { if let Some(weak_neighbour) = self.south_neighbour.read().unwrap().as_ref() {
if let Some(neighbour) = weak_neighbour.upgrade() { if let Some(neighbour) = weak_neighbour.upgrade() {
return Some(neighbour); return Some(neighbour);
@ -297,7 +296,7 @@ impl<'a> Selectable<'a> {
/// # Arguments /// # Arguments
/// ///
/// * `selectable` the new south neighbour /// * `selectable` the new south neighbour
pub fn set_south_neighbour(&self, selectable: Option<&Arc<Selectable<'a>>>) { pub fn set_south_neighbour(&self, selectable: Option<&Arc<Selectable>>) {
if self.isolate { if self.isolate {
return; return;
} }
@ -310,7 +309,7 @@ impl<'a> Selectable<'a> {
} }
} }
pub fn connect_vertically<'c>(upper: &Arc<Selectable<'c>>, lower: &Arc<Selectable<'c>>) { pub fn connect_vertically<'c>(upper: &Arc<Selectable>, lower: &Arc<Selectable>) {
if upper.isolate || lower.isolate { if upper.isolate || lower.isolate {
return; return;
} }
@ -319,7 +318,7 @@ impl<'a> Selectable<'a> {
*lower.north_neighbour.write().unwrap() = Some(Arc::downgrade(upper)); *lower.north_neighbour.write().unwrap() = Some(Arc::downgrade(upper));
} }
pub fn connect_horizontally<'c>(left: &Arc<Selectable<'c>>, right: &Arc<Selectable<'c>>) { pub fn connect_horizontally<'c>(left: &Arc<Selectable>, right: &Arc<Selectable>) {
if left.isolate || right.isolate { if left.isolate || right.isolate {
return; return;
} }

View file

@ -80,7 +80,7 @@ impl Textable {
/// * `height_ratio` the ratio of the height in respect to the framable height /// * `height_ratio` the ratio of the height in respect to the framable height
/// * `text_alignment` where the text is aligned to /// * `text_alignment` where the text is aligned to
pub fn new( pub fn new(
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
framable: Arc<Framable>, framable: Arc<Framable>,
text: String, text: String,
height_ratio: f32, height_ratio: f32,
@ -143,7 +143,7 @@ impl Textable {
/// # Arguments /// # Arguments
/// ///
/// * `textable` is a `&Arc<Textable>` instance that is going to be added /// * `textable` is a `&Arc<Textable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_textable(self.ui_layer.load(SeqCst), self.clone()) gui_handler.add_textable(self.ui_layer.load(SeqCst), self.clone())
} }
@ -152,7 +152,7 @@ impl Textable {
/// # Arguments /// # Arguments
/// ///
/// * `textable` is a `&Arc<Textable>` instance that is going to be deleted /// * `textable` is a `&Arc<Textable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_textable(self.ui_layer.load(SeqCst), self) gui_handler.delete_textable(self.ui_layer.load(SeqCst), self)
} }
@ -172,7 +172,7 @@ impl Textable {
/// * `text_color` defines the color of the text /// * `text_color` defines the color of the text
pub fn set_text_color( pub fn set_text_color(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
text_color: Color, text_color: Color,
) -> Result<()> { ) -> Result<()> {
let set = gui_handler.color_descriptor(text_color)?; let set = gui_handler.color_descriptor(text_color)?;
@ -189,7 +189,7 @@ impl Textable {
/// * `text_alignment` where the text is aligned to /// * `text_alignment` where the text is aligned to
pub fn set_text_alignment( pub fn set_text_alignment(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
text_alignment: TextAlignment, text_alignment: TextAlignment,
) -> Result<()> { ) -> Result<()> {
*self.text_alignment.write().unwrap() = text_alignment; *self.text_alignment.write().unwrap() = text_alignment;
@ -219,7 +219,7 @@ impl Textable {
/// ///
/// * `text` the text to be displayed /// * `text` the text to be displayed
/// * `height_ratio` the ratio of the height in respect to the framable height /// * `height_ratio` the ratio of the height in respect to the framable height
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: impl ToString) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: impl ToString) -> Result<()> {
let text = text.to_string(); let text = text.to_string();
if text.is_empty() { if text.is_empty() {
@ -250,7 +250,7 @@ impl Textable {
/// # Arguments /// # Arguments
/// ///
/// * `height_ratio` the ratio of the height in respect to the framable height /// * `height_ratio` the ratio of the height in respect to the framable height
pub fn set_size(&self, gui_handler: &mut GuiHandler<'_>, height_ratio: f32) -> Result<()> { pub fn set_size(&self, gui_handler: &mut GuiHandler, height_ratio: f32) -> Result<()> {
*self.height_ratio.write().unwrap() = height_ratio; *self.height_ratio.write().unwrap() = height_ratio;
self.update_text(gui_handler)?; self.update_text(gui_handler)?;
@ -284,7 +284,7 @@ impl Textable {
} }
/// Updates the texts buffer /// Updates the texts buffer
pub fn update_text(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn update_text(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.calculate_text_size(); self.calculate_text_size();
let (x, y) = self.calc_pos_from_alignment(); let (x, y) = self.calc_pos_from_alignment();
@ -327,7 +327,7 @@ impl Textable {
fn create_buffer( fn create_buffer(
&self, &self,
gui_handler: &mut GuiHandler<'_>, gui_handler: &mut GuiHandler,
win_x: f32, win_x: f32,
win_y: f32, win_y: f32,
) -> Result<()> { ) -> Result<()> {
@ -499,7 +499,7 @@ impl Textable {
Ok(()) Ok(())
} }
fn text_changed_through_write(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { fn text_changed_through_write(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.vertex_count self.vertex_count
.store(self.text.read().unwrap().len() as u32 * 6, SeqCst); .store(self.text.read().unwrap().len() as u32 * 6, SeqCst);
self.character_size.store( self.character_size.store(
@ -515,19 +515,19 @@ impl Textable {
} }
impl ModifyText for Textable { impl ModifyText for Textable {
fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: String) -> Result<()> { fn set_text(&self, gui_handler: &mut GuiHandler, text: String) -> Result<()> {
*self.text.write().unwrap() = text; *self.text.write().unwrap() = text;
self.text_changed_through_write(gui_handler) self.text_changed_through_write(gui_handler)
} }
fn add_letter(&self, gui_handler: &mut GuiHandler<'_>, letter: char) -> Result<String> { fn add_letter(&self, gui_handler: &mut GuiHandler, letter: char) -> Result<String> {
self.text.write().unwrap().push(letter); self.text.write().unwrap().push(letter);
self.text_changed_through_write(gui_handler)?; self.text_changed_through_write(gui_handler)?;
Ok(self.text.read().unwrap().clone()) Ok(self.text.read().unwrap().clone())
} }
fn remove_last(&self, gui_handler: &mut GuiHandler<'_>) -> Result<Option<String>> { fn remove_last(&self, gui_handler: &mut GuiHandler) -> Result<Option<String>> {
self.text.write().unwrap().pop(); self.text.write().unwrap().pop();
self.text_changed_through_write(gui_handler)?; self.text_changed_through_write(gui_handler)?;

View file

@ -9,9 +9,9 @@ use std::sync::{
}; };
pub trait ModifyText: Send + Sync { pub trait ModifyText: Send + Sync {
fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: String) -> Result<()>; fn set_text(&self, gui_handler: &mut GuiHandler, text: String) -> Result<()>;
fn add_letter(&self, gui_handler: &mut GuiHandler<'_>, letter: char) -> Result<String>; fn add_letter(&self, gui_handler: &mut GuiHandler, letter: char) -> Result<String>;
fn remove_last(&self, gui_handler: &mut GuiHandler<'_>) -> Result<Option<String>>; fn remove_last(&self, gui_handler: &mut GuiHandler) -> Result<Option<String>>;
} }
/// `Writeable` gives the ability to modify the text inside an `Textable` /// `Writeable` gives the ability to modify the text inside an `Textable`
@ -20,7 +20,7 @@ pub struct Writeable {
text_change_executable: Arc<Executable<Option<String>>>, text_change_executable: Arc<Executable<Option<String>>>,
activation_changed_executable: activation_changed_executable:
RwLock<Option<Box<dyn Fn(&mut GuiHandler<'_>, bool) -> Result<()> + Send + Sync>>>, RwLock<Option<Box<dyn Fn(&mut GuiHandler, bool) -> Result<()> + Send + Sync>>>,
ui_layer: AtomicI32, ui_layer: AtomicI32,
} }
@ -41,19 +41,19 @@ impl Writeable {
})) }))
} }
pub fn set_active(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn set_active(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
if gui_handler.set_active_writeable(self.clone())? { if gui_handler.set_active_writeable(self.clone())? {
if let Some(exec) = &*self.activation_changed_executable.read().unwrap() { if let Some(exec) = &*self.activation_changed_executable.read().unwrap() {
exec(true)?; exec(gui_handler, true)?;
} }
} }
Ok(()) Ok(())
} }
pub(crate) fn inactivation_event(&self) -> Result<()> { pub(crate) fn inactivation_event(&self, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(exec) = &*self.activation_changed_executable.read().unwrap() { if let Some(exec) = &*self.activation_changed_executable.read().unwrap() {
exec(false)?; exec(gui_handler, false)?;
} }
Ok(()) Ok(())
@ -61,7 +61,7 @@ impl Writeable {
pub fn set_activation_changed<F>(&self, f: F) pub fn set_activation_changed<F>(&self, f: F)
where where
F: Fn(&mut GuiHandler<'_>, bool) -> Result<()> + Send + Sync + 'static, F: Fn(&mut GuiHandler, bool) -> Result<()> + Send + Sync + 'static,
{ {
*self.activation_changed_executable.write().unwrap() = Some(Box::new(f)); *self.activation_changed_executable.write().unwrap() = Some(Box::new(f));
} }
@ -71,7 +71,7 @@ impl Writeable {
/// # Arguments /// # Arguments
/// ///
/// * `writeable` is a `&Arc<Writeable>` instance that is going to be added /// * `writeable` is a `&Arc<Writeable>` instance that is going to be added
pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn add(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.add_writeable(self.ui_layer.load(SeqCst), self.clone()) gui_handler.add_writeable(self.ui_layer.load(SeqCst), self.clone())
} }
@ -80,7 +80,7 @@ impl Writeable {
/// # Arguments /// # Arguments
/// ///
/// * `writeable` is a `&Arc<Writeable>` instance that is going to be deleted /// * `writeable` is a `&Arc<Writeable>` instance that is going to be deleted
pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn delete(self: &Arc<Self>, gui_handler: &mut GuiHandler) -> Result<()> {
gui_handler.delete_writeable(self.ui_layer.load(SeqCst), self) gui_handler.delete_writeable(self.ui_layer.load(SeqCst), self)
} }
@ -93,7 +93,7 @@ impl Writeable {
/// # Arguments /// # Arguments
/// ///
/// * `text` that replaces the current text /// * `text` that replaces the current text
pub fn set_text(&self, gui_handler: &mut GuiHandler<'_>, text: String) -> Result<()> { pub fn set_text(&self, gui_handler: &mut GuiHandler, text: String) -> Result<()> {
self.modifyable.set_text(gui_handler, text.clone())?; self.modifyable.set_text(gui_handler, text.clone())?;
self.text_change_executable self.text_change_executable
@ -107,19 +107,17 @@ impl Writeable {
/// # Arguments /// # Arguments
/// ///
/// * `letter` the letter thats going to be added /// * `letter` the letter thats going to be added
pub fn add_letter(&self, gui_handler: &mut GuiHandler<'_>, letter: char) -> Result<()> { pub fn add_letter(&self, gui_handler: &mut GuiHandler, letter: char) -> Result<()> {
self.text_change_executable.execute( let s = self.modifyable.add_letter(gui_handler, letter)?;
gui_handler, self.text_change_executable.execute(gui_handler, Some(s))?;
Some(self.modifyable.add_letter(gui_handler, letter)?),
)?;
Ok(()) Ok(())
} }
/// Removes the last letter /// Removes the last letter
pub fn remove_last(&self, gui_handler: &mut GuiHandler<'_>) -> Result<()> { pub fn remove_last(&self, gui_handler: &mut GuiHandler) -> Result<()> {
self.text_change_executable let s = self.modifyable.remove_last(gui_handler)?;
.execute(gui_handler, self.modifyable.remove_last(gui_handler)?)?; self.text_change_executable.execute(gui_handler, s)?;
Ok(()) Ok(())
} }

View file

@ -180,7 +180,7 @@ impl TextToScreen {
} }
} }
pub struct GuiHandler<'a> { pub struct GuiHandler {
device: Arc<Device>, device: Arc<Device>,
queue: Arc<Mutex<Queue>>, queue: Arc<Mutex<Queue>>,
@ -229,7 +229,7 @@ pub struct GuiHandler<'a> {
hover_sound: Option<AssetPath>, hover_sound: Option<AssetPath>,
// ----- gui handling ----- // ----- gui handling -----
layers: Vec<(i32, Elements<'a>)>, layers: Vec<(i32, Elements)>,
mouse_x: u32, mouse_x: u32,
mouse_y: u32, mouse_y: u32,
@ -239,14 +239,14 @@ pub struct GuiHandler<'a> {
current_writeable: Option<Arc<Writeable>>, current_writeable: Option<Arc<Writeable>>,
current_hoverable: Option<Arc<Hoverable>>, current_hoverable: Option<Arc<Hoverable>>,
current_clickable: Option<Arc<Clickable>>, current_clickable: Option<Arc<Clickable>>,
current_selectable: Option<Arc<Selectable<'a>>>, current_selectable: Option<Arc<Selectable>>,
text_sample_count: VkSampleCountFlags, text_sample_count: VkSampleCountFlags,
callback_list: Vec<Box<dyn FnOnce() -> Result<()> + Send + Sync>>, callback_list: Vec<Box<dyn FnOnce(&mut Self) -> Result<()> + Send + Sync>>,
} }
impl<'a> GuiHandler<'a> { impl GuiHandler {
pub fn new( pub fn new(
gui_handler_create_info: GuiHandlerCreateInfo<'_>, gui_handler_create_info: GuiHandlerCreateInfo<'_>,
context: &impl ContextInterface, context: &impl ContextInterface,
@ -710,7 +710,7 @@ impl<'a> GuiHandler<'a> {
Ok(false) Ok(false)
} }
pub fn current_selectable(&self) -> Option<Arc<Selectable<'a>>> { pub fn current_selectable(&self) -> Option<Arc<Selectable>> {
self.current_selectable.clone() self.current_selectable.clone()
} }
@ -860,14 +860,19 @@ impl<'a> GuiHandler<'a> {
self.tooltip_ui = tooltip.into(); self.tooltip_ui = tooltip.into();
} }
pub(crate) fn add_callback<F: FnOnce() -> Result<()> + Send + Sync + 'static>(&mut self, f: F) { pub(crate) fn add_callback<F: FnOnce(&mut Self) -> Result<()> + Send + Sync + 'static>(
&mut self,
f: F,
) {
self.callback_list.push(Box::new(f)); self.callback_list.push(Box::new(f));
} }
pub fn process_callbacks(&mut self) -> Result<()> { pub fn process_callbacks(&mut self) -> Result<()> {
let callbacks = mem::take(&mut self.callback_list); let callbacks = mem::take(&mut self.callback_list);
callbacks.into_iter().try_for_each(|callback| callback()) callbacks
.into_iter()
.try_for_each(|callback| callback(self))
} }
fn render( fn render(
@ -1045,7 +1050,7 @@ macro_rules! remove_element {
} }
// object handling // object handling
impl<'a> GuiHandler<'a> { impl<'a> GuiHandler {
// framable // framable
pub(crate) fn add_framable(&mut self, layer: i32, framable: Arc<Framable>) -> Result<()> { pub(crate) fn add_framable(&mut self, layer: i32, framable: Arc<Framable>) -> Result<()> {
add_element!(self.layers, layer, framable); add_element!(self.layers, layer, framable);
@ -1084,11 +1089,7 @@ impl<'a> GuiHandler<'a> {
} }
// selectable // selectable
pub(crate) fn add_selectable( pub(crate) fn add_selectable(&mut self, layer: i32, selectable: Arc<Selectable>) -> Result<()> {
&mut self,
layer: i32,
selectable: Arc<Selectable<'a>>,
) -> Result<()> {
add_element!(self.layers, layer, selectable); add_element!(self.layers, layer, selectable);
Ok(()) Ok(())
@ -1097,7 +1098,7 @@ impl<'a> GuiHandler<'a> {
pub(crate) fn delete_selectable( pub(crate) fn delete_selectable(
&mut self, &mut self,
layer: i32, layer: i32,
selectable: &Arc<Selectable<'a>>, selectable: &Arc<Selectable>,
) -> Result<()> { ) -> Result<()> {
if self.current_selectable.is_some() { if self.current_selectable.is_some() {
// unwrap is safe, just tested for `is_some` // unwrap is safe, just tested for `is_some`
@ -1181,13 +1182,15 @@ impl<'a> GuiHandler<'a> {
} }
pub(crate) fn set_active_writeable(&mut self, writeable: Arc<Writeable>) -> Result<bool> { pub(crate) fn set_active_writeable(&mut self, writeable: Arc<Writeable>) -> Result<bool> {
let callback_self = unsafe { remove_life_time_mut(self) };
if let Some(current) = self.current_writeable.as_ref() { if let Some(current) = self.current_writeable.as_ref() {
// change nothing if both are the same // change nothing if both are the same
if Arc::ptr_eq(current, &writeable) { if Arc::ptr_eq(current, &writeable) {
return Ok(false); return Ok(false);
} }
current.inactivation_event()?; current.inactivation_event(callback_self)?;
} }
self.current_writeable = Some(writeable); self.current_writeable = Some(writeable);
@ -1200,14 +1203,14 @@ impl<'a> GuiHandler<'a> {
layer: i32, layer: i32,
writeable: &Arc<Writeable>, writeable: &Arc<Writeable>,
) -> Result<()> { ) -> Result<()> {
{ let callback_self = unsafe { remove_life_time_mut(self) };
if let Some(w) = &self.current_writeable { if let Some(w) = &self.current_writeable {
if Arc::ptr_eq(w, writeable) { if Arc::ptr_eq(w, writeable) {
w.inactivation_event()?; w.inactivation_event(callback_self)?;
self.current_writeable = None; self.current_writeable = None;
} }
} }
}
remove_element!(self.layers, layer, writeable); remove_element!(self.layers, layer, writeable);
@ -1233,7 +1236,7 @@ impl<'a> GuiHandler<'a> {
Ok(()) Ok(())
} }
pub(crate) fn set_selectable(&mut self, selectable: Option<Arc<Selectable<'a>>>) -> Result<()> { pub(crate) fn set_selectable(&mut self, selectable: Option<Arc<Selectable>>) -> Result<()> {
let callback_self = unsafe { remove_life_time_mut(self) }; let callback_self = unsafe { remove_life_time_mut(self) };
if let Some(selectable) = &self.current_selectable { if let Some(selectable) = &self.current_selectable {
@ -1273,7 +1276,7 @@ impl<'a> GuiHandler<'a> {
} }
// private - create rendering stuff // private - create rendering stuff
impl<'a> GuiHandler<'a> { impl<'a> GuiHandler {
fn create_render_targets( fn create_render_targets(
device: &Arc<Device>, device: &Arc<Device>,
target_images: &TargetMode<Vec<Arc<Image>>>, target_images: &TargetMode<Vec<Arc<Image>>>,
@ -1587,7 +1590,7 @@ impl<'a> GuiHandler<'a> {
} }
} }
impl<'a> GuiHandler<'a> { impl<'a> GuiHandler {
pub fn process( pub fn process(
&mut self, &mut self,
buffer_recorder: &mut CommandBufferRecorder<'_>, buffer_recorder: &mut CommandBufferRecorder<'_>,
@ -1691,8 +1694,8 @@ impl<'a> GuiHandler<'a> {
} }
} }
impl<'a> From<&'a ecs::World> for &'a GuiHandler<'_> { impl<'a> From<&'a ecs::World> for &'a GuiHandler {
fn from(world: &'a ecs::World) -> Self { fn from(world: &'a ecs::World) -> &'a GuiHandler {
world.resources.get::<GuiHandler<'_>>() world.resources.get::<GuiHandler>()
} }
} }

View file

@ -32,7 +32,7 @@ pub struct Keyboard {
} }
impl Keyboard { impl Keyboard {
pub fn new(gui_handler: &GuiHandler<'a>) -> Result<Arc<Self>> { pub fn new(gui_handler: &GuiHandler) -> Result<Arc<Self>> {
let text_field_gui: Arc<GuiBuilder> = let text_field_gui: Arc<GuiBuilder> =
GuiBuilder::from_str(gui_handler, include_str!("text_field.xml"))?; GuiBuilder::from_str(gui_handler, include_str!("text_field.xml"))?;