cotis-layout 0.1.0-alpha.1

Flexbox-style layout engine for Cotis
Documentation
//! Shared types and helpers for cotis-layout examples (cotis 0.1.0-alpha + cotis-layout 0.1.0-alpha + cotis-pipes).

use cotis::cotis_app::{CotisApp, RenderApp, UiFn};
use cotis::utils::ElementIdConfig;
use cotis_defaults::element_configs::image_config::ImageConfig;
use cotis_defaults::element_configs::premade_configs::GenericElementConfig;
use cotis_defaults::element_configs::style::CotisStyle;
use cotis_defaults::render_commands::render_t_list::RenderTList;
use cotis_defaults::render_commands::{Border, ClipEnd, ClipStart, Image, Rectangle, Text};
use cotis_layout::layout_struct::RenderCommandOutput;
use cotis_layout::preamble::CotisLayoutManager;
use cotis_pipes::cotis_layout_pipes::CotisLayoutToRenderListPipeForGenerics;
use cotis_raylib::cotis_defaults_images::{JPEGImage, PNGImage, PathBasedImage, SVGImage};
use cotis_raylib::prelude::RaylibRender;
use cotis_utils::math::Dimensions;

/// Element config with path-based images from cotis-defaults.
pub type ExampleConfig = GenericElementConfig<'static, PathBasedImage, ()>;

/// Clip + rectangle + image + border + text pipeline decoded by [`CotisLayoutToRenderListPipeForGenerics`].
pub type StandardRenderCmds = RenderTList<
    ClipStart<'static, ()>,
    RenderTList<
        ClipEnd,
        RenderTList<
            Rectangle<'static, ()>,
            RenderTList<
                Image<'static, PathBasedImage, ()>,
                RenderTList<Border<'static, ()>, RenderTList<Text<'static, ()>, ()>>,
            >,
        >,
    >,
>;

/// Rectangle + image + border + text pipeline (no clip).
pub type SimpleRenderCmds = RenderTList<
    Rectangle<'static, ()>,
    RenderTList<
        Image<'static, PathBasedImage, ()>,
        RenderTList<Border<'static, ()>, RenderTList<Text<'static, ()>, ()>>,
    >,
>;

pub type ExampleCotisApp =
    CotisApp<RaylibRender, CotisLayoutManager, CotisLayoutToRenderListPipeForGenerics>;

pub fn new_cotis_app(renderer: RaylibRender, dimensions: Dimensions) -> ExampleCotisApp {
    let manager = CotisLayoutManager::new(dimensions);
    CotisApp::new(renderer, manager, CotisLayoutToRenderListPipeForGenerics)
}

/// Minimal element config for leaf nodes (text-only children).
pub fn empty_example_config() -> ExampleConfig {
    ExampleConfig {
        id_config: ElementIdConfig::new_empty(),
        style: CotisStyle::default(),
        image: None,
        custom: None,
        extra_data: None,
    }
}

pub fn image_config(image: PathBasedImage) -> ImageConfig<'static, PathBasedImage> {
    ImageConfig::new_owned(image)
}

/// Example assets shipped under `examples/`.
pub const APPLE_IMAGE_PATH: &str = "./examples/Apple.png";
pub const APPLE_JPEG_IMAGE_PATH: &str = "./examples/Apple.jpg";
pub const CALENDAR_PNG_PATH: &str = "./examples/calendar_icon_2.png";
/// Demonstrates [`SVGImage`]; uses the calendar PNG asset until an `.svg` is added.
pub const CALENDAR_SVG_PATH: &str = "./examples/calendar_icon_2.png";

/// Example images loaded from local asset paths.
pub mod example_images {
    use super::{
        APPLE_IMAGE_PATH, APPLE_JPEG_IMAGE_PATH, CALENDAR_PNG_PATH, CALENDAR_SVG_PATH, JPEGImage,
        PNGImage, PathBasedImage, SVGImage,
    };

    pub fn apple_png() -> PathBasedImage {
        PathBasedImage::Png(PNGImage::new_const(APPLE_IMAGE_PATH))
    }

    pub fn calendar_png() -> PathBasedImage {
        PathBasedImage::Png(PNGImage::new_const(CALENDAR_PNG_PATH))
    }

    pub fn apple_jpeg() -> PathBasedImage {
        PathBasedImage::Jpeg(JPEGImage::new_const(APPLE_JPEG_IMAGE_PATH))
    }

    pub fn calendar_svg() -> PathBasedImage {
        PathBasedImage::Svg(SVGImage::new_const(CALENDAR_SVG_PATH))
    }
}

pub fn compute_frame<'layout, Cmds, Ui>(app: &'layout mut ExampleCotisApp, ui: Ui)
where
    RaylibRender: cotis::renders::CotisRenderer<Cmds>,
    CotisLayoutToRenderListPipeForGenerics:
        cotis::pipes::LayoutRenderPipe<RenderCommandOutput<ExampleConfig>, Cmds>,
    Ui: for<'a> UiFn<
            'a,
            cotis_layout::preamble::CotisLayoutRun<'layout, ExampleConfig>,
            ExampleConfig,
        >,
{
    RenderApp::<
        ExampleConfig,
        RenderCommandOutput<ExampleConfig>,
        Cmds,
        RaylibRender,
        CotisLayoutManager,
    >::compute_frame(app, ui);
}