From 46b7e5e9c6ced7c83a1984c02cb31aef2b1d574c Mon Sep 17 00:00:00 2001 From: Ickshonpe Date: Thu, 23 Feb 2023 15:59:03 +0000 Subject: [PATCH 01/31] changes: * Added the module `border` * Added `BorderStyle` and `CalculatedBorder` types. * Added `border_style` and `calculated_border` fields to `NodeBundle` and `ButtonBundle` * Added the `extract_uinode_borders` system to the UI Render App. * Added the `calculate_borders_system` which calculates the border geometry for each node. * Added the UI example `borders` * Changed the button example to give the button a border. --- Cargo.toml | 10 +++ crates/bevy_ui/src/borders.rs | 99 ++++++++++++++++++++++ crates/bevy_ui/src/lib.rs | 12 ++- crates/bevy_ui/src/node_bundles.rs | 16 +++- crates/bevy_ui/src/render/mod.rs | 49 +++++++++++ crates/bevy_ui/src/ui_node.rs | 29 +++++++ examples/README.md | 1 + examples/ui/borders.rs | 132 +++++++++++++++++++++++++++++ examples/ui/button.rs | 14 ++- 9 files changed, 356 insertions(+), 6 deletions(-) create mode 100644 crates/bevy_ui/src/borders.rs create mode 100644 examples/ui/borders.rs diff --git a/Cargo.toml b/Cargo.toml index 0b1427689d214..b1c3435a259a2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1553,6 +1553,16 @@ category = "Transforms" wasm = true # UI (User Interface) +[[example]] +name = "borders" +path = "examples/ui/borders.rs" + +[package.metadata.example.borders] +name = "Borders" +description = "Demonstrates how to create a node with a border" +category = "UI (User Interface)" +wasm = true + [[example]] name = "button" path = "examples/ui/button.rs" diff --git a/crates/bevy_ui/src/borders.rs b/crates/bevy_ui/src/borders.rs new file mode 100644 index 0000000000000..6ee2e0fbcbea8 --- /dev/null +++ b/crates/bevy_ui/src/borders.rs @@ -0,0 +1,99 @@ +use bevy_ecs::prelude::Component; +use bevy_ecs::query::Changed; +use bevy_ecs::query::Or; +use bevy_ecs::query::With; +use bevy_ecs::reflect::ReflectComponent; +use bevy_ecs::system::Query; +use bevy_hierarchy::Children; +use bevy_hierarchy::Parent; +use bevy_math::Rect; +use bevy_math::Vec2; +use bevy_reflect::Reflect; + +use crate::Node; +use crate::Style; +use crate::Val; + +/// Stores the calculated border geometry +#[derive(Component, Copy, Clone, Debug, Reflect)] +#[reflect(Component)] +pub struct CalculatedBorder { + /// The four rects that make up the border + pub edges: [Option; 4], +} + +impl CalculatedBorder { + const DEFAULT: Self = Self { edges: [None; 4] }; +} + +impl Default for CalculatedBorder { + fn default() -> Self { + Self::DEFAULT + } +} + +fn resolve_thickness(value: Val, parent_width: f32) -> f32 { + match value { + Val::Px(px) => px, + Val::Percent(percent) => parent_width * percent / 100., + Val::Auto => 0., + Val::Undefined => 0., + } +} + +/// Generates the border geometry +pub fn calculate_borders_system( + parent_query: Query<&Node, With>, + mut border_query: Query< + (&Node, &Style, &mut CalculatedBorder, Option<&Parent>), + Or<(Changed, Changed