cotis-wgpu 0.1.0-alpha

Desktop wgpu renderer backend for Cotis
Documentation
use cotis::cotis_app::{CotisApp, 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_layout::preamble::CotisLayoutManager;
use cotis_pipes::cotis_layout_pipes::CotisLayoutToRenderListPipeForGenerics;
use cotis_utils::math::Dimensions;
use cotis_wgpu::prelude::*;

mod support;
use support::{
    ExampleConfig, FillColor, StandardRenderCmds, compute_frame, empty_example_config, solid_color,
};

fn main() {
    env_logger::init();
    test_entry(CotisWgpuRenderer::auto_start());
}

fn test_entry(renderer: CotisWgpuRenderer) {
    let dimensions = Dimensions::new(1000.0, 800.0);
    let manager = CotisLayoutManager::new(dimensions);
    let mut app = CotisApp::new(renderer, manager, CotisLayoutToRenderListPipeForGenerics);
    let test = Demo;
    while !app.render().window_should_close() {
        compute_frame::<StandardRenderCmds, _>(&mut app, &test);
    }
}

struct Demo;

impl<'inside, Configurer> UiFn<'inside, Configurer, ExampleConfig> for &Demo
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_ui(layout);
    }
}

fn draw_ui<Conf>(parent: &mut ConfiguredParentElement<'_, Conf, ExampleConfig>)
where
    Conf: ConfigureElements<ExampleConfig>
        + cotis::element_configuring::ConfigureLeafElements<
            cotis_defaults::element_configs::text_config::TextElementConfig<'static>,
        >,
{
    let mut row = parent.new_element();
    row.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: 24.0,
                    bottom: 24.0,
                    left: 24.0,
                    right: 24.0,
                },
                child_gap: 16.0,
                child_alignment: Alignment {
                    x: LayoutAlignmentX::Left,
                    y: LayoutAlignmentY::Center,
                },
                layout_direction: LayoutDirection::LeftToRight,
            },
            background_color: solid_color(30.0, 30.0, 40.0, 255.0),
            corner_radius: CornerRadiiStyle {
                top_left: 20.0,
                top_right: 20.0,
                bottom_left: 20.0,
                bottom_right: 20.0,
            },
            border: BorderElementStyle {
                color: Color::rgba(90.0, 90.0, 120.0, 255.0),
                width: BorderWidthStyle {
                    left: 2.0,
                    right: 2.0,
                    top: 2.0,
                    bottom: 2.0,
                    between_children: 0.0,
                },
            },
            floating: None,
            ..Default::default()
        },
        image: None,
        custom: None,
        extra_data: None,
    });
    let mut row_parent = row.make_parent();

    add_card(
        &mut row_parent,
        "Hello",
        solid_color(60.0, 20.0, 20.0, 255.0),
    );
    add_card(
        &mut row_parent,
        "cotis-wgpu",
        solid_color(20.0, 60.0, 20.0, 255.0),
    );
    add_card(
        &mut row_parent,
        "Resize me",
        solid_color(20.0, 20.0, 60.0, 255.0),
    );

    row_parent.close_element();
}

fn add_card<Conf>(
    parent: &mut ConfiguredParentElement<'_, Conf, ExampleConfig>,
    label: &str,
    background: FillColor,
) where
    Conf: ConfigureElements<ExampleConfig>
        + cotis::element_configuring::ConfigureLeafElements<
            cotis_defaults::element_configs::text_config::TextElementConfig<'static>,
        >,
{
    let mut card = parent.new_element();
    card.set_config(ExampleConfig {
        id_config: ElementIdConfig::new_empty(),
        style: CotisStyle {
            sizing: Sizing::DoubleAxis(DoubleAxisSizing {
                width: AxisSizing::Fixed(220.0),
                height: AxisSizing::Fixed(140.0),
            }),
            layout: LayoutStyle {
                padding: Padding {
                    top: 12.0,
                    bottom: 12.0,
                    left: 12.0,
                    right: 12.0,
                },
                child_gap: 0.0,
                child_alignment: Alignment {
                    x: LayoutAlignmentX::Center,
                    y: LayoutAlignmentY::Center,
                },
                layout_direction: LayoutDirection::LeftToRight,
            },
            background_color: background,
            corner_radius: CornerRadiiStyle {
                top_left: 6.0,
                top_right: 6.0,
                bottom_left: 6.0,
                bottom_right: 6.0,
            },
            ..Default::default()
        },
        image: None,
        custom: None,
        extra_data: None,
    });
    let mut card_parent = card.make_parent();

    let mut text_el = card_parent.new_element();
    text_el.set_config(empty_example_config());
    text_el.set_leaf_config(TextElementConfig {
        text: cotis::utils::OwnedOrRef::Owned(label.to_owned().into_boxed_str()),
        config: TextConfig {
            font_id: 0,
            font_size: 22.0,
            letter_spacing: 1.0,
            line_height: 0.0,
            wrap_mode: TextElementConfigWrapMode::Words,
            alignment: TextAlignment::Center,
        },
        style: TextStyle {
            color: solid_color(240.0, 240.0, 240.0, 255.0),
        },
    });

    card_parent.close_element();
}