Compare commits
2 commits
b13253ecbc
...
540b3b8165
Author | SHA1 | Date | |
---|---|---|---|
540b3b8165 | |||
8cec32830a |
18 changed files with 548 additions and 250 deletions
|
@ -23,7 +23,11 @@ pub struct 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(
|
||||
#[cfg(feature = "audio")]
|
||||
gui_handler,
|
||||
|
@ -31,22 +35,28 @@ impl GuiBuilder {
|
|||
)
|
||||
.with_context(|| format!("validator for {}", path.full_path()))?;
|
||||
|
||||
Ok(Arc::new(Self::_new(gui_handler, validator).with_context(
|
||||
|| format!("for file {}", path.full_path()),
|
||||
)?))
|
||||
Ok(Arc::new(
|
||||
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(
|
||||
#[cfg(feature = "audio")]
|
||||
gui_handler,
|
||||
s,
|
||||
)?;
|
||||
|
||||
Ok(Arc::new(Self::_new(gui_handler, validator)?))
|
||||
Ok(Arc::new(Self::_new(commands, gui_handler, validator)?))
|
||||
}
|
||||
|
||||
pub fn merge<'a>(
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
pathes: impl IntoIterator<Item = &'a AssetPath>,
|
||||
) -> Result<Arc<Self>> {
|
||||
|
@ -66,7 +76,7 @@ impl GuiBuilder {
|
|||
path,
|
||||
)?;
|
||||
|
||||
let builder = Self::_new(gui_handler, validator)
|
||||
let builder = Self::_new(commands, gui_handler, validator)
|
||||
.with_context(|| format!("for file {}", path.full_path()))?;
|
||||
|
||||
me.grids.extend(builder.grids);
|
||||
|
@ -86,7 +96,11 @@ impl GuiBuilder {
|
|||
}
|
||||
|
||||
#[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 mut ids = HashMap::new();
|
||||
|
@ -97,6 +111,7 @@ impl GuiBuilder {
|
|||
|
||||
for child in &root.children {
|
||||
let tree = Self::create_tree(
|
||||
commands,
|
||||
gui_handler,
|
||||
&mut ids,
|
||||
child,
|
||||
|
@ -179,7 +194,10 @@ impl GuiBuilder {
|
|||
impl Functionality for GuiBuilder {
|
||||
fn set_click_callbacks(
|
||||
&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<()> {
|
||||
for (function_name, callback) in functions {
|
||||
let suffix_less_function_name = handle_function_suffix(function_name);
|
||||
|
@ -195,7 +213,7 @@ impl Functionality for GuiBuilder {
|
|||
&self,
|
||||
_functions: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
Ok(())
|
||||
|
@ -205,7 +223,7 @@ impl Functionality for GuiBuilder {
|
|||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
for (function_name, callback) in callbacks {
|
||||
|
@ -222,7 +240,11 @@ impl Functionality for GuiBuilder {
|
|||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>,
|
||||
Box<
|
||||
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
for (function_name, callback) in callbacks {
|
||||
|
@ -245,9 +267,14 @@ impl Visibility for GuiBuilder {
|
|||
.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 {
|
||||
grid.set_visibility(world, visibility)?;
|
||||
grid.set_visibility(commands, gui_handler, visibility)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -269,7 +296,7 @@ impl GuiElementTraits 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() {
|
||||
decline_callback()?;
|
||||
}
|
||||
|
@ -277,11 +304,21 @@ impl TopGui for GuiBuilder {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn next_tab(&self, _world: &mut World, _: bool) -> Result<()> {
|
||||
fn next_tab(
|
||||
&self,
|
||||
_commands: &mut Commands,
|
||||
_gui_handler: &mut GuiHandler,
|
||||
_: bool,
|
||||
) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn previous_tab(&self, _world: &mut World, _: bool) -> Result<()> {
|
||||
fn previous_tab(
|
||||
&self,
|
||||
_commands: &mut Commands,
|
||||
_gui_handler: &mut GuiHandler,
|
||||
_: bool,
|
||||
) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -303,18 +340,18 @@ impl TopLevelGui for GuiBuilder {
|
|||
Some(self)
|
||||
}
|
||||
|
||||
fn enable(&self, world: &mut World) -> Result<()> {
|
||||
self.set_visibility(world, true)?;
|
||||
fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
|
||||
self.set_visibility(commands, gui_handler, true)?;
|
||||
|
||||
if let Some(button) = &self.default_select {
|
||||
button.select(world.resources.get_mut()?)?;
|
||||
button.select(gui_handler)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn disable(&self, world: &mut World) -> Result<()> {
|
||||
self.set_visibility(world, false)?;
|
||||
fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
|
||||
self.set_visibility(commands, gui_handler, false)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -342,6 +379,7 @@ impl_element!(MultiLineTextField);
|
|||
// private
|
||||
impl GuiBuilder {
|
||||
fn create_tree(
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
ids: &mut HashMap<String, UiElement>,
|
||||
root_grid_info: &Arc<GridInfo>,
|
||||
|
@ -394,6 +432,7 @@ impl GuiBuilder {
|
|||
|
||||
for child in root_grid_info.children.read().unwrap().iter() {
|
||||
Self::create_child(
|
||||
commands,
|
||||
gui_handler,
|
||||
child,
|
||||
&root_grid,
|
||||
|
@ -408,6 +447,7 @@ impl GuiBuilder {
|
|||
}
|
||||
|
||||
fn create_child(
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
child: &UiInfoElement,
|
||||
grid: &Grid,
|
||||
|
@ -423,6 +463,7 @@ impl GuiBuilder {
|
|||
Self::insert_id(ids, &button_info.id, &button)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
button.clone(),
|
||||
button_info
|
||||
|
@ -458,6 +499,7 @@ impl GuiBuilder {
|
|||
Self::insert_id(ids, &label_info.id, &label)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
label.clone(),
|
||||
label_info.x_slot.get().with_context(|| "x_slot of label")?,
|
||||
|
@ -470,11 +512,12 @@ impl GuiBuilder {
|
|||
}
|
||||
UiInfoElement::MultiLineLabel(multi_line_label_info) => {
|
||||
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)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
multi_line_label.clone(),
|
||||
multi_line_label_info
|
||||
|
@ -492,12 +535,16 @@ impl GuiBuilder {
|
|||
multi_line_label.set_layer(layer)?;
|
||||
}
|
||||
UiInfoElement::MultiLineTextField(multi_line_text_field_info) => {
|
||||
let multi_line_text_field =
|
||||
MultiLineTextField::try_from(multi_line_text_field_info, gui_handler)?;
|
||||
let multi_line_text_field = MultiLineTextField::try_from(
|
||||
multi_line_text_field_info,
|
||||
commands,
|
||||
gui_handler,
|
||||
)?;
|
||||
|
||||
GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
multi_line_text_field.clone(),
|
||||
multi_line_text_field_info
|
||||
|
@ -520,6 +567,7 @@ impl GuiBuilder {
|
|||
Self::insert_id(ids, &text_field_info.id, &text_field)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
text_field.clone(),
|
||||
text_field_info
|
||||
|
@ -542,6 +590,7 @@ impl GuiBuilder {
|
|||
Self::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
icon.clone(),
|
||||
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)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
progress_bar.clone(),
|
||||
progress_bar_info
|
||||
|
@ -580,6 +630,7 @@ impl GuiBuilder {
|
|||
Self::insert_id(ids, &grid_info.id, &sub_grid)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
sub_grid.clone(),
|
||||
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() {
|
||||
Self::create_child(
|
||||
commands,
|
||||
gui_handler,
|
||||
child,
|
||||
&sub_grid,
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::{any::Any, collections::HashMap, sync::Arc};
|
||||
|
||||
use assetpath::AssetPath;
|
||||
use ecs::World;
|
||||
use ecs::Commands;
|
||||
|
||||
use super::validator::{
|
||||
buttoninfo::NeighbourInfo,
|
||||
|
@ -18,27 +18,39 @@ pub struct 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(
|
||||
#[cfg(feature = "audio")]
|
||||
gui_handler,
|
||||
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(
|
||||
#[cfg(feature = "audio")]
|
||||
gui_handler,
|
||||
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 mut ids = HashMap::new();
|
||||
|
@ -57,7 +69,14 @@ impl GuiSnippet {
|
|||
GuiBuilder::insert_id(&mut ids, &grid_info.id, &grid)?;
|
||||
|
||||
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)?;
|
||||
|
@ -94,8 +113,13 @@ impl Visibility for GuiSnippet {
|
|||
self.grid.visible()
|
||||
}
|
||||
|
||||
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> {
|
||||
self.grid.set_visibility(world, visibility)
|
||||
fn set_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 {
|
||||
fn set_click_callbacks(
|
||||
&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<()> {
|
||||
for (function_name, callback) in functions {
|
||||
let suffix_less_function_name = handle_function_suffix(function_name);
|
||||
|
@ -164,7 +191,7 @@ impl Functionality for GuiSnippet {
|
|||
&self,
|
||||
_functions: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
Ok(())
|
||||
|
@ -174,7 +201,7 @@ impl Functionality for GuiSnippet {
|
|||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut World, bool) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
for (function_name, callback) in callbacks {
|
||||
|
@ -191,7 +218,11 @@ impl Functionality for GuiSnippet {
|
|||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>,
|
||||
Box<
|
||||
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
for (function_name, callback) in callbacks {
|
||||
|
@ -207,6 +238,7 @@ impl Functionality for GuiSnippet {
|
|||
|
||||
impl GuiSnippet {
|
||||
fn create_child(
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
child: &UiInfoElement,
|
||||
grid: &Grid,
|
||||
|
@ -220,6 +252,7 @@ impl GuiSnippet {
|
|||
GuiBuilder::insert_id(ids, &button_info.id, &button)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
button.clone(),
|
||||
button_info.x_slot.get()?,
|
||||
|
@ -236,6 +269,7 @@ impl GuiSnippet {
|
|||
GuiBuilder::insert_id(ids, &label_info.id, &label)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
label,
|
||||
label_info.x_slot.get()?,
|
||||
|
@ -246,11 +280,12 @@ impl GuiSnippet {
|
|||
}
|
||||
UiInfoElement::MultiLineLabel(multi_line_label_info) => {
|
||||
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)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
multi_line_label,
|
||||
multi_line_label_info.x_slot.get()?,
|
||||
|
@ -260,12 +295,16 @@ impl GuiSnippet {
|
|||
)?;
|
||||
}
|
||||
UiInfoElement::MultiLineTextField(multi_line_text_field_info) => {
|
||||
let multi_line_text_field =
|
||||
MultiLineTextField::try_from(multi_line_text_field_info, gui_handler)?;
|
||||
let multi_line_text_field = MultiLineTextField::try_from(
|
||||
multi_line_text_field_info,
|
||||
commands,
|
||||
gui_handler,
|
||||
)?;
|
||||
|
||||
GuiBuilder::insert_id(ids, &multi_line_text_field_info.id, &multi_line_text_field)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
multi_line_text_field,
|
||||
multi_line_text_field_info.x_slot.get()?,
|
||||
|
@ -280,6 +319,7 @@ impl GuiSnippet {
|
|||
GuiBuilder::insert_id(ids, &text_field_info.id, &text_field)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
text_field,
|
||||
text_field_info.x_slot.get()?,
|
||||
|
@ -294,6 +334,7 @@ impl GuiSnippet {
|
|||
GuiBuilder::insert_id(ids, &icon_info.id, UiElement::Icon(Arc::downgrade(&icon)))?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
icon,
|
||||
icon_info.x_slot.get()?,
|
||||
|
@ -308,6 +349,7 @@ impl GuiSnippet {
|
|||
GuiBuilder::insert_id(ids, &progress_bar_info.id, &progress_bar)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
progress_bar,
|
||||
progress_bar_info.x_slot.get()?,
|
||||
|
@ -322,6 +364,7 @@ impl GuiSnippet {
|
|||
GuiBuilder::insert_id(ids, &grid_info.id, &sub_grid)?;
|
||||
|
||||
grid.attach(
|
||||
commands,
|
||||
gui_handler,
|
||||
sub_grid.clone(),
|
||||
grid_info.x_slot.get()?,
|
||||
|
@ -331,7 +374,14 @@ impl GuiSnippet {
|
|||
)?;
|
||||
|
||||
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,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -315,23 +315,32 @@ impl Button {
|
|||
|
||||
pub fn set_callback<F>(&self, callback: F)
|
||||
where
|
||||
F: Fn(&mut Commands) -> Result<()> + Send + Sync + 'static,
|
||||
F: Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync + 'static,
|
||||
{
|
||||
self.click_executable
|
||||
.set_callback(move |commands: &mut Commands, _: ()| callback(commands));
|
||||
self.click_executable.set_callback(
|
||||
move |commands: &mut Commands, gui_handler: &mut GuiHandler, _: ()| {
|
||||
callback(commands, gui_handler)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn set_select_callback<F>(&self, callback: F)
|
||||
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
|
||||
.set_callback(move |commands: &mut Commands, select: bool| callback(commands, select));
|
||||
self.on_select_executable.set_callback(
|
||||
move |commands: &mut Commands, gui_handler: &mut GuiHandler, select: bool| {
|
||||
callback(commands, gui_handler, select)
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn set_custom_callback<F>(&self, callback: F)
|
||||
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);
|
||||
}
|
||||
|
@ -448,7 +457,12 @@ impl Visibility for Button {
|
|||
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) {
|
||||
self.visible.store(visibility, SeqCst);
|
||||
|
||||
|
@ -579,19 +593,18 @@ impl Button {
|
|||
fn create_selected_changed_callback(button: Arc<Button>) {
|
||||
let button_weak = Arc::downgrade(&button);
|
||||
|
||||
let selected_changed = move |commands: &mut Commands, selected: bool| {
|
||||
if let Some(button) = button_weak.upgrade() {
|
||||
let gui_handler = world.resources.get_mut()?;
|
||||
|
||||
if selected {
|
||||
button.set_button_state(gui_handler, ButtonState::Selected)?;
|
||||
} else {
|
||||
button.set_button_state(gui_handler, ButtonState::Normal)?;
|
||||
let selected_changed =
|
||||
move |_commands: &mut Commands, gui_handler: &mut GuiHandler, selected: bool| {
|
||||
if let Some(button) = button_weak.upgrade() {
|
||||
if selected {
|
||||
button.set_button_state(gui_handler, ButtonState::Selected)?;
|
||||
} else {
|
||||
button.set_button_state(gui_handler, ButtonState::Normal)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
};
|
||||
Ok(())
|
||||
};
|
||||
|
||||
button.select_executable.set_callback(selected_changed);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use crate::prelude::*;
|
||||
use anyhow::Result;
|
||||
use ecs::Commands;
|
||||
|
||||
use crate::builder::validator::gridinfo::GridInfo;
|
||||
|
||||
|
@ -199,6 +200,7 @@ impl Grid {
|
|||
|
||||
pub fn detach(
|
||||
&self,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
pos_x: usize,
|
||||
pos_y: usize,
|
||||
|
@ -231,7 +233,7 @@ impl Grid {
|
|||
Some((child, _x, _y)) => {
|
||||
if self.visible() {
|
||||
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
|
||||
pub fn detach_item(
|
||||
&self,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
item: Arc<dyn GuiElementTraits>,
|
||||
) -> Result<bool> {
|
||||
|
@ -265,7 +268,7 @@ impl Grid {
|
|||
|
||||
if self.visible() {
|
||||
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(
|
||||
&self,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
child: Arc<dyn GuiElementTraits>,
|
||||
pos_x: usize,
|
||||
|
@ -458,7 +462,7 @@ impl Grid {
|
|||
ChildState::Some { child, .. } => {
|
||||
if self.visible() {
|
||||
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 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)?;
|
||||
|
||||
if let Some(background) = self.background.read().unwrap().as_ref() {
|
||||
background.disable(gui_handler)?;
|
||||
}
|
||||
|
||||
self.set_tree_visibility(gui_handler, false)?;
|
||||
self.set_tree_visibility(commands, gui_handler, false)?;
|
||||
|
||||
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();
|
||||
|
||||
for row in tree.deref() {
|
||||
for child_state in row {
|
||||
if let Some((child, ..)) = child_state.get_some() {
|
||||
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)
|
||||
}
|
||||
|
||||
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) {
|
||||
self.visible.store(visibility, SeqCst);
|
||||
|
||||
|
@ -804,9 +818,9 @@ impl Visibility for Grid {
|
|||
background.enable(gui_handler)?;
|
||||
}
|
||||
|
||||
self.set_tree_visibility(gui_handler, true)?;
|
||||
self.set_tree_visibility(commands, gui_handler, true)?;
|
||||
} else {
|
||||
self.disable_tree(gui_handler)?;
|
||||
self.disable_tree(commands, gui_handler)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ use crate::{
|
|||
};
|
||||
use anyhow::Result;
|
||||
use assetpath::AssetPath;
|
||||
use ecs::*;
|
||||
use ecs::Commands;
|
||||
use utilities::prelude::*;
|
||||
use vulkan_rs::prelude::*;
|
||||
|
||||
|
@ -281,10 +281,13 @@ impl Visibility for Icon {
|
|||
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) {
|
||||
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
|
||||
|
||||
self.visible.store(visibility, SeqCst);
|
||||
|
||||
if visibility {
|
||||
|
|
|
@ -8,7 +8,7 @@ use super::{
|
|||
wrapper::{IconizableWrapper, TextableWrapper},
|
||||
};
|
||||
|
||||
use ecs::*;
|
||||
use ecs::Commands;
|
||||
use vulkan_rs::prelude::*;
|
||||
|
||||
use std::sync::{
|
||||
|
@ -230,10 +230,13 @@ impl Visibility for Label {
|
|||
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() {
|
||||
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
|
||||
|
||||
self.visible.store(visibility, SeqCst);
|
||||
|
||||
if visibility {
|
||||
|
|
|
@ -6,7 +6,7 @@ use std::sync::{
|
|||
};
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use ecs::World;
|
||||
use ecs::Commands;
|
||||
use utilities::prelude::*;
|
||||
|
||||
pub struct MultiLineLabelBuilder {
|
||||
|
@ -57,7 +57,11 @@ impl MultiLineLabelBuilder {
|
|||
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)?;
|
||||
base_grid.set_margin(0);
|
||||
base_grid.set_padding(0);
|
||||
|
@ -73,7 +77,7 @@ impl MultiLineLabelBuilder {
|
|||
.set_text_alignment(self.text_alignment)
|
||||
.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 {
|
||||
|
@ -185,6 +189,7 @@ impl MultiLineLabel {
|
|||
|
||||
pub fn try_from(
|
||||
multi_line_label_info: &MultiLineLabelInfo,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
) -> Result<Arc<Self>> {
|
||||
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.build(gui_handler)
|
||||
multi_line_label_builder.build(commands, gui_handler)
|
||||
}
|
||||
|
||||
fn iter_label<F>(&self, mut f: F) -> Result<()>
|
||||
|
@ -251,8 +256,13 @@ impl Visibility for MultiLineLabel {
|
|||
self.grid.visible()
|
||||
}
|
||||
|
||||
fn set_visibility(&self, world: &mut World, visibility: bool) -> Result<()> {
|
||||
self.grid.set_visibility(world, visibility)
|
||||
fn set_visibility(
|
||||
&self,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
visibility: bool,
|
||||
) -> Result<()> {
|
||||
self.grid.set_visibility(commands, gui_handler, visibility)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -63,7 +63,11 @@ impl MultiLineTextFieldBuilder {
|
|||
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)?;
|
||||
base_grid.set_margin(0);
|
||||
base_grid.set_padding(0);
|
||||
|
@ -79,7 +83,7 @@ impl MultiLineTextFieldBuilder {
|
|||
.set_text_alignment(self.text_alignment)
|
||||
.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));
|
||||
|
@ -97,9 +101,9 @@ impl MultiLineTextFieldBuilder {
|
|||
multi_line_text_field.text_changed_exec.set_callback({
|
||||
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() {
|
||||
tf.update_text(world.resources.get_mut()?)?;
|
||||
tf.update_text(gui_handler)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -305,6 +309,7 @@ impl MultiLineTextField {
|
|||
|
||||
pub fn try_from(
|
||||
multi_line_text_field_info: &MultiLineTextFieldInfo,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
) -> Result<Arc<Self>> {
|
||||
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.build(gui_handler)
|
||||
multi_line_text_field_builder.build(commands, gui_handler)
|
||||
}
|
||||
|
||||
fn iter_label<F>(&self, mut f: F) -> Result<()>
|
||||
|
@ -371,10 +376,13 @@ impl Visibility for MultiLineTextField {
|
|||
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() {
|
||||
let gui_handler = world.resources.get_mut()?;
|
||||
|
||||
if visibility {
|
||||
self.writeable.add(gui_handler)?;
|
||||
} else {
|
||||
|
@ -382,7 +390,7 @@ impl Visibility for MultiLineTextField {
|
|||
}
|
||||
}
|
||||
|
||||
self.grid.set_visibility(world, visibility)
|
||||
self.grid.set_visibility(commands, gui_handler, visibility)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use crate::{builder::validator::progressbar_info::ProgressBarInfo, prelude::*};
|
||||
|
||||
use anyhow::Result;
|
||||
use ecs::*;
|
||||
use ecs::Commands;
|
||||
use std::sync::{
|
||||
Arc, Mutex,
|
||||
atomic::{AtomicBool, Ordering::SeqCst},
|
||||
|
@ -279,10 +279,13 @@ impl Visibility for ProgressBar {
|
|||
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() {
|
||||
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
|
||||
|
||||
self.visible.store(visibility, SeqCst);
|
||||
|
||||
if visibility {
|
||||
|
|
|
@ -170,7 +170,7 @@ impl TextField {
|
|||
|
||||
pub fn set_text_changed_callback<F>(&self, f: F)
|
||||
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);
|
||||
}
|
||||
|
@ -311,10 +311,13 @@ impl Visibility for TextField {
|
|||
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() {
|
||||
let gui_handler: &mut GuiHandler = world.resources.get_mut()?;
|
||||
|
||||
self.visible.store(visibility, SeqCst);
|
||||
|
||||
if visibility {
|
||||
|
|
|
@ -10,8 +10,8 @@ pub trait TopLevelGui: Send + Sync {
|
|||
fn top_gui(&self) -> Option<&dyn TopGui>;
|
||||
fn elements(&self) -> Option<&HashMap<String, UiElement>>;
|
||||
fn functionality(&self) -> Option<&dyn Functionality>;
|
||||
fn enable(&self, commands: &mut Commands) -> Result<()>;
|
||||
fn disable(&self, commands: &mut Commands) -> Result<()>;
|
||||
fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()>;
|
||||
fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()>;
|
||||
}
|
||||
|
||||
pub fn any_to<T>(any: &dyn Any) -> Result<&T>
|
||||
|
@ -27,14 +27,17 @@ where
|
|||
pub trait Functionality {
|
||||
fn set_click_callbacks(
|
||||
&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<()>;
|
||||
|
||||
fn set_select_callbacks(
|
||||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut Commands, bool) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()>;
|
||||
|
||||
|
@ -42,7 +45,11 @@ pub trait Functionality {
|
|||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>,
|
||||
Box<
|
||||
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>,
|
||||
)>,
|
||||
) -> Result<()>;
|
||||
|
||||
|
@ -50,7 +57,7 @@ pub trait Functionality {
|
|||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut Commands, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()>;
|
||||
}
|
||||
|
@ -84,7 +91,12 @@ pub trait Gridable {
|
|||
|
||||
pub trait Visibility {
|
||||
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> {
|
||||
|
|
|
@ -9,7 +9,8 @@ use crate::prelude::*;
|
|||
|
||||
/// `Executable` holds a closure which can be executed
|
||||
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> {
|
||||
|
@ -27,7 +28,7 @@ impl<I: Send + Sync + 'static> Executable<I> {
|
|||
/// * `callback` is a `Option<Callback>` closure
|
||||
pub fn set_callback<F>(&self, callback: impl Into<Option<F>>)
|
||||
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();
|
||||
|
||||
|
@ -42,7 +43,9 @@ impl<I: Send + Sync + 'static> Executable<I> {
|
|||
if let Some(callback) = self.callback.read().unwrap().as_ref() {
|
||||
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(())
|
||||
|
|
|
@ -40,8 +40,15 @@ pub struct Selectable {
|
|||
on_select_executable: Arc<Executable<bool>>,
|
||||
|
||||
// used for custom buttons
|
||||
custom_callback:
|
||||
RwLock<Option<Box<dyn Fn(&mut Commands, ControllerButton) -> Result<bool> + Send + Sync>>>,
|
||||
custom_callback: RwLock<
|
||||
Option<
|
||||
Box<
|
||||
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>,
|
||||
>,
|
||||
>,
|
||||
}
|
||||
|
||||
impl Selectable {
|
||||
|
@ -117,7 +124,10 @@ impl Selectable {
|
|||
|
||||
pub fn set_custom_callback<F>(&self, custom_callback: F)
|
||||
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));
|
||||
}
|
||||
|
@ -181,10 +191,11 @@ impl Selectable {
|
|||
pub(crate) fn custom_click_event(
|
||||
&self,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
button: ControllerButton,
|
||||
) -> Result<bool> {
|
||||
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")]
|
||||
{
|
||||
if let Some(audible) = self.click_audible.read().unwrap().as_ref() {
|
||||
|
|
|
@ -2,19 +2,31 @@
|
|||
use anyhow::Result;
|
||||
use ecs::Commands;
|
||||
|
||||
use crate::prelude::GuiHandler;
|
||||
|
||||
pub trait TopGui: Send + Sync {
|
||||
/// 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
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `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
|
||||
/// ///
|
||||
/// # Arguments
|
||||
/// * `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<()>;
|
||||
}
|
||||
|
|
|
@ -245,7 +245,7 @@ pub struct GuiHandler {
|
|||
|
||||
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 {
|
||||
|
@ -730,11 +730,12 @@ impl GuiHandler {
|
|||
|
||||
pub fn accept_custom_selection(
|
||||
&self,
|
||||
world: &mut World,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
button: ControllerButton,
|
||||
) -> Result<bool> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -742,13 +743,15 @@ impl GuiHandler {
|
|||
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
|
||||
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_gui) = top_level_gui.top_gui() {
|
||||
top_gui.decline(world)?;
|
||||
top_gui.decline(commands, callback_self)?;
|
||||
return Ok(true);
|
||||
}
|
||||
}
|
||||
|
@ -756,13 +759,15 @@ impl GuiHandler {
|
|||
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
|
||||
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_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);
|
||||
}
|
||||
}
|
||||
|
@ -770,13 +775,19 @@ impl GuiHandler {
|
|||
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
|
||||
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_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);
|
||||
}
|
||||
}
|
||||
|
@ -869,14 +880,16 @@ impl GuiHandler {
|
|||
commands: &mut Commands,
|
||||
top_gui: Option<Arc<dyn TopLevelGui>>,
|
||||
) -> Result<()> {
|
||||
let callback_self = unsafe { remove_life_time_mut(self) };
|
||||
|
||||
match (&self.top_ui, &top_gui) {
|
||||
(Some(current), Some(incoming)) => {
|
||||
if !Arc::ptr_eq(current, incoming) {
|
||||
current.disable(commands)?;
|
||||
current.disable(commands, callback_self)?;
|
||||
}
|
||||
}
|
||||
(Some(current), None) => {
|
||||
current.disable(commands)?;
|
||||
current.disable(commands, callback_self)?;
|
||||
}
|
||||
|
||||
_ => (),
|
||||
|
@ -892,14 +905,16 @@ impl GuiHandler {
|
|||
commands: &mut Commands,
|
||||
tooltip: Option<Arc<dyn TopLevelGui>>,
|
||||
) -> Result<()> {
|
||||
let callback_self = unsafe { remove_life_time_mut(self) };
|
||||
|
||||
match (&self.tooltip_ui, &tooltip) {
|
||||
(Some(current), Some(incoming)) => {
|
||||
if !Arc::ptr_eq(current, incoming) {
|
||||
current.disable(commands)?;
|
||||
current.disable(commands, callback_self)?;
|
||||
}
|
||||
}
|
||||
(Some(current), None) => {
|
||||
current.disable(commands)?;
|
||||
current.disable(commands, callback_self)?;
|
||||
}
|
||||
|
||||
_ => (),
|
||||
|
@ -910,19 +925,25 @@ impl GuiHandler {
|
|||
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,
|
||||
f: 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);
|
||||
|
||||
callbacks
|
||||
.into_iter()
|
||||
.try_for_each(|callback| callback(commands))
|
||||
.try_for_each(|callback| callback(commands, gui_handler))
|
||||
}
|
||||
|
||||
fn render(
|
||||
|
|
|
@ -26,32 +26,48 @@ pub struct Keyboard {
|
|||
|
||||
mode: Arc<RwLock<KeyboardMode>>,
|
||||
|
||||
decline_callback: Arc<RwLock<Option<Box<dyn Fn(&mut World) -> Result<()> + Send + Sync>>>>,
|
||||
accept_callback:
|
||||
Arc<RwLock<Option<Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>>>>,
|
||||
decline_callback: Arc<
|
||||
RwLock<Option<Box<dyn Fn(&mut Commands, &mut GuiHandler) -> Result<()> + Send + Sync>>>,
|
||||
>,
|
||||
accept_callback: Arc<
|
||||
RwLock<
|
||||
Option<
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
>,
|
||||
>,
|
||||
>,
|
||||
|
||||
elements: HashMap<String, UiElement>,
|
||||
}
|
||||
|
||||
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> =
|
||||
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 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));
|
||||
|
||||
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));
|
||||
|
||||
let mode = Arc::new(RwLock::new(KeyboardMode::UpperCase));
|
||||
|
||||
let (lower_case, upper_case, specials) = Self::setup(
|
||||
world,
|
||||
commands,
|
||||
gui_handler,
|
||||
text_field.clone(),
|
||||
&mode,
|
||||
decline_callback.clone(),
|
||||
|
@ -83,17 +99,30 @@ impl Keyboard {
|
|||
}
|
||||
|
||||
fn setup(
|
||||
world: &mut World,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
textfield: Arc<TextField>,
|
||||
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<
|
||||
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>)> {
|
||||
let lower_case = GuiBuilder::from_str(world, include_str!("lower_case.xml"))?;
|
||||
let upper_case = GuiBuilder::from_str(world, include_str!("upper_case.xml"))?;
|
||||
let specials = GuiBuilder::from_str(world, include_str!("specials.xml"))?;
|
||||
let lower_case =
|
||||
GuiBuilder::from_str(commands, gui_handler, include_str!("lower_case.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
|
||||
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())?;
|
||||
|
||||
let back = Box::new(move |world: &mut World| {
|
||||
if let Some(callback) = decline_callback.read().unwrap().as_ref() {
|
||||
(callback)(world)?;
|
||||
}
|
||||
let back = Box::new(
|
||||
move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||
if let Some(callback) = decline_callback.read().unwrap().as_ref() {
|
||||
(callback)(commands, gui_handler)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
Ok(())
|
||||
},
|
||||
);
|
||||
|
||||
let accept = {
|
||||
let weak_textfield = Arc::downgrade(&textfield);
|
||||
let weak_accept = Arc::downgrade(&accept_callback);
|
||||
|
||||
Box::new(move |world: &mut World| {
|
||||
if let Some(textfield) = weak_textfield.upgrade() {
|
||||
if let Some(accept_callback) = weak_accept.upgrade() {
|
||||
if let Some(text) = textfield.text() {
|
||||
if let Some(callback) = accept_callback.read().unwrap().as_ref() {
|
||||
(callback)(world, &text)?;
|
||||
Box::new(
|
||||
move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||
if let Some(textfield) = weak_textfield.upgrade() {
|
||||
if let Some(accept_callback) = weak_accept.upgrade() {
|
||||
if let Some(text) = textfield.text() {
|
||||
if let Some(callback) = accept_callback.read().unwrap().as_ref() {
|
||||
(callback)(commands, gui_handler, &text)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let switch_to_upper = {
|
||||
|
@ -227,22 +260,24 @@ impl Keyboard {
|
|||
let weak_lower = Arc::downgrade(&lower_case);
|
||||
let weak_upper = Arc::downgrade(&upper_case);
|
||||
|
||||
Box::new(move |world: &mut World| {
|
||||
if let Some(lower) = weak_lower.upgrade() {
|
||||
if let Some(upper) = weak_upper.upgrade() {
|
||||
let mut mode = mode.write().unwrap();
|
||||
Box::new(
|
||||
move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||
if let Some(lower) = weak_lower.upgrade() {
|
||||
if let Some(upper) = weak_upper.upgrade() {
|
||||
let mut mode = mode.write().unwrap();
|
||||
|
||||
if let KeyboardMode::LowerCase = mode.deref() {
|
||||
*mode = KeyboardMode::UpperCase;
|
||||
if let KeyboardMode::LowerCase = mode.deref() {
|
||||
*mode = KeyboardMode::UpperCase;
|
||||
|
||||
lower.disable(world)?;
|
||||
upper.enable(world)?;
|
||||
lower.disable(commands, gui_handler)?;
|
||||
upper.enable(commands, gui_handler)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let switch_to_special = {
|
||||
|
@ -250,22 +285,24 @@ impl Keyboard {
|
|||
let weak_upper = Arc::downgrade(&upper_case);
|
||||
let weak_specials = Arc::downgrade(&specials);
|
||||
|
||||
Box::new(move |world: &mut World| {
|
||||
if let Some(specials) = weak_specials.upgrade() {
|
||||
if let Some(upper) = weak_upper.upgrade() {
|
||||
let mut mode = mode.write().unwrap();
|
||||
Box::new(
|
||||
move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||
if let Some(specials) = weak_specials.upgrade() {
|
||||
if let Some(upper) = weak_upper.upgrade() {
|
||||
let mut mode = mode.write().unwrap();
|
||||
|
||||
if let KeyboardMode::UpperCase = mode.deref() {
|
||||
*mode = KeyboardMode::Specials;
|
||||
if let KeyboardMode::UpperCase = mode.deref() {
|
||||
*mode = KeyboardMode::Specials;
|
||||
|
||||
upper.disable(world)?;
|
||||
specials.enable(world)?;
|
||||
upper.disable(commands, gui_handler)?;
|
||||
specials.enable(commands, gui_handler)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let switch_to_lower = {
|
||||
|
@ -273,34 +310,38 @@ impl Keyboard {
|
|||
let weak_lower = Arc::downgrade(&lower_case);
|
||||
let weak_specials = Arc::downgrade(&specials);
|
||||
|
||||
Box::new(move |world: &mut World| {
|
||||
if let Some(lower) = weak_lower.upgrade() {
|
||||
if let Some(specials) = weak_specials.upgrade() {
|
||||
let mut mode = mode.write().unwrap();
|
||||
Box::new(
|
||||
move |commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||
if let Some(lower) = weak_lower.upgrade() {
|
||||
if let Some(specials) = weak_specials.upgrade() {
|
||||
let mut mode = mode.write().unwrap();
|
||||
|
||||
if let KeyboardMode::Specials = mode.deref() {
|
||||
*mode = KeyboardMode::LowerCase;
|
||||
if let KeyboardMode::Specials = mode.deref() {
|
||||
*mode = KeyboardMode::LowerCase;
|
||||
|
||||
specials.disable(world)?;
|
||||
lower.enable(world)?;
|
||||
specials.disable(commands, gui_handler)?;
|
||||
lower.enable(commands, gui_handler)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let space_bar = {
|
||||
let weak_textfield = Arc::downgrade(&textfield);
|
||||
|
||||
Box::new(move |world: &mut World| {
|
||||
if let Some(text_field) = weak_textfield.upgrade() {
|
||||
text_field.add_letter(world.resources.get_mut()?, ' ')?;
|
||||
}
|
||||
Box::new(
|
||||
move |_commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||
if let Some(text_field) = weak_textfield.upgrade() {
|
||||
text_field.add_letter(gui_handler, ' ')?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
})
|
||||
Ok(())
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
lower_case.set_click_callbacks(vec![
|
||||
|
@ -335,40 +376,51 @@ impl Keyboard {
|
|||
let weak_textfield = Arc::downgrade(&textfield);
|
||||
let weak_button = Arc::downgrade(&button_ref);
|
||||
|
||||
button_ref.set_callback(Box::new(move |world: &mut World| {
|
||||
if let Some(textfield) = weak_textfield.upgrade() {
|
||||
if let Some(button) = weak_button.upgrade() {
|
||||
if let Some(text) = button.text()? {
|
||||
textfield
|
||||
.add_letter(world.resources.get_mut()?, text.as_bytes()[0] as char)?;
|
||||
button_ref.set_callback(Box::new(
|
||||
move |_commands: &mut Commands, gui_handler: &mut GuiHandler| {
|
||||
if let Some(textfield) = weak_textfield.upgrade() {
|
||||
if let Some(button) = weak_button.upgrade() {
|
||||
if let Some(text) = button.text()? {
|
||||
textfield.add_letter(gui_handler, text.as_bytes()[0] as char)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}));
|
||||
Ok(())
|
||||
},
|
||||
));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
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() {
|
||||
(callback)(world)?;
|
||||
(callback)(commands, gui_handler)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn next_tab(&self, _world: &mut World, _: bool) -> Result<()> {
|
||||
fn next_tab(
|
||||
&self,
|
||||
_commands: &mut Commands,
|
||||
_gui_handler: &mut GuiHandler,
|
||||
_: bool,
|
||||
) -> Result<()> {
|
||||
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
|
||||
if !second_level {
|
||||
self.text_field.remove_last(world.resources.get_mut()?)?;
|
||||
self.text_field.remove_last(gui_handler)?;
|
||||
}
|
||||
|
||||
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 gui = match mode.deref() {
|
||||
|
@ -396,11 +453,11 @@ impl Visibility for Keyboard {
|
|||
};
|
||||
|
||||
if visibility {
|
||||
gui.enable(world)?;
|
||||
self.text_field_gui.enable(world)?;
|
||||
gui.enable(commands, gui_handler)?;
|
||||
self.text_field_gui.enable(commands, gui_handler)?;
|
||||
} else {
|
||||
gui.disable(world)?;
|
||||
self.text_field_gui.disable(world)?;
|
||||
gui.disable(commands, gui_handler)?;
|
||||
self.text_field_gui.disable(commands, gui_handler)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -438,15 +495,15 @@ impl TopLevelGui for Keyboard {
|
|||
Some(self)
|
||||
}
|
||||
|
||||
fn enable(&self, world: &mut World) -> Result<()> {
|
||||
self.set_visibility(world, true)?;
|
||||
self.text_field.focus_input(world.resources.get_mut()?)?;
|
||||
fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
|
||||
self.set_visibility(commands, gui_handler, true)?;
|
||||
self.text_field.focus_input(gui_handler)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn disable(&self, world: &mut World) -> Result<()> {
|
||||
self.set_visibility(world, false)?;
|
||||
fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
|
||||
self.set_visibility(commands, gui_handler, false)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -455,7 +512,10 @@ impl TopLevelGui for Keyboard {
|
|||
impl Functionality for Keyboard {
|
||||
fn set_click_callbacks(
|
||||
&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<()> {
|
||||
if let Some((_, callback)) = callbacks.into_iter().find(|(name, _)| *name == "decline") {
|
||||
*self.decline_callback.write().unwrap() = Some(callback);
|
||||
|
@ -468,7 +528,7 @@ impl Functionality for Keyboard {
|
|||
&self,
|
||||
_: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut World, bool) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, bool) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
Ok(())
|
||||
|
@ -478,7 +538,11 @@ impl Functionality for Keyboard {
|
|||
&self,
|
||||
_: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut World, ControllerButton) -> Result<bool> + Send + Sync>,
|
||||
Box<
|
||||
dyn Fn(&mut Commands, &mut GuiHandler, ControllerButton) -> Result<bool>
|
||||
+ Send
|
||||
+ Sync,
|
||||
>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
Ok(())
|
||||
|
@ -488,7 +552,7 @@ impl Functionality for Keyboard {
|
|||
&self,
|
||||
callbacks: Vec<(
|
||||
&str,
|
||||
Box<dyn Fn(&mut World, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
Box<dyn Fn(&mut Commands, &mut GuiHandler, &dyn Any) -> Result<()> + Send + Sync>,
|
||||
)>,
|
||||
) -> Result<()> {
|
||||
for (name, callback) in callbacks {
|
||||
|
|
33
src/state.rs
33
src/state.rs
|
@ -111,12 +111,13 @@ pub(crate) struct State {
|
|||
|
||||
impl State {
|
||||
pub(crate) fn new<'a>(
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
name: &str,
|
||||
creation_type: CreationType<'a>,
|
||||
) -> Result<Arc<Self>> {
|
||||
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,
|
||||
};
|
||||
|
||||
|
@ -215,14 +216,14 @@ impl 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() {
|
||||
Some(decline) => {
|
||||
(decline)(commands)?;
|
||||
}
|
||||
None => {
|
||||
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(())
|
||||
}
|
||||
|
||||
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() {
|
||||
Some(next_tab) => {
|
||||
(next_tab)(commands)?;
|
||||
}
|
||||
None => {
|
||||
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(())
|
||||
}
|
||||
|
||||
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() {
|
||||
Some(previous_tab) => {
|
||||
(previous_tab)(commands)?;
|
||||
}
|
||||
None => {
|
||||
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()
|
||||
}
|
||||
|
||||
fn enable(&self, commands: &mut Commands) -> Result<()> {
|
||||
self.top_level_gui.enable(commands)?;
|
||||
fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
|
||||
self.top_level_gui.enable(commands, gui_handler)?;
|
||||
|
||||
if let Some(activate) = self.on_activate.read().unwrap().as_ref() {
|
||||
(activate)(commands)?;
|
||||
|
@ -288,8 +299,8 @@ impl TopLevelGui for State {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn disable(&self, commands: &mut Commands) -> Result<()> {
|
||||
self.top_level_gui.disable(commands)?;
|
||||
fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
|
||||
self.top_level_gui.disable(commands, gui_handler)?;
|
||||
|
||||
if let Some(deactivate) = self.on_deactivate.read().unwrap().as_ref() {
|
||||
(deactivate)(commands)?;
|
||||
|
|
|
@ -63,7 +63,7 @@ pub enum StateUpdateType<'a> {
|
|||
ClickCallbacks(
|
||||
Vec<(
|
||||
&'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(
|
||||
Vec<(
|
||||
&'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(
|
||||
Vec<(
|
||||
&'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(
|
||||
Vec<(
|
||||
&'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
|
||||
pub fn add_state<'a>(
|
||||
&mut self,
|
||||
commands: &mut Commands,
|
||||
gui_handler: &mut GuiHandler,
|
||||
id: &str,
|
||||
creation_type: impl Into<CreationType<'a>>,
|
||||
) -> Result<()> {
|
||||
self.states.insert(
|
||||
id.to_string(),
|
||||
State::new(gui_handler, id, creation_type.into())?,
|
||||
State::new(commands, gui_handler, id, creation_type.into())?,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
|
@ -222,13 +227,13 @@ impl States {
|
|||
}
|
||||
|
||||
// execute deactivate on old state
|
||||
old_state.disable(commands)?;
|
||||
old_state.disable(commands, gui_handler)?;
|
||||
}
|
||||
|
||||
// set new state, either no state or requested state
|
||||
match state {
|
||||
Some(state) => {
|
||||
state.enable(commands)?;
|
||||
state.enable(commands, gui_handler)?;
|
||||
gui_handler.set_top_gui(commands, Some(state.clone()))?;
|
||||
|
||||
if logging {
|
||||
|
|
Loading…
Reference in a new issue