Completely de'world'ify

This commit is contained in:
hodasemi 2025-04-10 07:58:35 +02:00
parent 74ae317fec
commit 8cec32830a
18 changed files with 548 additions and 250 deletions

View file

@ -23,7 +23,11 @@ pub struct GuiBuilder {
} }
impl GuiBuilder { impl GuiBuilder {
pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result<Arc<Self>> { pub fn new(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
path: &AssetPath,
) -> Result<Arc<Self>> {
let validator = Validator::new( let validator = Validator::new(
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
gui_handler, gui_handler,
@ -31,22 +35,28 @@ impl GuiBuilder {
) )
.with_context(|| format!("validator for {}", path.full_path()))?; .with_context(|| format!("validator for {}", path.full_path()))?;
Ok(Arc::new(Self::_new(gui_handler, validator).with_context( Ok(Arc::new(
|| format!("for file {}", path.full_path()), Self::_new(commands, gui_handler, validator)
)?)) .with_context(|| format!("for file {}", path.full_path()))?,
))
} }
pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result<Arc<Self>> { pub fn from_str(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
s: &str,
) -> Result<Arc<Self>> {
let validator = Validator::from_str( let validator = Validator::from_str(
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
gui_handler, gui_handler,
s, s,
)?; )?;
Ok(Arc::new(Self::_new(gui_handler, validator)?)) Ok(Arc::new(Self::_new(commands, gui_handler, validator)?))
} }
pub fn merge<'a>( pub fn merge<'a>(
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
pathes: impl IntoIterator<Item = &'a AssetPath>, pathes: impl IntoIterator<Item = &'a AssetPath>,
) -> Result<Arc<Self>> { ) -> Result<Arc<Self>> {
@ -66,7 +76,7 @@ impl GuiBuilder {
path, path,
)?; )?;
let builder = Self::_new(gui_handler, validator) let builder = Self::_new(commands, gui_handler, validator)
.with_context(|| format!("for file {}", path.full_path()))?; .with_context(|| format!("for file {}", path.full_path()))?;
me.grids.extend(builder.grids); me.grids.extend(builder.grids);
@ -86,7 +96,11 @@ impl GuiBuilder {
} }
#[inline] #[inline]
fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result<Self> { fn _new(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
validator: Validator,
) -> Result<Self> {
let root = validator.root(); let root = validator.root();
let mut ids = HashMap::new(); let mut ids = HashMap::new();
@ -97,6 +111,7 @@ impl GuiBuilder {
for child in &root.children { for child in &root.children {
let tree = Self::create_tree( let tree = Self::create_tree(
commands,
gui_handler, gui_handler,
&mut ids, &mut ids,
child, child,
@ -179,7 +194,10 @@ impl GuiBuilder {
impl Functionality for GuiBuilder { impl Functionality for GuiBuilder {
fn set_click_callbacks( fn set_click_callbacks(
&self, &self,
functions: Vec<(&str, Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>)>, functions: Vec<(
&str,
Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()> { ) -> Result<()> {
for (function_name, callback) in functions { for (function_name, callback) in functions {
let suffix_less_function_name = handle_function_suffix(function_name); let suffix_less_function_name = handle_function_suffix(function_name);
@ -195,7 +213,7 @@ impl Functionality for GuiBuilder {
&self, &self,
_functions: Vec<( _functions: Vec<(
&str, &str,
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
Ok(()) Ok(())
@ -205,7 +223,7 @@ impl Functionality for GuiBuilder {
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
for (function_name, callback) in callbacks { for (function_name, callback) in callbacks {
@ -222,7 +240,11 @@ impl Functionality for GuiBuilder {
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>, Box<
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync,
>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
for (function_name, callback) in callbacks { for (function_name, callback) in callbacks {
@ -245,9 +267,14 @@ impl Visibility for GuiBuilder {
.unwrap_or(false) .unwrap_or(false)
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
for grid in &self.grids { for grid in &self.grids {
grid.set_visibility(world, visibility)?; grid.set_visibility(commands, gui_handler, visibility)?;
} }
Ok(()) Ok(())
@ -269,7 +296,7 @@ impl GuiElementTraits for GuiBuilder {
} }
impl TopGui for GuiBuilder { impl TopGui for GuiBuilder {
fn decline(&self, _world: &mut World) -> Result<()> { fn decline(&self, _commands: &mut Commands, _gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(decline_callback) = self.decline_callback.read().unwrap().as_ref() { if let Some(decline_callback) = self.decline_callback.read().unwrap().as_ref() {
decline_callback()?; decline_callback()?;
} }
@ -277,11 +304,21 @@ impl TopGui for GuiBuilder {
Ok(()) Ok(())
} }
fn next_tab(&self, _world: &mut World, _: bool) -> Result<()> { fn next_tab(
&self,
_commands: &mut Commands,
_gui_handler: &mut GuiHandler,
_: bool,
) -> Result<()> {
Ok(()) Ok(())
} }
fn previous_tab(&self, _world: &mut World, _: bool) -> Result<()> { fn previous_tab(
&self,
_commands: &mut Commands,
_gui_handler: &mut GuiHandler,
_: bool,
) -> Result<()> {
Ok(()) Ok(())
} }
} }
@ -303,18 +340,18 @@ impl TopLevelGui for GuiBuilder {
Some(self) Some(self)
} }
fn enable(&self, world: &mut World) -> Result<()> { fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(world, true)?; self.set_visibility(commands, gui_handler, true)?;
if let Some(button) = &self.default_select { if let Some(button) = &self.default_select {
button.select(world.resources.get_mut()?)?; button.select(gui_handler)?;
} }
Ok(()) Ok(())
} }
fn disable(&self, world: &mut World) -> Result<()> { fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(world, false)?; self.set_visibility(commands, gui_handler, false)?;
Ok(()) Ok(())
} }
@ -342,6 +379,7 @@ impl_element!(MultiLineTextField);
// private // private
impl GuiBuilder { impl GuiBuilder {
fn create_tree( fn create_tree(
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
ids: &mut HashMap<String, UiElement>, ids: &mut HashMap<String, UiElement>,
root_grid_info: &Arc<GridInfo>, root_grid_info: &Arc<GridInfo>,
@ -394,6 +432,7 @@ impl GuiBuilder {
for child in root_grid_info.children.read().unwrap().iter() { for child in root_grid_info.children.read().unwrap().iter() {
Self::create_child( Self::create_child(
commands,
gui_handler, gui_handler,
child, child,
&root_grid, &root_grid,
@ -408,6 +447,7 @@ impl GuiBuilder {
} }
fn create_child( fn create_child(
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
child: &UiInfoElement, child: &UiInfoElement,
grid: &Grid, grid: &Grid,
@ -423,6 +463,7 @@ impl GuiBuilder {
Self::insert_id(ids, &button_info.id, &button)?; Self::insert_id(ids, &button_info.id, &button)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
button.clone(), button.clone(),
button_info button_info
@ -458,6 +499,7 @@ impl GuiBuilder {
Self::insert_id(ids, &label_info.id, &label)?; Self::insert_id(ids, &label_info.id, &label)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
label.clone(), label.clone(),
label_info.x_slot.get().with_context(|| "x_slot of label")?, label_info.x_slot.get().with_context(|| "x_slot of label")?,
@ -470,11 +512,12 @@ impl GuiBuilder {
} }
UiInfoElement::MultiLineLabel(multi_line_label_info) => { UiInfoElement::MultiLineLabel(multi_line_label_info) => {
let multi_line_label = let multi_line_label =
MultiLineLabel::try_from(multi_line_label_info, gui_handler)?; MultiLineLabel::try_from(multi_line_label_info, commands, gui_handler)?;
GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?; GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
multi_line_label.clone(), multi_line_label.clone(),
multi_line_label_info multi_line_label_info
@ -492,12 +535,16 @@ impl GuiBuilder {
multi_line_label.set_layer(layer)?; multi_line_label.set_layer(layer)?;
} }
UiInfoElement::MultiLineTextField(multi_line_text_field_info) => { UiInfoElement::MultiLineTextField(multi_line_text_field_info) => {
let multi_line_text_field = let multi_line_text_field = MultiLineTextField::try_from(
MultiLineTextField::try_from(multi_line_text_field_info, gui_handler)?; multi_line_text_field_info,
commands,
gui_handler,
)?;
GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?; GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
multi_line_text_field.clone(), multi_line_text_field.clone(),
multi_line_text_field_info multi_line_text_field_info
@ -520,6 +567,7 @@ impl GuiBuilder {
Self::insert_id(ids, &text_field_info.id, &text_field)?; Self::insert_id(ids, &text_field_info.id, &text_field)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
text_field.clone(), text_field.clone(),
text_field_info text_field_info
@ -542,6 +590,7 @@ impl GuiBuilder {
Self::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?; Self::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
icon.clone(), icon.clone(),
icon_info.x_slot.get().with_context(|| "x_slot of icon")?, icon_info.x_slot.get().with_context(|| "x_slot of icon")?,
@ -558,6 +607,7 @@ impl GuiBuilder {
Self::insert_id(ids, &progress_bar_info.id, &progress_bar)?; Self::insert_id(ids, &progress_bar_info.id, &progress_bar)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
progress_bar.clone(), progress_bar.clone(),
progress_bar_info progress_bar_info
@ -580,6 +630,7 @@ impl GuiBuilder {
Self::insert_id(ids, &grid_info.id, &sub_grid)?; Self::insert_id(ids, &grid_info.id, &sub_grid)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
sub_grid.clone(), sub_grid.clone(),
grid_info.x_slot.get().with_context(|| "x_slot of grid")?, grid_info.x_slot.get().with_context(|| "x_slot of grid")?,
@ -593,6 +644,7 @@ impl GuiBuilder {
for child in grid_info.children.read().unwrap().iter() { for child in grid_info.children.read().unwrap().iter() {
Self::create_child( Self::create_child(
commands,
gui_handler, gui_handler,
child, child,
&sub_grid, &sub_grid,

View file

@ -1,7 +1,7 @@
use std::{any::Any, collections::HashMap, sync::Arc}; use std::{any::Any, collections::HashMap, sync::Arc};
use assetpath::AssetPath; use assetpath::AssetPath;
use ecs::World; use ecs::Commands;
use super::validator::{ use super::validator::{
buttoninfo::NeighbourInfo, buttoninfo::NeighbourInfo,
@ -18,27 +18,39 @@ pub struct GuiSnippet {
} }
impl GuiSnippet { impl GuiSnippet {
pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result<Arc<Self>> { pub fn new(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
path: &AssetPath,
) -> Result<Arc<Self>> {
let validator = Validator::new( let validator = Validator::new(
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
gui_handler, gui_handler,
path, path,
)?; )?;
Self::_new(gui_handler, validator) Self::_new(commands, gui_handler, validator)
} }
pub fn from_str(gui_handler: &mut GuiHandler, s: &str) -> Result<Arc<Self>> { pub fn from_str(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
s: &str,
) -> Result<Arc<Self>> {
let validator = Validator::from_str( let validator = Validator::from_str(
#[cfg(feature = "audio")] #[cfg(feature = "audio")]
gui_handler, gui_handler,
s, s,
)?; )?;
Self::_new(gui_handler, validator) Self::_new(commands, gui_handler, validator)
} }
fn _new(gui_handler: &mut GuiHandler, validator: Validator) -> Result<Arc<Self>> { fn _new(
commands: &mut Commands,
gui_handler: &mut GuiHandler,
validator: Validator,
) -> Result<Arc<Self>> {
let root = validator.root(); let root = validator.root();
let mut ids = HashMap::new(); let mut ids = HashMap::new();
@ -57,7 +69,14 @@ impl GuiSnippet {
GuiBuilder::insert_id(&mut ids, &grid_info.id, &grid)?; GuiBuilder::insert_id(&mut ids, &grid_info.id, &grid)?;
for child in grid_info.children.read().unwrap().iter() { for child in grid_info.children.read().unwrap().iter() {
Self::create_child(gui_handler, child, &grid, &mut ids, &mut custom_neighbours)?; Self::create_child(
commands,
gui_handler,
child,
&grid,
&mut ids,
&mut custom_neighbours,
)?;
} }
GuiBuilder::connect_custom_neighbours(&ids, custom_neighbours)?; GuiBuilder::connect_custom_neighbours(&ids, custom_neighbours)?;
@ -94,8 +113,13 @@ impl Visibility for GuiSnippet {
self.grid.visible() self.grid.visible()
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
self.grid.set_visibility(world, visibility) &self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
self.grid.set_visibility(commands, gui_handler, visibility)
} }
} }
@ -148,7 +172,10 @@ impl Gridable for GuiSnippet {
impl Functionality for GuiSnippet { impl Functionality for GuiSnippet {
fn set_click_callbacks( fn set_click_callbacks(
&self, &self,
functions: Vec<(&str, Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>)>, functions: Vec<(
&str,
Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()> { ) -> Result<()> {
for (function_name, callback) in functions { for (function_name, callback) in functions {
let suffix_less_function_name = handle_function_suffix(function_name); let suffix_less_function_name = handle_function_suffix(function_name);
@ -164,7 +191,7 @@ impl Functionality for GuiSnippet {
&self, &self,
_functions: Vec<( _functions: Vec<(
&str, &str,
Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
Ok(()) Ok(())
@ -174,7 +201,7 @@ impl Functionality for GuiSnippet {
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut World, bool) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
for (function_name, callback) in callbacks { for (function_name, callback) in callbacks {
@ -191,7 +218,11 @@ impl Functionality for GuiSnippet {
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>, Box<
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync,
>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
for (function_name, callback) in callbacks { for (function_name, callback) in callbacks {
@ -207,6 +238,7 @@ impl Functionality for GuiSnippet {
impl GuiSnippet { impl GuiSnippet {
fn create_child( fn create_child(
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
child: &UiInfoElement, child: &UiInfoElement,
grid: &Grid, grid: &Grid,
@ -220,6 +252,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &button_info.id, &button)?; GuiBuilder::insert_id(ids, &button_info.id, &button)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
button.clone(), button.clone(),
button_info.x_slot.get()?, button_info.x_slot.get()?,
@ -236,6 +269,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &label_info.id, &label)?; GuiBuilder::insert_id(ids, &label_info.id, &label)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
label, label,
label_info.x_slot.get()?, label_info.x_slot.get()?,
@ -246,11 +280,12 @@ impl GuiSnippet {
} }
UiInfoElement::MultiLineLabel(multi_line_label_info) => { UiInfoElement::MultiLineLabel(multi_line_label_info) => {
let multi_line_label = let multi_line_label =
MultiLineLabel::try_from(multi_line_label_info, gui_handler)?; MultiLineLabel::try_from(multi_line_label_info, commands, gui_handler)?;
GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?; GuiBuilder::insert_id(ids, &multi_line_label_info.id, &multi_line_label)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
multi_line_label, multi_line_label,
multi_line_label_info.x_slot.get()?, multi_line_label_info.x_slot.get()?,
@ -260,12 +295,16 @@ impl GuiSnippet {
)?; )?;
} }
UiInfoElement::MultiLineTextField(multi_line_text_field_info) => { UiInfoElement::MultiLineTextField(multi_line_text_field_info) => {
let multi_line_text_field = let multi_line_text_field = MultiLineTextField::try_from(
MultiLineTextField::try_from(multi_line_text_field_info, gui_handler)?; multi_line_text_field_info,
commands,
gui_handler,
)?;
GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?; GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
multi_line_text_field, multi_line_text_field,
multi_line_text_field_info.x_slot.get()?, multi_line_text_field_info.x_slot.get()?,
@ -280,6 +319,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &text_field_info.id, &text_field)?; GuiBuilder::insert_id(ids, &text_field_info.id, &text_field)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
text_field, text_field,
text_field_info.x_slot.get()?, text_field_info.x_slot.get()?,
@ -294,6 +334,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?; GuiBuilder::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
icon, icon,
icon_info.x_slot.get()?, icon_info.x_slot.get()?,
@ -308,6 +349,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &progress_bar_info.id, &progress_bar)?; GuiBuilder::insert_id(ids, &progress_bar_info.id, &progress_bar)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
progress_bar, progress_bar,
progress_bar_info.x_slot.get()?, progress_bar_info.x_slot.get()?,
@ -322,6 +364,7 @@ impl GuiSnippet {
GuiBuilder::insert_id(ids, &grid_info.id, &sub_grid)?; GuiBuilder::insert_id(ids, &grid_info.id, &sub_grid)?;
grid.attach( grid.attach(
commands,
gui_handler, gui_handler,
sub_grid.clone(), sub_grid.clone(),
grid_info.x_slot.get()?, grid_info.x_slot.get()?,
@ -331,7 +374,14 @@ impl GuiSnippet {
)?; )?;
for child in grid_info.children.read().unwrap().iter() { for child in grid_info.children.read().unwrap().iter() {
Self::create_child(gui_handler, child, &sub_grid, ids, neighbour_infos)?; Self::create_child(
commands,
gui_handler,
child,
&sub_grid,
ids,
neighbour_infos,
)?;
} }
} }
} }

View file

@ -315,23 +315,32 @@ impl Button {
pub fn set_callback<F>(&self, callback: F) pub fn set_callback<F>(&self, callback: F)
where where
F: Fn(&mut Commands) -> Result<()> + Send + Sync + 'static, F: Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync + 'static,
{ {
self.click_executable self.click_executable.set_callback(
.set_callback(move |commands: &mut Commands, _: ()| callback(commands)); move |commands: &mut Commands, gui_handler: &mut GuiHandler, _: ()| {
callback(commands, gui_handler)
},
);
} }
pub fn set_select_callback<F>(&self, callback: F) pub fn set_select_callback<F>(&self, callback: F)
where where
F: Fn(&mut Commands, bool) -> Result<()> + Send + Sync + 'static, F: Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync + 'static,
{ {
self.on_select_executable self.on_select_executable.set_callback(
.set_callback(move |commands: &mut Commands, select: bool| callback(commands, select)); move |commands: &mut Commands, gui_handler: &mut GuiHandler, select: bool| {
callback(commands, gui_handler, select)
},
);
} }
pub fn set_custom_callback<F>(&self, callback: F) pub fn set_custom_callback<F>(&self, callback: F)
where where
F: Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync + 'static, F: Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync
+ 'static,
{ {
self.selectable.set_custom_callback(callback); self.selectable.set_custom_callback(callback);
} }
@ -448,7 +457,12 @@ impl Visibility for Button {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> { fn set_visibility(
&self,
_commands: &mut Commands,
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);
@ -579,19 +593,18 @@ impl Button {
fn create_selected_changed_callback(button: Arc<Button>) { fn create_selected_changed_callback(button: Arc<Button>) {
let button_weak = Arc::downgrade(&button); let button_weak = Arc::downgrade(&button);
let selected_changed = move |commands: &mut Commands, selected: bool| { let selected_changed =
if let Some(button) = button_weak.upgrade() { move |_commands: &mut Commands, gui_handler: &mut GuiHandler, selected: bool| {
let gui_handler = world.resources.get_mut()?; if let Some(button) = button_weak.upgrade() {
if selected {
if selected { button.set_button_state(gui_handler, ButtonState::Selected)?;
button.set_button_state(gui_handler, ButtonState::Selected)?; } else {
} else { button.set_button_state(gui_handler, ButtonState::Normal)?;
button.set_button_state(gui_handler, ButtonState::Normal)?; }
} }
}
Ok(()) Ok(())
}; };
button.select_executable.set_callback(selected_changed); button.select_executable.set_callback(selected_changed);
} }

View file

@ -1,5 +1,6 @@
use crate::prelude::*; use crate::prelude::*;
use anyhow::Result; use anyhow::Result;
use ecs::Commands;
use crate::builder::validator::gridinfo::GridInfo; use crate::builder::validator::gridinfo::GridInfo;
@ -199,6 +200,7 @@ impl Grid {
pub fn detach( pub fn detach(
&self, &self,
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
pos_x: usize, pos_x: usize,
pos_y: usize, pos_y: usize,
@ -231,7 +233,7 @@ impl Grid {
Some((child, _x, _y)) => { Some((child, _x, _y)) => {
if self.visible() { if self.visible() {
if let Some(child_visibility) = child.visibility() { if let Some(child_visibility) = child.visibility() {
child_visibility.set_visibility(gui_handler, false)?; child_visibility.set_visibility(commands, gui_handler, false)?;
} }
} }
@ -244,6 +246,7 @@ impl Grid {
/// Returns `true` if item got detached /// Returns `true` if item got detached
pub fn detach_item( pub fn detach_item(
&self, &self,
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
item: Arc<dyn GuiElementTraits>, item: Arc<dyn GuiElementTraits>,
) -> Result<bool> { ) -> Result<bool> {
@ -265,7 +268,7 @@ impl Grid {
if self.visible() { if self.visible() {
if let Some(child_visibility) = child.visibility() { if let Some(child_visibility) = child.visibility() {
child_visibility.set_visibility(gui_handler, false)?; child_visibility.set_visibility(commands, gui_handler, false)?;
} }
} }
} }
@ -408,6 +411,7 @@ impl Grid {
pub fn attach( pub fn attach(
&self, &self,
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
child: Arc<dyn GuiElementTraits>, child: Arc<dyn GuiElementTraits>,
pos_x: usize, pos_x: usize,
@ -458,7 +462,7 @@ impl Grid {
ChildState::Some { child, .. } => { ChildState::Some { child, .. } => {
if self.visible() { if self.visible() {
if let Some(child_visibility) = child.visibility() { if let Some(child_visibility) = child.visibility() {
child_visibility.set_visibility(gui_handler, false)?; child_visibility.set_visibility(commands, gui_handler, false)?;
} }
} }
} }
@ -468,7 +472,7 @@ impl Grid {
if self.visible() { if self.visible() {
if let Some(child_visibility) = child.visibility() { if let Some(child_visibility) = child.visibility() {
child_visibility.set_visibility(gui_handler, true)?; child_visibility.set_visibility(commands, gui_handler, true)?;
} }
} }
@ -622,26 +626,31 @@ impl Grid {
} }
} }
fn disable_tree(&self, gui_handler: &mut GuiHandler) -> Result<()> { fn disable_tree(&self, commands: &mut Commands, 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() {
background.disable(gui_handler)?; background.disable(gui_handler)?;
} }
self.set_tree_visibility(gui_handler, false)?; self.set_tree_visibility(commands, gui_handler, false)?;
Ok(()) Ok(())
} }
fn set_tree_visibility(&self, gui_handler: &mut GuiHandler, visible: bool) -> Result<()> { fn set_tree_visibility(
&self,
commands: &mut Commands,
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() {
for child_state in row { for child_state in row {
if let Some((child, ..)) = child_state.get_some() { if let Some((child, ..)) = child_state.get_some() {
if let Some(visibility) = child.visibility() { if let Some(visibility) = child.visibility() {
visibility.set_visibility(gui_handler, visible)?; visibility.set_visibility(commands, gui_handler, visible)?;
} }
} }
} }
@ -793,7 +802,12 @@ impl Visibility for Grid {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()> { fn set_visibility(
&self,
commands: &mut Commands,
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);
@ -804,9 +818,9 @@ impl Visibility for Grid {
background.enable(gui_handler)?; background.enable(gui_handler)?;
} }
self.set_tree_visibility(gui_handler, true)?; self.set_tree_visibility(commands, gui_handler, true)?;
} else { } else {
self.disable_tree(gui_handler)?; self.disable_tree(commands, gui_handler)?;
} }
} }

View file

@ -4,7 +4,7 @@ use crate::{
}; };
use anyhow::Result; use anyhow::Result;
use assetpath::AssetPath; use assetpath::AssetPath;
use ecs::*; use ecs::Commands;
use utilities::prelude::*; use utilities::prelude::*;
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
@ -281,10 +281,13 @@ impl Visibility for Icon {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
&self,
_commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
if visibility != self.visible.load(SeqCst) { if visibility != self.visible.load(SeqCst) {
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
if visibility { if visibility {

View file

@ -8,7 +8,7 @@ use super::{
wrapper::{IconizableWrapper, TextableWrapper}, wrapper::{IconizableWrapper, TextableWrapper},
}; };
use ecs::*; use ecs::Commands;
use vulkan_rs::prelude::*; use vulkan_rs::prelude::*;
use std::sync::{ use std::sync::{
@ -230,10 +230,13 @@ impl Visibility for Label {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
&self,
_commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
if visibility { if visibility {

View file

@ -6,7 +6,7 @@ use std::sync::{
}; };
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use ecs::World; use ecs::Commands;
use utilities::prelude::*; use utilities::prelude::*;
pub struct MultiLineLabelBuilder { pub struct MultiLineLabelBuilder {
@ -57,7 +57,11 @@ impl MultiLineLabelBuilder {
self self
} }
pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<MultiLineLabel>> { pub fn build(
self,
commands: &mut Commands,
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);
@ -73,7 +77,7 @@ impl MultiLineLabelBuilder {
.set_text_alignment(self.text_alignment) .set_text_alignment(self.text_alignment)
.build(gui_handler)?; .build(gui_handler)?;
base_grid.attach(gui_handler, label, 0, i as usize, 1, 1)?; base_grid.attach(commands, gui_handler, label, 0, i as usize, 1, 1)?;
} }
Ok(Arc::new(MultiLineLabel { Ok(Arc::new(MultiLineLabel {
@ -185,6 +189,7 @@ impl MultiLineLabel {
pub fn try_from( pub fn try_from(
multi_line_label_info: &MultiLineLabelInfo, multi_line_label_info: &MultiLineLabelInfo,
commands: &mut Commands,
gui_handler: &mut GuiHandler, 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();
@ -213,7 +218,7 @@ impl MultiLineLabel {
multi_line_label_builder = multi_line_label_builder.set_text(text); multi_line_label_builder = multi_line_label_builder.set_text(text);
} }
multi_line_label_builder.build(gui_handler) multi_line_label_builder.build(commands, gui_handler)
} }
fn iter_label<F>(&self, mut f: F) -> Result<()> fn iter_label<F>(&self, mut f: F) -> Result<()>
@ -251,8 +256,13 @@ impl Visibility for MultiLineLabel {
self.grid.visible() self.grid.visible()
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
self.grid.set_visibility(world, visibility) &self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
self.grid.set_visibility(commands, gui_handler, visibility)
} }
} }

View file

@ -63,7 +63,11 @@ impl MultiLineTextFieldBuilder {
self self
} }
pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<MultiLineTextField>> { pub fn build(
self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
) -> Result<Arc<MultiLineTextField>> {
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);
@ -79,7 +83,7 @@ impl MultiLineTextFieldBuilder {
.set_text_alignment(self.text_alignment) .set_text_alignment(self.text_alignment)
.build(gui_handler)?; .build(gui_handler)?;
base_grid.attach(gui_handler, label, 0, i as usize, 1, 1)?; base_grid.attach(commands, gui_handler, label, 0, i as usize, 1, 1)?;
} }
let text = Arc::new(SplittedText::new(self.text)); let text = Arc::new(SplittedText::new(self.text));
@ -97,9 +101,9 @@ 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 |commands: &mut Commands, _text| { move |_commands: &mut Commands, gui_handler: &mut GuiHandler, _text| {
if let Some(tf) = weak_tf.upgrade() { if let Some(tf) = weak_tf.upgrade() {
tf.update_text(world.resources.get_mut()?)?; tf.update_text(gui_handler)?;
} }
Ok(()) Ok(())
@ -305,6 +309,7 @@ impl MultiLineTextField {
pub fn try_from( pub fn try_from(
multi_line_text_field_info: &MultiLineTextFieldInfo, multi_line_text_field_info: &MultiLineTextFieldInfo,
commands: &mut Commands,
gui_handler: &mut GuiHandler, 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();
@ -333,7 +338,7 @@ impl MultiLineTextField {
multi_line_text_field_builder = multi_line_text_field_builder.set_text(text); multi_line_text_field_builder = multi_line_text_field_builder.set_text(text);
} }
multi_line_text_field_builder.build(gui_handler) multi_line_text_field_builder.build(commands, gui_handler)
} }
fn iter_label<F>(&self, mut f: F) -> Result<()> fn iter_label<F>(&self, mut f: F) -> Result<()>
@ -371,10 +376,13 @@ impl Visibility for MultiLineTextField {
self.grid.visible() self.grid.visible()
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
let gui_handler = world.resources.get_mut()?;
if visibility { if visibility {
self.writeable.add(gui_handler)?; self.writeable.add(gui_handler)?;
} else { } else {
@ -382,7 +390,7 @@ impl Visibility for MultiLineTextField {
} }
} }
self.grid.set_visibility(world, visibility) self.grid.set_visibility(commands, gui_handler, visibility)
} }
} }

View file

@ -1,7 +1,7 @@
use crate::{builder::validator::progressbar_info::ProgressBarInfo, prelude::*}; use crate::{builder::validator::progressbar_info::ProgressBarInfo, prelude::*};
use anyhow::Result; use anyhow::Result;
use ecs::*; use ecs::Commands;
use std::sync::{ use std::sync::{
Arc, Mutex, Arc, Mutex,
atomic::{AtomicBool, Ordering::SeqCst}, atomic::{AtomicBool, Ordering::SeqCst},
@ -279,10 +279,13 @@ impl Visibility for ProgressBar {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
&self,
_commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
if visibility { if visibility {

View file

@ -170,7 +170,7 @@ 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(&mut Commands, Option<String>) -> Result<()> + Send + Sync + 'static, F: Fn(&mut Commands, &mut GuiHandler, Option<String>) -> Result<()> + Send + Sync + 'static,
{ {
self.text_changed_executable.set_callback(f); self.text_changed_executable.set_callback(f);
} }
@ -311,10 +311,13 @@ impl Visibility for TextField {
self.visible.load(SeqCst) self.visible.load(SeqCst)
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
&self,
_commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
if visibility != self.visible() { if visibility != self.visible() {
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
self.visible.store(visibility, SeqCst); self.visible.store(visibility, SeqCst);
if visibility { if visibility {

View file

@ -10,8 +10,8 @@ pub trait TopLevelGui: Send + Sync {
fn top_gui(&self) -> Option<&dyn TopGui>; fn top_gui(&self) -> Option<&dyn TopGui>;
fn elements(&self) -> Option<&HashMap<String, UiElement>>; fn elements(&self) -> Option<&HashMap<String, UiElement>>;
fn functionality(&self) -> Option<&dyn Functionality>; fn functionality(&self) -> Option<&dyn Functionality>;
fn enable(&self, commands: &mut Commands) -> Result<()>; fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()>;
fn disable(&self, commands: &mut Commands) -> Result<()>; fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()>;
} }
pub fn any_to<T>(any: &dyn Any) -> Result<&T> pub fn any_to<T>(any: &dyn Any) -> Result<&T>
@ -27,14 +27,17 @@ where
pub trait Functionality { pub trait Functionality {
fn set_click_callbacks( fn set_click_callbacks(
&self, &self,
callbacks: Vec<(&str, Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>)>, callbacks: Vec<(
&str,
Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()>; ) -> Result<()>;
fn set_select_callbacks( fn set_select_callbacks(
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()>; ) -> Result<()>;
@ -42,7 +45,11 @@ pub trait Functionality {
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>, Box<
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync,
>,
)>, )>,
) -> Result<()>; ) -> Result<()>;
@ -50,7 +57,7 @@ pub trait Functionality {
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()>; ) -> Result<()>;
} }
@ -84,7 +91,12 @@ pub trait Gridable {
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,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()>;
} }
pub enum GuiElement<'a> { pub enum GuiElement<'a> {

View file

@ -9,7 +9,8 @@ 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> { pub struct Executable<I: Send + Sync> {
callback: RwLock<Option<Arc<dyn Fn(&mut Commands, I) -> Result<()> + Send + Sync>>>, callback:
RwLock<Option<Arc<dyn Fn(&mut Commands, &mut GuiHandler, I) -> Result<()> + Send + Sync>>>,
} }
impl<I: Send + Sync + 'static> Executable<I> { impl<I: Send + Sync + 'static> Executable<I> {
@ -27,7 +28,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(&mut Commands, I) -> Result<()> + Send + Sync + 'static, F: Fn(&mut Commands, &mut GuiHandler, I) -> Result<()> + Send + Sync + 'static,
{ {
let mut function = self.callback.write().unwrap(); let mut function = self.callback.write().unwrap();
@ -42,7 +43,9 @@ impl<I: Send + Sync + 'static> Executable<I> {
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 |commands| (callback)(commands, input)); gui_handler.add_callback(move |commands, gui_handler| {
(callback)(commands, gui_handler, input)
});
} }
Ok(()) Ok(())

View file

@ -40,8 +40,15 @@ pub struct Selectable {
on_select_executable: Arc<Executable<bool>>, on_select_executable: Arc<Executable<bool>>,
// used for custom buttons // used for custom buttons
custom_callback: custom_callback: RwLock<
RwLock<Option<Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>>>, Option<
Box<
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync,
>,
>,
>,
} }
impl Selectable { impl Selectable {
@ -117,7 +124,10 @@ impl Selectable {
pub fn set_custom_callback<F>(&self, custom_callback: F) pub fn set_custom_callback<F>(&self, custom_callback: F)
where where
F: Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync + 'static, F: Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync
+ 'static,
{ {
*self.custom_callback.write().unwrap() = Some(Box::new(custom_callback)); *self.custom_callback.write().unwrap() = Some(Box::new(custom_callback));
} }
@ -181,10 +191,11 @@ impl Selectable {
pub(crate) fn custom_click_event( pub(crate) fn custom_click_event(
&self, &self,
commands: &mut Commands, commands: &mut Commands,
gui_handler: &mut GuiHandler,
button: ControllerButton, button: ControllerButton,
) -> Result<bool> { ) -> Result<bool> {
if let Some(custom_callback) = self.custom_callback.read().unwrap().as_ref() { if let Some(custom_callback) = self.custom_callback.read().unwrap().as_ref() {
if custom_callback(commands, button)? { if custom_callback(commands, gui_handler, button)? {
#[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() {

View file

@ -2,19 +2,31 @@
use anyhow::Result; use anyhow::Result;
use ecs::Commands; use ecs::Commands;
use crate::prelude::GuiHandler;
pub trait TopGui: Send + Sync { pub trait TopGui: Send + Sync {
/// Decline method which is executed on `InputMap::B` press /// Decline method which is executed on `InputMap::B` press
fn decline(&self, commands: &mut Commands) -> Result<()>; fn decline(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()>;
/// Method which is executed on `InputMap::RightButton` press /// Method which is executed on `InputMap::RightButton` press
/// ///
/// # Arguments /// # Arguments
/// * `second_level` adds support for multiple tab layers, e.g. RB and RT press on controller /// * `second_level` adds support for multiple tab layers, e.g. RB and RT press on controller
fn next_tab(&self, commands: &mut Commands, second_level: bool) -> Result<()>; fn next_tab(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()>;
/// Method which is executed on `InputMap::LeftButton` press /// Method which is executed on `InputMap::LeftButton` press
/// /// /// ///
/// # Arguments /// # Arguments
/// * `second_level` adds support for multiple tab layers, e.g. RB and RT press on controller /// * `second_level` adds support for multiple tab layers, e.g. RB and RT press on controller
fn previous_tab(&self, commands: &mut Commands, second_level: bool) -> Result<()>; fn previous_tab(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()>;
} }

View file

@ -245,7 +245,7 @@ pub struct GuiHandler {
text_sample_count: VkSampleCountFlags, text_sample_count: VkSampleCountFlags,
callback_list: Vec<Box<dyn FnOnce(&mut Commands) -> Result<()> + Send + Sync>>, callback_list: Vec<Box<dyn FnOnce(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>>,
} }
impl GuiHandler { impl GuiHandler {
@ -730,11 +730,12 @@ impl GuiHandler {
pub fn accept_custom_selection( pub fn accept_custom_selection(
&self, &self,
world: &mut World, commands: &mut Commands,
gui_handler: &mut GuiHandler,
button: ControllerButton, button: ControllerButton,
) -> Result<bool> { ) -> Result<bool> {
if let Some(current_selectable) = &self.current_selectable { if let Some(current_selectable) = &self.current_selectable {
if current_selectable.custom_click_event(world, button)? { if current_selectable.custom_click_event(commands, gui_handler, button)? {
return Ok(true); return Ok(true);
} }
} }
@ -742,13 +743,15 @@ impl GuiHandler {
Ok(false) Ok(false)
} }
pub fn decline_topgui(&mut self, world: &mut World) -> Result<bool> { pub fn decline_topgui(&mut self, commands: &mut Commands) -> Result<bool> {
let callback_self = unsafe { remove_life_time_mut(self) };
// workaround for unwanted borrowing behaviour inside decline function // workaround for unwanted borrowing behaviour inside decline function
let opt_top_level_gui = self.top_ui.as_ref().cloned(); let opt_top_level_gui = self.top_ui.as_ref().cloned();
if let Some(top_level_gui) = opt_top_level_gui { if let Some(top_level_gui) = opt_top_level_gui {
if let Some(top_gui) = top_level_gui.top_gui() { if let Some(top_gui) = top_level_gui.top_gui() {
top_gui.decline(world)?; top_gui.decline(commands, callback_self)?;
return Ok(true); return Ok(true);
} }
} }
@ -756,13 +759,15 @@ impl GuiHandler {
Ok(false) Ok(false)
} }
pub fn next_tab_topgui(&mut self, world: &mut World, second_level: bool) -> Result<bool> { pub fn next_tab_topgui(&mut self, commands: &mut Commands, second_level: bool) -> Result<bool> {
let callback_self = unsafe { remove_life_time_mut(self) };
// workaround for unwanted borrowing behaviour inside decline function // workaround for unwanted borrowing behaviour inside decline function
let opt_top_level_gui = self.top_ui.as_ref().cloned(); let opt_top_level_gui = self.top_ui.as_ref().cloned();
if let Some(top_level_gui) = opt_top_level_gui { if let Some(top_level_gui) = opt_top_level_gui {
if let Some(top_gui) = top_level_gui.top_gui() { if let Some(top_gui) = top_level_gui.top_gui() {
top_gui.next_tab(world, second_level)?; top_gui.next_tab(commands, callback_self, second_level)?;
return Ok(true); return Ok(true);
} }
} }
@ -770,13 +775,19 @@ impl GuiHandler {
Ok(false) Ok(false)
} }
pub fn previous_tab_topgui(&mut self, world: &mut World, second_level: bool) -> Result<bool> { pub fn previous_tab_topgui(
&mut self,
commands: &mut Commands,
second_level: bool,
) -> Result<bool> {
let callback_self = unsafe { remove_life_time_mut(self) };
// workaround for unwanted borrowing behaviour inside decline function // workaround for unwanted borrowing behaviour inside decline function
let opt_top_level_gui = self.top_ui.as_ref().cloned(); let opt_top_level_gui = self.top_ui.as_ref().cloned();
if let Some(top_level_gui) = opt_top_level_gui { if let Some(top_level_gui) = opt_top_level_gui {
if let Some(top_gui) = top_level_gui.top_gui() { if let Some(top_gui) = top_level_gui.top_gui() {
top_gui.previous_tab(world, second_level)?; top_gui.previous_tab(commands, callback_self, second_level)?;
return Ok(true); return Ok(true);
} }
} }
@ -869,14 +880,16 @@ impl GuiHandler {
commands: &mut Commands, commands: &mut Commands,
top_gui: Option<Arc<dyn TopLevelGui>>, top_gui: Option<Arc<dyn TopLevelGui>>,
) -> Result<()> { ) -> Result<()> {
let callback_self = unsafe { remove_life_time_mut(self) };
match (&self.top_ui, &top_gui) { match (&self.top_ui, &top_gui) {
(Some(current), Some(incoming)) => { (Some(current), Some(incoming)) => {
if !Arc::ptr_eq(current, incoming) { if !Arc::ptr_eq(current, incoming) {
current.disable(commands)?; current.disable(commands, callback_self)?;
} }
} }
(Some(current), None) => { (Some(current), None) => {
current.disable(commands)?; current.disable(commands, callback_self)?;
} }
_ => (), _ => (),
@ -892,14 +905,16 @@ impl GuiHandler {
commands: &mut Commands, commands: &mut Commands,
tooltip: Option<Arc<dyn TopLevelGui>>, tooltip: Option<Arc<dyn TopLevelGui>>,
) -> Result<()> { ) -> Result<()> {
let callback_self = unsafe { remove_life_time_mut(self) };
match (&self.tooltip_ui, &tooltip) { match (&self.tooltip_ui, &tooltip) {
(Some(current), Some(incoming)) => { (Some(current), Some(incoming)) => {
if !Arc::ptr_eq(current, incoming) { if !Arc::ptr_eq(current, incoming) {
current.disable(commands)?; current.disable(commands, callback_self)?;
} }
} }
(Some(current), None) => { (Some(current), None) => {
current.disable(commands)?; current.disable(commands, callback_self)?;
} }
_ => (), _ => (),
@ -910,19 +925,25 @@ impl GuiHandler {
Ok(()) Ok(())
} }
pub(crate) fn add_callback<F: FnOnce(&mut Commands) -> Result<()> + Send + Sync + 'static>( pub(crate) fn add_callback<
F: FnOnce(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync + 'static,
>(
&mut self, &mut self,
f: F, f: F,
) { ) {
self.callback_list.push(Box::new(f)); self.callback_list.push(Box::new(f));
} }
pub fn process_callbacks(&mut self, commands: &mut Commands) -> Result<()> { pub fn process_callbacks(
&mut self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
) -> Result<()> {
let callbacks = mem::take(&mut self.callback_list); let callbacks = mem::take(&mut self.callback_list);
callbacks callbacks
.into_iter() .into_iter()
.try_for_each(|callback| callback(commands)) .try_for_each(|callback| callback(commands, gui_handler))
} }
fn render( fn render(

View file

@ -26,32 +26,48 @@ pub struct Keyboard {
mode: Arc<RwLock<KeyboardMode>>, mode: Arc<RwLock<KeyboardMode>>,
decline_callback: Arc<RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>>, decline_callback: Arc<
accept_callback: RwLock<Option<Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>>>,
Arc<RwLock<Option<Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>>>>, >,
accept_callback: Arc<
RwLock<
Option<
Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
>,
>,
>,
elements: HashMap<String, UiElement>, elements: HashMap<String, UiElement>,
} }
impl Keyboard { impl Keyboard {
pub fn new(world: &mut World) -> Result<Arc<Self>> { pub fn new(commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<Arc<Self>> {
let text_field_gui: Arc<GuiBuilder> = let text_field_gui: Arc<GuiBuilder> =
GuiBuilder::from_str(world, include_str!("text_field.xml"))?; GuiBuilder::from_str(commands, gui_handler, include_str!("text_field.xml"))?;
let text_field: Arc<TextField> = text_field_gui.element("field")?; let text_field: Arc<TextField> = text_field_gui.element("field")?;
let decline_callback: Arc< let decline_callback: Arc<
RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>, RwLock<Option<Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>>>,
> = Arc::new(RwLock::new(None)); > = Arc::new(RwLock::new(None));
let accept_callback: Arc< let accept_callback: Arc<
RwLock<Option<Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>>>, RwLock<
Option<
Box<
dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()>
+ Send
+ Sync,
>,
>,
>,
> = Arc::new(RwLock::new(None)); > = Arc::new(RwLock::new(None));
let mode = Arc::new(RwLock::new(KeyboardMode::UpperCase)); let mode = Arc::new(RwLock::new(KeyboardMode::UpperCase));
let (lower_case, upper_case, specials) = Self::setup( let (lower_case, upper_case, specials) = Self::setup(
world, commands,
gui_handler,
text_field.clone(), text_field.clone(),
&mode, &mode,
decline_callback.clone(), decline_callback.clone(),
@ -83,17 +99,30 @@ impl Keyboard {
} }
fn setup( fn setup(
world: &mut World, commands: &mut Commands,
gui_handler: &mut GuiHandler,
textfield: Arc<TextField>, textfield: Arc<TextField>,
mode: &Arc<RwLock<KeyboardMode>>, mode: &Arc<RwLock<KeyboardMode>>,
decline_callback: Arc<RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>>, decline_callback: Arc<
RwLock<Option<Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>>>,
>,
accept_callback: Arc< accept_callback: Arc<
RwLock<Option<Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>>>, RwLock<
Option<
Box<
dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()>
+ Send
+ Sync,
>,
>,
>,
>, >,
) -> Result<(Arc<GuiBuilder>, Arc<GuiBuilder>, Arc<GuiBuilder>)> { ) -> Result<(Arc<GuiBuilder>, Arc<GuiBuilder>, Arc<GuiBuilder>)> {
let lower_case = GuiBuilder::from_str(world, include_str!("lower_case.xml"))?; let lower_case =
let upper_case = GuiBuilder::from_str(world, include_str!("upper_case.xml"))?; GuiBuilder::from_str(commands, gui_handler, include_str!("lower_case.xml"))?;
let specials = GuiBuilder::from_str(world, include_str!("specials.xml"))?; let upper_case =
GuiBuilder::from_str(commands, gui_handler, include_str!("upper_case.xml"))?;
let specials = GuiBuilder::from_str(commands, gui_handler, include_str!("specials.xml"))?;
// first row // first row
Self::set_text_callback(&lower_case, "q", textfield.clone())?; Self::set_text_callback(&lower_case, "q", textfield.clone())?;
@ -195,31 +224,35 @@ impl Keyboard {
Self::set_text_callback(&specials, ".", textfield.clone())?; Self::set_text_callback(&specials, ".", textfield.clone())?;
Self::set_text_callback(&specials, "_", textfield.clone())?; Self::set_text_callback(&specials, "_", textfield.clone())?;
let back = Box::new(move |world: &mut World| { let back = Box::new(
if let Some(callback) = decline_callback.read().unwrap().as_ref() { move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
(callback)(world)?; if let Some(callback) = decline_callback.read().unwrap().as_ref() {
} (callback)(commands, gui_handler)?;
}
Ok(()) Ok(())
}); },
);
let accept = { let accept = {
let weak_textfield = Arc::downgrade(&textfield); let weak_textfield = Arc::downgrade(&textfield);
let weak_accept = Arc::downgrade(&accept_callback); let weak_accept = Arc::downgrade(&accept_callback);
Box::new(move |world: &mut World| { Box::new(
if let Some(textfield) = weak_textfield.upgrade() { move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
if let Some(accept_callback) = weak_accept.upgrade() { if let Some(textfield) = weak_textfield.upgrade() {
if let Some(text) = textfield.text() { if let Some(accept_callback) = weak_accept.upgrade() {
if let Some(callback) = accept_callback.read().unwrap().as_ref() { if let Some(text) = textfield.text() {
(callback)(world, &text)?; if let Some(callback) = accept_callback.read().unwrap().as_ref() {
(callback)(commands, gui_handler, &text)?;
}
} }
} }
} }
}
Ok(()) Ok(())
}) },
)
}; };
let switch_to_upper = { let switch_to_upper = {
@ -227,22 +260,24 @@ impl Keyboard {
let weak_lower = Arc::downgrade(&lower_case); let weak_lower = Arc::downgrade(&lower_case);
let weak_upper = Arc::downgrade(&upper_case); let weak_upper = Arc::downgrade(&upper_case);
Box::new(move |world: &mut World| { Box::new(
if let Some(lower) = weak_lower.upgrade() { move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
if let Some(upper) = weak_upper.upgrade() { if let Some(lower) = weak_lower.upgrade() {
let mut mode = mode.write().unwrap(); if let Some(upper) = weak_upper.upgrade() {
let mut mode = mode.write().unwrap();
if let KeyboardMode::LowerCase = mode.deref() { if let KeyboardMode::LowerCase = mode.deref() {
*mode = KeyboardMode::UpperCase; *mode = KeyboardMode::UpperCase;
lower.disable(world)?; lower.disable(commands, gui_handler)?;
upper.enable(world)?; upper.enable(commands, gui_handler)?;
}
} }
} }
}
Ok(()) Ok(())
}) },
)
}; };
let switch_to_special = { let switch_to_special = {
@ -250,22 +285,24 @@ impl Keyboard {
let weak_upper = Arc::downgrade(&upper_case); let weak_upper = Arc::downgrade(&upper_case);
let weak_specials = Arc::downgrade(&specials); let weak_specials = Arc::downgrade(&specials);
Box::new(move |world: &mut World| { Box::new(
if let Some(specials) = weak_specials.upgrade() { move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
if let Some(upper) = weak_upper.upgrade() { if let Some(specials) = weak_specials.upgrade() {
let mut mode = mode.write().unwrap(); if let Some(upper) = weak_upper.upgrade() {
let mut mode = mode.write().unwrap();
if let KeyboardMode::UpperCase = mode.deref() { if let KeyboardMode::UpperCase = mode.deref() {
*mode = KeyboardMode::Specials; *mode = KeyboardMode::Specials;
upper.disable(world)?; upper.disable(commands, gui_handler)?;
specials.enable(world)?; specials.enable(commands, gui_handler)?;
}
} }
} }
}
Ok(()) Ok(())
}) },
)
}; };
let switch_to_lower = { let switch_to_lower = {
@ -273,34 +310,38 @@ impl Keyboard {
let weak_lower = Arc::downgrade(&lower_case); let weak_lower = Arc::downgrade(&lower_case);
let weak_specials = Arc::downgrade(&specials); let weak_specials = Arc::downgrade(&specials);
Box::new(move |world: &mut World| { Box::new(
if let Some(lower) = weak_lower.upgrade() { move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
if let Some(specials) = weak_specials.upgrade() { if let Some(lower) = weak_lower.upgrade() {
let mut mode = mode.write().unwrap(); if let Some(specials) = weak_specials.upgrade() {
let mut mode = mode.write().unwrap();
if let KeyboardMode::Specials = mode.deref() { if let KeyboardMode::Specials = mode.deref() {
*mode = KeyboardMode::LowerCase; *mode = KeyboardMode::LowerCase;
specials.disable(world)?; specials.disable(commands, gui_handler)?;
lower.enable(world)?; lower.enable(commands, gui_handler)?;
}
} }
} }
}
Ok(()) Ok(())
}) },
)
}; };
let space_bar = { let space_bar = {
let weak_textfield = Arc::downgrade(&textfield); let weak_textfield = Arc::downgrade(&textfield);
Box::new(move |world: &mut World| { Box::new(
if let Some(text_field) = weak_textfield.upgrade() { move |_commands: &mut Commands, gui_handler: &mut GuiHandler| {
text_field.add_letter(world.resources.get_mut()?, ' ')?; if let Some(text_field) = weak_textfield.upgrade() {
} text_field.add_letter(gui_handler, ' ')?;
}
Ok(()) Ok(())
}) },
)
}; };
lower_case.set_click_callbacks(vec![ lower_case.set_click_callbacks(vec![
@ -335,40 +376,51 @@ impl Keyboard {
let weak_textfield = Arc::downgrade(&textfield); let weak_textfield = Arc::downgrade(&textfield);
let weak_button = Arc::downgrade(&button_ref); let weak_button = Arc::downgrade(&button_ref);
button_ref.set_callback(Box::new(move |world: &mut World| { button_ref.set_callback(Box::new(
if let Some(textfield) = weak_textfield.upgrade() { move |_commands: &mut Commands, gui_handler: &mut GuiHandler| {
if let Some(button) = weak_button.upgrade() { if let Some(textfield) = weak_textfield.upgrade() {
if let Some(text) = button.text()? { if let Some(button) = weak_button.upgrade() {
textfield if let Some(text) = button.text()? {
.add_letter(world.resources.get_mut()?, text.as_bytes()[0] as char)?; textfield.add_letter(gui_handler, text.as_bytes()[0] as char)?;
}
} }
} }
}
Ok(()) Ok(())
})); },
));
Ok(()) Ok(())
} }
} }
impl TopGui for Keyboard { impl TopGui for Keyboard {
fn decline(&self, world: &mut World) -> Result<()> { fn decline(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
if let Some(callback) = self.decline_callback.read().unwrap().as_ref() { if let Some(callback) = self.decline_callback.read().unwrap().as_ref() {
(callback)(world)?; (callback)(commands, gui_handler)?;
} }
Ok(()) Ok(())
} }
fn next_tab(&self, _world: &mut World, _: bool) -> Result<()> { fn next_tab(
&self,
_commands: &mut Commands,
_gui_handler: &mut GuiHandler,
_: bool,
) -> Result<()> {
Ok(()) Ok(())
} }
fn previous_tab(&self, world: &mut World, second_level: bool) -> Result<()> { fn previous_tab(
&self,
_commands: &mut Commands,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()> {
// abuse event // abuse event
if !second_level { if !second_level {
self.text_field.remove_last(world.resources.get_mut()?)?; self.text_field.remove_last(gui_handler)?;
} }
Ok(()) Ok(())
@ -386,7 +438,12 @@ impl Visibility for Keyboard {
} }
} }
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> { fn set_visibility(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
visibility: bool,
) -> Result<()> {
let mode = self.mode.read().unwrap(); let mode = self.mode.read().unwrap();
let gui = match mode.deref() { let gui = match mode.deref() {
@ -396,11 +453,11 @@ impl Visibility for Keyboard {
}; };
if visibility { if visibility {
gui.enable(world)?; gui.enable(commands, gui_handler)?;
self.text_field_gui.enable(world)?; self.text_field_gui.enable(commands, gui_handler)?;
} else { } else {
gui.disable(world)?; gui.disable(commands, gui_handler)?;
self.text_field_gui.disable(world)?; self.text_field_gui.disable(commands, gui_handler)?;
} }
Ok(()) Ok(())
@ -438,15 +495,15 @@ impl TopLevelGui for Keyboard {
Some(self) Some(self)
} }
fn enable(&self, world: &mut World) -> Result<()> { fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(world, true)?; self.set_visibility(commands, gui_handler, true)?;
self.text_field.focus_input(world.resources.get_mut()?)?; self.text_field.focus_input(gui_handler)?;
Ok(()) Ok(())
} }
fn disable(&self, world: &mut World) -> Result<()> { fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.set_visibility(world, false)?; self.set_visibility(commands, gui_handler, false)?;
Ok(()) Ok(())
} }
@ -455,7 +512,10 @@ impl TopLevelGui for Keyboard {
impl Functionality for Keyboard { impl Functionality for Keyboard {
fn set_click_callbacks( fn set_click_callbacks(
&self, &self,
callbacks: Vec<(&str, Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>)>, callbacks: Vec<(
&str,
Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>,
)>,
) -> Result<()> { ) -> Result<()> {
if let Some((_, callback)) = callbacks.into_iter().find(|(name, _)| *name == "decline") { if let Some((_, callback)) = callbacks.into_iter().find(|(name, _)| *name == "decline") {
*self.decline_callback.write().unwrap() = Some(callback); *self.decline_callback.write().unwrap() = Some(callback);
@ -468,7 +528,7 @@ impl Functionality for Keyboard {
&self, &self,
_: Vec<( _: Vec<(
&str, &str,
Box<dyn Fn(&mut World, bool) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
Ok(()) Ok(())
@ -478,7 +538,11 @@ impl Functionality for Keyboard {
&self, &self,
_: Vec<( _: Vec<(
&str, &str,
Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>, Box<
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync,
>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
Ok(()) Ok(())
@ -488,7 +552,7 @@ impl Functionality for Keyboard {
&self, &self,
callbacks: Vec<( callbacks: Vec<(
&str, &str,
Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
)>, )>,
) -> Result<()> { ) -> Result<()> {
for (name, callback) in callbacks { for (name, callback) in callbacks {

View file

@ -111,12 +111,13 @@ pub(crate) struct State {
impl State { impl State {
pub(crate) fn new<'a>( pub(crate) fn new<'a>(
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
name: &str, name: &str,
creation_type: CreationType<'a>, creation_type: CreationType<'a>,
) -> Result<Arc<Self>> { ) -> Result<Arc<Self>> {
let gui = match creation_type { let gui = match creation_type {
CreationType::File(path) => GuiBuilder::new(gui_handler, path)?, CreationType::File(path) => GuiBuilder::new(commands, gui_handler, path)?,
CreationType::TopGui(top_gui) => top_gui, CreationType::TopGui(top_gui) => top_gui,
}; };
@ -215,14 +216,14 @@ impl State {
} }
impl TopGui for State { impl TopGui for State {
fn decline(&self, commands: &mut Commands) -> Result<()> { fn decline(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
match self.decline.read().unwrap().as_ref() { match self.decline.read().unwrap().as_ref() {
Some(decline) => { Some(decline) => {
(decline)(commands)?; (decline)(commands)?;
} }
None => { None => {
if let Some(top_gui) = self.top_level_gui.top_gui() { if let Some(top_gui) = self.top_level_gui.top_gui() {
top_gui.decline(commands)?; top_gui.decline(commands, gui_handler)?;
} }
} }
} }
@ -230,14 +231,19 @@ impl TopGui for State {
Ok(()) Ok(())
} }
fn next_tab(&self, commands: &mut Commands, second_level: bool) -> Result<()> { fn next_tab(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()> {
match self.next_tab.read().unwrap().as_ref() { match self.next_tab.read().unwrap().as_ref() {
Some(next_tab) => { Some(next_tab) => {
(next_tab)(commands)?; (next_tab)(commands)?;
} }
None => { None => {
if let Some(top_gui) = self.top_level_gui.top_gui() { if let Some(top_gui) = self.top_level_gui.top_gui() {
top_gui.next_tab(commands, second_level)?; top_gui.next_tab(commands, gui_handler, second_level)?;
} }
} }
} }
@ -245,14 +251,19 @@ impl TopGui for State {
Ok(()) Ok(())
} }
fn previous_tab(&self, commands: &mut Commands, second_level: bool) -> Result<()> { fn previous_tab(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()> {
match self.previous_tab.read().unwrap().as_ref() { match self.previous_tab.read().unwrap().as_ref() {
Some(previous_tab) => { Some(previous_tab) => {
(previous_tab)(commands)?; (previous_tab)(commands)?;
} }
None => { None => {
if let Some(top_gui) = self.top_level_gui.top_gui() { if let Some(top_gui) = self.top_level_gui.top_gui() {
top_gui.previous_tab(commands, second_level)?; top_gui.previous_tab(commands, gui_handler, second_level)?;
} }
} }
} }
@ -278,8 +289,8 @@ impl TopLevelGui for State {
self.top_level_gui.functionality() self.top_level_gui.functionality()
} }
fn enable(&self, commands: &mut Commands) -> Result<()> { fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.top_level_gui.enable(commands)?; self.top_level_gui.enable(commands, gui_handler)?;
if let Some(activate) = self.on_activate.read().unwrap().as_ref() { if let Some(activate) = self.on_activate.read().unwrap().as_ref() {
(activate)(commands)?; (activate)(commands)?;
@ -288,8 +299,8 @@ impl TopLevelGui for State {
Ok(()) Ok(())
} }
fn disable(&self, commands: &mut Commands) -> Result<()> { fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.top_level_gui.disable(commands)?; self.top_level_gui.disable(commands, gui_handler)?;
if let Some(deactivate) = self.on_deactivate.read().unwrap().as_ref() { if let Some(deactivate) = self.on_deactivate.read().unwrap().as_ref() {
(deactivate)(commands)?; (deactivate)(commands)?;

View file

@ -63,7 +63,7 @@ pub enum StateUpdateType<'a> {
ClickCallbacks( ClickCallbacks(
Vec<( Vec<(
&'a str, &'a str,
Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>,
)>, )>,
), ),
@ -71,7 +71,7 @@ pub enum StateUpdateType<'a> {
SelectCallbacks( SelectCallbacks(
Vec<( Vec<(
&'a str, &'a str,
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
)>, )>,
), ),
@ -79,7 +79,11 @@ pub enum StateUpdateType<'a> {
CustomClickCallbacks( CustomClickCallbacks(
Vec<( Vec<(
&'a str, &'a str,
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>, Box<
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
+ Send
+ Sync,
>,
)>, )>,
), ),
@ -87,7 +91,7 @@ pub enum StateUpdateType<'a> {
VecCallbacks( VecCallbacks(
Vec<( Vec<(
&'a str, &'a str,
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>, Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
)>, )>,
), ),
@ -166,13 +170,14 @@ impl States {
/// Adds a single state /// Adds a single state
pub fn add_state<'a>( pub fn add_state<'a>(
&mut self, &mut self,
commands: &mut Commands,
gui_handler: &mut GuiHandler, gui_handler: &mut GuiHandler,
id: &str, id: &str,
creation_type: impl Into<CreationType<'a>>, creation_type: impl Into<CreationType<'a>>,
) -> Result<()> { ) -> Result<()> {
self.states.insert( self.states.insert(
id.to_string(), id.to_string(),
State::new(gui_handler, id, creation_type.into())?, State::new(commands, gui_handler, id, creation_type.into())?,
); );
Ok(()) Ok(())
@ -222,13 +227,13 @@ impl States {
} }
// execute deactivate on old state // execute deactivate on old state
old_state.disable(commands)?; old_state.disable(commands, gui_handler)?;
} }
// set new state, either no state or requested state // set new state, either no state or requested state
match state { match state {
Some(state) => { Some(state) => {
state.enable(commands)?; state.enable(commands, gui_handler)?;
gui_handler.set_top_gui(commands, Some(state.clone()))?; gui_handler.set_top_gui(commands, Some(state.clone()))?;
if logging { if logging {