bevy_pages 0.2.0

A lightweight and elegant framework to upgrade your Bevy UI experience.
Documentation
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![allow(clippy::type_complexity)]
#![allow(clippy::too_many_arguments)]

use crate::page::Page;
use crate::spawner::PageSpawner;
use crate::systems::PageSystemSet;
use bevy::app::{App, Plugin, Update};
use bevy::asset::{AssetApp, AssetEvent};
use bevy::prelude::{IntoScheduleConfigs, on_message, resource_exists};

/// Contains the basic element structures for a clean UI.
pub mod element;

/// Contains UI events and event handling systems.
pub mod events;

/// Contains the page asset loader.
pub mod loader;

/// Contains the [Page] struct for core UI work.
pub mod page;

/// Contains the [PageSpawner] resource and related systems to spawn UI pages.
pub mod spawner;

/// Contains custom widget functionality.
pub mod widgets;

// TODO: make this public & document
pub(crate) mod parser;

pub(crate) mod systems;

/// The main plugin for `bevy_pages`.
///
/// This is required to spawn and manage UI pages.
pub struct PagesPlugin {
    /// The initial read capacity for the asset loading process.
    ///
    /// This should be the average size (in bytes) of the XML files you plan to load.
    ///
    /// Defaults to `2048`.
    pub initial_read_capacity: usize,
}

impl Plugin for PagesPlugin {
    fn build(&self, app: &mut App) {
        app.init_asset::<Page>()
            .register_asset_loader(loader::PageLoader {
                initial_read_capacity: self.initial_read_capacity,
            })
            .insert_resource(PageSpawner::new())
            .configure_sets(Update, PageSystemSet.run_if(resource_exists::<Page>))
            // General Interactions
            .add_systems(Update, systems::interactions.in_set(PageSystemSet))
            // Checkbox Widget Logic
            .add_systems(
                Update,
                widgets::checkbox::sync_visuals.in_set(PageSystemSet),
            )
            .add_observer(widgets::checkbox::toggle_checkbox)
            // Text Input Widget Logic
            .add_systems(
                Update,
                (
                    widgets::text_input::sync_visuals,
                    widgets::text_input::handle_typing,
                    widgets::text_input::handle_click_outside,
                )
                    .in_set(PageSystemSet),
            )
            .add_observer(widgets::text_input::handle_focus)
            // Slider Widget Logic
            .add_systems(
                Update,
                (
                    widgets::slider::handle_slider_input,
                    widgets::slider::sync_visuals,
                )
                    .in_set(PageSystemSet),
            )
            // Progress Bar Widget Logic
            .add_systems(
                Update,
                widgets::progress_bar::sync_progress_bar_visuals.in_set(PageSystemSet),
            )
            // Tooltip Widget Logic
            .add_systems(Update, widgets::tooltip::sync_visuals.in_set(PageSystemSet))
            // Scroll View Widget Logic
            .add_systems(
                Update,
                (
                    widgets::scroll_view::update_scroll_bounds,
                    widgets::scroll_view::scroll_view_mouse_wheel,
                    widgets::scroll_view::scroll_view_keyboard,
                    widgets::scroll_view::apply_scroll_physics,
                    widgets::scroll_view::update_visuals,
                )
                    .chain()
                    .in_set(PageSystemSet),
            )
            // Dropdown Widget Logic
            .add_systems(
                Update,
                (
                    widgets::dropdown::option_select,
                    widgets::dropdown::trigger_menu,
                    widgets::dropdown::visibility,
                    widgets::dropdown::close_on_outside_click,
                )
                    .in_set(PageSystemSet),
            )
            // Spawn & Despawn Logic
            .add_systems(
                Update,
                spawner::spawn_page.run_if(on_message::<AssetEvent<Page>>),
            )
            .add_observer(spawner::despawn_page);
    }
}

impl Default for PagesPlugin {
    fn default() -> Self {
        Self {
            initial_read_capacity: 2048,
        }
    }
}