bevy/crates/bevy_ui/src/entity.rs

180 lines
5.9 KiB
Rust
Raw Normal View History

//! This module contains the bundles used in Bevy's UI
use crate::{
widget::{Button, ImageMode},
CalculatedSize, FocusPolicy, Interaction, Node, Style, UiColor, UiImage,
};
use bevy_ecs::{bundle::Bundle, prelude::Component};
2020-07-10 08:37:06 +00:00
use bevy_render::{
camera::{Camera, DepthCalculation, OrthographicProjection, WindowOrigin},
view::{Visibility, VisibleEntities},
2020-07-10 08:37:06 +00:00
};
use bevy_text::Text;
use bevy_transform::prelude::{GlobalTransform, Transform};
2020-04-06 21:20:53 +00:00
/// The basic UI node
#[derive(Bundle, Clone, Debug, Default)]
pub struct NodeBundle {
/// Describes the size of the node
2020-04-06 21:20:53 +00:00
pub node: Node,
/// Describes the style including flexbox settings
2020-07-26 19:27:09 +00:00
pub style: Style,
/// Describes the color of the node
pub color: UiColor,
/// Describes the image of the node
pub image: UiImage,
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The transform of the node
pub transform: Transform,
/// The global transform of the node
pub global_transform: GlobalTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
2020-04-06 21:20:53 +00:00
}
2020-05-03 19:35:07 +00:00
/// A UI node that is an image
#[derive(Bundle, Clone, Debug, Default)]
pub struct ImageBundle {
/// Describes the size of the node
pub node: Node,
/// Describes the style including flexbox settings
pub style: Style,
/// Configures how the image should scale
pub image_mode: ImageMode,
/// The calculated size based on the given image
pub calculated_size: CalculatedSize,
/// The color of the node
pub color: UiColor,
/// The image of the node
pub image: UiImage,
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The transform of the node
pub transform: Transform,
/// The global transform of the node
pub global_transform: GlobalTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
}
/// A UI node that is text
#[derive(Bundle, Clone, Debug)]
pub struct TextBundle {
/// Describes the size of the node
2020-05-18 01:09:29 +00:00
pub node: Node,
/// Describes the style including flexbox settings
2020-07-26 19:27:09 +00:00
pub style: Style,
/// Contains the text of the node
pub text: Text,
/// The calculated size based on the given image
pub calculated_size: CalculatedSize,
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The transform of the node
pub transform: Transform,
/// The global transform of the node
pub global_transform: GlobalTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
2020-05-18 01:09:29 +00:00
}
impl Default for TextBundle {
2020-05-18 01:09:29 +00:00
fn default() -> Self {
TextBundle {
focus_policy: FocusPolicy::Pass,
2020-07-26 19:27:09 +00:00
text: Default::default(),
node: Default::default(),
calculated_size: Default::default(),
2020-07-26 19:27:09 +00:00
style: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
visibility: Default::default(),
2020-05-18 01:09:29 +00:00
}
}
}
/// A UI node that is a button
#[derive(Bundle, Clone, Debug)]
pub struct ButtonBundle {
/// Describes the size of the node
pub node: Node,
/// Marker component that signals this node is a button
pub button: Button,
/// Describes the style including flexbox settings
2020-07-26 19:27:09 +00:00
pub style: Style,
/// Describes whether and how the button has been interacted with by the input
pub interaction: Interaction,
/// Whether this node should block interaction with lower nodes
pub focus_policy: FocusPolicy,
/// The color of the node
pub color: UiColor,
/// The image of the node
pub image: UiImage,
/// The transform of the node
pub transform: Transform,
/// The global transform of the node
pub global_transform: GlobalTransform,
/// Describes the visibility properties of the node
pub visibility: Visibility,
}
impl Default for ButtonBundle {
fn default() -> Self {
ButtonBundle {
button: Button,
interaction: Default::default(),
2020-07-26 19:27:09 +00:00
focus_policy: Default::default(),
node: Default::default(),
2020-07-26 19:27:09 +00:00
style: Default::default(),
color: Default::default(),
image: Default::default(),
transform: Default::default(),
global_transform: Default::default(),
visibility: Default::default(),
}
}
}
#[derive(Component, Default)]
pub struct CameraUi;
2020-07-25 06:04:45 +00:00
/// The camera that is needed to see UI elements
#[derive(Bundle, Debug)]
pub struct UiCameraBundle<M: Component> {
/// The camera component
2020-07-25 06:04:45 +00:00
pub camera: Camera,
/// The orthographic projection settings
2020-07-25 06:04:45 +00:00
pub orthographic_projection: OrthographicProjection,
/// The transform of the camera
2020-07-25 06:04:45 +00:00
pub transform: Transform,
/// The global transform of the camera
pub global_transform: GlobalTransform,
/// Contains visible entities
// FIXME there is no frustrum culling for UI
pub visible_entities: VisibleEntities,
pub marker: M,
2020-07-25 06:04:45 +00:00
}
impl Default for UiCameraBundle<CameraUi> {
2020-07-25 06:04:45 +00:00
fn default() -> Self {
// we want 0 to be "closest" and +far to be "farthest" in 2d, so we offset
// the camera's translation by far and use a right handed coordinate system
let far = 1000.0;
UiCameraBundle {
2020-07-25 06:04:45 +00:00
camera: Camera {
..Default::default()
},
orthographic_projection: OrthographicProjection {
far,
window_origin: WindowOrigin::BottomLeft,
depth_calculation: DepthCalculation::ZDifference,
2020-07-25 06:04:45 +00:00
..Default::default()
},
transform: Transform::from_xyz(0.0, 0.0, far - 0.1),
global_transform: Default::default(),
visible_entities: Default::default(),
marker: CameraUi,
2020-07-25 06:04:45 +00:00
}
}
}