use super::anchor::{
Alignment, AnchorX, AnchorY, OverlayAnchoring, OverlayOrigin, resolve_anchor_origin_at,
};
use super::animation::OverlayAnimations;
use super::clip::OverlayClip;
use super::transform::OverlayTransform;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct LabelItem {
pub anchoring: OverlayAnchoring,
pub transform: OverlayTransform,
pub style: crate::overlay::OverlayStyle,
pub animations: Option<Box<OverlayAnimations>>,
pub clip: OverlayClip,
pub text: String,
pub text_style: crate::overlay::TextStyle,
pub max_width: Option<f32>,
pub z_order: i32,
}
impl Default for LabelItem {
fn default() -> Self {
Self {
anchoring: crate::overlay::OverlayAnchoring::default().with_align(
crate::overlay::Alignment::new(AnchorX::Left, AnchorY::Middle),
),
text: String::new(),
text_style: crate::overlay::TextStyle::default(),
transform: OverlayTransform::IDENTITY,
style: crate::overlay::OverlayStyle::default(),
animations: None,
clip: OverlayClip::default(),
max_width: None,
z_order: 0,
}
}
}
impl LabelItem {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
..Default::default()
}
}
pub fn with_anchor(mut self, anchor: OverlayOrigin) -> Self {
self.anchoring.origin = anchor;
self
}
pub fn with_world_anchor(mut self, pos: impl crate::scene::IntoWorldPosition) -> Self {
self.anchoring.origin = OverlayOrigin::World(pos.into_world_position());
self
}
pub fn with_screen_anchor(mut self, pos: [f32; 2]) -> Self {
self.anchoring.origin =
OverlayOrigin::Viewport(Alignment::new(AnchorX::Left, AnchorY::Top));
self.transform.translate = pos;
self
}
pub fn with_fill(mut self, fill: crate::overlay::OverlayFill) -> Self {
self.style.fill = fill;
self
}
pub fn with_colour(mut self, colour: impl Into<crate::colour::Colour>) -> Self {
self.style.fill = crate::overlay::OverlayFill::Solid(colour.into());
self
}
pub fn with_style(mut self, style: crate::overlay::OverlayStyle) -> Self {
self.style = style;
self
}
pub fn with_inner_shadows(mut self, shadows: Vec<crate::overlay::ShadowLayer>) -> Self {
self.style.inner_shadows = shadows;
self
}
pub fn with_inner_shadow(mut self, shadow: crate::overlay::ShadowLayer) -> Self {
self.style.inner_shadows.push(shadow);
self
}
pub fn with_text_style(mut self, text_style: crate::overlay::TextStyle) -> Self {
self.text_style = text_style;
self
}
pub fn with_font_size(mut self, font_size: f32) -> Self {
self.text_style.size = font_size;
self
}
pub fn with_font(mut self, font: crate::overlay::font::FontHandle) -> Self {
self.text_style.font = Some(font);
self
}
pub fn with_align_x(mut self, align_x: AnchorX) -> Self {
self.anchoring.align.x = align_x;
self
}
pub fn with_align_y(mut self, align_y: AnchorY) -> Self {
self.anchoring.align.y = align_y;
self
}
pub fn with_align(mut self, align: Alignment) -> Self {
self.anchoring.align = align;
self
}
pub fn resolve_top_left(
&self,
size: [f32; 2],
ascent: f32,
viewport_size: [f32; 2],
view: &glam::Mat4,
proj: &glam::Mat4,
) -> Option<[f32; 2]> {
self.resolve_top_left_at(size, ascent, viewport_size, view, proj, glam::DVec3::ZERO)
}
pub fn resolve_top_left_at(
&self,
size: [f32; 2],
ascent: f32,
viewport_size: [f32; 2],
view: &glam::Mat4,
proj: &glam::Mat4,
world_origin: glam::DVec3,
) -> Option<[f32; 2]> {
let origin = resolve_anchor_origin_at(
&self.anchoring.origin,
viewport_size,
view,
proj,
world_origin,
)?;
Some([
origin[0] + self.transform.translate[0] + self.anchoring.align.x.align_shift(size[0]),
origin[1]
+ self.transform.translate[1]
+ self.anchoring.align.y.align_shift(size[1], Some(ascent)),
])
}
pub fn with_position(mut self, position: [f32; 2]) -> Self {
self.transform.translate = position;
self
}
pub fn with_opacity(mut self, opacity: f32) -> Self {
self.style.opacity = opacity;
self
}
pub fn with_max_width(mut self, max_width: f32) -> Self {
self.max_width = Some(max_width);
self
}
pub fn with_z_order(mut self, z_order: i32) -> Self {
self.z_order = z_order;
self
}
pub fn with_clip(mut self, clip_id: u32) -> Self {
self.clip.mask = Some(clip_id);
self
}
pub fn with_shadows(mut self, shadows: Vec<crate::overlay::ShadowLayer>) -> Self {
self.style.shadows = shadows;
self
}
pub fn with_shadow(mut self, shadow: crate::overlay::ShadowLayer) -> Self {
self.style.shadows.push(shadow);
self
}
pub fn with_outline(
mut self,
colour: impl Into<crate::colour::Colour>,
width: f32,
mode: crate::overlay::OutlineMode,
) -> Self {
if width <= 0.0 {
return self;
}
let colour = colour.into();
let band = |spread: f32| {
crate::overlay::ShadowLayer::new(colour, 0.0, [0.0, 0.0]).with_spread(spread)
};
match mode {
crate::overlay::OutlineMode::Inset => self.style.inner_shadows.push(band(width)),
crate::overlay::OutlineMode::Outer => self.style.shadows.push(band(width)),
crate::overlay::OutlineMode::Centre => {
self.style.inner_shadows.push(band(width * 0.5));
self.style.shadows.push(band(width * 0.5));
}
}
self
}
pub fn with_rotation(mut self, radians: f32) -> Self {
self.transform.rotation = radians;
self
}
pub fn with_rotation_pivot(mut self, pivot: [f32; 2]) -> Self {
self.transform.pivot = pivot;
self
}
pub fn with_transform(mut self, transform: OverlayTransform) -> Self {
self.transform = transform;
self
}
pub fn with_scale(mut self, scale: f32) -> Self {
self.transform.scale = scale;
self
}
pub fn with_tint(mut self, tint: [f32; 4]) -> Self {
self.style.tint = tint;
self
}
pub fn with_animations(mut self, animations: OverlayAnimations) -> Self {
self.animations = Some(Box::new(animations));
self
}
pub fn with_clip_rect(mut self, clip_rect: [f32; 4]) -> Self {
self.clip.rect = Some(clip_rect);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn resolve_top_left_default_is_the_position() {
let label = LabelItem::new("hello")
.with_screen_anchor([40.0, 25.0])
.with_align_x(AnchorX::Left)
.with_align_y(AnchorY::Top);
let tl = label
.resolve_top_left(
[60.0, 14.0],
11.0,
[800.0, 600.0],
&glam::Mat4::IDENTITY,
&glam::Mat4::IDENTITY,
)
.unwrap();
assert_eq!(tl, [40.0, 25.0]);
}
#[test]
fn resolve_top_left_shifts_by_alignment() {
let base = LabelItem::new("hello").with_anchor(OverlayOrigin::Viewport(Alignment::new(
AnchorX::Right,
AnchorY::Bottom,
)));
let size = [60.0, 14.0];
let right = base
.clone()
.with_align_x(AnchorX::Right)
.with_align_y(AnchorY::Bottom)
.resolve_top_left(
size,
11.0,
[800.0, 600.0],
&glam::Mat4::IDENTITY,
&glam::Mat4::IDENTITY,
)
.unwrap();
assert_eq!(right, [800.0 - 60.0, 600.0 - 14.0]);
let centred = base
.with_align_x(AnchorX::Middle)
.with_align_y(AnchorY::Middle)
.resolve_top_left(
size,
11.0,
[800.0, 600.0],
&glam::Mat4::IDENTITY,
&glam::Mat4::IDENTITY,
)
.unwrap();
assert_eq!(centred, [800.0 - 30.0, 600.0 - 7.0]);
}
#[test]
fn resolve_top_left_puts_the_baseline_on_the_origin() {
let label = LabelItem::new("two\nlines")
.with_screen_anchor([100.0, 200.0])
.with_align_y(AnchorY::Baseline);
let tl = label
.resolve_top_left(
[60.0, 30.0],
11.0,
[800.0, 600.0],
&glam::Mat4::IDENTITY,
&glam::Mat4::IDENTITY,
)
.unwrap();
assert_eq!(tl[1], 200.0 - 11.0);
}
#[test]
fn resolve_top_left_culls_behind_the_camera() {
let label = LabelItem::new("hello").with_world_anchor([0.0, 0.0, 10.0]);
let behind = glam::Mat4::from_cols_array(&[
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, 0.0, 0.0, 0.0, 0.0,
]);
assert!(
label
.resolve_top_left(
[60.0, 14.0],
11.0,
[800.0, 600.0],
&glam::Mat4::IDENTITY,
&behind
)
.is_none()
);
}
}