Skip to content

Commit d3e156a

Browse files
committed
Put all HUD elements in a single flexbox
1 parent 6c667f5 commit d3e156a

File tree

5 files changed

+88
-92
lines changed

5 files changed

+88
-92
lines changed

crates/controller/src/hud/actionbar.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ pub(crate) struct ActionBarPlugin;
1616

1717
impl Plugin for ActionBarPlugin {
1818
fn build(&self, app: &mut App) {
19-
app.add_system(setup.in_schedule(OnEnter(GameState::Playing)))
20-
.add_system(cleanup.in_schedule(OnExit(GameState::Playing)))
19+
app.add_system(cleanup.in_schedule(OnExit(GameState::Playing)))
2120
.add_system(
2221
detect_update
2322
.in_base_set(GameSet::PostUpdate)
@@ -54,27 +53,13 @@ struct ActiveEntity(Option<Entity>);
5453
#[derive(Component)]
5554
struct ButtonAction(UnitType);
5655

57-
fn cleanup(mut commands: Commands) {
58-
commands.remove_resource::<ActionBarNode>();
59-
commands.remove_resource::<ActiveEntity>();
60-
}
61-
62-
fn setup(mut commands: Commands) {
56+
pub(crate) fn setup_actionbar(commands: &mut Commands) -> Entity {
6357
let entity = commands
6458
.spawn((
6559
NodeBundle {
6660
style: Style {
67-
size: Size {
68-
width: Val::Percent(60.),
69-
height: Val::Percent(15.),
70-
},
71-
position_type: PositionType::Absolute,
72-
position: UiRect::new(
73-
Val::Percent(20.),
74-
Val::Percent(80.),
75-
Val::Percent(85.),
76-
Val::Percent(100.),
77-
),
61+
size: Size::height(Val::Percent(50.)),
62+
flex_grow: 1.,
7863
..default()
7964
},
8065
background_color: HUD_COLOR.into(),
@@ -87,6 +72,12 @@ fn setup(mut commands: Commands) {
8772

8873
commands.insert_resource(ActionBarNode(entity));
8974
commands.init_resource::<ActiveEntity>();
75+
entity
76+
}
77+
78+
fn cleanup(mut commands: Commands) {
79+
commands.remove_resource::<ActionBarNode>();
80+
commands.remove_resource::<ActiveEntity>();
9081
}
9182

9283
fn detect_update(mut active: ResMut<ActiveEntity>, selected: Query<Entity, With<Selected>>) {
Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
use bevy::prelude::*;
2-
use de_core::{cleanup::DespawnOnGameExit, gamestate::GameState};
32

43
use super::{interaction::InteractionBlocker, HUD_COLOR};
54

6-
pub(crate) struct DetailsPlugin;
7-
8-
impl Plugin for DetailsPlugin {
9-
fn build(&self, app: &mut App) {
10-
app.add_system(spawn_details.in_schedule(OnEnter(GameState::Playing)));
11-
}
12-
}
13-
14-
fn spawn_details(mut commands: Commands) {
15-
commands.spawn((
16-
NodeBundle {
17-
style: Style {
18-
size: Size {
19-
width: Val::Percent(20.),
20-
height: Val::Percent(30.),
5+
pub(crate) fn setup_details(commands: &mut Commands) -> Entity {
6+
commands
7+
.spawn((
8+
NodeBundle {
9+
style: Style {
10+
size: Size {
11+
width: Val::Percent(20.),
12+
height: Val::Percent(100.),
13+
},
14+
flex_shrink: 0.,
15+
..default()
2116
},
22-
position_type: PositionType::Absolute,
23-
position: UiRect::new(
24-
Val::Percent(0.),
25-
Val::Percent(20.),
26-
Val::Percent(70.),
27-
Val::Percent(100.),
28-
),
17+
background_color: HUD_COLOR.into(),
2918
..default()
3019
},
31-
background_color: HUD_COLOR.into(),
32-
..default()
33-
},
34-
DespawnOnGameExit,
35-
InteractionBlocker,
36-
));
20+
InteractionBlocker,
21+
))
22+
.id()
3723
}

crates/controller/src/hud/minimap/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ mod fill;
77
mod interaction;
88
mod nodes;
99

10+
pub(crate) use nodes::setup_minimap;
11+
1012
pub(crate) struct MinimapPlugin;
1113

1214
impl Plugin for MinimapPlugin {

crates/controller/src/hud/minimap/nodes.rs

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use bevy::{
22
prelude::*,
33
render::render_resource::{Extent3d, TextureDimension, TextureFormat},
44
};
5-
use de_core::{baseset::GameSet, cleanup::DespawnOnGameExit, gamestate::GameState};
5+
use de_core::{baseset::GameSet, gamestate::GameState};
66
use de_map::size::MapBounds;
77

88
use crate::hud::{interaction::InteractionBlocker, HUD_COLOR};
@@ -11,65 +11,55 @@ pub(super) struct NodesPlugin;
1111

1212
impl Plugin for NodesPlugin {
1313
fn build(&self, app: &mut App) {
14-
app.add_system(setup.in_schedule(OnEnter(GameState::Playing)))
15-
.add_system(
16-
update_resolution
17-
.in_base_set(GameSet::PreMovement)
18-
.run_if(in_state(GameState::Playing)),
19-
);
14+
app.add_system(
15+
update_resolution
16+
.in_base_set(GameSet::PreMovement)
17+
.run_if(in_state(GameState::Playing)),
18+
);
2019
}
2120
}
2221

2322
#[derive(Component)]
2423
pub(super) struct MinimapNode;
2524

26-
fn setup(mut commands: Commands, mut images: ResMut<Assets<Image>>, map_bounds: Res<MapBounds>) {
27-
let handle = images.add(new_image(UVec2::splat(128)));
25+
pub(crate) fn setup_minimap(
26+
commands: &mut Commands,
27+
mut images: ResMut<Assets<Image>>,
28+
map_bounds: Res<MapBounds>,
29+
) -> Entity {
30+
let handle = images.add(new_image(UVec2::new(256, 128)));
2831
let map_size = map_bounds.size();
2932
let aspect = map_size.x / map_size.y;
3033

3134
commands
32-
.spawn(NodeBundle {
33-
style: Style {
34-
position_type: PositionType::Absolute,
35-
position: UiRect::bottom(Val::Px(0.)),
36-
size: Size::new(Val::Percent(100.), Val::Percent(30.)),
37-
justify_content: JustifyContent::End,
35+
.spawn((
36+
NodeBundle {
37+
style: Style {
38+
max_size: Size::new(Val::Percent(20.), Val::Percent(100.)),
39+
size: Size::height(Val::Percent(100.)),
40+
padding: UiRect::all(Val::Percent(1.)),
41+
..default()
42+
},
43+
background_color: HUD_COLOR.into(),
3844
..default()
3945
},
40-
..default()
41-
})
42-
.insert(DespawnOnGameExit)
46+
InteractionBlocker,
47+
))
4348
.with_children(|parent| {
4449
parent
45-
.spawn(NodeBundle {
50+
.spawn(ImageBundle {
4651
style: Style {
47-
size: Size::width(Val::Percent(20.)),
48-
padding: UiRect::all(Val::Percent(1.)),
49-
align_items: AlignItems::Center,
50-
justify_content: JustifyContent::Center,
52+
size: Size::all(Val::Percent(100.)),
53+
aspect_ratio: Some(aspect),
5154
..default()
5255
},
53-
background_color: HUD_COLOR.into(),
56+
background_color: Color::WHITE.into(),
57+
image: handle.into(),
5458
..default()
5559
})
56-
.insert(InteractionBlocker)
57-
.with_children(|parent| {
58-
parent
59-
.spawn(ImageBundle {
60-
style: Style {
61-
max_size: Size::all(Val::Percent(100.)),
62-
size: Size::height(Val::Percent(100.)),
63-
aspect_ratio: Some(aspect),
64-
..default()
65-
},
66-
background_color: Color::WHITE.into(),
67-
image: handle.into(),
68-
..default()
69-
})
70-
.insert(MinimapNode);
71-
});
72-
});
60+
.insert(MinimapNode);
61+
})
62+
.id()
7363
}
7464

7565
type ChangedMinimap = (Changed<Node>, With<MinimapNode>);

crates/controller/src/hud/mod.rs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ mod menu;
77
mod minimap;
88
mod selection;
99

10+
use de_core::{cleanup::DespawnOnGameExit, gamestate::GameState};
11+
use de_map::size::MapBounds;
1012
pub(crate) use interaction::HudNodes;
1113
pub(crate) use menu::{GameMenuSet, ToggleGameMenu};
1214
pub(crate) use selection::UpdateSelectionBoxEvent;
1315

1416
use self::{
15-
actionbar::ActionBarPlugin, details::DetailsPlugin, menu::MenuPlugin, minimap::MinimapPlugin,
17+
actionbar::{setup_actionbar, ActionBarPlugin},
18+
details::setup_details,
19+
menu::MenuPlugin,
20+
minimap::{setup_minimap, MinimapPlugin},
1621
selection::SelectionPlugin,
1722
};
1823

@@ -23,9 +28,31 @@ pub(crate) struct HudPlugin;
2328
impl Plugin for HudPlugin {
2429
fn build(&self, app: &mut App) {
2530
app.add_plugin(SelectionPlugin)
26-
.add_plugin(DetailsPlugin)
2731
.add_plugin(ActionBarPlugin)
2832
.add_plugin(MenuPlugin)
29-
.add_plugin(MinimapPlugin);
33+
.add_plugin(MinimapPlugin)
34+
.add_system(setup.in_schedule(OnEnter(GameState::Playing)));
3035
}
3136
}
37+
38+
fn setup(mut commands: Commands, images: ResMut<Assets<Image>>, map_bounds: Res<MapBounds>) {
39+
let details = setup_details(&mut commands);
40+
let actionbar = setup_actionbar(&mut commands);
41+
let minimap = setup_minimap(&mut commands, images, map_bounds);
42+
43+
commands
44+
.spawn(NodeBundle {
45+
style: Style {
46+
position_type: PositionType::Absolute,
47+
position: UiRect::bottom(Val::Px(0.)),
48+
size: Size::new(Val::Percent(100.), Val::Percent(30.)),
49+
align_items: AlignItems::End,
50+
..default()
51+
},
52+
..default()
53+
})
54+
.insert(DespawnOnGameExit)
55+
.add_child(details)
56+
.add_child(actionbar)
57+
.add_child(minimap);
58+
}

0 commit comments

Comments
 (0)