Start redesigning ui to match new ecs
This commit is contained in:
parent
645dc8d017
commit
74ae317fec
14 changed files with 191 additions and 211 deletions
|
@ -23,31 +23,31 @@ pub struct GuiBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GuiBuilder {
|
impl GuiBuilder {
|
||||||
pub fn new(world: &mut World, path: &AssetPath) -> Result<Arc<Self>> {
|
pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result<Arc<Self>> {
|
||||||
let validator = Validator::new(
|
let validator = Validator::new(
|
||||||
#[cfg(feature = "audio")]
|
#[cfg(feature = "audio")]
|
||||||
world.resources.get::<GuiHandler>(),
|
gui_handler,
|
||||||
path,
|
path,
|
||||||
)
|
)
|
||||||
.with_context(|| format!("validator for {}", path.full_path()))?;
|
.with_context(|| format!("validator for {}", path.full_path()))?;
|
||||||
|
|
||||||
Ok(Arc::new(Self::_new(world, validator).with_context(
|
Ok(Arc::new(Self::_new(gui_handler, validator).with_context(
|
||||||
|| format!("for file {}", path.full_path()),
|
|| format!("for file {}", path.full_path()),
|
||||||
)?))
|
)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(world: &mut World, s: &str) -> Result<Arc<Self>> {
|
pub fn from_str(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")]
|
||||||
world.resources.get::<GuiHandler>(),
|
gui_handler,
|
||||||
s,
|
s,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Ok(Arc::new(Self::_new(world, validator)?))
|
Ok(Arc::new(Self::_new(gui_handler, validator)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn merge<'a>(
|
pub fn merge<'a>(
|
||||||
world: &mut World,
|
gui_handler: &mut GuiHandler,
|
||||||
pathes: impl IntoIterator<Item = &'a AssetPath>,
|
pathes: impl IntoIterator<Item = &'a AssetPath>,
|
||||||
) -> Result<Arc<Self>> {
|
) -> Result<Arc<Self>> {
|
||||||
let mut me = Self {
|
let mut me = Self {
|
||||||
|
@ -62,11 +62,11 @@ impl GuiBuilder {
|
||||||
for path in pathes.into_iter() {
|
for path in pathes.into_iter() {
|
||||||
let validator = Validator::new(
|
let validator = Validator::new(
|
||||||
#[cfg(feature = "audio")]
|
#[cfg(feature = "audio")]
|
||||||
world.resources.get::<GuiHandler>(),
|
gui_handler,
|
||||||
path,
|
path,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let builder = Self::_new(world, validator)
|
let builder = Self::_new(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 +86,7 @@ impl GuiBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn _new(world: &mut World, validator: Validator) -> Result<Self> {
|
fn _new(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();
|
||||||
|
@ -95,11 +95,8 @@ impl GuiBuilder {
|
||||||
|
|
||||||
let mut grids = Vec::new();
|
let mut grids = Vec::new();
|
||||||
|
|
||||||
let gui_handler = world.resources.get_mut_unchecked::<GuiHandler>();
|
|
||||||
|
|
||||||
for child in &root.children {
|
for child in &root.children {
|
||||||
let tree = Self::create_tree(
|
let tree = Self::create_tree(
|
||||||
world,
|
|
||||||
gui_handler,
|
gui_handler,
|
||||||
&mut ids,
|
&mut ids,
|
||||||
child,
|
child,
|
||||||
|
@ -182,7 +179,7 @@ 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 World) -> Result<()> + Send + Sync>)>,
|
functions: Vec<(&str, Box<dyn Fn(&mut Commands) -> 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);
|
||||||
|
@ -198,7 +195,7 @@ impl Functionality for GuiBuilder {
|
||||||
&self,
|
&self,
|
||||||
_functions: Vec<(
|
_functions: Vec<(
|
||||||
&str,
|
&str,
|
||||||
Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -208,7 +205,7 @@ impl Functionality for GuiBuilder {
|
||||||
&self,
|
&self,
|
||||||
callbacks: Vec<(
|
callbacks: Vec<(
|
||||||
&str,
|
&str,
|
||||||
Box<dyn Fn(&mut World, bool) -> Result<()> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for (function_name, callback) in callbacks {
|
for (function_name, callback) in callbacks {
|
||||||
|
@ -225,7 +222,7 @@ impl Functionality for GuiBuilder {
|
||||||
&self,
|
&self,
|
||||||
callbacks: Vec<(
|
callbacks: Vec<(
|
||||||
&str,
|
&str,
|
||||||
Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
for (function_name, callback) in callbacks {
|
for (function_name, callback) in callbacks {
|
||||||
|
@ -345,7 +342,6 @@ impl_element!(MultiLineTextField);
|
||||||
// private
|
// private
|
||||||
impl GuiBuilder {
|
impl GuiBuilder {
|
||||||
fn create_tree(
|
fn create_tree(
|
||||||
world: &mut World,
|
|
||||||
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>,
|
||||||
|
@ -398,7 +394,6 @@ 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(
|
||||||
world,
|
|
||||||
gui_handler,
|
gui_handler,
|
||||||
child,
|
child,
|
||||||
&root_grid,
|
&root_grid,
|
||||||
|
@ -413,7 +408,6 @@ impl GuiBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn create_child(
|
fn create_child(
|
||||||
world: &mut World,
|
|
||||||
gui_handler: &mut GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
child: &UiInfoElement,
|
child: &UiInfoElement,
|
||||||
grid: &Grid,
|
grid: &Grid,
|
||||||
|
@ -429,7 +423,7 @@ impl GuiBuilder {
|
||||||
Self::insert_id(ids, &button_info.id, &button)?;
|
Self::insert_id(ids, &button_info.id, &button)?;
|
||||||
|
|
||||||
grid.attach(
|
grid.attach(
|
||||||
world,
|
gui_handler,
|
||||||
button.clone(),
|
button.clone(),
|
||||||
button_info
|
button_info
|
||||||
.x_slot
|
.x_slot
|
||||||
|
@ -464,7 +458,7 @@ impl GuiBuilder {
|
||||||
Self::insert_id(ids, &label_info.id, &label)?;
|
Self::insert_id(ids, &label_info.id, &label)?;
|
||||||
|
|
||||||
grid.attach(
|
grid.attach(
|
||||||
world,
|
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")?,
|
||||||
label_info.y_slot.get().with_context(|| "y_slot of label")?,
|
label_info.y_slot.get().with_context(|| "y_slot of label")?,
|
||||||
|
@ -476,12 +470,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, world, gui_handler)?;
|
MultiLineLabel::try_from(multi_line_label_info, 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(
|
||||||
world,
|
gui_handler,
|
||||||
multi_line_label.clone(),
|
multi_line_label.clone(),
|
||||||
multi_line_label_info
|
multi_line_label_info
|
||||||
.x_slot
|
.x_slot
|
||||||
|
@ -499,12 +493,12 @@ impl GuiBuilder {
|
||||||
}
|
}
|
||||||
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(multi_line_text_field_info, world, gui_handler)?;
|
MultiLineTextField::try_from(multi_line_text_field_info, 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(
|
||||||
world,
|
gui_handler,
|
||||||
multi_line_text_field.clone(),
|
multi_line_text_field.clone(),
|
||||||
multi_line_text_field_info
|
multi_line_text_field_info
|
||||||
.x_slot
|
.x_slot
|
||||||
|
@ -526,7 +520,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(
|
||||||
world,
|
gui_handler,
|
||||||
text_field.clone(),
|
text_field.clone(),
|
||||||
text_field_info
|
text_field_info
|
||||||
.x_slot
|
.x_slot
|
||||||
|
@ -548,7 +542,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(
|
||||||
world,
|
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")?,
|
||||||
icon_info.y_slot.get().with_context(|| "y_slot of icon")?,
|
icon_info.y_slot.get().with_context(|| "y_slot of icon")?,
|
||||||
|
@ -564,7 +558,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(
|
||||||
world,
|
gui_handler,
|
||||||
progress_bar.clone(),
|
progress_bar.clone(),
|
||||||
progress_bar_info
|
progress_bar_info
|
||||||
.x_slot
|
.x_slot
|
||||||
|
@ -586,7 +580,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(
|
||||||
world,
|
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")?,
|
||||||
grid_info.y_slot.get().with_context(|| "y_slot of grid")?,
|
grid_info.y_slot.get().with_context(|| "y_slot of grid")?,
|
||||||
|
@ -599,7 +593,6 @@ 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(
|
||||||
world,
|
|
||||||
gui_handler,
|
gui_handler,
|
||||||
child,
|
child,
|
||||||
&sub_grid,
|
&sub_grid,
|
||||||
|
|
|
@ -18,27 +18,27 @@ pub struct GuiSnippet {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GuiSnippet {
|
impl GuiSnippet {
|
||||||
pub fn new(world: &mut World, path: &AssetPath) -> Result<Arc<Self>> {
|
pub fn new(gui_handler: &mut GuiHandler, path: &AssetPath) -> Result<Arc<Self>> {
|
||||||
let validator = Validator::new(
|
let validator = Validator::new(
|
||||||
#[cfg(feature = "audio")]
|
#[cfg(feature = "audio")]
|
||||||
world.resources.get::<GuiHandler>(),
|
gui_handler,
|
||||||
path,
|
path,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Self::_new(world, validator)
|
Self::_new(gui_handler, validator)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_str(world: &mut World, s: &str) -> Result<Arc<Self>> {
|
pub fn from_str(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")]
|
||||||
world.resources.get::<GuiHandler>(),
|
gui_handler,
|
||||||
s,
|
s,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
Self::_new(world, validator)
|
Self::_new(gui_handler, validator)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn _new(world: &mut World, validator: Validator) -> Result<Arc<Self>> {
|
fn _new(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();
|
||||||
|
@ -51,22 +51,13 @@ impl GuiSnippet {
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
let gui_handler = world.resources.get_mut_unchecked::<GuiHandler>();
|
|
||||||
|
|
||||||
let grid_info = &root.children[0];
|
let grid_info = &root.children[0];
|
||||||
let grid = Grid::try_from(gui_handler, grid_info, false)?;
|
let grid = Grid::try_from(gui_handler, grid_info, false)?;
|
||||||
|
|
||||||
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(
|
Self::create_child(gui_handler, child, &grid, &mut ids, &mut custom_neighbours)?;
|
||||||
world,
|
|
||||||
gui_handler,
|
|
||||||
child,
|
|
||||||
&grid,
|
|
||||||
&mut ids,
|
|
||||||
&mut custom_neighbours,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GuiBuilder::connect_custom_neighbours(&ids, custom_neighbours)?;
|
GuiBuilder::connect_custom_neighbours(&ids, custom_neighbours)?;
|
||||||
|
@ -216,7 +207,6 @@ impl Functionality for GuiSnippet {
|
||||||
|
|
||||||
impl GuiSnippet {
|
impl GuiSnippet {
|
||||||
fn create_child(
|
fn create_child(
|
||||||
world: &mut World,
|
|
||||||
gui_handler: &mut GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
child: &UiInfoElement,
|
child: &UiInfoElement,
|
||||||
grid: &Grid,
|
grid: &Grid,
|
||||||
|
@ -230,7 +220,7 @@ impl GuiSnippet {
|
||||||
GuiBuilder::insert_id(ids, &button_info.id, &button)?;
|
GuiBuilder::insert_id(ids, &button_info.id, &button)?;
|
||||||
|
|
||||||
grid.attach(
|
grid.attach(
|
||||||
world,
|
gui_handler,
|
||||||
button.clone(),
|
button.clone(),
|
||||||
button_info.x_slot.get()?,
|
button_info.x_slot.get()?,
|
||||||
button_info.y_slot.get()?,
|
button_info.y_slot.get()?,
|
||||||
|
@ -246,7 +236,7 @@ impl GuiSnippet {
|
||||||
GuiBuilder::insert_id(ids, &label_info.id, &label)?;
|
GuiBuilder::insert_id(ids, &label_info.id, &label)?;
|
||||||
|
|
||||||
grid.attach(
|
grid.attach(
|
||||||
world,
|
gui_handler,
|
||||||
label,
|
label,
|
||||||
label_info.x_slot.get()?,
|
label_info.x_slot.get()?,
|
||||||
label_info.y_slot.get()?,
|
label_info.y_slot.get()?,
|
||||||
|
@ -256,12 +246,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, world, gui_handler)?;
|
MultiLineLabel::try_from(multi_line_label_info, 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(
|
||||||
world,
|
gui_handler,
|
||||||
multi_line_label,
|
multi_line_label,
|
||||||
multi_line_label_info.x_slot.get()?,
|
multi_line_label_info.x_slot.get()?,
|
||||||
multi_line_label_info.y_slot.get()?,
|
multi_line_label_info.y_slot.get()?,
|
||||||
|
@ -271,12 +261,12 @@ 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(multi_line_text_field_info, world, gui_handler)?;
|
MultiLineTextField::try_from(multi_line_text_field_info, 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(
|
||||||
world,
|
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()?,
|
||||||
multi_line_text_field_info.y_slot.get()?,
|
multi_line_text_field_info.y_slot.get()?,
|
||||||
|
@ -290,7 +280,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(
|
||||||
world,
|
gui_handler,
|
||||||
text_field,
|
text_field,
|
||||||
text_field_info.x_slot.get()?,
|
text_field_info.x_slot.get()?,
|
||||||
text_field_info.y_slot.get()?,
|
text_field_info.y_slot.get()?,
|
||||||
|
@ -304,7 +294,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(
|
||||||
world,
|
gui_handler,
|
||||||
icon,
|
icon,
|
||||||
icon_info.x_slot.get()?,
|
icon_info.x_slot.get()?,
|
||||||
icon_info.y_slot.get()?,
|
icon_info.y_slot.get()?,
|
||||||
|
@ -318,7 +308,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(
|
||||||
world,
|
gui_handler,
|
||||||
progress_bar,
|
progress_bar,
|
||||||
progress_bar_info.x_slot.get()?,
|
progress_bar_info.x_slot.get()?,
|
||||||
progress_bar_info.y_slot.get()?,
|
progress_bar_info.y_slot.get()?,
|
||||||
|
@ -332,7 +322,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(
|
||||||
world,
|
gui_handler,
|
||||||
sub_grid.clone(),
|
sub_grid.clone(),
|
||||||
grid_info.x_slot.get()?,
|
grid_info.x_slot.get()?,
|
||||||
grid_info.y_slot.get()?,
|
grid_info.y_slot.get()?,
|
||||||
|
@ -341,7 +331,7 @@ impl GuiSnippet {
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
for child in grid_info.children.read().unwrap().iter() {
|
for child in grid_info.children.read().unwrap().iter() {
|
||||||
Self::create_child(world, gui_handler, child, &sub_grid, ids, neighbour_infos)?;
|
Self::create_child(gui_handler, child, &sub_grid, ids, neighbour_infos)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -315,23 +315,23 @@ impl Button {
|
||||||
|
|
||||||
pub fn set_callback<F>(&self, callback: F)
|
pub fn set_callback<F>(&self, callback: F)
|
||||||
where
|
where
|
||||||
F: Fn(&mut World) -> Result<()> + Send + Sync + 'static,
|
F: Fn(&mut Commands) -> Result<()> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.click_executable
|
self.click_executable
|
||||||
.set_callback(move |world: &mut World, _: ()| callback(world));
|
.set_callback(move |commands: &mut Commands, _: ()| callback(commands));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_select_callback<F>(&self, callback: F)
|
pub fn set_select_callback<F>(&self, callback: F)
|
||||||
where
|
where
|
||||||
F: Fn(&mut World, bool) -> Result<()> + Send + Sync + 'static,
|
F: Fn(&mut Commands, bool) -> Result<()> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.on_select_executable
|
self.on_select_executable
|
||||||
.set_callback(move |world: &mut World, select: bool| callback(world, select));
|
.set_callback(move |commands: &mut Commands, select: bool| callback(commands, select));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_custom_callback<F>(&self, callback: F)
|
pub fn set_custom_callback<F>(&self, callback: F)
|
||||||
where
|
where
|
||||||
F: Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync + 'static,
|
F: Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.selectable.set_custom_callback(callback);
|
self.selectable.set_custom_callback(callback);
|
||||||
}
|
}
|
||||||
|
@ -448,10 +448,8 @@ impl Visibility for Button {
|
||||||
self.visible.load(SeqCst)
|
self.visible.load(SeqCst)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_visibility(&self, world: &mut World, 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) {
|
||||||
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
|
|
||||||
|
|
||||||
self.visible.store(visibility, SeqCst);
|
self.visible.store(visibility, SeqCst);
|
||||||
|
|
||||||
if visibility {
|
if visibility {
|
||||||
|
@ -581,7 +579,7 @@ 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 |world: &mut World, selected: bool| {
|
let selected_changed = move |commands: &mut Commands, selected: bool| {
|
||||||
if let Some(button) = button_weak.upgrade() {
|
if let Some(button) = button_weak.upgrade() {
|
||||||
let gui_handler = world.resources.get_mut()?;
|
let gui_handler = world.resources.get_mut()?;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ecs::*;
|
|
||||||
|
|
||||||
use crate::builder::validator::gridinfo::GridInfo;
|
use crate::builder::validator::gridinfo::GridInfo;
|
||||||
|
|
||||||
|
@ -200,7 +199,7 @@ impl Grid {
|
||||||
|
|
||||||
pub fn detach(
|
pub fn detach(
|
||||||
&self,
|
&self,
|
||||||
world: &mut World,
|
gui_handler: &mut GuiHandler,
|
||||||
pos_x: usize,
|
pos_x: usize,
|
||||||
pos_y: usize,
|
pos_y: usize,
|
||||||
) -> Result<Option<Arc<dyn GuiElementTraits>>> {
|
) -> Result<Option<Arc<dyn GuiElementTraits>>> {
|
||||||
|
@ -232,7 +231,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(world, false)?;
|
child_visibility.set_visibility(gui_handler, false)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,7 +242,11 @@ impl Grid {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if item got detached
|
/// Returns `true` if item got detached
|
||||||
pub fn detach_item(&self, world: &mut World, item: Arc<dyn GuiElementTraits>) -> Result<bool> {
|
pub fn detach_item(
|
||||||
|
&self,
|
||||||
|
gui_handler: &mut GuiHandler,
|
||||||
|
item: Arc<dyn GuiElementTraits>,
|
||||||
|
) -> Result<bool> {
|
||||||
let mut grid = self.children.write().unwrap();
|
let mut grid = self.children.write().unwrap();
|
||||||
let mut removed = false;
|
let mut removed = false;
|
||||||
|
|
||||||
|
@ -262,7 +265,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(world, false)?;
|
child_visibility.set_visibility(gui_handler, false)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -405,7 +408,7 @@ impl Grid {
|
||||||
|
|
||||||
pub fn attach(
|
pub fn attach(
|
||||||
&self,
|
&self,
|
||||||
world: &mut World,
|
gui_handler: &mut GuiHandler,
|
||||||
child: Arc<dyn GuiElementTraits>,
|
child: Arc<dyn GuiElementTraits>,
|
||||||
pos_x: usize,
|
pos_x: usize,
|
||||||
pos_y: usize,
|
pos_y: usize,
|
||||||
|
@ -444,14 +447,7 @@ impl Grid {
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.framable.is_framed() {
|
if self.framable.is_framed() {
|
||||||
self.child_position(
|
self.child_position(gui_handler, child_gridable, pos_x, pos_y, dim_x, dim_y)?;
|
||||||
world.resources.get_mut()?,
|
|
||||||
child_gridable,
|
|
||||||
pos_x,
|
|
||||||
pos_y,
|
|
||||||
dim_x,
|
|
||||||
dim_y,
|
|
||||||
)?;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
child_gridable.set_layer(self.framable.ui_layer())?;
|
child_gridable.set_layer(self.framable.ui_layer())?;
|
||||||
|
@ -462,7 +458,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(world, false)?;
|
child_visibility.set_visibility(gui_handler, false)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -472,7 +468,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(world, true)?;
|
child_visibility.set_visibility(gui_handler, true)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -626,26 +622,26 @@ impl Grid {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn disable_tree(&self, world: &mut World, 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() {
|
||||||
background.disable(gui_handler)?;
|
background.disable(gui_handler)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.set_tree_visibility(world, false)?;
|
self.set_tree_visibility(gui_handler, false)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_tree_visibility(&self, world: &mut World, 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() {
|
||||||
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(world, visible)?;
|
visibility.set_visibility(gui_handler, visible)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -797,10 +793,8 @@ impl Visibility for Grid {
|
||||||
self.visible.load(SeqCst)
|
self.visible.load(SeqCst)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_visibility(&self, world: &mut World, 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) {
|
||||||
let gui_handler = world.resources.get_mut_unchecked::<GuiHandler>();
|
|
||||||
|
|
||||||
self.visible.store(visibility, SeqCst);
|
self.visible.store(visibility, SeqCst);
|
||||||
|
|
||||||
if visibility {
|
if visibility {
|
||||||
|
@ -810,9 +804,9 @@ impl Visibility for Grid {
|
||||||
background.enable(gui_handler)?;
|
background.enable(gui_handler)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.set_tree_visibility(world, true)?;
|
self.set_tree_visibility(gui_handler, true)?;
|
||||||
} else {
|
} else {
|
||||||
self.disable_tree(world, gui_handler)?;
|
self.disable_tree(gui_handler)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -57,11 +57,7 @@ impl MultiLineLabelBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(
|
pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<MultiLineLabel>> {
|
||||||
self,
|
|
||||||
world: &mut World,
|
|
||||||
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);
|
||||||
|
@ -77,7 +73,7 @@ impl MultiLineLabelBuilder {
|
||||||
.set_text_alignment(self.text_alignment)
|
.set_text_alignment(self.text_alignment)
|
||||||
.build(gui_handler)?;
|
.build(gui_handler)?;
|
||||||
|
|
||||||
base_grid.attach(world, label, 0, i as usize, 1, 1)?;
|
base_grid.attach(gui_handler, label, 0, i as usize, 1, 1)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Arc::new(MultiLineLabel {
|
Ok(Arc::new(MultiLineLabel {
|
||||||
|
@ -189,7 +185,6 @@ impl MultiLineLabel {
|
||||||
|
|
||||||
pub fn try_from(
|
pub fn try_from(
|
||||||
multi_line_label_info: &MultiLineLabelInfo,
|
multi_line_label_info: &MultiLineLabelInfo,
|
||||||
world: &mut World,
|
|
||||||
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();
|
||||||
|
@ -218,7 +213,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(world, gui_handler)
|
multi_line_label_builder.build(gui_handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iter_label<F>(&self, mut f: F) -> Result<()>
|
fn iter_label<F>(&self, mut f: F) -> Result<()>
|
||||||
|
|
|
@ -63,11 +63,7 @@ impl MultiLineTextFieldBuilder {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn build(
|
pub fn build(self, gui_handler: &mut GuiHandler) -> Result<Arc<MultiLineTextField>> {
|
||||||
self,
|
|
||||||
world: &mut World,
|
|
||||||
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);
|
||||||
|
@ -83,7 +79,7 @@ impl MultiLineTextFieldBuilder {
|
||||||
.set_text_alignment(self.text_alignment)
|
.set_text_alignment(self.text_alignment)
|
||||||
.build(gui_handler)?;
|
.build(gui_handler)?;
|
||||||
|
|
||||||
base_grid.attach(world, label, 0, i as usize, 1, 1)?;
|
base_grid.attach(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));
|
||||||
|
@ -101,7 +97,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 |world: &mut World, _text| {
|
move |commands: &mut Commands, _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(world.resources.get_mut()?)?;
|
||||||
}
|
}
|
||||||
|
@ -309,7 +305,6 @@ impl MultiLineTextField {
|
||||||
|
|
||||||
pub fn try_from(
|
pub fn try_from(
|
||||||
multi_line_text_field_info: &MultiLineTextFieldInfo,
|
multi_line_text_field_info: &MultiLineTextFieldInfo,
|
||||||
world: &mut World,
|
|
||||||
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();
|
||||||
|
@ -338,7 +333,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(world, gui_handler)
|
multi_line_text_field_builder.build(gui_handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn iter_label<F>(&self, mut f: F) -> Result<()>
|
fn iter_label<F>(&self, mut f: F) -> Result<()>
|
||||||
|
|
|
@ -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 World, Option<String>) -> Result<()> + Send + Sync + 'static,
|
F: Fn(&mut Commands, Option<String>) -> Result<()> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.text_changed_executable.set_callback(f);
|
self.text_changed_executable.set_callback(f);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ecs::World;
|
use ecs::*;
|
||||||
|
|
||||||
use std::{any::Any, collections::HashMap, sync::Arc};
|
use std::{any::Any, collections::HashMap, sync::Arc};
|
||||||
|
|
||||||
|
@ -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, world: &mut World) -> Result<()>;
|
fn enable(&self, commands: &mut Commands) -> Result<()>;
|
||||||
fn disable(&self, world: &mut World) -> Result<()>;
|
fn disable(&self, commands: &mut Commands) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn any_to<T>(any: &dyn Any) -> Result<&T>
|
pub fn any_to<T>(any: &dyn Any) -> Result<&T>
|
||||||
|
@ -27,14 +27,14 @@ where
|
||||||
pub trait Functionality {
|
pub trait Functionality {
|
||||||
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) -> 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 World, bool) -> Result<()> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ pub trait Functionality {
|
||||||
&self,
|
&self,
|
||||||
callbacks: Vec<(
|
callbacks: Vec<(
|
||||||
&str,
|
&str,
|
||||||
Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ pub trait Functionality {
|
||||||
&self,
|
&self,
|
||||||
callbacks: Vec<(
|
callbacks: Vec<(
|
||||||
&str,
|
&str,
|
||||||
Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
) -> Result<()>;
|
) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
@ -84,7 +84,7 @@ pub trait Gridable {
|
||||||
|
|
||||||
pub trait Visibility {
|
pub trait Visibility {
|
||||||
fn visible(&self) -> bool;
|
fn visible(&self) -> bool;
|
||||||
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()>;
|
fn set_visibility(&self, gui_handler: &mut GuiHandler, visibility: bool) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum GuiElement<'a> {
|
pub enum GuiElement<'a> {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! `Executable` is a property to execute a closure
|
//! `Executable` is a property to execute a closure
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ecs::World;
|
use ecs::*;
|
||||||
|
|
||||||
use std::sync::{Arc, RwLock};
|
use std::sync::{Arc, RwLock};
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ 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 World, I) -> Result<()> + Send + Sync>>>,
|
callback: RwLock<Option<Arc<dyn Fn(&mut Commands, I) -> Result<()> + Send + Sync>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: Send + Sync + 'static> Executable<I> {
|
impl<I: Send + Sync + 'static> Executable<I> {
|
||||||
|
@ -27,7 +27,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 World, I) -> Result<()> + Send + Sync + 'static,
|
F: Fn(&mut Commands, I) -> Result<()> + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
let mut function = self.callback.write().unwrap();
|
let mut function = self.callback.write().unwrap();
|
||||||
|
|
||||||
|
@ -42,7 +42,7 @@ 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 |gui_handler| (callback)(gui_handler, input));
|
gui_handler.add_callback(move |commands| (callback)(commands, input));
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
use super::executable::Executable;
|
use super::executable::Executable;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ecs::World;
|
use ecs::*;
|
||||||
|
|
||||||
use std::sync::{
|
use std::sync::{
|
||||||
Arc, RwLock, Weak,
|
Arc, RwLock, Weak,
|
||||||
|
@ -41,7 +41,7 @@ pub struct Selectable {
|
||||||
|
|
||||||
// used for custom buttons
|
// used for custom buttons
|
||||||
custom_callback:
|
custom_callback:
|
||||||
RwLock<Option<Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>>>,
|
RwLock<Option<Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Selectable {
|
impl Selectable {
|
||||||
|
@ -117,7 +117,7 @@ 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 World, ControllerButton) -> Result<bool> + Send + Sync + 'static,
|
F: Fn(&mut Commands, 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));
|
||||||
}
|
}
|
||||||
|
@ -180,11 +180,11 @@ impl Selectable {
|
||||||
|
|
||||||
pub(crate) fn custom_click_event(
|
pub(crate) fn custom_click_event(
|
||||||
&self,
|
&self,
|
||||||
world: &mut World,
|
commands: &mut Commands,
|
||||||
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(world, button)? {
|
if custom_callback(commands, 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() {
|
||||||
|
|
|
@ -1,20 +1,20 @@
|
||||||
/// A trait that is used by the gui handler as the target for input
|
/// A trait that is used by the gui handler as the target for input
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use ecs::World;
|
use ecs::Commands;
|
||||||
|
|
||||||
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, world: &mut World) -> Result<()>;
|
fn decline(&self, commands: &mut Commands) -> 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, world: &mut World, second_level: bool) -> Result<()>;
|
fn next_tab(&self, commands: &mut Commands, 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, world: &mut World, second_level: bool) -> Result<()>;
|
fn previous_tab(&self, commands: &mut Commands, second_level: bool) -> Result<()>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@ use crate::prelude::*;
|
||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use assetpath::AssetPath;
|
use assetpath::AssetPath;
|
||||||
|
|
||||||
use ecs::{Resource, World};
|
use ecs::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use utilities::{impl_reprc, prelude::*};
|
use utilities::{impl_reprc, prelude::*};
|
||||||
use vulkan_rs::{prelude::*, render_target::sub_pass::InputAttachmentInfo};
|
use vulkan_rs::{prelude::*, render_target::sub_pass::InputAttachmentInfo};
|
||||||
|
@ -245,7 +245,7 @@ pub struct GuiHandler {
|
||||||
|
|
||||||
text_sample_count: VkSampleCountFlags,
|
text_sample_count: VkSampleCountFlags,
|
||||||
|
|
||||||
callback_list: Vec<Box<dyn FnOnce(&mut World) -> Result<()> + Send + Sync>>,
|
callback_list: Vec<Box<dyn FnOnce(&mut Commands) -> Result<()> + Send + Sync>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl GuiHandler {
|
impl GuiHandler {
|
||||||
|
@ -866,17 +866,17 @@ impl GuiHandler {
|
||||||
|
|
||||||
pub fn set_top_gui(
|
pub fn set_top_gui(
|
||||||
&mut self,
|
&mut self,
|
||||||
world: &mut World,
|
commands: &mut Commands,
|
||||||
top_gui: Option<Arc<dyn TopLevelGui>>,
|
top_gui: Option<Arc<dyn TopLevelGui>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
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(world)?;
|
current.disable(commands)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Some(current), None) => {
|
(Some(current), None) => {
|
||||||
current.disable(world)?;
|
current.disable(commands)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => (),
|
_ => (),
|
||||||
|
@ -889,17 +889,17 @@ impl GuiHandler {
|
||||||
|
|
||||||
pub fn set_tooltip(
|
pub fn set_tooltip(
|
||||||
&mut self,
|
&mut self,
|
||||||
world: &mut World,
|
commands: &mut Commands,
|
||||||
tooltip: Option<Arc<dyn TopLevelGui>>,
|
tooltip: Option<Arc<dyn TopLevelGui>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
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(world)?;
|
current.disable(commands)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(Some(current), None) => {
|
(Some(current), None) => {
|
||||||
current.disable(world)?;
|
current.disable(commands)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => (),
|
_ => (),
|
||||||
|
@ -910,19 +910,19 @@ impl GuiHandler {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn add_callback<F: FnOnce(&mut World) -> Result<()> + Send + Sync + 'static>(
|
pub(crate) fn add_callback<F: FnOnce(&mut Commands) -> 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, world: &mut World) -> Result<()> {
|
pub fn process_callbacks(&mut self, commands: &mut Commands) -> 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(world))
|
.try_for_each(|callback| callback(commands))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render(
|
fn render(
|
||||||
|
|
57
src/state.rs
57
src/state.rs
|
@ -1,6 +1,6 @@
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use anyhow::{Result, anyhow};
|
use anyhow::{Result, anyhow};
|
||||||
use ecs::World;
|
use ecs::*;
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::sync::{Arc, RwLock, Weak};
|
use std::sync::{Arc, RwLock, Weak};
|
||||||
|
@ -100,22 +100,23 @@ pub(crate) struct State {
|
||||||
|
|
||||||
pub(crate) top_level_gui: Arc<dyn TopLevelGui>,
|
pub(crate) top_level_gui: Arc<dyn TopLevelGui>,
|
||||||
|
|
||||||
pub(crate) on_activate: RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>,
|
pub(crate) on_activate: RwLock<Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>>,
|
||||||
pub(crate) on_deactivate: RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>,
|
pub(crate) on_deactivate:
|
||||||
|
RwLock<Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>>,
|
||||||
|
|
||||||
pub(crate) next_tab: RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>,
|
pub(crate) next_tab: RwLock<Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>>,
|
||||||
pub(crate) previous_tab: RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>,
|
pub(crate) previous_tab: RwLock<Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>>,
|
||||||
pub(crate) decline: RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>,
|
pub(crate) decline: RwLock<Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl State {
|
impl State {
|
||||||
pub(crate) fn new<'a>(
|
pub(crate) fn new<'a>(
|
||||||
world: &mut World,
|
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(world, path)?,
|
CreationType::File(path) => GuiBuilder::new(gui_handler, path)?,
|
||||||
CreationType::TopGui(top_gui) => top_gui,
|
CreationType::TopGui(top_gui) => top_gui,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -179,49 +180,49 @@ impl State {
|
||||||
|
|
||||||
pub(crate) fn set_on_activate(
|
pub(crate) fn set_on_activate(
|
||||||
&self,
|
&self,
|
||||||
on_activate: Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>,
|
on_activate: Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>,
|
||||||
) {
|
) {
|
||||||
*self.on_activate.write().unwrap() = on_activate;
|
*self.on_activate.write().unwrap() = on_activate;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_on_deactivate(
|
pub(crate) fn set_on_deactivate(
|
||||||
&self,
|
&self,
|
||||||
on_deactivate: Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>,
|
on_deactivate: Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>,
|
||||||
) {
|
) {
|
||||||
*self.on_deactivate.write().unwrap() = on_deactivate;
|
*self.on_deactivate.write().unwrap() = on_deactivate;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_previous_tab(
|
pub(crate) fn set_previous_tab(
|
||||||
&self,
|
&self,
|
||||||
previous_tab: Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>,
|
previous_tab: Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>,
|
||||||
) {
|
) {
|
||||||
*self.previous_tab.write().unwrap() = previous_tab;
|
*self.previous_tab.write().unwrap() = previous_tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_next_tab(
|
pub(crate) fn set_next_tab(
|
||||||
&self,
|
&self,
|
||||||
next_tab: Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>,
|
next_tab: Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>,
|
||||||
) {
|
) {
|
||||||
*self.next_tab.write().unwrap() = next_tab;
|
*self.next_tab.write().unwrap() = next_tab;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn set_decline(
|
pub(crate) fn set_decline(
|
||||||
&self,
|
&self,
|
||||||
decline: Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>,
|
decline: Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>,
|
||||||
) {
|
) {
|
||||||
*self.decline.write().unwrap() = decline;
|
*self.decline.write().unwrap() = decline;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TopGui for State {
|
impl TopGui for State {
|
||||||
fn decline(&self, world: &mut World) -> Result<()> {
|
fn decline(&self, commands: &mut Commands) -> Result<()> {
|
||||||
match self.decline.read().unwrap().as_ref() {
|
match self.decline.read().unwrap().as_ref() {
|
||||||
Some(decline) => {
|
Some(decline) => {
|
||||||
(decline)(world)?;
|
(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(world)?;
|
top_gui.decline(commands)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -229,14 +230,14 @@ impl TopGui for State {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn next_tab(&self, world: &mut World, second_level: bool) -> Result<()> {
|
fn next_tab(&self, commands: &mut Commands, 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)(world)?;
|
(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(world, second_level)?;
|
top_gui.next_tab(commands, second_level)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -244,14 +245,14 @@ impl TopGui for State {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn previous_tab(&self, world: &mut World, second_level: bool) -> Result<()> {
|
fn previous_tab(&self, commands: &mut Commands, 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)(world)?;
|
(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(world, second_level)?;
|
top_gui.previous_tab(commands, second_level)?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -277,21 +278,21 @@ impl TopLevelGui for State {
|
||||||
self.top_level_gui.functionality()
|
self.top_level_gui.functionality()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enable(&self, world: &mut World) -> Result<()> {
|
fn enable(&self, commands: &mut Commands) -> Result<()> {
|
||||||
self.top_level_gui.enable(world)?;
|
self.top_level_gui.enable(commands)?;
|
||||||
|
|
||||||
if let Some(activate) = self.on_activate.read().unwrap().as_ref() {
|
if let Some(activate) = self.on_activate.read().unwrap().as_ref() {
|
||||||
(activate)(world)?;
|
(activate)(commands)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn disable(&self, world: &mut World) -> Result<()> {
|
fn disable(&self, commands: &mut Commands) -> Result<()> {
|
||||||
self.top_level_gui.disable(world)?;
|
self.top_level_gui.disable(commands)?;
|
||||||
|
|
||||||
if let Some(deactivate) = self.on_deactivate.read().unwrap().as_ref() {
|
if let Some(deactivate) = self.on_deactivate.read().unwrap().as_ref() {
|
||||||
(deactivate)(world)?;
|
(deactivate)(commands)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -7,12 +7,14 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
pub trait FutureStateChange: Fn(&mut World) -> Result<()> + Send + Sync {
|
pub trait FutureStateChange:
|
||||||
|
Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync
|
||||||
|
{
|
||||||
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
|
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
|
||||||
where
|
where
|
||||||
Self: 'a;
|
Self: 'a;
|
||||||
|
|
||||||
fn as_fn(&self) -> &(dyn Fn(&mut World) -> Result<()> + Send + Sync)
|
fn as_fn(&self) -> &(dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync)
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
|
@ -21,7 +23,7 @@ pub trait FutureStateChange: Fn(&mut World) -> Result<()> + Send + Sync {
|
||||||
|
|
||||||
fn as_static(
|
fn as_static(
|
||||||
&'static self,
|
&'static self,
|
||||||
) -> &'static (dyn Fn(&mut World) -> Result<()> + Send + Sync + 'static)
|
) -> &'static (dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync + 'static)
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
|
@ -29,7 +31,9 @@ pub trait FutureStateChange: Fn(&mut World) -> Result<()> + Send + Sync {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<F: Fn(&mut World) -> Result<()> + Clone + Send + Sync> FutureStateChange for F {
|
impl<F: Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Clone + Send + Sync> FutureStateChange
|
||||||
|
for F
|
||||||
|
{
|
||||||
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
|
fn clone_box<'a>(&self) -> Box<dyn 'a + FutureStateChange>
|
||||||
where
|
where
|
||||||
Self: 'a,
|
Self: 'a,
|
||||||
|
@ -47,22 +51,27 @@ impl<'a> Clone for Box<dyn 'a + FutureStateChange> {
|
||||||
/// Update type
|
/// Update type
|
||||||
pub enum StateUpdateType<'a> {
|
pub enum StateUpdateType<'a> {
|
||||||
/// Updates the callback which is executed on next tab event
|
/// Updates the callback which is executed on next tab event
|
||||||
NextTab(Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>),
|
NextTab(Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>),
|
||||||
|
|
||||||
/// Updates the callback which is executed on previous tab event
|
/// Updates the callback which is executed on previous tab event
|
||||||
PreviousTab(Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>),
|
PreviousTab(Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>),
|
||||||
|
|
||||||
/// Updates the callback which is executed on decline event
|
/// Updates the callback which is executed on decline event
|
||||||
Decline(Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>),
|
Decline(Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>),
|
||||||
|
|
||||||
/// Updates callbacks by identifier
|
/// Updates callbacks by identifier
|
||||||
ClickCallbacks(Vec<(&'a str, Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>)>),
|
ClickCallbacks(
|
||||||
|
Vec<(
|
||||||
|
&'a str,
|
||||||
|
Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>,
|
||||||
|
)>,
|
||||||
|
),
|
||||||
|
|
||||||
/// Updates callbacks by identifier
|
/// Updates callbacks by identifier
|
||||||
SelectCallbacks(
|
SelectCallbacks(
|
||||||
Vec<(
|
Vec<(
|
||||||
&'a str,
|
&'a str,
|
||||||
Box<dyn Fn(&mut World, bool) -> Result<()> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
),
|
),
|
||||||
|
|
||||||
|
@ -70,7 +79,7 @@ pub enum StateUpdateType<'a> {
|
||||||
CustomClickCallbacks(
|
CustomClickCallbacks(
|
||||||
Vec<(
|
Vec<(
|
||||||
&'a str,
|
&'a str,
|
||||||
Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
),
|
),
|
||||||
|
|
||||||
|
@ -78,15 +87,15 @@ pub enum StateUpdateType<'a> {
|
||||||
VecCallbacks(
|
VecCallbacks(
|
||||||
Vec<(
|
Vec<(
|
||||||
&'a str,
|
&'a str,
|
||||||
Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>,
|
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>,
|
||||||
)>,
|
)>,
|
||||||
),
|
),
|
||||||
|
|
||||||
/// Updates the callback which is executed when this state gets activated on state change
|
/// Updates the callback which is executed when this state gets activated on state change
|
||||||
OnActivate(Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>),
|
OnActivate(Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>),
|
||||||
|
|
||||||
/// Updates the callback which is executed when this state gets deactivated on state change
|
/// Updates the callback which is executed when this state gets deactivated on state change
|
||||||
OnDeactivate(Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>),
|
OnDeactivate(Option<Box<dyn Fn(&mut Commands) -> Result<()> + Send + Sync>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Type of the state to be created
|
/// Type of the state to be created
|
||||||
|
@ -157,12 +166,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,
|
||||||
world: &mut World,
|
gui_handler: &mut GuiHandler,
|
||||||
id: &str,
|
id: &str,
|
||||||
creation_type: impl Into<CreationType<'a>>,
|
creation_type: impl Into<CreationType<'a>>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
self.states
|
self.states.insert(
|
||||||
.insert(id.to_string(), State::new(world, id, creation_type.into())?);
|
id.to_string(),
|
||||||
|
State::new(gui_handler, id, creation_type.into())?,
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -180,13 +191,16 @@ impl States {
|
||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `id` - Set state with the given identifier or None
|
/// * `id` - Set state with the given identifier or None
|
||||||
pub fn set_state<'b>(&self, world: &mut World, id: impl Into<Option<&'b str>>) -> Result<()> {
|
pub fn set_state<'b>(
|
||||||
let gui_handler: &mut GuiHandler = world.resources.get_mut_unchecked();
|
&self,
|
||||||
|
commands: &mut Commands,
|
||||||
|
gui_handler: &mut GuiHandler,
|
||||||
|
id: impl Into<Option<&'b str>>,
|
||||||
|
) -> Result<()> {
|
||||||
Self::_set_state(
|
Self::_set_state(
|
||||||
id.into().map(|id| self.get_state(id)).transpose()?,
|
id.into().map(|id| self.get_state(id)).transpose()?,
|
||||||
&mut *self.current_state.lock().unwrap(),
|
&mut *self.current_state.lock().unwrap(),
|
||||||
world,
|
commands,
|
||||||
gui_handler,
|
gui_handler,
|
||||||
self.log_state_change,
|
self.log_state_change,
|
||||||
)
|
)
|
||||||
|
@ -195,7 +209,7 @@ impl States {
|
||||||
fn _set_state(
|
fn _set_state(
|
||||||
state: Option<Arc<State>>,
|
state: Option<Arc<State>>,
|
||||||
current: &mut Option<Arc<State>>,
|
current: &mut Option<Arc<State>>,
|
||||||
world: &mut World,
|
commands: &mut Commands,
|
||||||
gui_handler: &mut GuiHandler,
|
gui_handler: &mut GuiHandler,
|
||||||
logging: bool,
|
logging: bool,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
|
@ -208,14 +222,14 @@ impl States {
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute deactivate on old state
|
// execute deactivate on old state
|
||||||
old_state.disable(world)?;
|
old_state.disable(commands)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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(world)?;
|
state.enable(commands)?;
|
||||||
gui_handler.set_top_gui(world, Some(state.clone()))?;
|
gui_handler.set_top_gui(commands, Some(state.clone()))?;
|
||||||
|
|
||||||
if logging {
|
if logging {
|
||||||
println!("Change UI State to {}", state.name);
|
println!("Change UI State to {}", state.name);
|
||||||
|
@ -224,7 +238,7 @@ impl States {
|
||||||
*current = Some(state);
|
*current = Some(state);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
gui_handler.set_top_gui(world, None)?;
|
gui_handler.set_top_gui(commands, None)?;
|
||||||
|
|
||||||
if logging {
|
if logging {
|
||||||
println!("Change UI State to None");
|
println!("Change UI State to None");
|
||||||
|
@ -258,20 +272,20 @@ impl States {
|
||||||
let weak_current_state = Arc::downgrade(&self.current_state);
|
let weak_current_state = Arc::downgrade(&self.current_state);
|
||||||
let logging = self.log_state_change;
|
let logging = self.log_state_change;
|
||||||
|
|
||||||
Ok(Box::new(move |world: &mut World| {
|
Ok(Box::new(
|
||||||
|
move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||||
if let Some(current) = weak_current_state.upgrade() {
|
if let Some(current) = weak_current_state.upgrade() {
|
||||||
let gui_handler: &mut GuiHandler = world.resources.get_mut_unchecked();
|
|
||||||
|
|
||||||
Self::_set_state(
|
Self::_set_state(
|
||||||
weak_state.as_ref().map(|w| w.upgrade()).flatten(),
|
weak_state.as_ref().map(|w| w.upgrade()).flatten(),
|
||||||
&mut *current.lock().unwrap(),
|
&mut *current.lock().unwrap(),
|
||||||
world,
|
commands,
|
||||||
gui_handler,
|
gui_handler,
|
||||||
logging,
|
logging,
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}))
|
},
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue