@@ -173,40 +173,143 @@ pub struct ProgressBar {
173173 progress : Entity ,
174174}
175175
176- #[ derive( Default ) ]
176+ #[ derive( Default , Clone , Copy ) ]
177+ pub enum ProgressBarPosition {
178+ #[ default]
179+ Left ,
180+ Center ,
181+ Right ,
182+ }
183+
184+ #[ derive( Default , Clone ) ]
177185pub struct ProgressBarPrefab {
178186 pub starting_percentage : f32 ,
187+ pub size : Vec2 ,
188+ pub border : f32 ,
189+ pub color : Color ,
190+ pub border_color : Color ,
191+ pub background_color : Color ,
192+ pub position : ProgressBarPosition ,
179193 pub transform : Transform ,
180194}
181195
182196impl Prefab for ProgressBarPrefab {
183197 fn construct ( & self , entity : Entity , commands : & mut Commands ) {
184- let mesh = commands
185- . spawn_bundle ( PbrBundle {
186- mesh : square_mesh ( ) ,
187- material : white_standard_material ( ) ,
188- ..default ( )
189- } )
190- . insert ( NotShadowCaster )
191- . insert ( NotShadowReceiver )
192- . id ( ) ;
193-
194- let progress = commands
195- . spawn_bundle ( SpatialBundle :: default ( ) )
196- . add_child ( mesh)
197- . id ( ) ;
198-
199- commands
200- . entity ( entity)
201- . insert_bundle ( SpatialBundle {
202- transform : self . transform ,
203- ..default ( )
204- } )
205- . insert ( ProgressBar {
206- percentage : self . starting_percentage ,
207- progress,
208- } )
209- . add_child ( progress) ;
198+ let bar = self . clone ( ) ;
199+ commands. add ( move |world : & mut World | {
200+ let ( progress_color, background_color, border_color) =
201+ world. resource_scope ( |_, mut materials : Mut < Assets < StandardMaterial > > | {
202+ (
203+ materials. add ( StandardMaterial {
204+ base_color : bar. color ,
205+ unlit : true ,
206+ alpha_mode : if bar. color . a ( ) < 1.0 {
207+ AlphaMode :: Blend
208+ } else {
209+ default ( )
210+ } ,
211+ ..default ( )
212+ } ) ,
213+ materials. add ( StandardMaterial {
214+ base_color : bar. background_color ,
215+ unlit : true ,
216+ alpha_mode : if bar. background_color . a ( ) < 1.0 {
217+ AlphaMode :: Blend
218+ } else {
219+ default ( )
220+ } ,
221+ ..default ( )
222+ } ) ,
223+ materials. add ( StandardMaterial {
224+ base_color : bar. border_color ,
225+ unlit : true ,
226+ alpha_mode : if bar. border_color . a ( ) < 1.0 {
227+ AlphaMode :: Blend
228+ } else {
229+ default ( )
230+ } ,
231+ ..default ( )
232+ } ) ,
233+ )
234+ } ) ;
235+
236+ let mesh = world
237+ . spawn ( )
238+ . insert_bundle ( PbrBundle {
239+ mesh : square_mesh ( ) ,
240+ material : progress_color,
241+ transform : Transform :: from_translation ( match bar. position {
242+ ProgressBarPosition :: Left => Vec3 :: X / 2.0 ,
243+ ProgressBarPosition :: Center => default ( ) ,
244+ ProgressBarPosition :: Right => -Vec3 :: X / 2.0 ,
245+ } ) ,
246+ ..default ( )
247+ } )
248+ . insert ( NotShadowCaster )
249+ . insert ( NotShadowReceiver )
250+ . id ( ) ;
251+
252+ let progress = world
253+ . spawn ( )
254+ . insert_bundle ( SpatialBundle {
255+ transform : Transform :: from_translation ( match bar. position {
256+ ProgressBarPosition :: Left => -Vec3 :: X / 2.0 ,
257+ ProgressBarPosition :: Center => default ( ) ,
258+ ProgressBarPosition :: Right => Vec3 :: X / 2.0 ,
259+ } ) ,
260+ ..default ( )
261+ } )
262+ . push_children ( & [ mesh] )
263+ . id ( ) ;
264+
265+ let background = world
266+ . spawn ( )
267+ . insert_bundle ( PbrBundle {
268+ mesh : square_mesh ( ) ,
269+ material : background_color,
270+ transform : Transform :: from_translation ( -Vec3 :: Z * 0.001 ) ,
271+ ..default ( )
272+ } )
273+ . insert ( NotShadowCaster )
274+ . insert ( NotShadowReceiver )
275+ . id ( ) ;
276+
277+ let inner = world
278+ . spawn ( )
279+ . insert_bundle ( SpatialBundle {
280+ transform : Transform :: from_scale (
281+ ( ( bar. size - bar. border ) . max ( Vec2 :: ZERO ) ) . extend ( 1.0 ) ,
282+ ) ,
283+ ..default ( )
284+ } )
285+ . push_children ( & [ progress, background] )
286+ . id ( ) ;
287+
288+ let border = world
289+ . spawn ( )
290+ . insert_bundle ( PbrBundle {
291+ mesh : square_mesh ( ) ,
292+ material : border_color,
293+ transform : Transform :: from_scale ( bar. size . extend ( 1.0 ) )
294+ . with_translation ( -Vec3 :: Z * 0.002 ) ,
295+ ..default ( )
296+ } )
297+ . insert ( NotShadowCaster )
298+ . insert ( NotShadowReceiver )
299+ . id ( ) ;
300+
301+ world
302+ . entity_mut ( entity)
303+ . insert_bundle ( SpatialBundle {
304+ transform : bar. transform ,
305+ ..default ( )
306+ } )
307+ . insert ( ProgressBar {
308+ percentage : bar. starting_percentage ,
309+ progress,
310+ } )
311+ . push_children ( & [ inner, border] ) ;
312+ } ) ;
210313 }
211314}
212315
@@ -340,7 +443,7 @@ fn track_world_hover(
340443 . iter ( )
341444 . filter ( |( _, _, entities) | entities. entities . contains ( & check_visibility_of) )
342445 . filter_map ( |( entity, cursor, _) | cursor. position . map ( |x| ( entity, x) ) )
343- . filter ( |( entity , position) | {
446+ . filter ( |( _ , position) | {
344447 let matrix = transform. compute_matrix ( ) . inverse ( ) ;
345448 let position = matrix. transform_point3 ( position. extend ( 0.0 ) ) . truncate ( ) ;
346449
0 commit comments