clay_layout/
text.rs

1use crate::{bindings::*, color::Color};
2
3#[derive(Debug, Clone, Copy)]
4#[repr(u8)]
5pub enum TextElementConfigWrapMode {
6    /// Wraps on whitespaces not breaking words
7    Words = Clay_TextElementConfigWrapMode_CLAY_TEXT_WRAP_WORDS,
8    /// Only wraps on new line characters
9    Newline = Clay_TextElementConfigWrapMode_CLAY_TEXT_WRAP_NEWLINES,
10    /// Never wraps, can overflow of parent layout
11    None = Clay_TextElementConfigWrapMode_CLAY_TEXT_WRAP_NONE,
12}
13
14#[derive(Debug, Clone, Copy)]
15#[repr(u8)]
16pub enum TextAlignment {
17    /// Aligns the text to the left.
18    Left = Clay_TextAlignment_CLAY_TEXT_ALIGN_LEFT,
19    /// Aligns the text to the center.
20    Center = Clay_TextAlignment_CLAY_TEXT_ALIGN_CENTER,
21    /// Aligns the text to the right.
22    Right = Clay_TextAlignment_CLAY_TEXT_ALIGN_RIGHT,
23}
24
25pub struct TextElementConfig {
26    inner: *mut Clay_TextElementConfig,
27}
28
29impl From<TextElementConfig> for *mut Clay_TextElementConfig {
30    fn from(value: TextElementConfig) -> Self {
31        value.inner
32    }
33}
34
35/// Configuration settings for rendering text elements.
36#[derive(Debug, Clone, Copy)]
37pub struct TextConfig {
38    /// The color of the text.
39    pub color: Color,
40    /// Clay does not manage fonts. It is up to the user to assign a unique ID to each font
41    /// and provide it via the [`font_id`](Text::font_id) field.
42    pub font_id: u16,
43    /// The font size of the text.
44    pub font_size: u16,
45    /// The spacing between letters.
46    pub letter_spacing: u16,
47    /// The height of each line of text.
48    pub line_height: u16,
49    /// Defines the text wrapping behavior.
50    pub wrap_mode: TextElementConfigWrapMode,
51    /// The alignment of the text.
52    pub alignment: TextAlignment,
53}
54
55impl TextConfig {
56    /// Creates a new `TextConfig` instance with default values.
57    pub fn new() -> Self {
58        Self::default()
59    }
60
61    /// Sets the text color.
62    #[inline]
63    pub fn color(&mut self, color: Color) -> &mut Self {
64        self.color = color;
65        self
66    }
67
68    /// Sets the font ID. The user is responsible for assigning unique font IDs.
69    #[inline]
70    pub fn font_id(&mut self, id: u16) -> &mut Self {
71        self.font_id = id;
72        self
73    }
74
75    /// Sets the font size.
76    #[inline]
77    pub fn font_size(&mut self, size: u16) -> &mut Self {
78        self.font_size = size;
79        self
80    }
81
82    /// Sets the letter spacing.
83    #[inline]
84    pub fn letter_spacing(&mut self, spacing: u16) -> &mut Self {
85        self.letter_spacing = spacing;
86        self
87    }
88
89    /// Sets the line height.
90    #[inline]
91    pub fn line_height(&mut self, height: u16) -> &mut Self {
92        self.line_height = height;
93        self
94    }
95
96    /// Sets the text wrapping mode.
97    #[inline]
98    pub fn wrap_mode(&mut self, mode: TextElementConfigWrapMode) -> &mut Self {
99        self.wrap_mode = mode;
100        self
101    }
102
103    /// Sets the text alignment.
104    #[inline]
105    pub fn alignment(&mut self, alignment: TextAlignment) -> &mut Self {
106        self.alignment = alignment;
107        self
108    }
109
110    /// Finalizes the text configuration and stores it in memory.
111    #[inline]
112    pub fn end(&self) -> TextElementConfig {
113        let memory = unsafe { Clay__StoreTextElementConfig((*self).into()) };
114        TextElementConfig { inner: memory }
115    }
116}
117
118impl Default for TextConfig {
119    fn default() -> Self {
120        Self {
121            color: Color::rgba(0., 0., 0., 0.),
122            font_id: 0,
123            font_size: 0,
124            letter_spacing: 0,
125            line_height: 0,
126            wrap_mode: TextElementConfigWrapMode::Words,
127            alignment: TextAlignment::Left,
128        }
129    }
130}
131
132impl From<TextConfig> for Clay_TextElementConfig {
133    fn from(value: TextConfig) -> Self {
134        Self {
135            userData: core::ptr::null_mut(),
136            textColor: value.color.into(),
137            fontId: value.font_id,
138            fontSize: value.font_size,
139            letterSpacing: value.letter_spacing,
140            lineHeight: value.line_height,
141            wrapMode: value.wrap_mode as _,
142            textAlignment: value.alignment as _,
143        }
144    }
145}
146
147impl From<Clay_TextElementConfig> for TextConfig {
148    fn from(value: Clay_TextElementConfig) -> Self {
149        Self {
150            color: value.textColor.into(),
151            font_id: value.fontId,
152            font_size: value.fontSize,
153            letter_spacing: value.letterSpacing,
154            line_height: value.lineHeight,
155            wrap_mode: unsafe {
156                core::mem::transmute::<u8, TextElementConfigWrapMode>(value.wrapMode)
157            },
158            alignment: unsafe { core::mem::transmute::<u8, TextAlignment>(value.textAlignment) },
159        }
160    }
161}