@@ -5,7 +5,7 @@ use std::{f32::consts::TAU, iter};
5
5
use bevy_app:: FixedUpdateScheduleIsCurrentlyRunning ;
6
6
use bevy_ecs:: {
7
7
component:: Tick ,
8
- system:: { Resource , SystemBuffer , SystemMeta , SystemParam } ,
8
+ system:: { Resource , SystemMeta , SystemParam } ,
9
9
world:: { unsafe_world_cell:: UnsafeWorldCell , World } ,
10
10
} ;
11
11
use bevy_math:: { Mat2 , Quat , Vec2 , Vec3 } ;
@@ -35,79 +35,63 @@ pub(crate) struct GizmoStorage {
35
35
36
36
/// A [`SystemParam`](bevy_ecs::system::SystemParam) for drawing gizmos.
37
37
pub struct Gizmos < ' s > {
38
- buffer : & ' s mut GizmoBuffer ,
39
- }
40
-
41
- #[ derive( Default ) ]
42
- struct GizmoBuffer {
43
- /// Which fixed update tick this belongs to, `None` if this isn't from a fixed update.
44
- fixed_time_update : Option < u64 > ,
45
- list_positions : Vec < PositionItem > ,
46
- list_colors : Vec < ColorItem > ,
47
- strip_positions : Vec < PositionItem > ,
48
- strip_colors : Vec < ColorItem > ,
49
- }
50
-
51
- impl SystemBuffer for GizmoBuffer {
52
- fn apply ( & mut self , _system_meta : & SystemMeta , world : & mut World ) {
53
- let mut storages = world. resource_mut :: < GizmoStorages > ( ) ;
54
-
55
- let storage = if let Some ( tick) = self . fixed_time_update {
56
- // If a new fixed update has begun, clear gizmos from previous fixed update
57
- if storages. fixed_update_tick < tick {
58
- storages. fixed_update_tick = tick;
59
- storages. fixed_update . list_positions . clear ( ) ;
60
- storages. fixed_update . list_colors . clear ( ) ;
61
- storages. fixed_update . strip_positions . clear ( ) ;
62
- storages. fixed_update . strip_colors . clear ( ) ;
63
- }
64
- & mut storages. fixed_update
65
- } else {
66
- & mut storages. frame
67
- } ;
68
-
69
- storage. list_positions . append ( & mut self . list_positions ) ;
70
- storage. list_colors . append ( & mut self . list_colors ) ;
71
- storage. strip_positions . append ( & mut self . strip_positions ) ;
72
- storage. strip_colors . append ( & mut self . strip_colors ) ;
73
- }
38
+ buffer : & ' s mut <Self as SystemParam >:: State ,
74
39
}
75
40
76
41
// Wrap to keep GizmoBuffer hidden
77
42
const _: ( ) = {
78
- pub struct Wrap ( GizmoBuffer ) ;
43
+ #[ derive( Default ) ]
44
+ pub struct GizmoBuffer {
45
+ /// Which fixed update tick this belongs to, `None` if this isn't from a fixed update.
46
+ fixed_time_update : Option < u64 > ,
47
+ list_positions : Vec < PositionItem > ,
48
+ list_colors : Vec < ColorItem > ,
49
+ strip_positions : Vec < PositionItem > ,
50
+ strip_colors : Vec < ColorItem > ,
51
+ }
79
52
80
53
// SAFETY: Only local state is accessed.
81
54
unsafe impl SystemParam for Gizmos < ' _ > {
82
- type State = Wrap ;
55
+ type State = GizmoBuffer ;
83
56
type Item < ' w , ' s > = Gizmos < ' s > ;
84
57
85
- fn init_state ( world : & mut World , _system_meta : & mut SystemMeta ) -> Self :: State {
86
- let fixed_time_update = world
87
- . get_resource :: < FixedUpdateScheduleIsCurrentlyRunning > ( )
88
- . map ( |current| current. update ) ;
89
- Wrap ( GizmoBuffer {
90
- fixed_time_update,
91
- list_positions : default ( ) ,
92
- list_colors : default ( ) ,
93
- strip_positions : default ( ) ,
94
- strip_colors : default ( ) ,
95
- } )
58
+ fn init_state ( _: & mut World , _system_meta : & mut SystemMeta ) -> Self :: State {
59
+ default ( )
96
60
}
97
61
98
- fn apply ( state : & mut Self :: State , system_meta : & SystemMeta , world : & mut World ) {
99
- state. 0 . apply ( system_meta, world) ;
62
+ fn apply ( state : & mut Self :: State , _system_meta : & SystemMeta , world : & mut World ) {
63
+ let mut storages = world. resource_mut :: < GizmoStorages > ( ) ;
64
+
65
+ let storage = if let Some ( tick) = state. fixed_time_update {
66
+ // If a new fixed update has begun, clear gizmos from previous fixed update
67
+ if storages. fixed_update_tick < tick {
68
+ storages. fixed_update_tick = tick;
69
+ storages. fixed_update . list_positions . clear ( ) ;
70
+ storages. fixed_update . list_colors . clear ( ) ;
71
+ storages. fixed_update . strip_positions . clear ( ) ;
72
+ storages. fixed_update . strip_colors . clear ( ) ;
73
+ }
74
+ & mut storages. fixed_update
75
+ } else {
76
+ & mut storages. frame
77
+ } ;
78
+
79
+ storage. list_positions . append ( & mut state. list_positions ) ;
80
+ storage. list_colors . append ( & mut state. list_colors ) ;
81
+ storage. strip_positions . append ( & mut state. strip_positions ) ;
82
+ storage. strip_colors . append ( & mut state. strip_colors ) ;
100
83
}
101
84
102
85
unsafe fn get_param < ' w , ' s > (
103
86
state : & ' s mut Self :: State ,
104
87
_system_meta : & SystemMeta ,
105
- _world : UnsafeWorldCell < ' w > ,
88
+ world : UnsafeWorldCell < ' w > ,
106
89
_change_tick : Tick ,
107
90
) -> Self :: Item < ' w , ' s > {
108
- Gizmos {
109
- buffer : & mut state. 0 ,
110
- }
91
+ state. fixed_time_update = world
92
+ . get_resource :: < FixedUpdateScheduleIsCurrentlyRunning > ( )
93
+ . map ( |current| current. update ) ;
94
+ Gizmos { buffer : state }
111
95
}
112
96
}
113
97
} ;
@@ -231,11 +215,8 @@ impl<'s> Gizmos<'s> {
231
215
pub fn linestrip_gradient ( & mut self , points : impl IntoIterator < Item = ( Vec3 , Color ) > ) {
232
216
let points = points. into_iter ( ) ;
233
217
234
- let GizmoBuffer {
235
- strip_positions,
236
- strip_colors,
237
- ..
238
- } = & mut * self . buffer ;
218
+ let strip_positions = & mut self . buffer . strip_positions ;
219
+ let strip_colors = & mut self . buffer . strip_colors ;
239
220
240
221
let ( min, _) = points. size_hint ( ) ;
241
222
strip_positions. reserve ( min) ;
0 commit comments