11use core:: fmt:: Debug ;
2+ use core:: ops:: { Deref , DerefMut } ;
23
34use crate :: { primitives:: Frustum , view:: VisibilitySystems } ;
45use bevy_app:: { App , Plugin , PostStartup , PostUpdate } ;
56use bevy_asset:: AssetEventSystems ;
6- use bevy_derive:: { Deref , DerefMut } ;
77use bevy_ecs:: prelude:: * ;
88use bevy_math:: { ops, AspectRatio , Mat4 , Rect , Vec2 , Vec3A , Vec4 } ;
99use bevy_reflect:: { std_traits:: ReflectDefault , Reflect , ReflectDeserialize , ReflectSerialize } ;
@@ -131,11 +131,10 @@ mod sealed {
131131/// custom projection.
132132///
133133/// The contained dynamic object can be downcast into a static type using [`CustomProjection::get`].
134- #[ derive( Component , Debug , Reflect , Deref , DerefMut ) ]
134+ #[ derive( Debug , Reflect ) ]
135135#[ reflect( Default , Clone ) ]
136136pub struct CustomProjection {
137137 #[ reflect( ignore) ]
138- #[ deref]
139138 dyn_projection : Box < dyn sealed:: DynCameraProjection > ,
140139}
141140
@@ -204,6 +203,20 @@ impl CustomProjection {
204203 }
205204}
206205
206+ impl Deref for CustomProjection {
207+ type Target = dyn CameraProjection ;
208+
209+ fn deref ( & self ) -> & Self :: Target {
210+ self . dyn_projection . as_ref ( )
211+ }
212+ }
213+
214+ impl DerefMut for CustomProjection {
215+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
216+ self . dyn_projection . as_mut ( )
217+ }
218+ }
219+
207220/// Component that defines how to compute a [`Camera`]'s projection matrix.
208221///
209222/// Common projections, like perspective and orthographic, are provided out of the box to handle the
@@ -240,7 +253,7 @@ impl Projection {
240253 // that, say, the `Debug` implementation is missing. Wrapping these traits behind a super
241254 // trait or some other indirection will make the errors harder to understand.
242255 //
243- // For example, we don't use the `DynCameraProjection`` trait bound, because it is not the
256+ // For example, we don't use the `DynCameraProjection` trait bound, because it is not the
244257 // trait the user should be implementing - they only need to worry about implementing
245258 // `CameraProjection`.
246259 P : CameraProjection + Debug + Send + Sync + Clone + ' static ,
@@ -251,44 +264,24 @@ impl Projection {
251264 }
252265}
253266
254- impl CameraProjection for Projection {
255- fn get_clip_from_view ( & self ) -> Mat4 {
256- match self {
257- Projection :: Perspective ( projection) => projection. get_clip_from_view ( ) ,
258- Projection :: Orthographic ( projection) => projection. get_clip_from_view ( ) ,
259- Projection :: Custom ( projection) => projection. get_clip_from_view ( ) ,
260- }
261- }
262-
263- fn get_clip_from_view_for_sub ( & self , sub_view : & super :: SubCameraView ) -> Mat4 {
264- match self {
265- Projection :: Perspective ( projection) => projection. get_clip_from_view_for_sub ( sub_view) ,
266- Projection :: Orthographic ( projection) => projection. get_clip_from_view_for_sub ( sub_view) ,
267- Projection :: Custom ( projection) => projection. get_clip_from_view_for_sub ( sub_view) ,
268- }
269- }
270-
271- fn update ( & mut self , width : f32 , height : f32 ) {
272- match self {
273- Projection :: Perspective ( projection) => projection. update ( width, height) ,
274- Projection :: Orthographic ( projection) => projection. update ( width, height) ,
275- Projection :: Custom ( projection) => projection. update ( width, height) ,
276- }
277- }
267+ impl Deref for Projection {
268+ type Target = dyn CameraProjection ;
278269
279- fn far ( & self ) -> f32 {
270+ fn deref ( & self ) -> & Self :: Target {
280271 match self {
281- Projection :: Perspective ( projection) => projection. far ( ) ,
282- Projection :: Orthographic ( projection) => projection. far ( ) ,
283- Projection :: Custom ( projection) => projection. far ( ) ,
272+ Projection :: Perspective ( projection) => projection,
273+ Projection :: Orthographic ( projection) => projection,
274+ Projection :: Custom ( projection) => projection. deref ( ) ,
284275 }
285276 }
277+ }
286278
287- fn get_frustum_corners ( & self , z_near : f32 , z_far : f32 ) -> [ Vec3A ; 8 ] {
279+ impl DerefMut for Projection {
280+ fn deref_mut ( & mut self ) -> & mut Self :: Target {
288281 match self {
289- Projection :: Perspective ( projection) => projection. get_frustum_corners ( z_near , z_far ) ,
290- Projection :: Orthographic ( projection) => projection. get_frustum_corners ( z_near , z_far ) ,
291- Projection :: Custom ( projection) => projection. get_frustum_corners ( z_near , z_far ) ,
282+ Projection :: Perspective ( projection) => projection,
283+ Projection :: Orthographic ( projection) => projection,
284+ Projection :: Custom ( projection) => projection. deref_mut ( ) ,
292285 }
293286 }
294287}
0 commit comments