bevy_rectray 0.5.0

A minimal 2d layout system for bevy.
Documentation
use bevy::{
    ecs::{query::With, system::Query},
    math::Vec2,
    prelude::{
        Component, Reflect, ReflectComponent, ReflectDefault, ReflectDeserialize, ReflectSerialize,
    },
    window::{PrimaryWindow, Window},
};
use serde::{Deserialize, Serialize};

use crate::{RectrayFrame, Transform2D};

/// Synchronize the size of [`RectrayFrame`] with [`PrimaryWindow`].
#[derive(Debug, Clone, Copy, Default, Reflect, Serialize, Deserialize, Component)]
#[reflect(Default, Serialize, Deserialize, Component)]
pub struct RectrayWindow;

/// Set [`Transform2D::offset`] to [`PrimaryWindow`]'s cursor position.
#[derive(Debug, Clone, Copy, Default, Reflect, Serialize, Deserialize, Component)]
#[reflect(Default, Serialize, Deserialize, Component)]
pub struct RectrayCursor;

pub fn window_frame_system(
    windows: Query<&Window, With<PrimaryWindow>>,
    mut frames: Query<&mut RectrayFrame, With<RectrayWindow>>,
    mut cursors: Query<&mut Transform2D, With<RectrayCursor>>,
) {
    let Ok(window) = windows.single() else {
        return;
    };
    let size = window.size();
    for mut frame in &mut frames {
        frame.dimension = size;
    }
    if let Some(pos) = window.cursor_position() {
        for mut transform in &mut cursors {
            transform.offset = (pos - size / 2.) * Vec2::new(1., -1.);
        }
    }
}