fyrox_ui/formatted_text/
run.rs1use crate::core::type_traits::prelude::*;
22use crate::font::FontHeight;
23use std::ops::{Deref, DerefMut};
24
25use super::*;
26
27#[deprecated]
28pub type RunBuilder = Run;
29
30#[derive(Clone, PartialEq, Debug, Default, Reflect)]
31pub struct RunSet(Vec<Run>);
32
33impl From<&[Run]> for RunSet {
34 fn from(value: &[Run]) -> Self {
35 Self(value.to_vec())
36 }
37}
38
39impl From<Vec<Run>> for RunSet {
40 fn from(value: Vec<Run>) -> Self {
41 Self(value)
42 }
43}
44
45impl Visit for RunSet {
46 fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
47 self.0.visit(name, visitor)
48 }
49}
50
51impl Deref for RunSet {
52 type Target = Vec<Run>;
53
54 fn deref(&self) -> &Self::Target {
55 &self.0
56 }
57}
58
59impl DerefMut for RunSet {
60 fn deref_mut(&mut self) -> &mut Self::Target {
61 &mut self.0
62 }
63}
64
65impl RunSet {
66 pub fn font_at(&self, index: usize) -> Option<FontResource> {
67 for run in self.0.iter().rev() {
68 if run.range.contains(&(index as u32)) && run.font().is_some() {
69 return Some(run.font().unwrap().clone());
70 }
71 }
72 None
73 }
74 pub fn font_size_at(&self, index: usize) -> Option<f32> {
75 for run in self.0.iter().rev() {
76 if run.range.contains(&(index as u32)) && run.font_size().is_some() {
77 return Some(run.font_size().unwrap());
78 }
79 }
80 None
81 }
82 pub fn brush_at(&self, index: usize) -> Option<Brush> {
83 for run in self.0.iter().rev() {
84 if run.range.contains(&(index as u32)) && run.brush().is_some() {
85 return Some(run.brush().unwrap().clone());
86 }
87 }
88 None
89 }
90 pub fn shadow_at(&self, index: usize) -> Option<bool> {
91 for run in self.0.iter().rev() {
92 if run.range.contains(&(index as u32)) && run.shadow().is_some() {
93 return Some(run.shadow().unwrap());
94 }
95 }
96 None
97 }
98 pub fn shadow_brush_at(&self, index: usize) -> Option<Brush> {
99 for run in self.0.iter().rev() {
100 if run.range.contains(&(index as u32)) && run.shadow_brush().is_some() {
101 return Some(run.shadow_brush().unwrap().clone());
102 }
103 }
104 None
105 }
106 pub fn shadow_dilation_at(&self, index: usize) -> Option<f32> {
107 for run in self.0.iter().rev() {
108 if run.range.contains(&(index as u32)) && run.shadow_dilation().is_some() {
109 return Some(run.shadow_dilation().unwrap());
110 }
111 }
112 None
113 }
114 pub fn shadow_offset_at(&self, index: usize) -> Option<Vector2<f32>> {
115 for run in self.0.iter().rev() {
116 if run.range.contains(&(index as u32)) && run.shadow_offset().is_some() {
117 return Some(run.shadow_offset().unwrap());
118 }
119 }
120 None
121 }
122}
123
124#[derive(Clone, PartialEq, Debug, Default, Visit, Reflect, TypeUuidProvider)]
126#[type_uuid(id = "f0e5cc5d-0b82-4d6f-a505-12f890ffe7ea")]
127pub struct Run {
128 pub range: Range<u32>,
130 font: Option<FontResource>,
131 brush: Option<Brush>,
132 font_size: Option<f32>,
133 shadow: Option<bool>,
134 shadow_brush: Option<Brush>,
135 shadow_dilation: Option<f32>,
136 shadow_offset: Option<Vector2<f32>>,
137}
138
139impl Run {
140 pub fn new(range: Range<u32>) -> Self {
141 Self {
142 range,
143 font: None,
144 brush: None,
145 font_size: None,
146 shadow: None,
147 shadow_brush: None,
148 shadow_dilation: None,
149 shadow_offset: None,
150 }
151 }
152 pub fn font(&self) -> Option<&FontResource> {
154 self.font.as_ref()
155 }
156 pub fn brush(&self) -> Option<&Brush> {
158 self.brush.as_ref()
159 }
160 pub fn font_size(&self) -> Option<f32> {
162 self.font_size
163 }
164 pub fn shadow(&self) -> Option<bool> {
167 self.shadow
168 }
169 pub fn shadow_brush(&self) -> Option<&Brush> {
171 self.shadow_brush.as_ref()
172 }
173 pub fn shadow_dilation(&self) -> Option<f32> {
175 self.shadow_dilation
176 }
177 pub fn shadow_offset(&self) -> Option<Vector2<f32>> {
179 self.shadow_offset
180 }
181 #[deprecated]
182 pub fn build(self) -> Self {
183 self
184 }
185 pub fn with_font(mut self, font: FontResource) -> Self {
187 self.font = Some(font);
188 self
189 }
190 pub fn with_brush(mut self, brush: Brush) -> Self {
192 self.brush = Some(brush);
193 self
194 }
195 pub fn with_size(mut self, size: f32) -> Self {
197 self.font_size = Some(size);
198 self
199 }
200 pub fn with_shadow(mut self, shadow: bool) -> Self {
202 self.shadow = Some(shadow);
203 self
204 }
205 pub fn with_shadow_brush(mut self, brush: Brush) -> Self {
207 self.shadow_brush = Some(brush);
208 self
209 }
210 pub fn with_shadow_dilation(mut self, size: f32) -> Self {
212 self.shadow_dilation = Some(size);
213 self
214 }
215 pub fn with_shadow_offset(mut self, offset: Vector2<f32>) -> Self {
217 self.shadow_offset = Some(offset);
218 self
219 }
220}
221
222#[derive(Clone, Copy, Eq, PartialEq)]
223pub enum DrawValueLayer {
224 Main,
225 Shadow,
226}
227
228#[derive(Clone, PartialEq, Debug)]
229pub struct GlyphDrawValues {
230 pub atlas_page_index: usize,
231 pub font: FontResource,
232 pub brush: Brush,
233 pub height: FontHeight,
235}