Compare commits

..

No commits in common. 'cc05c7c434f6312012c80e1ffc0882042aa20367' and '6ae7b094ad5cd6219f745375745fcd22138a9e18' have entirely different histories.

1
Cargo.lock generated

@ -2226,7 +2226,6 @@ version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"bevy_rapier3d", "bevy_rapier3d",
"serde",
] ]
[[package]] [[package]]

@ -43,7 +43,6 @@ path = "bin/chars.rs"
[dependencies] [dependencies]
bevy = "0.11" bevy = "0.11"
bevy_rapier3d = "*" bevy_rapier3d = "*"
serde = "1"
# From rapier docs # From rapier docs
[profile.dev.package.bevy_rapier3d] [profile.dev.package.bevy_rapier3d]

File diff suppressed because it is too large Load Diff

@ -68,27 +68,140 @@ fn spawn_tree(parent: &mut ChildBuilder, pos: UiKitPosition, depth: u8, length:
}; };
(0..length).for_each(|_| { (0..length).for_each(|_| {
parent parent
.spawn(( .spawn(UiKitButton::new(Color::PINK))
UiKitButton { color: Color::PINK },
UiKitSelect::default(),
UiKitLabel {
name: "Button".into(),
},
))
.with_children(|parent| { .with_children(|parent| {
if depth > 1 { if depth > 1 {
parent parent
.spawn(( .spawn(UiKitContainer::new(pos))
UiKitContainer { position: pos },
UiKitSelect::default(),
UiKitLabel { name: "Tab".into() },
))
.with_children(|parent| { .with_children(|parent| {
spawn_tree(parent, pos2, depth - 1, length); spawn_tree(parent, pos2, depth - 1, length);
}); });
} }
}); });
}); });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos))
// .with_children(|parent| {
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos))
// .with_children(|parent| {
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos))
// .with_children(|parent| {
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// parent
// .spawn(UiKitButton::new(Color::PINK))
// .with_children(|parent| {
// parent
// .spawn(UiKitContainer::new(pos2))
// .with_children(|parent| {
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// parent.spawn(UiKitButton::new(Color::PINK));
// });
// });
// });
// });
} }
fn init_ui2(mut commands: Commands) { fn init_ui2(mut commands: Commands) {
@ -113,8 +226,8 @@ fn init_ui2(mut commands: Commands) {
.spawn(ButtonBundle { .spawn(ButtonBundle {
style: Style { style: Style {
border: UiRect::all(Val::Px(1.0)), border: UiRect::all(Val::Px(1.0)),
// width: Val::Px(200.0), width: Val::Px(200.0),
// height: Val::Px(150.0), height: Val::Px(150.0),
align_self: AlignSelf::Center, align_self: AlignSelf::Center,
..default() ..default()
}, },
@ -141,7 +254,7 @@ fn init_ui2(mut commands: Commands) {
..default() ..default()
}) })
.with_children(|parent| { .with_children(|parent| {
spawn_tree(parent, UiKitPosition::Left, 3, 4); spawn_tree(parent, UiKitPosition::Left, 3, 7);
}); });
parent parent
@ -176,7 +289,7 @@ fn init_ui2(mut commands: Commands) {
..default() ..default()
}) })
.with_children(|parent| { .with_children(|parent| {
spawn_tree(parent, UiKitPosition::Top, 3, 3); spawn_tree(parent, UiKitPosition::Top, 3, 7);
}); });
}); });
} }

@ -1,605 +1,399 @@
/// TODO: /// TODO:
/// * Text box w/ "reset" button /// * Text box w/ clear button
/// * Generic Minimize/Close Components /// * Names/Labels management
/// * Title bar w/ min/close controls /// * Button color management
/// * Notice/Warning/Error Popups
/// * Textbox (ReadOnly)
/// * Textbox (ReadWrite)
/// * Move code to submodules /// * Move code to submodules
/// ///
/// BUGS:
/// * When selecting one tree, possible to select another without the first closing.
///
use bevy::{ use bevy::{
asset::Asset, input::{
input::mouse::{MouseScrollUnit, MouseWheel}, keyboard::KeyboardInput,
mouse::{MouseScrollUnit, MouseWheel},
ButtonState,
},
prelude::*, prelude::*,
window::PrimaryWindow, window::PrimaryWindow,
}; };
use std::cmp::PartialEq;
#[derive(Debug, Component, PartialEq)]
pub struct TargetAsset<T: Asset> {
pub handle: Handle<T>,
}
#[derive(Debug, Component, PartialEq)]
pub struct TargetEntity {
pub entity: Entity,
}
pub struct GameUiPlugin; pub struct GameUiPlugin;
impl Plugin for GameUiPlugin { impl Plugin for GameUiPlugin {
fn build(&self, app: &mut App) { fn build(&self, app: &mut App) {
app.add_event::<Alert>() app.add_systems(Update, (select_tab, select_textbox, text_editor, scroll))
.add_systems(Startup, init_alerts) .add_systems(PostUpdate, selection);
.add_systems(
Update,
(
create_titles,
manage_titles,
manage_button_interaction,
manage_select_active,
manage_cursor,
manage_scroll,
manage_collapse_hiding,
manage_collapse_active,
show_child_number,
manage_sort,
spawn_alert,
),
)
.add_systems(PostUpdate, close_window);
}
}
pub use title::*;
mod title {
use super::*;
#[derive(Debug, Component)]
pub struct Title {
pub text: String,
} }
#[derive(Debug, Component)]
pub struct Note {
pub text: String,
} }
#[derive(Debug, Component)] #[derive(Debug, Bundle)]
pub struct TitleText; pub struct UiKitContainer {
node_bundle: NodeBundle,
#[derive(Debug, Component)] select: UiKitSelect,
pub struct Minimize {
pub target: Entity,
} }
#[derive(Debug, Component)] #[derive(Copy, Clone)]
pub struct Close { pub enum UiKitPosition {
pub target: Entity, Top,
Left,
Right,
} }
pub fn create_titles( impl UiKitContainer {
events: Query< pub fn new(position: UiKitPosition) -> Self {
( let style = match position {
Entity, UiKitPosition::Top => Style {
&Title,
Option<&Note>,
Option<&Minimize>,
Option<&Close>,
),
Added<Title>,
>,
mut commands: Commands,
) {
events.for_each(|(entity, title, note, minimize, close)| {
commands.entity(entity).with_children(|parent| {
parent.spawn((
TextBundle {
text: Text {
sections: vec![
TextSection {
value: title.text.clone(),
style: TextStyle {
color: Color::BLACK,
..default()
},
},
TextSection {
value: note
.unwrap_or(&Note {
text: String::new(),
})
.text
.clone(),
style: TextStyle {
color: Color::BLACK,
..default()
},
},
],
..default()
},
style: Style {
margin: UiRect::all(Val::Px(3.0)),
padding: UiRect::all(Val::Px(3.0)),
left: Val::Px(0.0),
..default()
},
..default()
},
Sorting(0),
TitleText,
));
if minimize.is_some() || close.is_some() {
parent
.spawn(NodeBundle {
style: Style {
// border: UiRect::all(Val::Px(1.0)),
// margin: UiRect::all(Val::Px(1.0)),
// padding: UiRect::all(Val::Px(1.0)),
right: Val::Px(0.0),
..default()
},
background_color: Color::WHITE.into(),
border_color: Color::BLACK.into(),
..default()
})
.with_children(|parent| {
if let Some(Minimize { target }) = minimize {
parent
.spawn((
ButtonBundle {
style: Style {
border: UiRect::all(Val::Px(1.0)), border: UiRect::all(Val::Px(1.0)),
// margin: UiRect::all(Val::Px(1.0)), top: Val::Percent(102.0),
padding: UiRect::all(Val::Px(1.0)), left: Val::Px(-2.0),
..default() flex_direction: FlexDirection::Column,
}, align_items: AlignItems::FlexStart,
background_color: Color::WHITE.into(), display: Display::None,
border_color: Color::BLACK.into(),
..default()
},
Collapse { target: *target },
Active,
))
.with_children(|parent| {
parent.spawn(TextBundle::from_section(
"-",
TextStyle {
color: Color::BLACK,
..default() ..default()
}, },
)); UiKitPosition::Left => Style {
});
}
if let Some(Close { target }) = close {
parent
.spawn((
ButtonBundle {
style: Style {
border: UiRect::all(Val::Px(1.0)), border: UiRect::all(Val::Px(1.0)),
// margin: UiRect::all(Val::Px(1.0)), left: Val::Percent(100.0),
padding: UiRect::all(Val::Px(1.0)), top: Val::Px(-2.0),
flex_direction: FlexDirection::Column,
justify_items: JustifyItems::Start,
display: Display::None,
..default() ..default()
}, },
background_color: Color::WHITE.into(), UiKitPosition::Right => Style {
border_color: Color::BLACK.into(), border: UiRect::all(Val::Px(1.0)),
right: Val::Percent(104.0),
top: Val::Px(-2.0),
flex_direction: FlexDirection::Column,
justify_items: JustifyItems::Start,
display: Display::None,
..default() ..default()
}, },
Close { target: *target }, };
)) UiKitContainer {
.with_children(|parent| { node_bundle: NodeBundle {
parent.spawn(TextBundle::from_section( style,
"x", background_color: BackgroundColor(Color::PURPLE),
TextStyle { border_color: BorderColor(Color::BLACK),
color: Color::BLACK,
..default() ..default()
}, },
)); select: UiKitSelect::None,
});
} }
});
} }
});
});
} }
pub fn manage_titles( #[derive(Debug, Bundle)]
events: Query<(&Title, Option<&Note>, &Children), Or<(Changed<Title>, Changed<Note>)>>, pub struct UiKitButton {
mut texts: Query<&mut Text, With<TitleText>>, button_bundle: ButtonBundle,
) { select: UiKitSelect,
events.iter().for_each(|(title, note, children)| { }
children.iter().for_each(|child| {
if let Ok(mut text) = texts.get_mut(*child) { impl UiKitButton {
*text = Text { pub fn new(color: Color) -> Self {
sections: vec![ UiKitButton {
TextSection { button_bundle: ButtonBundle {
value: title.text.clone(), style: Style {
style: TextStyle { border: UiRect::all(Val::Px(1.0)),
color: Color::BLACK, width: Val::Px(100.0),
height: Val::Px(50.0),
flex_direction: FlexDirection::Column,
..default() ..default()
}, },
}, background_color: BackgroundColor(color),
TextSection { border_color: BorderColor(Color::BLACK),
value: note
.unwrap_or(&Note {
text: String::new(),
})
.text
.clone(),
style: TextStyle {
color: Color::BLACK,
..default() ..default()
}, },
}, select: UiKitSelect::None,
],
..default()
} }
} }
})
})
} }
pub fn close_window( #[derive(Debug, Component, Copy, Clone, PartialEq)]
events: Query<(&Interaction, &Close), (With<Button>, Changed<Interaction>)>, pub enum UiKitSelect {
mut commands: Commands, Active,
) { None,
events
.iter()
.filter(|(&interaction, _)| interaction == Interaction::Pressed)
.for_each(|(_, Close { target })| {
commands.entity(*target).despawn_recursive();
});
}
} }
pub use collapse::*; #[derive(Debug, Bundle)]
mod collapse { pub struct UiKitTextInput {
use super::*; text_bundle: TextBundle,
interaction: Interaction,
#[derive(Debug, Component)] select: UiKitSelect,
pub struct Collapse {
pub target: Entity,
} }
pub fn manage_collapse_active( impl UiKitTextInput {
events: Query< pub fn new() -> Self {
(Entity, Option<&Active>, &Interaction), UiKitTextInput {
(Changed<Interaction>, With<Button>, With<Collapse>), text_bundle: TextBundle {
>, style: Style {
mut commands: Commands, width: Val::Percent(100.0),
) { height: Val::Percent(100.0),
events ..default()
.iter() },
.filter(|(_, _, &interaction)| interaction == Interaction::Pressed) ..default()
.for_each(|(entity, active, _)| { },
match active { interaction: Interaction::None,
Some(_) => { select: UiKitSelect::None,
commands.entity(entity).remove::<Active>();
} }
None => {
// Set this butotn to active
commands.entity(entity).insert(Active);
} }
};
});
} }
pub fn manage_collapse_hiding( /// When an item is selected/de-selected change it's display accordingly
added: Query<Entity, (Added<Active>, With<Collapse>)>, fn selection(
mut removed: RemovedComponents<Active>, mut events: Query<
collapses: Query<&Collapse, With<Button>>, (&mut BackgroundColor, &UiKitSelect, &Children),
(Changed<UiKitSelect>, With<Button>),
>,
mut styles: Query<&mut Style>, mut styles: Query<&mut Style>,
) { ) {
// Added collapse, display the target entity events
added.iter().for_each(|e| { .iter_mut()
if let Ok(Collapse { target }) = collapses.get(e) { .for_each(|(mut bg_color, select, children)| {
if let Ok(mut style) = styles.get_mut(*target) { bg_color.0 = match select {
style.display = Display::Flex; UiKitSelect::Active => Color::RED,
UiKitSelect::None => Color::WHITE,
};
children.iter().for_each(|&child| {
if let Ok(mut style) = styles.get_mut(child) {
style.display = match select {
UiKitSelect::Active => Display::Flex,
UiKitSelect::None => Display::None,
} }
} }
}); });
// Removed collapse, hide the target entity
removed.iter().for_each(|e| {
if let Ok(Collapse { target }) = collapses.get(e) {
if let Ok(mut style) = styles.get_mut(*target) {
style.display = Display::None;
}
}
}); });
} }
pub fn show_child_number( /// Toggle a UI Nav tree open/closed
events: Query<(Entity, &Children), (Changed<Children>, With<Node>)>, ///
mut tabs: Query<(Entity, &Collapse)>, /// PERF: This is hella not performant, we just usually don't have many elements to iterate over so
mut commands: Commands, /// it's tolerable.
///
// TODO: Should not be able to select multiple children in branch of tree
// TODO: Port to ui.rs
fn select_tab(
events: Query<Entity, (Changed<Interaction>, With<Button>)>,
interactions: Query<&Interaction, With<Button>>,
mut selects: Query<&mut UiKitSelect>,
parents: Query<&Parent>,
children: Query<&Children>,
) { ) {
// Any time a widget changes events.iter().for_each(|entity| {
events.iter().for_each(|(entity, children)| { // Otherwise, update nav tree(s)
// Find any tabs which have this as a target if let Ok(interaction) = interactions.get(entity) {
tabs.iter_mut() match interaction {
.find_map(|(button, collapse)| { Interaction::Pressed | Interaction::Hovered => {
if entity == collapse.target { {
Some(button) let parent = parents.get(entity).expect("entity has parent");
} else { children
None .get(parent.get())
} .expect("parent has children")
})
.iter() .iter()
.for_each(|button| { .filter(|&e| *e != entity)
let num_children = children.len(); .for_each(|sibling| {
commands.entity(*button).insert(Note { if let Ok(mut select) = selects.get_mut(*sibling) {
text: format!("({})", num_children), *select = UiKitSelect::None
});
});
});
} }
});
} }
pub use buttons::*; if let Ok(mut select) = selects.get_mut(entity) {
mod buttons { *select = UiKitSelect::Active
use super::*; }
}
Interaction::None => {
// Find the ancestor which does not have a parent
let root_parent = parents
.iter_ancestors(entity)
.find(|&e| parents.get(e).is_err())
.expect("entity has root parent");
let family: Vec<Entity> = children.iter_descendants(root_parent).collect();
let family_inactive = family
.iter()
.filter_map(|member| interactions.get(*member).ok())
.all(|&interaction| interaction == Interaction::None);
#[derive(Debug, Component)] // Zero out children
pub struct Active; let descendants: Vec<Entity> = children.iter_descendants(entity).collect();
let descendants_inactive = descendants
.iter()
.filter_map(|child| interactions.get(*child).ok())
.all(|&interaction| interaction == Interaction::None);
pub fn manage_button_interaction( // The entire tree is inactive
events: Query< if family_inactive {
(Entity, &Interaction, Option<&Active>), family.iter().for_each(|member| {
(Changed<Interaction>, With<Button>), if let Ok(mut select) = selects.get_mut(*member) {
>, *select = UiKitSelect::None
added_active: Query<Entity, Added<Active>>,
mut removed_active: RemovedComponents<Active>,
mut bg_colors: Query<&mut BackgroundColor>,
) {
added_active.for_each(|e| {
if let Ok(mut bg_color) = bg_colors.get_mut(e) {
*bg_color = Color::ORANGE.into();
} }
}); });
removed_active.iter().for_each(|e| { if let Ok(mut select) = selects.get_mut(entity) {
if let Ok(mut bg_color) = bg_colors.get_mut(e) { *select = UiKitSelect::None
*bg_color = Color::WHITE.into();
}
});
events.for_each(|(entity, interaction, active)| {
if let Ok(mut bg_color) = bg_colors.get_mut(entity) {
match active {
Some(_) => match interaction {
Interaction::None => *bg_color = Color::ORANGE.into(),
Interaction::Pressed => *bg_color = Color::YELLOW.into(),
Interaction::Hovered => *bg_color = Color::RED.into(),
},
None => match interaction {
Interaction::None => *bg_color = Color::WHITE.into(),
Interaction::Pressed => *bg_color = Color::WHITE.into(),
Interaction::Hovered => *bg_color = Color::GRAY.into(),
},
} }
// Just the sub-tree is inactive
} else if descendants_inactive {
descendants.iter().for_each(|child| {
if let Ok(mut select) = selects.get_mut(*child) {
*select = UiKitSelect::None
} }
}) });
if let Ok(mut select) = selects.get_mut(entity) {
*select = UiKitSelect::None
} }
// This node is active (usually a parent of an active child)
/// Marks a container node as having single- or multi-active components } else if let Ok(mut select) = selects.get_mut(entity) {
#[derive(Debug, Component)] *select = UiKitSelect::Active
pub enum Select {
Multi,
Single,
} }
pub fn manage_select_active(
events: Query<(Entity, &Parent), Added<Active>>,
children: Query<(&Select, &Children)>,
mut commands: Commands,
) {
events.iter().for_each(|(entity, parent)| {
if let Ok((select, childs)) = children.get(parent.get()) {
match select {
Select::Single => {
childs
.iter()
.filter(|&child| *child != entity)
.for_each(|&child| {
commands.entity(child).remove::<Active>();
})
} }
Select::Multi => (),
} }
} }
}); });
} }
}
pub use scroll::*; fn select_textbox(
mod scroll { mut events: Query<(&Interaction, &mut UiKitSelect), (With<Text>, Changed<Interaction>)>,
use super::*;
#[derive(Debug, Component)]
pub struct Scroll;
pub fn manage_scroll(
mut events: EventReader<MouseWheel>,
mut query: Query<(&Interaction, &Parent, &Children, &mut Style), With<Scroll>>,
interactions: Query<&Interaction>,
) { ) {
events.iter().for_each(|MouseWheel { unit, y, .. }| { events.iter_mut().for_each(|(interaction, mut select)| {
query *select = match interaction {
.iter_mut() Interaction::Pressed => UiKitSelect::Active,
.filter(|(&interaction, parent, children, _)| { Interaction::Hovered => UiKitSelect::Active,
let self_hover = interaction == Interaction::Hovered; Interaction::None => UiKitSelect::None,
let child_hover = children.iter().any(|&child| {
if let Ok(&interaction) = interactions.get(child) {
interaction == Interaction::Hovered
} else {
false
} }
}); });
let parent_hover = interactions
.get(parent.get())
.map_or(false, |&interaction| interaction == Interaction::Hovered);
self_hover || child_hover || parent_hover
})
.for_each(|(_, _, _, mut style)| {
let factor = 2.0;
let val = match unit {
MouseScrollUnit::Line => match style.top {
Val::Px(v) => v + (y * factor),
_ => (*y) * factor,
},
MouseScrollUnit::Pixel => match style.top {
Val::Px(v) => v + (y * factor),
_ => (*y) * factor,
},
};
style.top = Val::Px(val.min(0.0));
});
});
} }
}
use cursor::*;
mod cursor {
use super::*;
pub fn manage_cursor( fn text_editor(
events: Query<&Interaction, Changed<Interaction>>, keyboard: Res<Input<KeyCode>>,
mut window: Query<&mut Window, With<PrimaryWindow>>, mut events: EventReader<KeyboardInput>,
mut query: Query<&mut Text, With<UiKitSelect>>,
) { ) {
events.for_each(|interaction| { events.iter().for_each(
let mut win = window.single_mut(); |KeyboardInput {
key_code,
win.cursor.icon = match interaction { state,
Interaction::None => CursorIcon::Default, scan_code,
Interaction::Hovered => CursorIcon::Hand, ..
Interaction::Pressed => CursorIcon::Grabbing, }| {
} match state {
}) ButtonState::Pressed => {
} if let Some(kc) = key_code {
} query.iter_mut().for_each(|mut text| {
use KeyCode::*;
pub use sort::*;
mod sort { let style = TextStyle {
use std::cmp::Ordering; color: Color::BLACK,
..default()
use super::*; };
#[derive(Debug, Component)]
pub struct Sorting(pub u8);
pub fn manage_sort( if *kc == Back {
mut events: Query<&mut Children, Changed<Children>>, text.sections.pop();
sorting: Query<Option<&Sorting>>, } else {
) { let c = match kc {
events.iter_mut().for_each(|mut children| { // Letters
children.sort_by(|&a, &b| match (sorting.get(a), sorting.get(b)) { A | B | C | D | E | F | G | H | I | J | K | L | M | N | O
(Ok(Some(Sorting(ord_a))), Ok(Some(Sorting(ord_b)))) => { | P | Q | R | S | T | U | V | W | X | Y | Z => {
if ord_a > ord_b { if keyboard.any_pressed([ShiftLeft, ShiftRight]) {
Ordering::Greater format!("{:?}", kc).to_uppercase()
} else if ord_a < ord_b {
Ordering::Less
} else { } else {
Ordering::Equal format!("{:?}", kc).to_lowercase()
}
}
// Top Row
Grave => "`".to_string(),
Key1 | Numpad1 => "1".to_string(),
Key2 | Numpad2 => "2".to_string(),
Key3 | Numpad3 => "3".to_string(),
Key4 | Numpad4 => "4".to_string(),
Key5 | Numpad5 => "5".to_string(),
Key6 | Numpad6 => "6".to_string(),
Key7 | Numpad7 => "7".to_string(),
Key8 | Numpad8 => "8".to_string(),
Key9 | Numpad9 => "9".to_string(),
Key0 | Numpad0 => "0".to_string(),
Minus => "-".to_string(),
Equals => "=".to_string(),
// Left side
Tab => "\t".to_string(),
// Right side
// Row 2
BracketLeft => "[".to_string(),
BracketRight => "]".to_string(),
Backslash => "\\".to_string(),
// Row 3
Semicolon => ";".to_string(),
Apostrophe => "'".to_string(),
Return => "\n".to_string(),
// Row 4
Comma => ",".to_string(),
Period => ".".to_string(),
Slash => "/".to_string(),
// Space
Space => " ".to_string(),
// None
_ => "".to_string(),
};
if c.len() > 0 {
text.sections.push(TextSection::new(c, style));
} }
} }
(Ok(Some(_)), Err(_)) | (Ok(Some(_)), Ok(None)) => Ordering::Less,
(Err(_), Ok(Some(_))) | (Ok(None), Ok(Some(_))) => Ordering::Greater,
_ => Ordering::Equal,
}); });
})
} }
} }
_ => (),
pub use alert::*;
pub mod alert {
use super::*;
#[derive(Debug, Event)]
pub enum Alert {
Info(String),
Warn(String),
Danger(String),
} }
#[derive(Debug, Component)]
pub struct AlertsWidget;
pub fn init_alerts(mut commands: Commands) {
commands.spawn((
NodeBundle {
style: Style {
top: Val::Px(0.0),
right: Val::Px(0.0),
position_type: PositionType::Absolute,
width: Val::Percent(33.0),
padding: UiRect::all(Val::Px(1.0)),
margin: UiRect::all(Val::Px(1.0)),
border: UiRect::all(Val::Px(1.0)),
flex_direction: FlexDirection::Column,
justify_items: JustifyItems::Center,
..default()
},
border_color: Color::WHITE.into(),
..default()
}, },
AlertsWidget, )
));
} }
pub fn spawn_alert( // TODO: Reset default position when de-activated
mut events: EventReader<Alert>, fn scroll(
root: Query<Entity, With<AlertsWidget>>, mut scrolls: EventReader<MouseWheel>,
mut commands: Commands, mut query: Query<(&mut Style, &UiKitSelect, Entity, &Children, &Parent)>,
changes: Query<Entity, Changed<UiKitSelect>>,
) { ) {
events.iter().for_each(|alert| { // Brute force: When all Actives are set to None (event) reset all style tops...
info!("Processing alert {:?}", alert); changes.iter().for_each(|_| {
let all_inactive = query
let (color, text) = match alert { .iter_mut()
Alert::Info(text) => (Color::BLUE, text), .all(|(_, select, _, _, _)| *select == UiKitSelect::None);
Alert::Warn(text) => (Color::ORANGE, text), all_inactive.then(|| {
Alert::Danger(text) => (Color::RED, text), query
}; .iter_mut()
commands.entity(root.single()).with_children(|parent| { .for_each(|(mut style, _, _, _, _)| style.top = Val::Px(0.0));
parent
.spawn(NodeBundle {
style: Style {
width: Val::Percent(100.0),
padding: UiRect::all(Val::Px(1.0)),
margin: UiRect::all(Val::Px(1.0)),
border: UiRect::all(Val::Px(2.0)),
flex_direction: FlexDirection::Column,
justify_self: JustifySelf::Center,
..default()
},
border_color: color.into(),
..default()
})
.with_children(|parent| {
parent.spawn((
NodeBundle {
style: Style {
padding: UiRect::all(Val::Px(1.0)),
margin: UiRect::all(Val::Px(1.0)),
border: UiRect::all(Val::Px(1.0)),
flex_direction: FlexDirection::Row,
align_items: AlignItems::Center,
align_content: AlignContent::Center,
justify_content: JustifyContent::SpaceBetween,
..default()
},
background_color: color.into(),
..default()
},
Title {
text: "Alert".into(),
},
Close {
target: parent.parent_entity(),
},
Sorting(0),
));
parent.spawn(TextBundle::from_section(text, TextStyle { ..default() }));
}); });
}); });
scrolls.iter().for_each(|MouseWheel { unit, y, .. }| {
// Find the leaf selected entity
let leaf = query
.iter()
.find(|(_, select, _, children, parent)| {
// This node is active
let self_active = **select == UiKitSelect::Active;
// All children are not selected
let children_inactive = children.iter().all(|&child| {
if let Ok((_, select, _, _, _)) = query.get(child) {
*select == UiKitSelect::None
} else {
true
}
}); });
// Both must be true
self_active && children_inactive
})
.map(|(_, _, _, _, parent)| parent.get());
if let Some(l) = leaf {
if let Ok((mut style, _, _, _, _)) = query.get_mut(l) {
if *y != 0.0 {
let delta = match unit {
MouseScrollUnit::Line => *y,
MouseScrollUnit::Pixel => 5.0,
};
style.top.try_sub_assign(Val::Px(delta));
info!("Top: {:?}", style.top);
}
} }
};
});
} }

Loading…
Cancel
Save