use bevy::app::{App, Plugin as BevyPlugin};
use bevy::ecs::prelude::*;
use bevy::render::camera::Camera;
use bevy::ui::Style;
use bevy_mod_sysfail::quick_sysfail;
use cuicui_layout::content_sized::AppContentSizeExt;
use cuicui_layout::{LayoutRect, LayoutRootCamera, Root, ScreenRoot};
pub use dsl::UiDsl;
mod fixup;
pub mod content_sized;
pub mod dsl;
#[cfg(doctest)]
#[doc = include_str!("../../README.md")]
pub struct TestWorkspaceReadme;
#[quick_sysfail]
pub fn update_ui_camera_root(
ui_cameras: Query<&Camera, (With<LayoutRootCamera>, Changed<Camera>)>,
mut roots: Query<&mut Root, With<ScreenRoot>>,
) {
for cam in &ui_cameras {
let size = cam.logical_viewport_size()?;
for mut root in &mut roots {
let bounds = root.size_mut();
*bounds.width = size.x;
*bounds.height = size.y;
}
}
}
#[quick_sysfail]
pub fn set_added_camera_root(
ui_cameras: Query<&Camera, With<LayoutRootCamera>>,
mut roots: Query<&mut Root, Added<ScreenRoot>>,
) {
for mut root in &mut roots {
let Some(camera) = ui_cameras.iter().next() else {
continue;
};
let size = camera.logical_viewport_size()?;
let bounds = root.size_mut();
*bounds.width = size.x;
*bounds.height = size.y;
}
}
pub fn set_layout_style(mut query: Query<(&mut Style, &LayoutRect), Changed<LayoutRect>>) {
use bevy::ui::{PositionType, Val};
query.for_each_mut(|(mut style, pos)| {
style.position_type = PositionType::Absolute;
style.left = Val::Px(pos.pos().x);
style.top = Val::Px(pos.pos().y);
let width = Val::Px(pos.size().width);
style.min_width = width;
style.max_width = width;
style.width = width;
let height = Val::Px(pos.size().height);
style.min_height = height;
style.max_height = height;
style.height = height;
});
}
pub struct Plugin;
impl BevyPlugin for Plugin {
fn build(&self, app: &mut App) {
use bevy::prelude::{Last, PostUpdate, Update};
use bevy::ui::UiSystem;
use cuicui_layout::ComputeLayoutSet;
app.add_plugins(cuicui_layout::Plugin)
.add_content_sized::<content_sized::UiContentSize>()
.add_systems(
Update,
(update_ui_camera_root, set_added_camera_root).before(ComputeLayoutSet),
)
.add_systems(PostUpdate, set_layout_style.before(UiSystem::Layout))
.add_systems(
Last,
(fixup::add_text_components, fixup::add_image_components),
);
}
}