Skip to main content

wde_ui/
node.rs

1//! Components describing UI nodes: their layout style and how they are drawn.
2use bevy::prelude::*;
3use wde_renderer::prelude::{Texture, WdeColor};
4
5use crate::style::UiStyle;
6
7/// A UI node, laid out by taffy from its [`UiStyle`]. Its UI children are the entity's
8/// [`Children`] that are also [`UiNode`]s. A node without a [`UiNode`] parent is a root, laid out
9/// against the whole window.
10#[derive(Component, Clone, Default, Debug)]
11#[require(ComputedUiNode)]
12pub struct UiNode(pub UiStyle);
13
14/// Layout result of a [`UiNode`], in logical window pixels (origin at the top-left corner).
15#[derive(Component, Clone, Copy, Default, Debug, PartialEq)]
16pub struct ComputedUiNode {
17    /// Top-left corner of the node.
18    pub position: Vec2,
19    pub size: Vec2,
20    /// Border widths: left, top, right, bottom.
21    pub border: Vec4,
22    /// Padding: left, top, right, bottom.
23    pub padding: Vec4,
24    /// Draw order: nodes with a higher order are drawn on top.
25    pub order: u32
26}
27impl ComputedUiNode {
28    pub fn rect(&self) -> Rect {
29        Rect::from_corners(self.position, self.position + self.size)
30    }
31}
32
33/// Fill color of a [`UiNode`]. Nodes without one (nor a [`UiImage`]) are invisible, and let the
34/// pointer through.
35#[derive(Component, Clone, Copy, Debug)]
36pub struct UiBackground(pub WdeColor);
37
38/// Color of the border, whose widths are set by [`UiStyle::border`].
39#[derive(Component, Clone, Copy, Debug)]
40pub struct UiBorderColor(pub WdeColor);
41
42/// Radius of the node corners, in logical pixels.
43#[derive(Component, Clone, Copy, Default, Debug)]
44pub struct UiCornerRadius(pub f32);
45
46/// Image stretched over the whole node, in place of its [`UiBackground`]. The texture holds sRGB
47/// data, as loaded by default.
48#[derive(Component, Clone, Debug)]
49pub struct UiImage {
50    pub texture: Handle<Texture>,
51    /// Multiplies the image colors.
52    pub tint: WdeColor
53}
54impl UiImage {
55    pub fn new(texture: Handle<Texture>) -> Self {
56        Self {
57            texture,
58            tint: WdeColor::WHITE
59        }
60    }
61    pub fn with_tint(self, tint: WdeColor) -> Self {
62        Self { tint, ..self }
63    }
64}