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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
//! [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
//! [![Crates.io](https://img.shields.io/crates/v/bevy_tiled_camera)](https://crates.io/crates/bevy_tiled_camera)
//! [![docs](https://docs.rs/bevy_tiled_camera/badge.svg)](https://docs.rs/bevy_tiled_camera/)
//!
//! # `Bevy Tiled Camera`
//! A camera for properly displaying low resolution pixel perfect 2D games in
//! bevy. It works by adjusting the viewport to match a target resolution, which
//! is defined by a tile count and the number of pixels per tile.
//!
//! ![](images/demo.gif)
//!
//! ## Example
//! ```rust no_run
//! use bevy_tiled_camera::*;
//! use bevy::prelude::*;
//!
//! fn setup(mut commands:Commands) {
//!   // Sets up a camera to display 80 x 35 tiles.
//!   // Defaults to 8 pixels per tile.
//!   let camera_bundle = TiledCameraBundle::unit_cam([80,35]);
//!
//!   commands.spawn(camera_bundle);
//! }
//!
//! fn main() {
//!     App::new()
//!     .add_plugins((DefaultPlugins, TiledCameraPlugin))
//!     .add_systems(Startup, setup)
//!     .run();
//! }
//! ```
//! # World Space
//! Your world space defines how transforms and scaling is treated in your game.
//! You can choose between [WorldSpace::Units] or [WorldSpace::Pixels].
//! The camera supports either, and it's up to you to decide which you prefer.
//!
//! ## Versions
//! | bevy | bevy_tiled_camera |
//! | --- | --- |
//! | 0.13 | 0.9.0 |
//! | 0.12 | 0.8.0 |
//! | 0.11 | 0.7.0 |
//! | 0.10 | 0.6.0 |
//! | 0.9  | 0.5.0 |
//! | 0.8  | 0.4.0 |
//! | 0.6  | 0.3.0 |
//! | 0.5  | 0.2.4 |
//! | 0.5  | 0.2.3 |
//!
//! ## Blurry sprites
//! By default bevy will create all new images with linear image sampling. This
//! is good for smaller, high resolution images but causes severe blurriness
//! with low resolution images. To fix it you can manually set the image
//! sampler to nearest when creating your images, or change the default to
//! always spawn new images with nearest sampling:
//!
//! ```rust no_run
//! use bevy::prelude::*;
//! use bevy_tiled_camera::*;
//!
//!
//! App::new()
//! // Must be inserted during app initialization, before rendering plugins
//! .add_plugins((
//!     DefaultPlugins.set(ImagePlugin::default_nearest()),
//!     TiledCameraPlugin,
//! ))
//! .run();
//!
//! ```
use bevy::{
    ecs::prelude::*,
    math::{IVec2, Mat4, UVec2, Vec2, Vec3},
    prelude::{
        default, App, Camera, Camera2dBundle, Color, GlobalTransform, OrthographicProjection,
        Plugin, Update,
    },
    render::camera::{ClearColorConfig, ScalingMode, Viewport},
    window::{PrimaryWindow, Window, WindowResized},
};
use sark_grids::{
    point::{Point2d, Size2d},
    world_grid::WorldGrid,
    *,
};

pub use sark_grids::world_grid::WorldSpace;

pub struct TiledCameraPlugin;

impl Plugin for TiledCameraPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(Update, (on_window_resized, on_camera_changed));
    }
}

/// Component bundle with functions to specify how you want the camera set up.
///
/// ## Example
/// ```rust
/// use bevy_tiled_camera::TiledCameraBundle;
/// use bevy::prelude::Commands;
/// fn setup(mut commands:Commands) {
///   let camera_bundle = TiledCameraBundle::new()
///       .with_pixels_per_tile([8,8])
///       .with_tile_count([80,45]);
///
///   commands.spawn(camera_bundle);
/// }
/// ```
#[derive(Bundle)]
pub struct TiledCameraBundle {
    cam2d_bundle: Camera2dBundle,
    tiled_camera: TiledCamera,
}

impl TiledCameraBundle {
    #[allow(clippy::new_without_default)]
    pub fn new() -> Self {
        Self {
            cam2d_bundle: Camera2dBundle { ..default() },
            tiled_camera: default(),
        }
    }

    /// Construct a [`TiledCamera`] set to [`WorldSpace::Units`].
    pub fn unit_cam(tile_count: impl Size2d) -> Self {
        Self::new()
            .with_world_space(WorldSpace::Units)
            .with_tile_count(tile_count)
    }

    /// Construct a [`TiledCamera`] set to [`WorldSpace::Pixels`].
    pub fn pixel_cam(tile_count: impl Size2d) -> Self {
        Self::new()
            .with_world_space(WorldSpace::Pixels)
            .with_tile_count(tile_count)
    }

    /// Set the camera's [`WorldSpace`].
    pub fn with_world_space(mut self, world_space: WorldSpace) -> Self {
        self.tiled_camera.set_world_space(world_space);
        self
    }

    /// Set the camera's clear color.
    pub fn with_clear_color(mut self, color: Color) -> Self {
        self.cam2d_bundle.camera.clear_color = ClearColorConfig::Custom(color);
        self
    }

    /// Set the camera's pixels per tile.
    ///
    /// This along with tile count and [`WorldSpace`] define how the camera
    /// sizes the viewport.
    pub fn with_pixels_per_tile(mut self, ppt: impl Size2d) -> Self {
        self.tiled_camera.pixels_per_tile = ppt.as_uvec2();
        self.tiled_camera.grid.pixels_per_tile = ppt.as_uvec2();
        self
    }

    /// Set the camera's tile count.
    ///
    /// This along with pixels per tile and [`WorldSpace`] define how the camera
    /// sizes the viewport.
    pub fn with_tile_count(mut self, tile_count: impl Size2d) -> Self {
        self.tiled_camera.tile_count = tile_count.as_uvec2();
        self.tiled_camera.grid.tile_count = tile_count.as_uvec2();
        self
    }

    /// Set the initial world position for the camera.
    pub fn with_camera_position(mut self, world_pos: impl Point2d) -> Self {
        let pos = &mut self.cam2d_bundle.transform.translation;
        *pos = world_pos.as_vec2().extend(pos.z);
        self
    }
}

/// A camera with a virtual grid for displaying low resolution pixel art.
///
/// Contains various functions for translating points between world space and
/// the camera's virtual grid tiles.
#[derive(Component)]
pub struct TiledCamera {
    /// Pixels per tile determines the size of your tiles/art, depending on
    /// the camera's [`WorldSpace`].
    pub pixels_per_tile: UVec2,
    /// The number of virtual grid tiles in the camera's viewport.
    pub tile_count: UVec2,
    /// World grid used for transforming positions.
    grid: WorldGrid,
    /// Camera zoom from the last viewport update.
    zoom: u32,
    /// Viewport size from the last viewport update.
    vp_size: UVec2,
    /// Viewport position from the last viewport update.
    vp_pos: UVec2,
    /// Window resolution from the last viewport update.
    win_size: UVec2,
    ortho_size: f32,
}

impl TiledCamera {
    /// Creates a camera set to [`WorldSpace::Units`].
    pub fn unit_cam(tile_count: impl Size2d, pixels_per_tile: impl Size2d) -> Self {
        let tile_count = tile_count.as_uvec2();
        let pixels_per_tile = pixels_per_tile.as_uvec2();
        Self {
            pixels_per_tile,
            tile_count,
            grid: WorldGrid::unit_grid(tile_count, pixels_per_tile),
            ..default()
        }
    }

    /// Creates a camera set to [`WorldSpace::Pixels`].
    pub fn pixel_cam(tile_count: impl Size2d, pixels_per_tile: impl Size2d) -> Self {
        let tile_count = tile_count.as_uvec2();
        let pixels_per_tile = pixels_per_tile.as_uvec2();
        Self {
            pixels_per_tile,
            tile_count,
            grid: WorldGrid::pixel_grid(tile_count, pixels_per_tile),
            ..default()
        }
    }

    /// Retrieve the target resolution (in pixels) of the camera.
    pub fn target_resolution(&self) -> UVec2 {
        self.pixels_per_tile * self.tile_count
    }

    // Viewport size from the last viewport update
    pub fn viewport_size(&self) -> UVec2 {
        self.vp_size
    }

    // Viewport position from the last viewport update
    pub fn viewport_pos(&self) -> UVec2 {
        self.vp_pos
    }

    /// Window resolution from the last viewport update
    pub fn window_resolution(&self) -> UVec2 {
        self.win_size
    }

    /// The orthographic size of the camera from the last viewport update
    pub fn orthographic_size(&self) -> f32 {
        self.ortho_size
    }

    /// Returns an iterator that yields the center of the camera's virtual grid
    /// tiles in world space.
    pub fn tile_center_iter(&self, transform: &GlobalTransform) -> impl Iterator<Item = Vec2> {
        let xy = transform.translation().truncate();
        self.grid.tile_center_iter().map(move |p| p + xy)
    }

    /// Returns an iterator that yields the position of the camera's virtual
    /// grid tiles in world space.
    ///
    /// A tile's "position" refers to the bottom left corner of the tile.
    pub fn tile_pos_iter(&self, cam_transform: &GlobalTransform) -> impl Iterator<Item = Vec2> {
        let xy = cam_transform.translation().truncate();
        self.grid.tile_pos_iter().map(move |p| p + xy)
    }

    /// Transform from world space to camera-local space.
    pub fn world_to_local(&self, cam_transform: &GlobalTransform, world_pos: impl Point2d) -> Vec2 {
        world_pos.as_vec2() - cam_transform.translation().truncate()
    }

    /// Transform from camera-local space to world space.
    pub fn local_to_world(&self, cam_transform: &GlobalTransform, local_pos: impl Point2d) -> Vec2 {
        local_pos.as_vec2() + cam_transform.translation().truncate()
    }

    /// Convert a world position to it's virtual tile index.
    ///
    /// Tile indices are relative to the camera center.
    pub fn world_to_index(
        &self,
        cam_transform: &GlobalTransform,
        world_pos: impl Point2d,
    ) -> IVec2 {
        let local = self.world_to_local(cam_transform, world_pos);
        self.grid.pos_to_index(local)
    }

    /// Convert a world position to it's virtual tile position.
    ///
    /// A tile's "position" refers to the bottom left point of the tile.
    pub fn world_to_tile(&self, cam_transform: &GlobalTransform, world_pos: impl Point2d) -> Vec2 {
        let local = self.world_to_local(cam_transform, world_pos);
        self.grid.pos_to_tile_pos(local)
    }

    /// Convert a tile index to it's virtual tile position in world space.
    ///
    /// Tiles indices are relative to the camera center.
    ///
    /// A tile's "position" refers to the bottom left point of the tile.
    pub fn index_to_tile_pos(&self, cam_transform: &GlobalTransform, pos: impl GridPoint) -> Vec2 {
        let p = self.grid.index_to_pos(pos);
        self.local_to_world(cam_transform, p)
    }

    /// Return the world center of the virtual tile at the given tile index.
    ///
    /// Tile indices are relative to the camera center.
    pub fn index_to_tile_center(
        &self,
        cam_transform: &GlobalTransform,
        index: impl GridPoint,
    ) -> Vec2 {
        let p = self.grid.index_to_tile_center(index);
        self.local_to_world(cam_transform, p)
    }

    /// Change the camera's [`WorldSpace`].
    pub fn set_world_space(&mut self, world_space: WorldSpace) {
        self.grid.world_space = world_space;
    }

    /// Get the camera's [`WorldSpace`].
    pub fn world_space(&self) -> WorldSpace {
        self.grid.world_space
    }

    /// Get unit size or [`None`], depending on the camera's [`WorldSpace`].
    ///
    /// This can be used for sizing spawned sprites. If the camera's [`WorldSpace`]
    /// is [`WorldSpace::Units`] then a unit sized sprite should be the size of
    /// a tile.
    /// Otherwise it should use the default sprite size, which is the pixel dimensions
    /// of the sprite's texture.
    pub fn unit_size(&self) -> Option<Vec2> {
        match self.grid.world_space {
            WorldSpace::Units => Some(self.grid.tile_size_world()),
            WorldSpace::Pixels => None,
        }
    }

    /// How much the camera view is scaled up, based on target resolution and window size.
    pub fn zoom(&self) -> u32 {
        self.zoom
    }

    // MIT License
    // Copyright (c) 2021 Aevyrie
    // https://github.com/aevyrie/bevy_mod_raycast
    /// Convert a screen position (IE: The mouse cursor position) to it's corresponding world position.
    pub fn screen_to_world(
        &self,
        screen_pos: Vec2,
        camera: &Camera,
        camera_transform: &GlobalTransform,
    ) -> Option<Vec2> {
        let screen_size = self.vp_size.as_vec2();
        let screen_pos = (screen_pos - self.vp_pos.as_vec2()).round();

        let view = camera_transform.compute_matrix();
        let projection = camera.projection_matrix();

        // 2D Normalized device coordinate cursor position from (-1, -1) to (1, 1)
        let cursor_ndc = (screen_pos / screen_size) * 2.0 - Vec2::from([1.0, 1.0]);
        let ndc_to_world: Mat4 = view * projection.inverse();
        let world_to_ndc = projection * view;

        // Calculate the camera's near plane using the projection matrix
        let projection = projection.to_cols_array_2d();
        let camera_near = (2.0 * projection[3][2]) / (2.0 * projection[2][2] - 2.0);

        // Compute the cursor position at the near plane. The bevy camera looks at -Z.
        let ndc_near = world_to_ndc.transform_point3(-Vec3::Z * camera_near).z;
        let cursor_pos_near = ndc_to_world.transform_point3(cursor_ndc.extend(ndc_near));
        let tile_size = self.grid.tile_size_world();
        let cursor_pos_near = cursor_pos_near.truncate() * tile_size;
        // Former viewport issue - had to flip y. Was fixed in 0.9 release
        //cursor_pos_near.y = -cursor_pos_near.y;
        Some(cursor_pos_near)
    }

    /// Converts a world position to a screen position (0..resolution)
    pub fn world_to_screen(
        &self,
        world_pos: impl Point2d,
        camera: &Camera,
        camera_transform: &GlobalTransform,
    ) -> Option<Vec2> {
        let window_size = self.vp_size.as_vec2();

        // Build a transform to convert from world to NDC using camera data
        let world_to_ndc: Mat4 =
            camera.projection_matrix() * camera_transform.compute_matrix().inverse();
        let ndc_space_coords: Vec3 = world_to_ndc.project_point3(world_pos.as_vec2().extend(0.0));

        // NDC z-values outside of 0 < z < 1 are outside the camera frustum and are thus not in screen space
        if ndc_space_coords.z < 0.0 || ndc_space_coords.z > 1.0 {
            return None;
        }

        // Once in NDC space, we can discard the z element and rescale x/y to fit the screen
        let screen_space_coords = (ndc_space_coords.truncate() + Vec2::ONE) / 2.0 * window_size;
        if !screen_space_coords.is_nan() {
            Some((screen_space_coords + self.vp_pos.as_vec2()).round())
        } else {
            None
        }
    }

    /// Retrieve the camera's [`WorldGrid`].
    pub fn world_grid(&self) -> &WorldGrid {
        &self.grid
    }
}

impl Default for TiledCamera {
    fn default() -> Self {
        let pixels_per_tile = UVec2::new(8, 8);
        let tile_count = UVec2::new(80, 35);
        Self {
            pixels_per_tile,
            tile_count,
            grid: WorldGrid::unit_grid(tile_count, pixels_per_tile),
            zoom: 1,
            vp_size: UVec2::ONE,
            vp_pos: UVec2::ZERO,
            win_size: UVec2::ONE,
            ortho_size: 0.0,
        }
    }
}

fn on_window_resized(
    primary_window: Query<(Entity, &Window), With<PrimaryWindow>>,
    mut resize_events: EventReader<WindowResized>,
    mut q_cam: Query<(&mut OrthographicProjection, &mut Camera, &mut TiledCamera)>,
) {
    // We need to dynamically resize the camera's viewports whenever the window
    // size changes. A resize_event is sent when the window is first created,
    // allowing us to reuse this system for initial setup.
    let (primary_window_entity, primary_window) = primary_window.single();

    for resize_event in resize_events.read() {
        if resize_event.window == primary_window_entity {
            let wres = UVec2::new(
                primary_window.physical_width(),
                primary_window.physical_height(),
            );

            if let Ok((mut proj, mut cam, mut tiled_cam)) = q_cam.get_single_mut() {
                update_viewport(&mut tiled_cam, wres, &mut proj, &mut cam);
            }
        }
    }
}

fn on_camera_changed(
    primary_window: Query<&Window, With<PrimaryWindow>>,
    mut q_cam: Query<
        (&mut OrthographicProjection, &mut Camera, &mut TiledCamera),
        Changed<TiledCamera>,
    >,
) {
    for (mut proj, mut cam, mut tiled_cam) in q_cam.iter_mut() {
        if let Ok(window) = primary_window.get_single() {
            let wres = UVec2::new(window.physical_width(), window.physical_height());
            update_viewport(&mut tiled_cam, wres, &mut proj, &mut cam);
        }
    }
}

fn update_viewport(
    tiled_cam: &mut TiledCamera,
    wres: UVec2,
    proj: &mut OrthographicProjection,
    cam: &mut Camera,
) {
    let tres = tiled_cam.target_resolution().as_vec2();
    let wres = wres.as_vec2();
    let zoom = (wres / tres).floor().min_element().max(1.0);

    // The 'size' of the orthographic projection.
    //
    // For a `FixedVertical` projection this refers to the size of the
    // projection in vertical units.
    let ortho_size = match tiled_cam.world_space() {
        WorldSpace::Units => tiled_cam.tile_count.y as f32,
        WorldSpace::Pixels => tiled_cam.tile_count.y as f32 * tiled_cam.pixels_per_tile.y as f32,
    };

    proj.scaling_mode = ScalingMode::FixedVertical(ortho_size);

    let vp_size = tres * zoom;
    let vp_pos = if wres.cmple(tres).any() {
        Vec2::ZERO
    } else {
        (wres / 2.0) - (vp_size / 2.0)
    }
    .floor();

    cam.viewport = Some(Viewport {
        physical_position: vp_pos.as_uvec2(),
        physical_size: vp_size.as_uvec2(),
        ..default()
    });

    // Camera values may have been changed manually - update grid values.
    tiled_cam.grid.tile_count = tiled_cam.tile_count;
    tiled_cam.grid.pixels_per_tile = tiled_cam.pixels_per_tile;
    tiled_cam.zoom = zoom as u32;
    tiled_cam.vp_pos = vp_pos.as_uvec2();
    tiled_cam.vp_size = vp_size.as_uvec2();
    tiled_cam.win_size = wres.as_uvec2();
    tiled_cam.ortho_size = ortho_size;
}

#[cfg(test)]
mod tests {
    use super::*;

    fn unit_cam(pos: impl Point2d, tile_count: impl Size2d) -> (GlobalTransform, TiledCamera) {
        (
            GlobalTransform::from_translation(pos.as_vec2().extend(0.0)),
            TiledCamera::unit_cam(tile_count, [8, 8]),
        )
    }

    fn make_pixel_cam(
        pos: impl Point2d,
        tile_count: impl Size2d,
    ) -> (GlobalTransform, TiledCamera) {
        (
            GlobalTransform::from_translation(pos.as_vec2().extend(0.0)),
            TiledCamera::pixel_cam(tile_count, [8, 8]),
        )
    }

    #[test]
    fn world_to_index() {
        let (t, cam) = unit_cam([5.0, 5.0], [3, 3]);
        let p = cam.world_to_index(&t, [4.5, 4.5]);
        assert_eq!([0, 0], p.to_array());

        let (t, cam) = unit_cam([5.0, 5.0], [4, 4]);
        let p = cam.world_to_index(&t, [4.5, 4.5]);
        assert_eq!([-1, -1], p.to_array());

        let (t, cam) = make_pixel_cam([16.0, 16.0], [3, 3]);
        let p = cam.world_to_index(&t, [12.0, 12.0]);
        assert_eq!([0, 0], p.to_array());

        let (t, cam) = make_pixel_cam([16.0, 16.0], [4, 4]);
        let p = cam.world_to_index(&t, [12.0, 12.0]);
        assert_eq!([-1, -1], p.to_array());
    }

    #[test]
    fn index_to_world() {
        let (t, cam) = make_pixel_cam([5, 5], [4, 4]);
        let p = cam.index_to_tile_pos(&t, [3, 3]);
        assert_eq!([29.0, 29.0], p.to_array());

        let (t, cam) = unit_cam([5, 5], [4, 4]);
        let p = cam.index_to_tile_pos(&t, [3, 3]);
        assert_eq!([8.0, 8.0], p.to_array());

        let (t, cam) = make_pixel_cam([16, 16], [3, 3]);
        let p = cam.index_to_tile_pos(&t, [3, 3]);
        assert_eq!([36.0, 36.0], p.to_array());
    }

    #[test]
    fn new() {
        let cam = TiledCameraBundle::pixel_cam([5, 5]).tiled_camera;
        assert_eq!(cam.world_space(), WorldSpace::Pixels);

        let cam = TiledCameraBundle::unit_cam([5, 5]).tiled_camera;
        assert_eq!(cam.world_space(), WorldSpace::Units);
    }
}