dora_ssr/dora/
label.rs

1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10	fn label_type() -> i32;
11	fn label_set_alignment(slf: i64, val: i32);
12	fn label_get_alignment(slf: i64) -> i32;
13	fn label_set_alpha_ref(slf: i64, val: f32);
14	fn label_get_alpha_ref(slf: i64) -> f32;
15	fn label_set_text_width(slf: i64, val: f32);
16	fn label_get_text_width(slf: i64) -> f32;
17	fn label_set_spacing(slf: i64, val: f32);
18	fn label_get_spacing(slf: i64) -> f32;
19	fn label_set_line_gap(slf: i64, val: f32);
20	fn label_get_line_gap(slf: i64) -> f32;
21	fn label_set_outline_color(slf: i64, val: i32);
22	fn label_get_outline_color(slf: i64) -> i32;
23	fn label_set_outline_width(slf: i64, val: f32);
24	fn label_get_outline_width(slf: i64) -> f32;
25	fn label_set_smooth(slf: i64, val: i64);
26	fn label_get_smooth(slf: i64) -> i64;
27	fn label_set_text(slf: i64, val: i64);
28	fn label_get_text(slf: i64) -> i64;
29	fn label_set_blend_func(slf: i64, val: i64);
30	fn label_get_blend_func(slf: i64) -> i64;
31	fn label_set_depth_write(slf: i64, val: i32);
32	fn label_is_depth_write(slf: i64) -> i32;
33	fn label_set_batched(slf: i64, val: i32);
34	fn label_is_batched(slf: i64) -> i32;
35	fn label_set_effect(slf: i64, val: i64);
36	fn label_get_effect(slf: i64) -> i64;
37	fn label_get_character_count(slf: i64) -> i32;
38	fn label_get_character(slf: i64, index: i32) -> i64;
39	fn label_get_automatic_width() -> f32;
40	fn label_new(font_name: i64, font_size: i32, sdf: i32) -> i64;
41	fn label_with_str(font_str: i64) -> i64;
42}
43use crate::dora::IObject;
44use crate::dora::INode;
45impl INode for Label { }
46/// A node for rendering text using a TrueType font.
47pub struct Label { raw: i64 }
48crate::dora_object!(Label);
49impl Label {
50	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
51		(unsafe { label_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
52			match raw {
53				0 => None,
54				_ => Some(Box::new(Label { raw: raw }))
55			}
56		})
57	}
58	/// Sets the text alignment setting.
59	pub fn set_alignment(&mut self, val: crate::dora::TextAlign) {
60		unsafe { label_set_alignment(self.raw(), val as i32) };
61	}
62	/// Gets the text alignment setting.
63	pub fn get_alignment(&self) -> crate::dora::TextAlign {
64		return unsafe { core::mem::transmute(label_get_alignment(self.raw())) };
65	}
66	/// Sets the alpha threshold value. Pixels with alpha values below this value will not be drawn.
67	/// Only works with `label.effect = SpriteEffect::new("builtin:vs_sprite", "builtin:fs_spritealphatest")`.
68	pub fn set_alpha_ref(&mut self, val: f32) {
69		unsafe { label_set_alpha_ref(self.raw(), val) };
70	}
71	/// Gets the alpha threshold value. Pixels with alpha values below this value will not be drawn.
72	/// Only works with `label.effect = SpriteEffect::new("builtin:vs_sprite", "builtin:fs_spritealphatest")`.
73	pub fn get_alpha_ref(&self) -> f32 {
74		return unsafe { label_get_alpha_ref(self.raw()) };
75	}
76	/// Sets the width of the text used for text wrapping.
77	/// Set to `Label::AutomaticWidth` to disable wrapping.
78	/// Default is `Label::AutomaticWidth`.
79	pub fn set_text_width(&mut self, val: f32) {
80		unsafe { label_set_text_width(self.raw(), val) };
81	}
82	/// Gets the width of the text used for text wrapping.
83	/// Set to `Label::AutomaticWidth` to disable wrapping.
84	/// Default is `Label::AutomaticWidth`.
85	pub fn get_text_width(&self) -> f32 {
86		return unsafe { label_get_text_width(self.raw()) };
87	}
88	/// Sets the gap in pixels between characters.
89	pub fn set_spacing(&mut self, val: f32) {
90		unsafe { label_set_spacing(self.raw(), val) };
91	}
92	/// Gets the gap in pixels between characters.
93	pub fn get_spacing(&self) -> f32 {
94		return unsafe { label_get_spacing(self.raw()) };
95	}
96	/// Sets the gap in pixels between lines of text.
97	pub fn set_line_gap(&mut self, val: f32) {
98		unsafe { label_set_line_gap(self.raw(), val) };
99	}
100	/// Gets the gap in pixels between lines of text.
101	pub fn get_line_gap(&self) -> f32 {
102		return unsafe { label_get_line_gap(self.raw()) };
103	}
104	/// Sets the color of the outline, only works with SDF label.
105	pub fn set_outline_color(&mut self, val: &crate::dora::Color) {
106		unsafe { label_set_outline_color(self.raw(), val.to_argb() as i32) };
107	}
108	/// Gets the color of the outline, only works with SDF label.
109	pub fn get_outline_color(&self) -> crate::dora::Color {
110		return unsafe { crate::dora::Color::from(label_get_outline_color(self.raw())) };
111	}
112	/// Sets the width of the outline, only works with SDF label.
113	pub fn set_outline_width(&mut self, val: f32) {
114		unsafe { label_set_outline_width(self.raw(), val) };
115	}
116	/// Gets the width of the outline, only works with SDF label.
117	pub fn get_outline_width(&self) -> f32 {
118		return unsafe { label_get_outline_width(self.raw()) };
119	}
120	/// Sets the smooth value of the text, only works with SDF label, default is (0.7, 0.7).
121	pub fn set_smooth(&mut self, val: &crate::dora::Vec2) {
122		unsafe { label_set_smooth(self.raw(), val.into_i64()) };
123	}
124	/// Gets the smooth value of the text, only works with SDF label, default is (0.7, 0.7).
125	pub fn get_smooth(&self) -> crate::dora::Vec2 {
126		return unsafe { crate::dora::Vec2::from(label_get_smooth(self.raw())) };
127	}
128	/// Sets the text to be rendered.
129	pub fn set_text(&mut self, val: &str) {
130		unsafe { label_set_text(self.raw(), crate::dora::from_string(val)) };
131	}
132	/// Gets the text to be rendered.
133	pub fn get_text(&self) -> String {
134		return unsafe { crate::dora::to_string(label_get_text(self.raw())) };
135	}
136	/// Sets the blend function for the label.
137	pub fn set_blend_func(&mut self, val: crate::dora::BlendFunc) {
138		unsafe { label_set_blend_func(self.raw(), val.to_value()) };
139	}
140	/// Gets the blend function for the label.
141	pub fn get_blend_func(&self) -> crate::dora::BlendFunc {
142		return unsafe { crate::dora::BlendFunc::from(label_get_blend_func(self.raw())) };
143	}
144	/// Sets whether depth writing is enabled. (Default is false)
145	pub fn set_depth_write(&mut self, val: bool) {
146		unsafe { label_set_depth_write(self.raw(), if val { 1 } else { 0 }) };
147	}
148	/// Gets whether depth writing is enabled. (Default is false)
149	pub fn is_depth_write(&self) -> bool {
150		return unsafe { label_is_depth_write(self.raw()) != 0 };
151	}
152	/// Sets whether the label is using batched rendering.
153	/// When using batched rendering the `label.get_character()` function will no longer work, but it provides better rendering performance. Default is true.
154	pub fn set_batched(&mut self, val: bool) {
155		unsafe { label_set_batched(self.raw(), if val { 1 } else { 0 }) };
156	}
157	/// Gets whether the label is using batched rendering.
158	/// When using batched rendering the `label.get_character()` function will no longer work, but it provides better rendering performance. Default is true.
159	pub fn is_batched(&self) -> bool {
160		return unsafe { label_is_batched(self.raw()) != 0 };
161	}
162	/// Sets the sprite effect used to render the text.
163	pub fn set_effect(&mut self, val: &crate::dora::SpriteEffect) {
164		unsafe { label_set_effect(self.raw(), val.raw()) };
165	}
166	/// Gets the sprite effect used to render the text.
167	pub fn get_effect(&self) -> crate::dora::SpriteEffect {
168		return unsafe { crate::dora::SpriteEffect::from(label_get_effect(self.raw())).unwrap() };
169	}
170	/// Gets the number of characters in the label.
171	pub fn get_character_count(&self) -> i32 {
172		return unsafe { label_get_character_count(self.raw()) };
173	}
174	/// Returns the sprite for the character at the specified index.
175	///
176	/// # Arguments
177	///
178	/// * `index` - The index of the character sprite to retrieve.
179	///
180	/// # Returns
181	///
182	/// * `Option<Sprite>` - The sprite for the character, or `None` if the index is out of range.
183	pub fn get_character(&mut self, index: i32) -> Option<crate::dora::Sprite> {
184		unsafe { return crate::dora::Sprite::from(label_get_character(self.raw(), index)); }
185	}
186	/// Gets the value to use for automatic width calculation
187	pub fn get_automatic_width() -> f32 {
188		return unsafe { label_get_automatic_width() };
189	}
190	/// Creates a new Label object with the specified font name and font size.
191	///
192	/// # Arguments
193	///
194	/// * `font_name` - The name of the font to use for the label. Can be font file path with or without file extension.
195	/// * `font_size` - The size of the font to use for the label.
196	/// * `sdf` - Whether to use SDF rendering or not. With SDF rendering, the outline feature will be enabled.
197	///
198	/// # Returns
199	///
200	/// * `Label` - The new Label object.
201	pub fn new(font_name: &str, font_size: i32, sdf: bool) -> Option<Label> {
202		unsafe { return Label::from(label_new(crate::dora::from_string(font_name), font_size, if sdf { 1 } else { 0 })); }
203	}
204	/// Creates a new Label object with the specified font string.
205	///
206	/// # Arguments
207	///
208	/// * `font_str` - The font string to use for the label. Should be in the format "fontName;fontSize;sdf", where `sdf` should be "true" or "false".
209	///
210	/// # Returns
211	///
212	/// * `Label` - The new Label object.
213	pub fn with_str(font_str: &str) -> Option<crate::dora::Label> {
214		unsafe { return crate::dora::Label::from(label_with_str(crate::dora::from_string(font_str))); }
215	}
216}