use super::{owned_section::*, *};
use std::{borrow::Cow, f32, hash::*};
#[derive(Debug, Clone, PartialEq)]
pub struct VariedSection<'a> {
pub screen_position: (f32, f32),
pub bounds: (f32, f32),
pub z: f32,
pub layout: Layout<BuiltInLineBreaker>,
pub text: Vec<SectionText<'a>>,
}
impl Default for VariedSection<'static> {
#[inline]
fn default() -> Self {
Self {
screen_position: (0.0, 0.0),
bounds: (f32::INFINITY, f32::INFINITY),
z: 0.0,
layout: Layout::default(),
text: vec![],
}
}
}
impl<'a> From<VariedSection<'a>> for Cow<'a, VariedSection<'a>> {
fn from(owned: VariedSection<'a>) -> Self {
Cow::Owned(owned)
}
}
impl<'a, 'b> From<&'b VariedSection<'a>> for Cow<'b, VariedSection<'a>> {
fn from(owned: &'b VariedSection<'a>) -> Self {
Cow::Borrowed(owned)
}
}
impl Hash for VariedSection<'_> {
fn hash<H: Hasher>(&self, state: &mut H) {
use ordered_float::OrderedFloat;
let VariedSection {
screen_position: (screen_x, screen_y),
bounds: (bound_w, bound_h),
z,
layout,
ref text,
} = *self;
let ord_floats: &[OrderedFloat<_>] = &[
screen_x.into(),
screen_y.into(),
bound_w.into(),
bound_h.into(),
z.into(),
];
layout.hash(state);
for t in text {
let SectionText {
text,
scale,
color,
font_id,
} = *t;
let ord_floats: &[OrderedFloat<_>] = &[
scale.x.into(),
scale.y.into(),
color[0].into(),
color[1].into(),
color[2].into(),
color[3].into(),
];
(text, font_id, ord_floats).hash(state);
}
ord_floats.hash(state);
}
}
impl VariedSection<'_> {
pub fn to_owned(&self) -> OwnedVariedSection {
OwnedVariedSection {
screen_position: self.screen_position,
bounds: self.bounds,
z: self.z,
layout: self.layout,
text: self.text.iter().map(OwnedSectionText::from).collect(),
}
}
}
impl From<&VariedSection<'_>> for SectionGeometry {
fn from(section: &VariedSection<'_>) -> Self {
Self {
bounds: section.bounds,
screen_position: section.screen_position,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Section<'a> {
pub text: &'a str,
pub screen_position: (f32, f32),
pub bounds: (f32, f32),
pub scale: Scale,
pub color: [f32; 4],
pub z: f32,
pub layout: Layout<BuiltInLineBreaker>,
pub font_id: FontId,
}
impl Default for Section<'static> {
#[inline]
fn default() -> Self {
Self {
text: "",
screen_position: (0.0, 0.0),
bounds: (f32::INFINITY, f32::INFINITY),
scale: Scale::uniform(16.0),
color: [0.0, 0.0, 0.0, 1.0],
z: 0.0,
layout: Layout::default(),
font_id: FontId::default(),
}
}
}
impl<'a> From<&Section<'a>> for VariedSection<'a> {
fn from(s: &Section<'a>) -> Self {
let Section {
text,
scale,
color,
screen_position,
bounds,
z,
layout,
font_id,
} = *s;
VariedSection {
text: vec![SectionText {
text,
scale,
color,
font_id,
}],
screen_position,
bounds,
z,
layout,
}
}
}
impl<'a> From<Section<'a>> for VariedSection<'a> {
fn from(s: Section<'a>) -> Self {
VariedSection::from(&s)
}
}
impl<'a> From<Section<'a>> for Cow<'a, VariedSection<'a>> {
fn from(section: Section<'a>) -> Self {
Cow::Owned(VariedSection::from(section))
}
}
impl<'a> From<&Section<'a>> for Cow<'a, VariedSection<'a>> {
fn from(section: &Section<'a>) -> Self {
Cow::Owned(VariedSection::from(section))
}
}