Skip to content

Commit 34c1b88

Browse files
authored
refactor: slot widget code quality improvements (#51)
1 parent 1894a61 commit 34c1b88

File tree

10 files changed

+254
-253
lines changed

10 files changed

+254
-253
lines changed

src/gameplay/hud/hotbar.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,24 +130,24 @@ fn spawn_hotbar(mut commands: Commands) {
130130
DespawnOnExit(Screen::Gameplay),
131131
Node {
132132
position_type: PositionType::Absolute,
133-
bottom: Val::Px(8.0),
134-
width: Val::Auto,
135-
height: Val::Auto,
136-
margin: UiRect::axes(Val::Auto, Val::ZERO),
133+
bottom: px(8.0),
134+
width: auto(),
135+
height: auto(),
136+
margin: auto().horizontal(),
137137
justify_content: JustifyContent::Center,
138138
align_items: AlignItems::Center,
139-
column_gap: Val::Px(8.0),
140-
row_gap: Val::Px(8.0),
139+
column_gap: px(8.0),
140+
row_gap: px(8.0),
141141
..default()
142142
},
143143
Pickable::IGNORE,
144144
Children::spawn(SpawnIter((0..DIGIT_KEYS.len()).map(|i| {
145145
(
146146
Name::new(format!("Hotbar Slot {}", i + 1)),
147147
Node {
148-
width: Val::Px(64.0),
149-
height: Val::Px(64.0),
150-
border: UiRect::all(Val::Px(4.0)),
148+
width: px(64.0),
149+
height: px(64.0),
150+
border: px(4.0).all(),
151151
display: Display::Flex,
152152
justify_content: JustifyContent::Center,
153153
align_items: AlignItems::Center,

src/gameplay/hud/inspect/info.rs

Lines changed: 57 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ use bevy::{prelude::*, ui_widgets::observe};
33
use crate::{
44
assets::indexing::IndexMap,
55
gameplay::{
6-
hud::{
7-
inspect::{InspectedEntity, InspectionMenuState},
8-
item_slot::{AddedToSlot, InSlot, RemovedFromSlot},
9-
},
10-
item::{Item, assets::ItemDef},
11-
recipe::{assets::RecipeDef, select::SelectedRecipe},
6+
hud::inspect::{InspectedEntity, InspectionMenuState},
7+
recipe::{Inputs, Outputs, assets::RecipeDef, select::SelectedRecipe},
8+
},
9+
widgets::{
10+
self,
11+
item::StackIcon,
12+
slot::{AddedToSlot, RemovedFromSlot},
1213
},
13-
widgets::{self, item::item_icon},
1414
};
1515

1616
pub fn plugin(app: &mut App) {
@@ -22,19 +22,18 @@ pub fn plugin(app: &mut App) {
2222

2323
#[derive(Component, Reflect)]
2424
#[reflect(Component)]
25-
pub struct HeldRelic(Handle<ItemDef>);
25+
pub struct HeldRelic(Entity);
2626

2727
#[allow(clippy::too_many_arguments)]
2828
pub fn open_recipe_menu(
2929
mut commands: Commands,
3030
inspected_entity: Res<InspectedEntity>,
31-
selected_recipes: Query<&SelectedRecipe>,
31+
structure_query: Query<(&SelectedRecipe, &Inputs, &Outputs)>,
3232
recipes: Res<Assets<RecipeDef>>,
3333
recipe_index: Res<IndexMap<RecipeDef>>,
3434
held_relics: Query<&HeldRelic>,
35-
asset_server: Res<AssetServer>,
3635
) {
37-
let Ok(selected_recipe) = selected_recipes.get(inspected_entity.0) else {
36+
let Ok((selected_recipe, inputs, outputs)) = structure_query.get(inspected_entity.0) else {
3837
return;
3938
};
4039

@@ -62,11 +61,11 @@ pub fn open_recipe_menu(
6261
.spawn((
6362
ChildOf(container_id),
6463
Node {
65-
width: Val::Percent(70.0),
66-
height: Val::Percent(70.0),
64+
width: percent(70.0),
65+
height: percent(70.0),
6766
display: Display::Flex,
6867
flex_direction: FlexDirection::Column,
69-
padding: UiRect::all(Val::Px(32.0)),
68+
padding: px(32.0).all(),
7069
..default()
7170
},
7271
BackgroundColor(Color::WHITE.with_alpha(0.5)),
@@ -76,7 +75,7 @@ pub fn open_recipe_menu(
7675
commands.spawn((
7776
ChildOf(menu_id),
7877
Node {
79-
width: Val::Percent(100.0),
78+
width: percent(100.0),
8079
display: Display::Flex,
8180
flex_direction: FlexDirection::Row,
8281
justify_content: JustifyContent::SpaceBetween,
@@ -101,8 +100,8 @@ pub fn open_recipe_menu(
101100
.spawn((
102101
ChildOf(menu_id),
103102
Node {
104-
width: Val::Percent(100.0),
105-
height: Val::Percent(100.0),
103+
width: percent(100.0),
104+
height: percent(100.0),
106105
display: Display::Flex,
107106
flex_direction: FlexDirection::Row,
108107
justify_content: JustifyContent::Center,
@@ -122,26 +121,11 @@ pub fn open_recipe_menu(
122121
))
123122
.id();
124123

125-
for (input_id, quantity) in recipe.input.iter() {
126-
commands.spawn((
127-
ChildOf(input_list_id),
128-
widgets::slot(),
129-
children![
130-
item_icon(asset_server.load(format!("manifests/items/{input_id}.item.toml"))),
131-
(
132-
Node {
133-
position_type: PositionType::Absolute,
134-
right: Val::ZERO,
135-
bottom: Val::ZERO,
136-
..default()
137-
},
138-
Pickable::IGNORE,
139-
BackgroundColor(Color::WHITE),
140-
TextColor(Color::BLACK),
141-
Text::new(quantity.to_string()),
142-
)
143-
],
144-
));
124+
for input in inputs.iter() {
125+
let slot = commands
126+
.spawn((ChildOf(input_list_id), widgets::slot::slot_container()))
127+
.id();
128+
commands.spawn(widgets::slot::slotted_stack(slot, input));
145129
}
146130

147131
let middle_column_id = commands
@@ -164,39 +148,42 @@ pub fn open_recipe_menu(
164148
));
165149

166150
let relic_slot_id = commands
167-
.spawn((ChildOf(middle_column_id), widgets::slot()))
151+
.spawn((
152+
ChildOf(middle_column_id),
153+
widgets::slot::slot_container(),
154+
observe(
155+
|added_to_slot: On<AddedToSlot>,
156+
inspected_entity: Res<InspectedEntity>,
157+
mut commands: Commands,
158+
slot_occupant_query: Query<&Children>,
159+
stack_icon_query: Query<&StackIcon>| {
160+
let Ok(children) = slot_occupant_query.get(added_to_slot.item) else {
161+
return;
162+
};
163+
164+
let Ok(stack_icon) = stack_icon_query.get(*children.first().unwrap()) else {
165+
return;
166+
};
167+
168+
commands
169+
.entity(inspected_entity.0)
170+
.insert(HeldRelic(stack_icon.0));
171+
},
172+
),
173+
observe(
174+
|_removed_from_slot: On<RemovedFromSlot>,
175+
inspected_entity: Res<InspectedEntity>,
176+
mut commands: Commands| {
177+
commands.entity(inspected_entity.0).remove::<HeldRelic>();
178+
},
179+
),
180+
))
168181
.id();
169182

170183
if let Ok(relic) = held_relics.get(inspected_entity.0) {
171-
commands.spawn((
172-
InSlot(relic_slot_id),
173-
ChildOf(relic_slot_id),
174-
item_icon(relic.0.clone()),
175-
));
184+
commands.spawn(widgets::slot::slotted_stack(relic_slot_id, relic.0));
176185
}
177186

178-
commands
179-
.entity(relic_slot_id)
180-
.observe(
181-
|added_to_slot: On<AddedToSlot>,
182-
inspected_entity: Res<InspectedEntity>,
183-
mut commands: Commands,
184-
items: Query<&Item>| {
185-
if let Ok(item) = items.get(added_to_slot.entity) {
186-
commands
187-
.entity(inspected_entity.0)
188-
.insert(HeldRelic(item.0.clone()));
189-
}
190-
},
191-
)
192-
.observe(
193-
|_removed_from_slot: On<RemovedFromSlot>,
194-
inspected_entity: Res<InspectedEntity>,
195-
mut commands: Commands| {
196-
commands.entity(inspected_entity.0).remove::<HeldRelic>();
197-
},
198-
);
199-
200187
let output_list_id = commands
201188
.spawn((
202189
ChildOf(recipe_view_id),
@@ -207,26 +194,11 @@ pub fn open_recipe_menu(
207194
))
208195
.id();
209196

210-
for (output_id, quantity) in recipe.output.iter() {
211-
commands.spawn((
212-
ChildOf(output_list_id),
213-
widgets::slot(),
214-
children![
215-
item_icon(asset_server.load(format!("manifests/items/{output_id}.item.toml"))),
216-
(
217-
Node {
218-
position_type: PositionType::Absolute,
219-
right: Val::ZERO,
220-
bottom: Val::ZERO,
221-
..default()
222-
},
223-
Pickable::IGNORE,
224-
BackgroundColor(Color::WHITE),
225-
TextColor(Color::BLACK),
226-
Text::new(quantity.to_string()),
227-
)
228-
],
229-
));
197+
for output in outputs.iter() {
198+
let slot = commands
199+
.spawn((ChildOf(output_list_id), widgets::slot::slot_container()))
200+
.id();
201+
commands.spawn(widgets::slot::slotted_stack(slot, output));
230202
}
231203
}
232204

src/gameplay/hud/inspect/select.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,18 @@ pub fn recipe_select_menu(mut commands: Commands, recipes: Res<Assets<RecipeDef>
3131
widgets::container(),
3232
Children::spawn_one((
3333
Node {
34-
width: Val::Percent(70.0),
35-
height: Val::Percent(70.0),
34+
width: percent(70.0),
35+
height: percent(70.0),
3636
display: Display::Flex,
3737
flex_direction: FlexDirection::Column,
38-
padding: UiRect::all(Val::Px(32.0)),
38+
padding: px(32.0).all(),
3939
..default()
4040
},
4141
BackgroundColor(Color::WHITE.with_alpha(0.5)),
4242
Children::spawn((
4343
Spawn((
4444
Node {
45-
width: Val::Percent(100.0),
45+
width: percent(100.0),
4646
display: Display::Flex,
4747
flex_direction: FlexDirection::Row,
4848
justify_content: JustifyContent::SpaceBetween,
@@ -71,7 +71,7 @@ pub fn recipe_select_menu(mut commands: Commands, recipes: Res<Assets<RecipeDef>
7171
|(recipe_id, recipe_name)| {
7272
(
7373
Node {
74-
padding: UiRect::all(Val::Px(32.0)),
74+
padding: px(32.0).all(),
7575
..default()
7676
},
7777
BackgroundColor(Color::BLACK.with_alpha(0.5)),

src/gameplay/hud/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use bevy::prelude::*;
22

33
pub mod hotbar;
44
pub mod inspect;
5-
pub mod item_slot;
65

76
pub fn plugin(app: &mut App) {
8-
app.add_plugins((hotbar::plugin, inspect::plugin, item_slot::plugin));
7+
app.add_plugins((hotbar::plugin, inspect::plugin));
98
}

src/gameplay/item/compendium.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use bevy::{input::common_conditions::input_just_pressed, prelude::*};
22

3-
use crate::{gameplay::item::assets::ItemDef, widgets::item::item_icon};
3+
use crate::gameplay::item::assets::ItemDef;
44

55
pub fn plugin(app: &mut App) {
66
app.init_state::<CompendiumState>();
@@ -30,11 +30,7 @@ fn toggle_compendium_state(
3030
}
3131
}
3232

33-
fn spawn_item_compendium(
34-
mut commands: Commands,
35-
items: Res<Assets<ItemDef>>,
36-
asset_server: Res<AssetServer>,
37-
) {
33+
fn spawn_item_compendium(mut commands: Commands, items: Res<Assets<ItemDef>>) {
3834
let mut items_sorted: Vec<_> = items.iter().collect();
3935
items_sorted.sort_by(|a, b| a.1.name.cmp(&b.1.name));
4036

@@ -43,8 +39,8 @@ fn spawn_item_compendium(
4339
Name::new("Item Compendium"),
4440
DespawnOnExit(CompendiumState::Item),
4541
Node {
46-
width: Val::Percent(100.0),
47-
height: Val::Percent(100.0),
42+
width: percent(100.0),
43+
height: percent(100.0),
4844
justify_content: JustifyContent::Center,
4945
align_items: AlignItems::Center,
5046
..default()
@@ -55,19 +51,19 @@ fn spawn_item_compendium(
5551
let item_list = commands
5652
.spawn((
5753
Node {
58-
padding: UiRect::all(Val::Px(16.0)),
54+
padding: px(16.0).all(),
5955
display: Display::Grid,
6056
grid_template_columns: vec![RepeatedGridTrack::auto(2)],
61-
column_gap: Val::Px(8.0),
62-
row_gap: Val::Px(8.0),
57+
column_gap: px(8.0),
58+
row_gap: px(8.0),
6359
..default()
6460
},
6561
BackgroundColor(Color::BLACK.with_alpha(0.5)),
6662
ChildOf(container),
6763
))
6864
.id();
6965

70-
for (item_asset_id, item_def) in items_sorted.iter() {
66+
for (_, item_def) in items_sorted.iter() {
7167
let item_box = commands
7268
.spawn((
7369
Node {
@@ -79,8 +75,8 @@ fn spawn_item_compendium(
7975
grid_template_rows: vec![RepeatedGridTrack::px(2, 32.0)],
8076
justify_items: JustifyItems::Start,
8177
align_items: AlignItems::Center,
82-
column_gap: Val::Px(16.0),
83-
padding: UiRect::all(Val::Px(4.0)),
78+
column_gap: px(16.0),
79+
padding: px(4.0).all(),
8480
..default()
8581
},
8682
ChildOf(item_list),
@@ -93,13 +89,13 @@ fn spawn_item_compendium(
9389
justify_self: JustifySelf::Center,
9490
grid_column: GridPlacement::span(1),
9591
grid_row: GridPlacement::span(2),
96-
width: Val::Px(56.0),
97-
height: Val::Px(56.0),
92+
width: px(56.0),
93+
height: px(56.0),
9894
..default()
9995
},
100-
children![item_icon(
101-
asset_server.get_id_handle(*item_asset_id).unwrap()
102-
)],
96+
// children![stack_icon(
97+
// asset_server.get_id_handle(*item_asset_id).unwrap()
98+
// )],
10399
ChildOf(item_box),
104100
));
105101

src/screens/loading.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ fn spawn_loading_screen(mut commands: Commands) {
1818
Name::new("Container"),
1919
DespawnOnExit(Screen::Loading),
2020
Node {
21-
width: Val::Percent(100.0),
22-
height: Val::Percent(100.0),
21+
width: percent(100.0),
22+
height: percent(100.0),
2323
justify_content: JustifyContent::Center,
2424
align_items: AlignItems::Center,
2525
..default()

0 commit comments

Comments
 (0)