sim_logic/rendering/extraction/
text.rs1use sim_engine::{Color, Layer, LogicalScreenPosition};
4
5use crate::{
6 identity::{LogicEntity, WorldGeneration},
7 text::{ScreenTextVisual, TextFont, TextMetrics, TextRegistry},
8};
9
10use super::super::ExtractionError;
11
12#[derive(Debug, Clone, PartialEq)]
18pub struct ResolvedScreenText {
19 source: LogicEntity,
20 visual: ScreenTextVisual,
21}
22
23impl ResolvedScreenText {
24 pub const fn source(&self) -> LogicEntity {
26 self.source
27 }
28
29 pub fn font(&self) -> &TextFont {
31 self.visual.font()
32 }
33
34 pub fn text(&self) -> &str {
36 self.visual.text()
37 }
38
39 pub fn metrics(&self) -> TextMetrics {
41 self.visual.metrics()
42 }
43
44 pub fn position(&self) -> LogicalScreenPosition {
46 self.visual.position()
47 }
48
49 pub fn baseline_origin(&self) -> LogicalScreenPosition {
51 self.visual.baseline_origin()
52 }
53
54 pub fn tint(&self) -> Color {
56 self.visual.tint()
57 }
58
59 pub fn layer(&self) -> Layer {
61 self.visual.layer()
62 }
63
64 pub fn draw_order_depth(&self) -> f32 {
66 self.visual.draw_order_depth()
67 }
68
69 pub const fn visual(&self) -> &ScreenTextVisual {
71 &self.visual
72 }
73}
74
75#[derive(Debug, Clone, PartialEq)]
76pub(crate) struct ScreenTextSource {
77 entity: LogicEntity,
78 visual: ScreenTextVisual,
79 registered: bool,
80}
81
82impl ScreenTextSource {
83 pub(crate) fn new(
84 entity: LogicEntity,
85 visual: &ScreenTextVisual,
86 registry: &TextRegistry,
87 ) -> Self {
88 Self {
89 entity,
90 visual: visual.clone(),
91 registered: registry.contains(visual.font()),
92 }
93 }
94
95 pub(super) fn resolve(
96 self,
97 generation: WorldGeneration,
98 ) -> Result<ResolvedScreenText, ExtractionError> {
99 if self.entity.world_generation() != generation {
100 return Err(ExtractionError::ForeignEntity {
101 entity: self.entity,
102 expected: generation,
103 });
104 }
105 if !self.registered {
106 return Err(ExtractionError::UnregisteredTextFont {
107 entity: self.entity,
108 font: self.visual.font().clone(),
109 });
110 }
111 self.visual
112 .validate()
113 .map_err(|error| ExtractionError::InvalidScreenText {
114 entity: self.entity,
115 error,
116 })?;
117 Ok(ResolvedScreenText {
118 source: self.entity,
119 visual: self.visual,
120 })
121 }
122}