@@ -3,14 +3,14 @@ use bevy::{prelude::*, ui_widgets::observe};
33use 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
1616pub 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) ]
2828pub 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
0 commit comments