Skip to content

Commit 86ee8e4

Browse files
authored
Move UiImage from ui_node to the widget::image module (bevyengine#16084)
# Objective `UiImage` isn't just a general image component now, it's the defining component for the image widget so it belongs in the image widget's module.
1 parent d01db9b commit 86ee8e4

File tree

6 files changed

+133
-132
lines changed

6 files changed

+133
-132
lines changed

crates/bevy_ui/src/accessibility.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{
22
experimental::UiChildren,
33
prelude::{Button, Label},
4-
widget::TextUiReader,
5-
ComputedNode, UiImage,
4+
widget::{TextUiReader, UiImage},
5+
ComputedNode,
66
};
77
use bevy_a11y::{
88
accesskit::{NodeBuilder, Rect, Role},

crates/bevy_ui/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub use measurement::*;
4040
pub use render::*;
4141
pub use ui_material::*;
4242
pub use ui_node::*;
43-
use widget::UiImageSize;
43+
use widget::{UiImage, UiImageSize};
4444

4545
/// The UI prelude.
4646
///
@@ -58,7 +58,7 @@ pub mod prelude {
5858
node_bundles::*,
5959
ui_material::*,
6060
ui_node::*,
61-
widget::{Button, Label},
61+
widget::{Button, Label, UiImage},
6262
Interaction, MaterialNode, UiMaterialPlugin, UiScale,
6363
},
6464
// `bevy_sprite` re-exports for texture slicing

crates/bevy_ui/src/render/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ mod render_pass;
44
mod ui_material_pipeline;
55
pub mod ui_texture_slice_pipeline;
66

7+
use crate::widget::UiImage;
78
use crate::{
89
experimental::UiChildren, BackgroundColor, BorderColor, CalculatedClip, ComputedNode,
910
DefaultUiCamera, Outline, ResolvedBorderRadius, TargetCamera, UiAntiAlias, UiBoxShadowSamples,
10-
UiImage, UiScale,
11+
UiScale,
1112
};
1213
use bevy_app::prelude::*;
1314
use bevy_asset::{load_internal_asset, AssetEvent, AssetId, Assets, Handle};

crates/bevy_ui/src/render/ui_texture_slice_pipeline.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ use bevy_transform::prelude::GlobalTransform;
3030
use bevy_utils::HashMap;
3131
use binding_types::{sampler, texture_2d};
3232
use bytemuck::{Pod, Zeroable};
33+
use widget::UiImage;
3334

3435
pub const UI_SLICER_SHADER_HANDLE: Handle<Shader> = Handle::weak_from_u128(11156288772117983964);
3536

crates/bevy_ui/src/ui_node.rs

Lines changed: 2 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
use crate::{widget::UiImageSize, ContentSize, FocusPolicy, UiRect, Val};
2-
use bevy_asset::Handle;
1+
use crate::{ContentSize, FocusPolicy, UiRect, Val};
32
use bevy_color::Color;
43
use bevy_ecs::{prelude::*, system::SystemParam};
54
use bevy_math::{vec4, Rect, Vec2, Vec4Swizzles};
65
use bevy_reflect::prelude::*;
76
use bevy_render::{
87
camera::{Camera, RenderTarget},
9-
texture::{Image, TRANSPARENT_IMAGE_HANDLE},
108
view::Visibility,
119
};
12-
use bevy_sprite::{BorderRect, TextureAtlas};
10+
use bevy_sprite::BorderRect;
1311
use bevy_transform::components::Transform;
1412
use bevy_utils::warn_once;
1513
use bevy_window::{PrimaryWindow, WindowRef};
@@ -2040,124 +2038,6 @@ impl Outline {
20402038
}
20412039
}
20422040

2043-
/// The 2D texture displayed for this UI node
2044-
#[derive(Component, Clone, Debug, Reflect)]
2045-
#[reflect(Component, Default, Debug)]
2046-
#[require(Node, UiImageSize)]
2047-
pub struct UiImage {
2048-
/// The tint color used to draw the image.
2049-
///
2050-
/// This is multiplied by the color of each pixel in the image.
2051-
/// The field value defaults to solid white, which will pass the image through unmodified.
2052-
pub color: Color,
2053-
/// Handle to the texture.
2054-
///
2055-
/// This defaults to a [`TRANSPARENT_IMAGE_HANDLE`], which points to a fully transparent 1x1 texture.
2056-
pub image: Handle<Image>,
2057-
/// The (optional) texture atlas used to render the image
2058-
pub texture_atlas: Option<TextureAtlas>,
2059-
/// Whether the image should be flipped along its x-axis
2060-
pub flip_x: bool,
2061-
/// Whether the image should be flipped along its y-axis
2062-
pub flip_y: bool,
2063-
/// An optional rectangle representing the region of the image to render, instead of rendering
2064-
/// the full image. This is an easy one-off alternative to using a [`TextureAtlas`].
2065-
///
2066-
/// When used with a [`TextureAtlas`], the rect
2067-
/// is offset by the atlas's minimal (top-left) corner position.
2068-
pub rect: Option<Rect>,
2069-
}
2070-
2071-
impl Default for UiImage {
2072-
/// A transparent 1x1 image with a solid white tint.
2073-
///
2074-
/// # Warning
2075-
///
2076-
/// This will be invisible by default.
2077-
/// To set this to a visible image, you need to set the `texture` field to a valid image handle,
2078-
/// or use [`Handle<Image>`]'s default 1x1 solid white texture (as is done in [`UiImage::solid_color`]).
2079-
fn default() -> Self {
2080-
UiImage {
2081-
// This should be white because the tint is multiplied with the image,
2082-
// so if you set an actual image with default tint you'd want its original colors
2083-
color: Color::WHITE,
2084-
texture_atlas: None,
2085-
// This texture needs to be transparent by default, to avoid covering the background color
2086-
image: TRANSPARENT_IMAGE_HANDLE,
2087-
flip_x: false,
2088-
flip_y: false,
2089-
rect: None,
2090-
}
2091-
}
2092-
}
2093-
2094-
impl UiImage {
2095-
/// Create a new [`UiImage`] with the given texture.
2096-
pub fn new(texture: Handle<Image>) -> Self {
2097-
Self {
2098-
image: texture,
2099-
color: Color::WHITE,
2100-
..Default::default()
2101-
}
2102-
}
2103-
2104-
/// Create a solid color [`UiImage`].
2105-
///
2106-
/// This is primarily useful for debugging / mocking the extents of your image.
2107-
pub fn solid_color(color: Color) -> Self {
2108-
Self {
2109-
image: Handle::default(),
2110-
color,
2111-
flip_x: false,
2112-
flip_y: false,
2113-
texture_atlas: None,
2114-
rect: None,
2115-
}
2116-
}
2117-
2118-
/// Create a [`UiImage`] from an image, with an associated texture atlas
2119-
pub fn from_atlas_image(image: Handle<Image>, atlas: TextureAtlas) -> Self {
2120-
Self {
2121-
image,
2122-
texture_atlas: Some(atlas),
2123-
..Default::default()
2124-
}
2125-
}
2126-
2127-
/// Set the color tint
2128-
#[must_use]
2129-
pub const fn with_color(mut self, color: Color) -> Self {
2130-
self.color = color;
2131-
self
2132-
}
2133-
2134-
/// Flip the image along its x-axis
2135-
#[must_use]
2136-
pub const fn with_flip_x(mut self) -> Self {
2137-
self.flip_x = true;
2138-
self
2139-
}
2140-
2141-
/// Flip the image along its y-axis
2142-
#[must_use]
2143-
pub const fn with_flip_y(mut self) -> Self {
2144-
self.flip_y = true;
2145-
self
2146-
}
2147-
2148-
#[must_use]
2149-
pub const fn with_rect(mut self, rect: Rect) -> Self {
2150-
self.rect = Some(rect);
2151-
self
2152-
}
2153-
}
2154-
2155-
impl From<Handle<Image>> for UiImage {
2156-
fn from(texture: Handle<Image>) -> Self {
2157-
Self::new(texture)
2158-
}
2159-
}
2160-
21612041
/// The calculated clip of the node
21622042
#[derive(Component, Default, Copy, Clone, Debug, Reflect)]
21632043
#[reflect(Component, Default, Debug)]

crates/bevy_ui/src/widget/image.rs

Lines changed: 124 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,132 @@
1-
use crate::{ContentSize, Measure, MeasureArgs, Node, NodeMeasure, UiImage, UiScale};
2-
use bevy_asset::Assets;
1+
use crate::{ContentSize, Measure, MeasureArgs, Node, NodeMeasure, UiScale};
2+
use bevy_asset::{Assets, Handle};
3+
use bevy_color::Color;
34
use bevy_ecs::prelude::*;
4-
use bevy_math::{UVec2, Vec2};
5+
use bevy_math::{Rect, UVec2, Vec2};
56
use bevy_reflect::{std_traits::ReflectDefault, Reflect};
6-
use bevy_render::texture::Image;
7-
use bevy_sprite::TextureAtlasLayout;
7+
use bevy_render::texture::{Image, TRANSPARENT_IMAGE_HANDLE};
8+
use bevy_sprite::{TextureAtlas, TextureAtlasLayout};
89
use bevy_window::{PrimaryWindow, Window};
910
use taffy::{MaybeMath, MaybeResolve};
1011

12+
/// The 2D texture displayed for this UI node
13+
#[derive(Component, Clone, Debug, Reflect)]
14+
#[reflect(Component, Default, Debug)]
15+
#[require(Node, UiImageSize)]
16+
pub struct UiImage {
17+
/// The tint color used to draw the image.
18+
///
19+
/// This is multiplied by the color of each pixel in the image.
20+
/// The field value defaults to solid white, which will pass the image through unmodified.
21+
pub color: Color,
22+
/// Handle to the texture.
23+
///
24+
/// This defaults to a [`TRANSPARENT_IMAGE_HANDLE`], which points to a fully transparent 1x1 texture.
25+
pub image: Handle<Image>,
26+
/// The (optional) texture atlas used to render the image
27+
pub texture_atlas: Option<TextureAtlas>,
28+
/// Whether the image should be flipped along its x-axis
29+
pub flip_x: bool,
30+
/// Whether the image should be flipped along its y-axis
31+
pub flip_y: bool,
32+
/// An optional rectangle representing the region of the image to render, instead of rendering
33+
/// the full image. This is an easy one-off alternative to using a [`TextureAtlas`].
34+
///
35+
/// When used with a [`TextureAtlas`], the rect
36+
/// is offset by the atlas's minimal (top-left) corner position.
37+
pub rect: Option<Rect>,
38+
}
39+
40+
impl Default for UiImage {
41+
/// A transparent 1x1 image with a solid white tint.
42+
///
43+
/// # Warning
44+
///
45+
/// This will be invisible by default.
46+
/// To set this to a visible image, you need to set the `texture` field to a valid image handle,
47+
/// or use [`Handle<Image>`]'s default 1x1 solid white texture (as is done in [`UiImage::solid_color`]).
48+
fn default() -> Self {
49+
UiImage {
50+
// This should be white because the tint is multiplied with the image,
51+
// so if you set an actual image with default tint you'd want its original colors
52+
color: Color::WHITE,
53+
texture_atlas: None,
54+
// This texture needs to be transparent by default, to avoid covering the background color
55+
image: TRANSPARENT_IMAGE_HANDLE,
56+
flip_x: false,
57+
flip_y: false,
58+
rect: None,
59+
}
60+
}
61+
}
62+
63+
impl UiImage {
64+
/// Create a new [`UiImage`] with the given texture.
65+
pub fn new(texture: Handle<Image>) -> Self {
66+
Self {
67+
image: texture,
68+
color: Color::WHITE,
69+
..Default::default()
70+
}
71+
}
72+
73+
/// Create a solid color [`UiImage`].
74+
///
75+
/// This is primarily useful for debugging / mocking the extents of your image.
76+
pub fn solid_color(color: Color) -> Self {
77+
Self {
78+
image: Handle::default(),
79+
color,
80+
flip_x: false,
81+
flip_y: false,
82+
texture_atlas: None,
83+
rect: None,
84+
}
85+
}
86+
87+
/// Create a [`UiImage`] from an image, with an associated texture atlas
88+
pub fn from_atlas_image(image: Handle<Image>, atlas: TextureAtlas) -> Self {
89+
Self {
90+
image,
91+
texture_atlas: Some(atlas),
92+
..Default::default()
93+
}
94+
}
95+
96+
/// Set the color tint
97+
#[must_use]
98+
pub const fn with_color(mut self, color: Color) -> Self {
99+
self.color = color;
100+
self
101+
}
102+
103+
/// Flip the image along its x-axis
104+
#[must_use]
105+
pub const fn with_flip_x(mut self) -> Self {
106+
self.flip_x = true;
107+
self
108+
}
109+
110+
/// Flip the image along its y-axis
111+
#[must_use]
112+
pub const fn with_flip_y(mut self) -> Self {
113+
self.flip_y = true;
114+
self
115+
}
116+
117+
#[must_use]
118+
pub const fn with_rect(mut self, rect: Rect) -> Self {
119+
self.rect = Some(rect);
120+
self
121+
}
122+
}
123+
124+
impl From<Handle<Image>> for UiImage {
125+
fn from(texture: Handle<Image>) -> Self {
126+
Self::new(texture)
127+
}
128+
}
129+
11130
/// The size of the image's texture
12131
///
13132
/// This component is updated automatically by [`update_image_content_size_system`]

0 commit comments

Comments
 (0)