use sim_engine::{Color, Layer, LogicalScreenPosition};
use crate::{
identity::{LogicEntity, WorldGeneration},
text::{ScreenTextVisual, TextFont, TextMetrics, TextRegistry},
};
use super::super::ExtractionError;
#[derive(Debug, Clone, PartialEq)]
pub struct ResolvedScreenText {
source: LogicEntity,
visual: ScreenTextVisual,
}
impl ResolvedScreenText {
pub const fn source(&self) -> LogicEntity {
self.source
}
pub fn font(&self) -> &TextFont {
self.visual.font()
}
pub fn text(&self) -> &str {
self.visual.text()
}
pub fn metrics(&self) -> TextMetrics {
self.visual.metrics()
}
pub fn position(&self) -> LogicalScreenPosition {
self.visual.position()
}
pub fn baseline_origin(&self) -> LogicalScreenPosition {
self.visual.baseline_origin()
}
pub fn tint(&self) -> Color {
self.visual.tint()
}
pub fn layer(&self) -> Layer {
self.visual.layer()
}
pub fn draw_order_depth(&self) -> f32 {
self.visual.draw_order_depth()
}
pub const fn visual(&self) -> &ScreenTextVisual {
&self.visual
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ScreenTextSource {
entity: LogicEntity,
visual: ScreenTextVisual,
registered: bool,
}
impl ScreenTextSource {
pub(crate) fn new(
entity: LogicEntity,
visual: &ScreenTextVisual,
registry: &TextRegistry,
) -> Self {
Self {
entity,
visual: visual.clone(),
registered: registry.contains(visual.font()),
}
}
pub(super) fn resolve(
self,
generation: WorldGeneration,
) -> Result<ResolvedScreenText, ExtractionError> {
if self.entity.world_generation() != generation {
return Err(ExtractionError::ForeignEntity {
entity: self.entity,
expected: generation,
});
}
if !self.registered {
return Err(ExtractionError::UnregisteredTextFont {
entity: self.entity,
font: self.visual.font().clone(),
});
}
self.visual
.validate()
.map_err(|error| ExtractionError::InvalidScreenText {
entity: self.entity,
error,
})?;
Ok(ResolvedScreenText {
source: self.entity,
visual: self.visual,
})
}
}