1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use bevy::camera::visibility::RenderLayers;
use bevy::ecs::change_detection::Tick;
use bevy::ecs::system::{SystemAccess, SystemMeta, SystemParam, SystemParamValidationError};
use bevy::ecs::world::unsafe_world_cell::UnsafeWorldCell;
use bevy::platform::cell::SyncCell;
use bevy::prelude::*;
use crate::prelude::*;
use crate::render::ShapePipelineType;
/// Describes a configuration that can be applied to a spawned shape.
#[derive(Clone, Reflect)]
pub struct ShapeConfig {
/// Transform with which the shape will be spawned.
pub transform: Transform,
pub alignment: Alignment,
/// When in 3D, overrides the point in global space that is used to determine the draw order of spawned shapes.
///
/// Can be modified to ensure that complex 3D UIs layer properly at oblique camera angles.
///
/// Defaults to `None`.
pub origin: Option<Vec3>,
pub color: Color,
/// If true spawned shape will have a [`ShapeFill`] with [`FillType::Stroke`], taking into account thickness and thickness_type.
/// This doesn't use [`FillType`] directly in order to avoid unnecessary shuffling of thickness values when using both types of shape.
pub hollow: bool,
pub thickness: f32,
pub thickness_type: ThicknessType,
pub cap: Cap,
pub roundness: f32,
pub corner_radii: Vec4,
#[reflect(ignore)]
pub render_layers: Option<RenderLayers>,
pub alpha_mode: ShapeAlphaMode,
/// Forcibly disables local anti-aliasing.
pub disable_laa: bool,
/// [`Canvas`] to draw the shape to.
pub canvas: Option<Entity>,
/// Texture to apply to the shape, color is determined as color * sample.
pub texture: Option<Handle<Image>>,
/// Set with set_2d, set_3d and set_canvas.
pub pipeline: ShapePipelineType,
/// Indicates whether or not the config will be reset after a system is run
pub reset: bool,
}
impl ShapeConfig {
/// Helper method to modify the configs transform taking into account rotation and scale.
pub fn translate(&mut self, dir: Vec3) {
self.transform.translation += self.transform.rotation * dir * self.transform.scale;
}
/// Helper method to set the configs transform.
pub fn set_translation(&mut self, translation: Vec3) {
self.transform.translation = translation;
}
/// Helper method to rotate the configs transform by a given [`Quat`].
pub fn rotate(&mut self, quat: Quat) {
self.transform.rotation *= quat;
}
/// Helper method to set the configs rotation.
pub fn set_rotation(&mut self, rotation: Quat) {
self.transform.rotation = rotation;
}
/// Helper method to rotate the configs transform around the x axis.
pub fn rotate_x(&mut self, angle: f32) {
self.rotate(Quat::from_rotation_x(angle))
}
/// Helper method to rotate the configs transform around the y axis.
pub fn rotate_y(&mut self, angle: f32) {
self.rotate(Quat::from_rotation_y(angle))
}
/// Helper method to rotate the configs transform around the z axis.
pub fn rotate_z(&mut self, angle: f32) {
self.rotate(Quat::from_rotation_z(angle))
}
/// Helper method to scale the configs transform.
pub fn scale(&mut self, scale: Vec3) {
self.transform.scale *= scale;
}
/// Helper method to set the configs scale.
pub fn set_scale(&mut self, scale: Vec3) {
self.transform.scale = scale;
}
/// Helper method to change shape render target to a canvas.
///
/// Also sets pipeline to Shape2d.
pub fn set_canvas(&mut self, canvas: Entity) {
self.pipeline = ShapePipelineType::Shape2d;
self.canvas = Some(canvas);
}
/// Helper method to change the target pipeline to the 3d pipeline.
pub fn set_3d(&mut self) {
self.pipeline = ShapePipelineType::Shape3d;
}
/// Helper method to change the target pipeline to the 2d pipeline.
pub fn set_2d(&mut self) {
self.pipeline = ShapePipelineType::Shape2d;
}
/// Helper method to clone the config without it's transform, useful when parenting.
pub fn without_transform(&self) -> Self {
let mut config = self.clone();
config.transform = Transform::IDENTITY;
config
}
// Helper method to set color from any or bevy's color variants
pub fn set_color(&mut self, color: impl Into<Color>) {
self.color = color.into();
}
}
impl ShapeConfig {
/// Default [`ShapeConfig`] with target set to the 2D pipeline.
pub fn default_2d() -> Self {
Self {
transform: default(),
origin: None,
color: Color::WHITE,
thickness: 0.1,
thickness_type: default(),
alignment: default(),
hollow: false,
cap: default(),
roundness: default(),
corner_radii: default(),
render_layers: None,
alpha_mode: ShapeAlphaMode::Blend,
disable_laa: false,
canvas: None,
texture: None,
pipeline: ShapePipelineType::Shape2d,
reset: true,
}
}
}
impl ShapeConfig {
/// Default [`ShapeConfig`] with target set to the 3D pipeline.
pub fn default_3d() -> Self {
let mut config = Self::default_2d();
config.pipeline = ShapePipelineType::Shape3d;
config
}
}
impl FromWorld for ShapeConfig {
fn from_world(world: &mut World) -> Self {
let config = world.resource::<BaseShapeConfig>();
config.0.clone()
}
}
pub struct ShapeConfigState {
shape_config: SyncCell<ShapeConfig>,
resource_state: <Res<'static, BaseShapeConfig> as SystemParam>::State,
}
unsafe impl SystemParam for &mut ShapeConfig {
type State = ShapeConfigState;
type Item<'w, 's> = &'s mut ShapeConfig;
fn init_state(world: &mut World) -> Self::State {
ShapeConfigState {
shape_config: SyncCell::new(ShapeConfig::from_world(world)),
resource_state: Res::<BaseShapeConfig>::init_state(world),
}
}
fn init_access(
state: &Self::State,
system_meta: &mut SystemMeta,
system_access: &mut SystemAccess,
world: &mut World,
) {
Res::<BaseShapeConfig>::init_access(
&state.resource_state,
system_meta,
system_access,
world,
);
}
#[inline]
unsafe fn get_param<'w, 's>(
state: &'s mut Self::State,
_system_meta: &SystemMeta,
_world: UnsafeWorldCell<'w>,
_change_tick: Tick,
) -> Result<Self::Item<'w, 's>, SystemParamValidationError> {
Ok(state.shape_config.get())
}
fn apply(state: &mut Self::State, _system_meta: &SystemMeta, world: &mut World) {
let state = state.shape_config.get();
if state.reset {
*state = world.resource::<BaseShapeConfig>().0.clone();
}
}
}