|
|
|
@ -21,475 +21,190 @@ 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_systems(Update, (select_tab, select_textbox, text_editor, scroll))
|
|
|
|
app.add_systems(
|
|
|
|
.add_systems(
|
|
|
|
Update,
|
|
|
|
PreUpdate,
|
|
|
|
(
|
|
|
|
(
|
|
|
|
init_titles,
|
|
|
|
spawn_container,
|
|
|
|
manage_button_interaction,
|
|
|
|
spawn_button,
|
|
|
|
manage_cursor,
|
|
|
|
/* manage_labels */ selection,
|
|
|
|
manage_scroll,
|
|
|
|
),
|
|
|
|
manage_collapse,
|
|
|
|
);
|
|
|
|
manage_sort,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub use self::container::*;
|
|
|
|
pub use title::*;
|
|
|
|
mod container {
|
|
|
|
mod title {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
pub struct UiKitContainer {
|
|
|
|
pub struct Title {
|
|
|
|
pub position: UiKitPosition,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
|
|
|
|
pub enum UiKitPosition {
|
|
|
|
|
|
|
|
Top,
|
|
|
|
|
|
|
|
Left,
|
|
|
|
|
|
|
|
Right,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn spawn_container(
|
|
|
|
|
|
|
|
events: Query<(Entity, &UiKitContainer, Option<&UiKitLabel>), Added<UiKitContainer>>,
|
|
|
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
events.iter().for_each(|(entity, container, label)| {
|
|
|
|
|
|
|
|
let base_style = Style {
|
|
|
|
|
|
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
|
|
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
|
|
|
|
display: Display::None,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
let style = match container.position {
|
|
|
|
|
|
|
|
UiKitPosition::Top => Style {
|
|
|
|
|
|
|
|
top: Val::Percent(100.0),
|
|
|
|
|
|
|
|
align_items: AlignItems::Start,
|
|
|
|
|
|
|
|
..base_style
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
UiKitPosition::Left => Style {
|
|
|
|
|
|
|
|
left: Val::Percent(100.0),
|
|
|
|
|
|
|
|
justify_items: JustifyItems::Start,
|
|
|
|
|
|
|
|
..base_style
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
UiKitPosition::Right => Style {
|
|
|
|
|
|
|
|
right: Val::Percent(104.0),
|
|
|
|
|
|
|
|
justify_items: JustifyItems::Start,
|
|
|
|
|
|
|
|
..base_style
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
commands
|
|
|
|
|
|
|
|
.entity(entity)
|
|
|
|
|
|
|
|
.insert(NodeBundle {
|
|
|
|
|
|
|
|
style,
|
|
|
|
|
|
|
|
background_color: BackgroundColor(Color::PURPLE),
|
|
|
|
|
|
|
|
border_color: BorderColor(Color::BLACK),
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.with_children(|parent| {
|
|
|
|
|
|
|
|
if let Some(label) = label {
|
|
|
|
|
|
|
|
parent.spawn(
|
|
|
|
|
|
|
|
TextBundle::from_section(
|
|
|
|
|
|
|
|
label.name.clone(),
|
|
|
|
|
|
|
|
TextStyle {
|
|
|
|
|
|
|
|
color: Color::BLACK,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.with_style(Style {
|
|
|
|
|
|
|
|
align_self: AlignSelf::Center,
|
|
|
|
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub use self::label::*;
|
|
|
|
|
|
|
|
mod label {
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
|
|
|
|
pub struct UiKitLabel {
|
|
|
|
|
|
|
|
pub name: String,
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Handle modified labels
|
|
|
|
pub fn init_titles(events: Query<(Entity, &Title), Added<Title>>, mut commands: Commands) {
|
|
|
|
pub fn manage_labels(
|
|
|
|
events.for_each(|(entity, Title { name })| {
|
|
|
|
events: Query<(Entity, &UiKitLabel), Added<UiKitLabel>>,
|
|
|
|
|
|
|
|
mut commands: Commands,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
events.iter().for_each(|(entity, label)| {
|
|
|
|
|
|
|
|
commands.entity(entity).with_children(|parent| {
|
|
|
|
commands.entity(entity).with_children(|parent| {
|
|
|
|
parent.spawn(
|
|
|
|
parent.spawn(TextBundle {
|
|
|
|
TextBundle::from_section(
|
|
|
|
text: Text {
|
|
|
|
label.name.clone(),
|
|
|
|
sections: vec![TextSection {
|
|
|
|
TextStyle {
|
|
|
|
value: name.clone(),
|
|
|
|
color: Color::BLACK,
|
|
|
|
style: TextStyle {
|
|
|
|
..default()
|
|
|
|
color: Color::BLACK,
|
|
|
|
},
|
|
|
|
..default()
|
|
|
|
)
|
|
|
|
},
|
|
|
|
.with_style(Style {
|
|
|
|
}],
|
|
|
|
align_self: AlignSelf::Center,
|
|
|
|
..default()
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
},
|
|
|
|
|
|
|
|
style: Style {
|
|
|
|
|
|
|
|
margin: UiRect::all(Val::Px(5.0)),
|
|
|
|
|
|
|
|
padding: UiRect::all(Val::Px(5.0)),
|
|
|
|
..default()
|
|
|
|
..default()
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
..default()
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub use self::button::*;
|
|
|
|
pub use collapse::*;
|
|
|
|
mod button {
|
|
|
|
mod collapse {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
pub struct UiKitButton {
|
|
|
|
pub struct Collapse {
|
|
|
|
pub color: Color,
|
|
|
|
pub target: Entity,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn spawn_button(
|
|
|
|
pub fn manage_collapse(
|
|
|
|
events: Query<(Entity, &UiKitButton, Option<&UiKitLabel>), Added<UiKitButton>>,
|
|
|
|
events: Query<(Entity, &Collapse, &Interaction), (Changed<Interaction>, With<Button>)>,
|
|
|
|
mut commands: Commands,
|
|
|
|
mut styles: Query<&mut Style>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
events.iter().for_each(|(entity, button, label)| {
|
|
|
|
events
|
|
|
|
commands
|
|
|
|
.iter()
|
|
|
|
.entity(entity)
|
|
|
|
.filter(|(_, _, &interaction)| interaction == Interaction::Pressed)
|
|
|
|
.insert(ButtonBundle {
|
|
|
|
.for_each(|(entity, collapse, _)| {
|
|
|
|
style: Style {
|
|
|
|
info!("Collapse press on {:?}", entity);
|
|
|
|
border: UiRect::all(Val::Px(1.0)),
|
|
|
|
if let Ok(mut style) = styles.get_mut(collapse.target) {
|
|
|
|
// width: Val::Px(100.0),
|
|
|
|
style.display = match style.display {
|
|
|
|
// height: Val::Px(50.0),
|
|
|
|
Display::None => Display::Flex,
|
|
|
|
flex_direction: FlexDirection::Column,
|
|
|
|
Display::Flex | Display::Grid => Display::None,
|
|
|
|
..default()
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
|
|
|
background_color: BackgroundColor(button.color),
|
|
|
|
})
|
|
|
|
border_color: BorderColor(Color::BLACK),
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
.with_children(|parent| {
|
|
|
|
|
|
|
|
if let Some(label) = label {
|
|
|
|
|
|
|
|
parent.spawn(
|
|
|
|
|
|
|
|
TextBundle::from_section(
|
|
|
|
|
|
|
|
label.name.clone(),
|
|
|
|
|
|
|
|
TextStyle {
|
|
|
|
|
|
|
|
color: Color::BLACK,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
.with_style(Style {
|
|
|
|
|
|
|
|
align_self: AlignSelf::Center,
|
|
|
|
|
|
|
|
justify_self: JustifySelf::Center,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub use self::select::*;
|
|
|
|
pub use buttons::*;
|
|
|
|
mod select {
|
|
|
|
mod buttons {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Component, Copy, Clone, PartialEq)]
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
pub enum UiKitSelect {
|
|
|
|
pub struct Active;
|
|
|
|
Active,
|
|
|
|
|
|
|
|
None,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl Default for UiKitSelect {
|
|
|
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
|
|
|
UiKitSelect::None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// When an item is selected/de-selected change it's display accordingly
|
|
|
|
pub fn manage_button_interaction(
|
|
|
|
pub fn selection(
|
|
|
|
|
|
|
|
mut events: Query<
|
|
|
|
mut events: Query<
|
|
|
|
(&mut BackgroundColor, &UiKitSelect, &Children),
|
|
|
|
(&Interaction, &mut BackgroundColor, Option<&Active>),
|
|
|
|
(Changed<UiKitSelect>, With<Button>),
|
|
|
|
(Changed<Interaction>, With<Button>),
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
mut styles: Query<&mut Style>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
events
|
|
|
|
events.for_each_mut(|(interaction, mut bg_color, active)| match active {
|
|
|
|
.iter_mut()
|
|
|
|
Some(_) => match interaction {
|
|
|
|
.for_each(|(mut bg_color, select, children)| {
|
|
|
|
Interaction::None => *bg_color = Color::ORANGE.into(),
|
|
|
|
bg_color.0 = match select {
|
|
|
|
Interaction::Pressed => *bg_color = Color::YELLOW.into(),
|
|
|
|
UiKitSelect::Active => Color::RED,
|
|
|
|
Interaction::Hovered => *bg_color = Color::RED.into(),
|
|
|
|
UiKitSelect::None => Color::WHITE,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
None => match interaction {
|
|
|
|
children.iter().for_each(|&child| {
|
|
|
|
Interaction::None => *bg_color = Color::GRAY.into(),
|
|
|
|
if let Ok(mut style) = styles.get_mut(child) {
|
|
|
|
Interaction::Pressed => *bg_color = Color::WHITE.into(),
|
|
|
|
style.display = match select {
|
|
|
|
Interaction::Hovered => *bg_color = Color::DARK_GRAY.into(),
|
|
|
|
UiKitSelect::Active => Display::Flex,
|
|
|
|
},
|
|
|
|
UiKitSelect::None => Display::None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub use self::text_input::*;
|
|
|
|
pub use scroll::*;
|
|
|
|
mod text_input {
|
|
|
|
mod scroll {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Bundle)]
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
pub struct UiKitTextInput {
|
|
|
|
pub struct Scroll;
|
|
|
|
text_bundle: TextBundle,
|
|
|
|
|
|
|
|
interaction: Interaction,
|
|
|
|
|
|
|
|
select: UiKitSelect,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl UiKitTextInput {
|
|
|
|
|
|
|
|
pub fn new() -> Self {
|
|
|
|
|
|
|
|
UiKitTextInput {
|
|
|
|
|
|
|
|
text_bundle: TextBundle {
|
|
|
|
|
|
|
|
style: Style {
|
|
|
|
|
|
|
|
// width: Val::Percent(100.0),
|
|
|
|
|
|
|
|
// height: Val::Percent(100.0),
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
interaction: Interaction::None,
|
|
|
|
|
|
|
|
select: UiKitSelect::None,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn select_textbox(
|
|
|
|
pub fn manage_scroll(
|
|
|
|
mut events: Query<(&Interaction, &mut UiKitSelect), (With<Text>, Changed<Interaction>)>,
|
|
|
|
mut events: EventReader<MouseWheel>,
|
|
|
|
|
|
|
|
mut query: Query<&mut Style, With<Scroll>>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
events.iter_mut().for_each(|(interaction, mut select)| {
|
|
|
|
events.iter().for_each(|MouseWheel { unit, y, .. }| {
|
|
|
|
*select = match interaction {
|
|
|
|
query.iter_mut().for_each(|mut style| {
|
|
|
|
Interaction::Pressed => UiKitSelect::Active,
|
|
|
|
let factor = 2.0;
|
|
|
|
Interaction::Hovered => UiKitSelect::Active,
|
|
|
|
let val = match unit {
|
|
|
|
Interaction::None => UiKitSelect::None,
|
|
|
|
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));
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn text_editor(
|
|
|
|
use cursor::*;
|
|
|
|
keyboard: Res<Input<KeyCode>>,
|
|
|
|
mod cursor {
|
|
|
|
mut events: EventReader<KeyboardInput>,
|
|
|
|
use super::*;
|
|
|
|
mut query: Query<&mut Text, With<UiKitSelect>>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
events.iter().for_each(
|
|
|
|
|
|
|
|
|KeyboardInput {
|
|
|
|
|
|
|
|
key_code,
|
|
|
|
|
|
|
|
state,
|
|
|
|
|
|
|
|
scan_code,
|
|
|
|
|
|
|
|
..
|
|
|
|
|
|
|
|
}| {
|
|
|
|
|
|
|
|
match state {
|
|
|
|
|
|
|
|
ButtonState::Pressed => {
|
|
|
|
|
|
|
|
if let Some(kc) = key_code {
|
|
|
|
|
|
|
|
query.iter_mut().for_each(|mut text| {
|
|
|
|
|
|
|
|
use KeyCode::*;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let style = TextStyle {
|
|
|
|
|
|
|
|
color: Color::BLACK,
|
|
|
|
|
|
|
|
..default()
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if *kc == Back {
|
|
|
|
pub fn manage_cursor(
|
|
|
|
text.sections.pop();
|
|
|
|
events: Query<&Interaction, Changed<Interaction>>,
|
|
|
|
} else {
|
|
|
|
mut window: Query<&mut Window, With<PrimaryWindow>>,
|
|
|
|
let c = match kc {
|
|
|
|
) {
|
|
|
|
// Letters
|
|
|
|
events.for_each(|interaction| {
|
|
|
|
A | B | C | D | E | F | G | H | I | J | K | L | M | N
|
|
|
|
let mut win = window.single_mut();
|
|
|
|
| O | P | Q | R | S | T | U | V | W | X | Y | Z => {
|
|
|
|
|
|
|
|
if keyboard.any_pressed([ShiftLeft, ShiftRight]) {
|
|
|
|
|
|
|
|
format!("{:?}", kc).to_uppercase()
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
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
|
|
|
|
win.cursor.icon = match interaction {
|
|
|
|
Tab => "\t".to_string(),
|
|
|
|
Interaction::None => CursorIcon::Default,
|
|
|
|
// Right side
|
|
|
|
Interaction::Hovered => CursorIcon::Hand,
|
|
|
|
// Row 2
|
|
|
|
Interaction::Pressed => CursorIcon::Grabbing,
|
|
|
|
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));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => (),
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Toggle a UI Nav tree open/closed
|
|
|
|
pub use sort::*;
|
|
|
|
///
|
|
|
|
mod sort {
|
|
|
|
/// PERF: This is hella not performant, we just usually don't have many elements to iterate over so
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
/// it's tolerable.
|
|
|
|
|
|
|
|
pub 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>,
|
|
|
|
|
|
|
|
) {
|
|
|
|
|
|
|
|
events.iter().for_each(|entity| {
|
|
|
|
|
|
|
|
// Otherwise, update nav tree(s)
|
|
|
|
|
|
|
|
if let Ok(interaction) = interactions.get(entity) {
|
|
|
|
|
|
|
|
match interaction {
|
|
|
|
|
|
|
|
Interaction::Pressed | Interaction::Hovered => {
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let parent = parents.get(entity).expect("entity has parent");
|
|
|
|
|
|
|
|
children
|
|
|
|
|
|
|
|
.get(parent.get())
|
|
|
|
|
|
|
|
.expect("parent has children")
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.filter(|&e| *e != entity)
|
|
|
|
|
|
|
|
.for_each(|sibling| {
|
|
|
|
|
|
|
|
if let Ok(mut select) = selects.get_mut(*sibling) {
|
|
|
|
|
|
|
|
*select = UiKitSelect::None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Ok(mut select) = selects.get_mut(entity) {
|
|
|
|
|
|
|
|
*select = UiKitSelect::Active
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Zero out children
|
|
|
|
use super::*;
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// The entire tree is inactive
|
|
|
|
#[derive(Debug, Component)]
|
|
|
|
if family_inactive {
|
|
|
|
pub struct Sorting(pub u8);
|
|
|
|
family.iter().for_each(|member| {
|
|
|
|
|
|
|
|
if let Ok(mut select) = selects.get_mut(*member) {
|
|
|
|
|
|
|
|
*select = UiKitSelect::None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
if let Ok(mut select) = selects.get_mut(entity) {
|
|
|
|
|
|
|
|
*select = UiKitSelect::None
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
} else if let Ok(mut select) = selects.get_mut(entity) {
|
|
|
|
|
|
|
|
*select = UiKitSelect::Active
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Reset default position when de-activated
|
|
|
|
pub fn manage_sort(
|
|
|
|
fn scroll(
|
|
|
|
mut events: Query<&mut Children, Changed<Children>>,
|
|
|
|
mut scrolls: EventReader<MouseWheel>,
|
|
|
|
sorting: Query<Option<&Sorting>>,
|
|
|
|
mut query: Query<(&mut Style, &UiKitSelect, Entity, &Children, &Parent)>,
|
|
|
|
) {
|
|
|
|
changes: Query<Entity, Changed<UiKitSelect>>,
|
|
|
|
events.iter_mut().for_each(|mut children| {
|
|
|
|
) {
|
|
|
|
children.sort_by(|&a, &b| match (sorting.get(a), sorting.get(b)) {
|
|
|
|
// Brute force: When all Actives are set to None (event) reset all style tops...
|
|
|
|
(Ok(Some(Sorting(ord_a))), Ok(Some(Sorting(ord_b)))) => {
|
|
|
|
changes.iter().for_each(|_| {
|
|
|
|
if ord_a > ord_b {
|
|
|
|
let all_inactive = query
|
|
|
|
Ordering::Greater
|
|
|
|
.iter_mut()
|
|
|
|
} else if ord_a < ord_b {
|
|
|
|
.all(|(_, select, _, _, _)| *select == UiKitSelect::None);
|
|
|
|
Ordering::Less
|
|
|
|
all_inactive.then(|| {
|
|
|
|
|
|
|
|
query
|
|
|
|
|
|
|
|
.iter_mut()
|
|
|
|
|
|
|
|
.for_each(|(mut style, _, _, _, _)| style.top = Val::Px(0.0));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
scrolls.iter().for_each(|MouseWheel { unit, y, .. }| {
|
|
|
|
|
|
|
|
// Find the leaf selected entity
|
|
|
|
|
|
|
|
let leaf = query
|
|
|
|
|
|
|
|
.iter()
|
|
|
|
|
|
|
|
.find(|(_, select, _, children, _)| {
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
info!("Equal");
|
|
|
|
|
|
|
|
Ordering::Equal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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));
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(Ok(Some(_)), Err(_)) | (Ok(Some(_)), Ok(None)) => Ordering::Less,
|
|
|
|
};
|
|
|
|
(Err(_), Ok(Some(_))) | (Ok(None), Ok(Some(_))) => Ordering::Greater,
|
|
|
|
});
|
|
|
|
_ => Ordering::Equal,
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|