1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use bevy::{prelude::*, reflect::TypeUuid};
use serde::{Deserialize, Serialize};
pub(crate) fn add_components(app: &mut AppBuilder) {
app.register_type::<Camera>()
.register_type::<Color>()
.register_type::<CameraSize>()
.register_type::<Sprite>()
.register_type::<SpriteSheet>()
.register_type::<Visible>();
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Reflect)]
#[reflect_value(Serialize, Deserialize, PartialEq, Component)]
pub struct Color {
pub r: f32,
pub g: f32,
pub b: f32,
pub a: f32,
}
impl Color {
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
pub fn from_rgba8(r: u8, g: u8, b: u8, a: u8) -> Self {
Self {
r: r as f32 / 255.0,
g: g as f32 / 255.0,
b: b as f32 / 255.0,
a: a as f32 / 255.0,
}
}
}
impl Default for Color {
fn default() -> Self {
Self {
r: 0.,
g: 0.,
b: 0.,
a: 1.,
}
}
}
#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct Camera {
pub size: CameraSize,
pub centered: bool,
pub background_color: Color,
pub letterbox_color: Color,
pub pixel_aspect_ratio: f32,
pub custom_shader: Option<String>,
}
impl Default for Camera {
fn default() -> Self {
Self {
size: Default::default(),
centered: true,
background_color: Color::default(),
letterbox_color: Color::default(),
pixel_aspect_ratio: 1.0,
custom_shader: None,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Reflect)]
#[reflect_value(PartialEq, Serialize, Deserialize)]
pub enum CameraSize {
FixedHeight(u32),
FixedWidth(u32),
LetterBoxed { width: u32, height: u32 },
}
impl Default for CameraSize {
fn default() -> Self {
Self::FixedHeight(200)
}
}
#[derive(Debug, Clone)]
pub struct CameraTargetSizes {
pub low: UVec2,
pub high: UVec2,
}
impl Camera {
pub fn get_target_sizes(&self, window: &bevy::window::Window) -> CameraTargetSizes {
let window_width = window.width();
let window_height = window.height();
let aspect_ratio = window_width / window_height;
let low_res = match self.size {
CameraSize::FixedHeight(height) => UVec2::new(
{
let x = (aspect_ratio * height as f32 / self.pixel_aspect_ratio).floor() as u32;
if x % 2 != 0 {
x - 1
} else {
x
}
},
height,
),
CameraSize::FixedWidth(width) => UVec2::new(width, {
let y = (width as f32 / aspect_ratio * self.pixel_aspect_ratio).floor() as u32;
if y % 2 != 0 {
y - 1
} else {
y
}
}),
CameraSize::LetterBoxed { width, height } => UVec2::new(width, height),
};
let multiple = (window_width as f32 / low_res.x as f32).ceil() as u32;
let high_res = low_res * multiple;
CameraTargetSizes {
low: low_res,
high: high_res,
}
}
}
#[derive(Debug, Clone, Reflect)]
#[reflect(Component)]
pub struct Sprite {
pub centered: bool,
pub flip_x: bool,
pub flip_y: bool,
pub offset: Vec2,
pub pixel_perfect: bool,
}
impl Default for Sprite {
fn default() -> Self {
Self {
centered: true,
flip_x: false,
flip_y: false,
offset: Vec2::default(),
pixel_perfect: true,
}
}
}
#[derive(Debug, Clone, TypeUuid, Reflect)]
#[uuid = "64746631-1afe-4ca6-8398-7c0df62f7813"]
#[reflect(Component)]
pub struct SpriteSheet {
pub grid_size: UVec2,
pub tile_index: u32,
}
impl Default for SpriteSheet {
fn default() -> Self {
Self {
grid_size: UVec2::splat(16),
tile_index: 0,
}
}
}
#[derive(Debug, Clone, Copy, Reflect)]
#[reflect(Component)]
pub struct Visible(pub bool);
bevy_retrograde_macros::impl_deref!(Visible, bool);
impl Default for Visible {
fn default() -> Self {
Visible(true)
}
}