1
- use bevy:: prelude:: * ;
1
+ use bevy:: { input :: system :: exit_on_esc_system , prelude:: * } ;
2
2
3
- const MAX_WIDTH : f32 = 400. ;
4
- const MAX_HEIGHT : f32 = 400. ;
3
+ const MAX_WIDTH : u16 = 401 ;
4
+ const MAX_HEIGHT : u16 = 401 ;
5
+
6
+ struct Dimensions {
7
+ width : u16 ,
8
+ height : u16 ,
9
+ }
5
10
6
11
fn main ( ) {
7
12
App :: new ( )
8
13
. insert_resource ( WindowDescriptor {
9
- width : MAX_WIDTH ,
10
- height : MAX_HEIGHT ,
14
+ width : MAX_WIDTH . try_into ( ) . unwrap ( ) ,
15
+ height : MAX_HEIGHT . try_into ( ) . unwrap ( ) ,
11
16
scale_factor_override : Some ( 1. ) ,
12
17
..Default :: default ( )
13
18
} )
19
+ . insert_resource ( Dimensions {
20
+ width : MAX_WIDTH ,
21
+ height : MAX_HEIGHT ,
22
+ } )
14
23
. add_plugins ( DefaultPlugins )
15
24
. insert_resource ( Phase :: ContractingY )
16
25
. add_system ( change_window_size)
26
+ . add_system ( sync_dimensions)
27
+ . add_system ( exit_on_esc_system)
17
28
. add_startup_system ( setup)
18
29
. run ( ) ;
19
30
}
@@ -28,7 +39,7 @@ enum Phase {
28
39
use Phase :: * ;
29
40
30
41
fn change_window_size (
31
- mut windows : ResMut < Windows > ,
42
+ mut windows : ResMut < Dimensions > ,
32
43
mut phase : ResMut < Phase > ,
33
44
mut first_complete : Local < bool > ,
34
45
) {
@@ -38,37 +49,49 @@ fn change_window_size(
38
49
* first_complete = true ;
39
50
return ;
40
51
}
41
- let primary = windows. get_primary_mut ( ) . unwrap ( ) ;
42
- let height = primary. height ( ) ;
43
- let width = primary. width ( ) ;
52
+ let height = windows. height ;
53
+ let width = windows. width ;
44
54
match * phase {
45
55
Phase :: ContractingY => {
46
- if height <= 0.5 {
56
+ if windows . height <= 1 {
47
57
* phase = ContractingX ;
58
+ } else {
59
+ windows. height -= 4 ;
48
60
}
49
- primary. set_resolution ( width, ( height - 4. ) . max ( 0.0 ) )
50
61
}
51
62
Phase :: ContractingX => {
52
- if width <= 0.5 {
63
+ if width <= 1 {
53
64
* phase = ExpandingY ;
65
+ } else {
66
+ windows. width -= 4 ;
54
67
}
55
- primary. set_resolution ( ( width - 4. ) . max ( 0.0 ) , height)
56
68
}
57
69
Phase :: ExpandingY => {
58
70
if height >= MAX_HEIGHT {
59
71
* phase = ExpandingX ;
72
+ } else {
73
+ windows. height += 4 ;
60
74
}
61
- primary. set_resolution ( width, height + 4. )
62
75
}
63
76
Phase :: ExpandingX => {
64
77
if width >= MAX_WIDTH {
65
78
* phase = ContractingY ;
79
+ } else {
80
+ windows. width += 4 ;
66
81
}
67
- primary. set_resolution ( width + 4. , height)
68
82
}
69
83
}
70
84
}
71
85
86
+ fn sync_dimensions ( dim : Res < Dimensions > , mut windows : ResMut < Windows > ) {
87
+ if dim. is_changed ( ) {
88
+ windows. get_primary_mut ( ) . unwrap ( ) . set_resolution (
89
+ dim. width . try_into ( ) . unwrap ( ) ,
90
+ dim. height . try_into ( ) . unwrap ( ) ,
91
+ ) ;
92
+ }
93
+ }
94
+
72
95
/// A simple 3d scene, taken from the `3d_scene` example
73
96
fn setup (
74
97
mut commands : Commands ,
0 commit comments