Compare commits

..

1 commit

Author SHA1 Message Date
b13253ecbc Update Rust crate quick-xml to 0.37.0
Some checks failed
Gavania Merge Build / build (pull_request) Has been cancelled
2025-04-09 21:05:17 +00:00
18 changed files with 251 additions and 549 deletions

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,31 +2,19 @@
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, gui_handler: &mut GuiHandler) -> Result<()>;
fn decline(&self, commands: &mut Commands) -> 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,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()>;
fn next_tab(&self, commands: &mut Commands, 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,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()>;
fn previous_tab(&self, commands: &mut Commands, second_level: bool) -> Result<()>;
}

View file

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

View file

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

View file

@ -111,13 +111,12 @@ 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(commands, gui_handler, path)?,
CreationType::File(path) => GuiBuilder::new(gui_handler, path)?,
CreationType::TopGui(top_gui) => top_gui,
};
@ -216,14 +215,14 @@ impl State {
}
impl TopGui for State {
fn decline(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
fn decline(&self, commands: &mut Commands) -> 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, gui_handler)?;
top_gui.decline(commands)?;
}
}
}
@ -231,19 +230,14 @@ impl TopGui for State {
Ok(())
}
fn next_tab(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()> {
fn next_tab(&self, commands: &mut Commands, 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, gui_handler, second_level)?;
top_gui.next_tab(commands, second_level)?;
}
}
}
@ -251,19 +245,14 @@ impl TopGui for State {
Ok(())
}
fn previous_tab(
&self,
commands: &mut Commands,
gui_handler: &mut GuiHandler,
second_level: bool,
) -> Result<()> {
fn previous_tab(&self, commands: &mut Commands, 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, gui_handler, second_level)?;
top_gui.previous_tab(commands, second_level)?;
}
}
}
@ -289,8 +278,8 @@ impl TopLevelGui for State {
self.top_level_gui.functionality()
}
fn enable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.top_level_gui.enable(commands, gui_handler)?;
fn enable(&self, commands: &mut Commands) -> Result<()> {
self.top_level_gui.enable(commands)?;
if let Some(activate) = self.on_activate.read().unwrap().as_ref() {
(activate)(commands)?;
@ -299,8 +288,8 @@ impl TopLevelGui for State {
Ok(())
}
fn disable(&self, commands: &mut Commands, gui_handler: &mut GuiHandler) -> Result<()> {
self.top_level_gui.disable(commands, gui_handler)?;
fn disable(&self, commands: &mut Commands) -> Result<()> {
self.top_level_gui.disable(commands)?;
if let Some(deactivate) = self.on_deactivate.read().unwrap().as_ref() {
(deactivate)(commands)?;

View file

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