use std::sync::atomic::AtomicU64;
use crate::*;
pub struct DrawText {
pub text: TextData,
pub position: Vec2,
pub font: egui::FontId,
pub color: Color,
pub align: TextAlign,
pub pro_params: Option<ProTextParams>,
}
#[derive(Clone, Debug)]
pub struct TextParams {
pub font: egui::FontId,
pub rotation: f32,
pub color: Color,
}
impl Default for TextParams {
fn default() -> TextParams {
TextParams {
font: egui::FontId::new(20.0, egui::FontFamily::Monospace),
color: WHITE,
rotation: 0.0,
}
}
}
#[doc(hidden)]
pub enum TextData {
Raw(String),
Rich(RichText),
}
pub fn draw_text_ex(
text: &str,
position: Vec2,
align: TextAlign,
params: TextParams,
) {
let _span = span!("draw_text_ex");
draw_text_internal(
TextData::Raw(text.to_string()),
position,
align,
None,
params,
);
}
pub fn draw_text(text: &str, position: Vec2, color: Color, align: TextAlign) {
draw_text_internal(
TextData::Raw(text.to_string()),
position,
align,
None,
TextParams { color, ..Default::default() },
)
}
pub fn draw_text_pro_experimental(
text: RichText,
position: Vec2,
color: Color,
align: TextAlign,
font_size: f32,
font: FontHandle,
) {
draw_text_internal(
TextData::Rich(text),
position,
align,
Some(ProTextParams { font_size, font }),
TextParams { color, ..Default::default() },
);
}
static FONT_HANDLE_COUNTER: AtomicU64 = AtomicU64::new(0);
#[doc(hidden)]
pub fn gen_font_handle() -> FontHandle {
FontHandle(
FONT_HANDLE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::SeqCst),
)
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct FontHandle(u64);
fn draw_text_internal(
text: TextData,
position: Vec2,
align: TextAlign,
pro_params: Option<ProTextParams>,
params: TextParams,
) {
GLOBAL_STATE.borrow_mut().text_queue.push(DrawText {
text,
position,
color: params.color,
font: params.font,
align,
pro_params,
});
}
#[doc(hidden)]
pub struct ProTextParams {
pub font: FontHandle,
pub font_size: f32,
}
#[derive(Copy, Clone, Debug)]
pub enum TextAlign {
TopLeft,
TopRight,
BottomLeft,
BottomRight,
Center,
}
#[derive(Copy, Clone, Debug)]
pub struct StyledGlyph {
#[allow(dead_code)]
pub char: char,
pub wiggle: bool,
pub color: Option<Color>,
}
pub struct RichText {
pub clean_text: String,
pub styled_glyphs: Vec<StyledGlyph>,
}
pub fn simple_styled_text(text: &str) -> RichText {
let mut i = 0;
let mut clean_text = String::new();
let mut styled_glyphs = vec![];
let chars = text.chars().collect_vec();
while i < chars.len() {
let mut c = chars[i];
if c == '*' {
i += 1;
if i == chars.len() {
break;
}
c = chars[i];
styled_glyphs.push(StyledGlyph {
char: c,
wiggle: true,
color: Some(PINK.boost(4.0)),
});
} else {
styled_glyphs.push(StyledGlyph {
char: c,
wiggle: false,
color: None,
});
}
clean_text.push(c);
i += 1;
}
RichText { clean_text, styled_glyphs }
}