cotis-layout 0.1.0-alpha.1

Flexbox-style layout engine for Cotis
Documentation
use cotis::cotis_app::UiFn;
use cotis::element_configuring::{ConfigureElements, ConfiguredParentElement};
use cotis::utils::ElementIdConfig;
use cotis_defaults::colors::Color;
use cotis_defaults::element_configs::style::sizing::{AxisSizing, DoubleAxisSizing, Sizing};
use cotis_defaults::element_configs::style::types::{
    Alignment, LayoutAlignmentX, LayoutAlignmentY, LayoutDirection, Padding,
};
use cotis_defaults::element_configs::style::{
    BorderElementStyle, BorderWidthStyle, CornerRadiiStyle, CotisStyle, LayoutStyle,
};
use cotis_defaults::element_configs::text_config::{
    TextAlignment, TextConfig, TextElementConfig, TextElementConfigWrapMode, TextStyle,
};
use cotis_raylib::prelude::RaylibRender;
use raylib::consts::TextureFilter;

mod support;
use support::{
    ExampleConfig, StandardRenderCmds, compute_frame, empty_example_config, example_images,
    image_config, new_cotis_app,
};

fn main() {
    test_entry(RaylibRender::auto_start())
}

fn test_entry(mut renderer: RaylibRender) {
    let mut app = new_cotis_app(
        renderer,
        cotis_utils::math::Dimensions::new(800.0 + 400.0, 800.0),
    );
    let _font = app
        .render()
        .load_font_ex_and_keep(
            "./examples/fonts/Roboto-Regular.ttf",
            35,
            None,
            Some(TextureFilter::TEXTURE_FILTER_BILINEAR),
        )
        .unwrap();
    let test = Test {};
    while !app.render().window_should_close() {
        compute_frame::<StandardRenderCmds, _>(&mut app, &test);
    }
}

struct Test {}

impl<'inside, Configurer> UiFn<'inside, Configurer, ExampleConfig> for &Test
where
    Configurer: ConfigureElements<ExampleConfig>
        + cotis::element_configuring::ConfigureLeafElements<
            cotis_defaults::element_configs::text_config::TextElementConfig<'static>,
        >,
{
    fn draw_in(self, layout: &mut ConfiguredParentElement<'inside, Configurer, ExampleConfig>) {
        draw_as_child(layout)
    }
}

pub fn draw_as_child<Conf>(parent: &mut ConfiguredParentElement<'_, Conf, ExampleConfig>)
where
    Conf: ConfigureElements<ExampleConfig>
        + cotis::element_configuring::ConfigureLeafElements<
            cotis_defaults::element_configs::text_config::TextElementConfig<'static>,
        >,
{
    let mut el0 = parent.new_element();
    el0.set_config(ExampleConfig {
        id_config: ElementIdConfig::new_empty(),
        style: CotisStyle {
            sizing: Sizing::DoubleAxis(DoubleAxisSizing {
                width: AxisSizing::Grow(0.0, f32::MAX),
                height: AxisSizing::Fit(0.0, f32::MAX),
            }),
            layout: LayoutStyle {
                padding: Padding {
                    top: 0.0,
                    bottom: 0.0,
                    right: 0.0,
                    left: 0.0,
                },
                child_gap: 0.0,
                child_alignment: Alignment {
                    x: LayoutAlignmentX::Left,
                    y: LayoutAlignmentY::Center,
                },
                layout_direction: LayoutDirection::LeftToRight,
            },
            background_color: Color::rgba(0.0, 0.0, 0.0, 0.0),
            corner_radius: CornerRadiiStyle {
                top_left: 0.0,
                top_right: 0.0,
                bottom_left: 0.0,
                bottom_right: 0.0,
            },
            floating: None,
            border: BorderElementStyle {
                color: Color::rgba(0.0, 0.0, 0.0, 255.0),
                width: BorderWidthStyle {
                    left: 0.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                    between_children: 0.0,
                },
            },
            ..Default::default()
        },
        image: None,
        custom: None,
        extra_data: None,
    });
    let mut el0_p = el0.make_parent();
    let mut el1 = el0_p.new_element();
    el1.set_config(ExampleConfig {
        id_config: ElementIdConfig::new_empty(),
        style: CotisStyle {
            sizing: Sizing::DoubleAxis(DoubleAxisSizing {
                width: AxisSizing::Fit(0.0, f32::MAX),
                height: AxisSizing::Fit(0.0, f32::MAX),
            }),
            layout: LayoutStyle {
                padding: Padding {
                    top: 10.0,
                    bottom: 10.0,
                    right: 20.0,
                    left: 20.0,
                },
                child_gap: 0.0,
                child_alignment: Alignment {
                    x: LayoutAlignmentX::Left,
                    y: LayoutAlignmentY::Top,
                },
                layout_direction: LayoutDirection::LeftToRight,
            },
            background_color: Color::rgba(26.0, 18.0, 9.0, 255.0),
            corner_radius: CornerRadiiStyle {
                top_left: 2.0,
                top_right: 2.0,
                bottom_left: 2.0,
                bottom_right: 2.0,
            },
            floating: None,
            border: BorderElementStyle {
                color: Color::rgba(0.0, 0.0, 0.0, 255.0),
                width: BorderWidthStyle {
                    left: 0.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                    between_children: 0.0,
                },
            },
            ..Default::default()
        },
        image: None,
        custom: None,
        extra_data: None,
    });
    let mut el1_p = el1.make_parent();
    let mut el2 = el1_p.new_element();
    el2.set_config(empty_example_config());
    el2.set_leaf_config(TextElementConfig {
        text: cotis::utils::OwnedOrRef::Owned("Read on".to_owned().into_boxed_str()),
        config: TextConfig {
            font_id: 0,
            font_size: 10.0,
            letter_spacing: 2.56,
            line_height: 0.0,
            wrap_mode: TextElementConfigWrapMode::Words,
            alignment: TextAlignment::Left,
        },
        style: TextStyle {
            color: Color::rgba(245.0, 240.0, 232.0, 255.0),
        },
    });
    el1_p.close_element();
    let mut el3 = el0_p.new_element();
    el3.set_config(ExampleConfig {
        id_config: ElementIdConfig::new_empty(),
        style: CotisStyle {
            sizing: Sizing::DoubleAxis(DoubleAxisSizing {
                width: AxisSizing::Grow(0.0, f32::MAX),
                height: AxisSizing::Fit(0.0, f32::MAX),
            }),
            layout: LayoutStyle {
                padding: Padding {
                    top: 0.0,
                    bottom: 0.0,
                    right: 0.0,
                    left: 0.0,
                },
                child_gap: 0.0,
                child_alignment: Alignment {
                    x: LayoutAlignmentX::Left,
                    y: LayoutAlignmentY::Top,
                },
                layout_direction: LayoutDirection::LeftToRight,
            },
            background_color: Color::rgba(0.0, 0.0, 0.0, 0.0),
            corner_radius: CornerRadiiStyle {
                top_left: 0.0,
                top_right: 0.0,
                bottom_left: 0.0,
                bottom_right: 0.0,
            },
            floating: None,
            border: BorderElementStyle {
                color: Color::rgba(0.0, 0.0, 0.0, 255.0),
                width: BorderWidthStyle {
                    left: 0.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                    between_children: 0.0,
                },
            },
            ..Default::default()
        },
        image: None,
        custom: None,
        extra_data: None,
    });
    el3.close_element();
    let mut el4 = el0_p.new_element();
    el4.set_config(ExampleConfig {
        id_config: ElementIdConfig::new_empty(),
        style: CotisStyle {
            sizing: Sizing::DoubleAxis(DoubleAxisSizing {
                width: AxisSizing::Fit(0.0, f32::MAX),
                height: AxisSizing::Fit(0.0, f32::MAX),
            }),
            layout: LayoutStyle {
                padding: Padding {
                    top: 0.0,
                    bottom: 0.0,
                    right: 0.0,
                    left: 0.0,
                },
                child_gap: 0.0,
                child_alignment: Alignment {
                    x: LayoutAlignmentX::Left,
                    y: LayoutAlignmentY::Top,
                },
                layout_direction: LayoutDirection::LeftToRight,
            },
            background_color: Color::rgba(0.0, 0.0, 0.0, 0.0),
            corner_radius: CornerRadiiStyle {
                top_left: 0.0,
                top_right: 0.0,
                bottom_left: 0.0,
                bottom_right: 0.0,
            },
            floating: None,
            border: BorderElementStyle {
                color: Color::rgba(0.0, 0.0, 0.0, 255.0),
                width: BorderWidthStyle {
                    left: 0.0,
                    right: 0.0,
                    top: 0.0,
                    bottom: 0.0,
                    between_children: 0.0,
                },
            },
            ..Default::default()
        },
        image: Some(image_config(example_images::calendar_png())),
        custom: None,
        extra_data: None,
    });
    let mut el4_p = el4.make_parent();
    let mut el5 = el4_p.new_element();
    el5.set_config(empty_example_config());
    el5.set_leaf_config(TextElementConfig {
        text: cotis::utils::OwnedOrRef::Owned("4 min read".to_owned().into_boxed_str()),
        config: TextConfig {
            font_id: 0,
            font_size: 10.0,
            letter_spacing: 0.8,
            line_height: 0.0,
            wrap_mode: TextElementConfigWrapMode::Words,
            alignment: TextAlignment::Left,
        },
        style: TextStyle {
            color: Color::rgba(140.0, 122.0, 94.0, 255.0),
        },
    });
    el4_p.close_element();
    el0_p.close_element();
}