2022-01-07 22:20:34 +00:00
|
|
|
//! This module contains the bundles used in Bevy's UI
|
|
|
|
|
2020-07-18 21:08:46 +00:00
|
|
|
use crate::{
|
2021-12-14 03:58:23 +00:00
|
|
|
widget::{Button, ImageMode},
|
2022-03-12 00:41:06 +00:00
|
|
|
CalculatedSize, FocusPolicy, Interaction, Node, Style, UiColor, UiImage,
|
2020-07-18 21:08:46 +00:00
|
|
|
};
|
2022-03-12 00:41:06 +00:00
|
|
|
use bevy_ecs::{bundle::Bundle, prelude::Component};
|
2020-07-10 08:37:06 +00:00
|
|
|
use bevy_render::{
|
2021-12-14 03:58:23 +00:00
|
|
|
camera::{Camera, DepthCalculation, OrthographicProjection, WindowOrigin},
|
2021-12-24 07:10:12 +00:00
|
|
|
view::{Visibility, VisibleEntities},
|
2020-07-10 08:37:06 +00:00
|
|
|
};
|
2021-03-10 22:37:02 +00:00
|
|
|
use bevy_text::Text;
|
2020-09-14 21:00:32 +00:00
|
|
|
use bevy_transform::prelude::{GlobalTransform, Transform};
|
2020-04-06 21:20:53 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The basic UI node
|
2021-12-14 03:58:23 +00:00
|
|
|
#[derive(Bundle, Clone, Debug, Default)]
|
2020-11-16 04:32:23 +00:00
|
|
|
pub struct NodeBundle {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the size of the node
|
2020-04-06 21:20:53 +00:00
|
|
|
pub node: Node,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the style including flexbox settings
|
2020-07-26 19:27:09 +00:00
|
|
|
pub style: Style,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the color of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
pub color: UiColor,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the image of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
pub image: UiImage,
|
2022-02-15 22:31:51 +00:00
|
|
|
/// Whether this node should block interaction with lower nodes
|
|
|
|
pub focus_policy: FocusPolicy,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The transform of the node
|
2020-06-25 17:13:00 +00:00
|
|
|
pub transform: Transform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The global transform of the node
|
2020-09-14 21:00:32 +00:00
|
|
|
pub global_transform: GlobalTransform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the visibility properties of the node
|
2021-12-24 07:10:12 +00:00
|
|
|
pub visibility: Visibility,
|
2020-04-06 21:20:53 +00:00
|
|
|
}
|
2020-05-03 19:35:07 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// A UI node that is an image
|
2021-12-14 03:58:23 +00:00
|
|
|
#[derive(Bundle, Clone, Debug, Default)]
|
2020-11-16 04:32:23 +00:00
|
|
|
pub struct ImageBundle {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the size of the node
|
2020-07-28 07:37:25 +00:00
|
|
|
pub node: Node,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the style including flexbox settings
|
2020-07-28 07:37:25 +00:00
|
|
|
pub style: Style,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Configures how the image should scale
|
2021-12-14 03:58:23 +00:00
|
|
|
pub image_mode: ImageMode,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The calculated size based on the given image
|
2020-07-28 07:37:25 +00:00
|
|
|
pub calculated_size: CalculatedSize,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The color of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
pub color: UiColor,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The image of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
pub image: UiImage,
|
2022-02-15 22:31:51 +00:00
|
|
|
/// Whether this node should block interaction with lower nodes
|
|
|
|
pub focus_policy: FocusPolicy,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The transform of the node
|
2020-07-28 07:37:25 +00:00
|
|
|
pub transform: Transform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The global transform of the node
|
2020-09-14 21:00:32 +00:00
|
|
|
pub global_transform: GlobalTransform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the visibility properties of the node
|
2021-12-24 07:10:12 +00:00
|
|
|
pub visibility: Visibility,
|
2020-07-28 07:37:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// A UI node that is text
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Bundle, Clone, Debug)]
|
2020-11-16 04:32:23 +00:00
|
|
|
pub struct TextBundle {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the size of the node
|
2020-05-18 01:09:29 +00:00
|
|
|
pub node: Node,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the style including flexbox settings
|
2020-07-26 19:27:09 +00:00
|
|
|
pub style: Style,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Contains the text of the node
|
2020-07-20 03:33:55 +00:00
|
|
|
pub text: Text,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The calculated size based on the given image
|
2020-07-28 04:04:04 +00:00
|
|
|
pub calculated_size: CalculatedSize,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Whether this node should block interaction with lower nodes
|
2020-07-19 00:03:37 +00:00
|
|
|
pub focus_policy: FocusPolicy,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The transform of the node
|
2020-06-25 17:13:00 +00:00
|
|
|
pub transform: Transform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The global transform of the node
|
2020-09-14 21:00:32 +00:00
|
|
|
pub global_transform: GlobalTransform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the visibility properties of the node
|
2021-12-24 07:10:12 +00:00
|
|
|
pub visibility: Visibility,
|
2020-05-18 01:09:29 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 04:32:23 +00:00
|
|
|
impl Default for TextBundle {
|
2020-05-18 01:09:29 +00:00
|
|
|
fn default() -> Self {
|
2020-11-16 04:32:23 +00:00
|
|
|
TextBundle {
|
2020-07-19 00:03:37 +00:00
|
|
|
focus_policy: FocusPolicy::Pass,
|
2020-07-26 19:27:09 +00:00
|
|
|
text: Default::default(),
|
|
|
|
node: Default::default(),
|
2020-07-28 04:04:04 +00:00
|
|
|
calculated_size: Default::default(),
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Default::default(),
|
2020-06-25 17:13:00 +00:00
|
|
|
transform: Default::default(),
|
2020-09-14 21:00:32 +00:00
|
|
|
global_transform: Default::default(),
|
2021-12-24 07:10:12 +00:00
|
|
|
visibility: Default::default(),
|
2020-05-18 01:09:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-18 21:08:46 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// A UI node that is a button
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Bundle, Clone, Debug)]
|
2020-11-16 04:32:23 +00:00
|
|
|
pub struct ButtonBundle {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the size of the node
|
2020-07-18 21:08:46 +00:00
|
|
|
pub node: Node,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Marker component that signals this node is a button
|
2020-07-18 21:08:46 +00:00
|
|
|
pub button: Button,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the style including flexbox settings
|
2020-07-26 19:27:09 +00:00
|
|
|
pub style: Style,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes whether and how the button has been interacted with by the input
|
2020-07-28 08:20:19 +00:00
|
|
|
pub interaction: Interaction,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Whether this node should block interaction with lower nodes
|
2020-07-18 21:08:46 +00:00
|
|
|
pub focus_policy: FocusPolicy,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The color of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
pub color: UiColor,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The image of the node
|
2021-12-14 03:58:23 +00:00
|
|
|
pub image: UiImage,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The transform of the node
|
2020-07-18 21:08:46 +00:00
|
|
|
pub transform: Transform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The global transform of the node
|
2020-09-14 21:00:32 +00:00
|
|
|
pub global_transform: GlobalTransform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Describes the visibility properties of the node
|
2021-12-24 07:10:12 +00:00
|
|
|
pub visibility: Visibility,
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 04:32:23 +00:00
|
|
|
impl Default for ButtonBundle {
|
2020-07-18 21:08:46 +00:00
|
|
|
fn default() -> Self {
|
2020-11-16 04:32:23 +00:00
|
|
|
ButtonBundle {
|
2020-07-18 21:08:46 +00:00
|
|
|
button: Button,
|
2020-07-28 08:20:19 +00:00
|
|
|
interaction: Default::default(),
|
2020-07-26 19:27:09 +00:00
|
|
|
focus_policy: Default::default(),
|
2020-07-18 21:08:46 +00:00
|
|
|
node: Default::default(),
|
2020-07-26 19:27:09 +00:00
|
|
|
style: Default::default(),
|
2021-12-14 03:58:23 +00:00
|
|
|
color: Default::default(),
|
|
|
|
image: Default::default(),
|
2020-07-18 21:08:46 +00:00
|
|
|
transform: Default::default(),
|
2020-09-14 21:00:32 +00:00
|
|
|
global_transform: Default::default(),
|
2021-12-24 07:10:12 +00:00
|
|
|
visibility: Default::default(),
|
2020-07-18 21:08:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-12 00:41:06 +00:00
|
|
|
#[derive(Component, Default)]
|
|
|
|
pub struct CameraUi;
|
2020-07-25 06:04:45 +00:00
|
|
|
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The camera that is needed to see UI elements
|
2020-10-08 18:43:01 +00:00
|
|
|
#[derive(Bundle, Debug)]
|
2022-03-12 00:41:06 +00:00
|
|
|
pub struct UiCameraBundle<M: Component> {
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The camera component
|
2020-07-25 06:04:45 +00:00
|
|
|
pub camera: Camera,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The orthographic projection settings
|
2020-07-25 06:04:45 +00:00
|
|
|
pub orthographic_projection: OrthographicProjection,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The transform of the camera
|
2020-07-25 06:04:45 +00:00
|
|
|
pub transform: Transform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// The global transform of the camera
|
2020-09-14 21:00:32 +00:00
|
|
|
pub global_transform: GlobalTransform,
|
2022-01-07 22:20:34 +00:00
|
|
|
/// Contains visible entities
|
2021-12-14 03:58:23 +00:00
|
|
|
// FIXME there is no frustrum culling for UI
|
|
|
|
pub visible_entities: VisibleEntities,
|
2022-03-12 00:41:06 +00:00
|
|
|
pub marker: M,
|
2020-07-25 06:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 00:41:06 +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;
|
2021-01-30 10:31:03 +00:00
|
|
|
UiCameraBundle {
|
2020-07-25 06:04:45 +00:00
|
|
|
camera: Camera {
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
orthographic_projection: OrthographicProjection {
|
|
|
|
far,
|
|
|
|
window_origin: WindowOrigin::BottomLeft,
|
2021-02-01 00:22:06 +00:00
|
|
|
depth_calculation: DepthCalculation::ZDifference,
|
2020-07-25 06:04:45 +00:00
|
|
|
..Default::default()
|
|
|
|
},
|
2021-01-07 01:17:06 +00:00
|
|
|
transform: Transform::from_xyz(0.0, 0.0, far - 0.1),
|
2020-09-14 21:00:32 +00:00
|
|
|
global_transform: Default::default(),
|
2021-12-14 03:58:23 +00:00
|
|
|
visible_entities: Default::default(),
|
2022-03-12 00:41:06 +00:00
|
|
|
marker: CameraUi,
|
2020-07-25 06:04:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|