krabmaga 0.6.1

A modern developing art for reliable and efficient Agent-based Model (ABM) simulation with the Rust language.
Documentation
use bevy::{
    prelude::{Camera, MessageReader, Query, Res, Transform},
    window::WindowResized,
};

use crate::visualization::simulation_descriptor::SimulationDescriptor;

pub fn camera_system(
    mut resize_event: MessageReader<WindowResized>,
    sim: Res<SimulationDescriptor>,
    mut query: Query<(&Camera, &mut Transform)>,
) {
    for e in resize_event.read() {
        let Ok((_camera, mut transform)) = query.single_mut() else {
            continue;
        };
        // Offset the whole simulation to the left to take the width of the UI panel into account.
        let ui_offset = -sim.ui_width;
        // Scale the simulation so it fills the portion of the screen not covered by the UI panel.
        let scale_x = sim.width / (e.width + ui_offset);

        // The translation x must depend on the scale_x to keep the left offset constant between window resizes.
        transform.translation.x = ui_offset * scale_x;
        transform.scale.x = scale_x;
        // Scale up the simulation enough to fill the window height
        transform.scale.y = sim.height / e.height;
    }
}