dora_ssr/dora/
sprite.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 sprite_type() -> i32;
11	fn sprite_set_depth_write(slf: i64, val: i32);
12	fn sprite_is_depth_write(slf: i64) -> i32;
13	fn sprite_set_alpha_ref(slf: i64, val: f32);
14	fn sprite_get_alpha_ref(slf: i64) -> f32;
15	fn sprite_set_texture_rect(slf: i64, val: i64);
16	fn sprite_get_texture_rect(slf: i64) -> i64;
17	fn sprite_get_texture(slf: i64) -> i64;
18	fn sprite_set_blend_func(slf: i64, val: i64);
19	fn sprite_get_blend_func(slf: i64) -> i64;
20	fn sprite_set_effect(slf: i64, val: i64);
21	fn sprite_get_effect(slf: i64) -> i64;
22	fn sprite_set_uwrap(slf: i64, val: i32);
23	fn sprite_get_uwrap(slf: i64) -> i32;
24	fn sprite_set_vwrap(slf: i64, val: i32);
25	fn sprite_get_vwrap(slf: i64) -> i32;
26	fn sprite_set_filter(slf: i64, val: i32);
27	fn sprite_get_filter(slf: i64) -> i32;
28	fn sprite_set_effect_as_default(slf: i64);
29	fn sprite_new() -> i64;
30	fn sprite_with_texture_rect(texture: i64, texture_rect: i64) -> i64;
31	fn sprite_with_texture(texture: i64) -> i64;
32	fn sprite_with_file(clip_str: i64) -> i64;
33}
34use crate::dora::IObject;
35use crate::dora::INode;
36impl INode for Sprite { }
37/// A struct to render texture in game scene tree hierarchy.
38pub struct Sprite { raw: i64 }
39crate::dora_object!(Sprite);
40impl Sprite {
41	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
42		(unsafe { sprite_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
43			match raw {
44				0 => None,
45				_ => Some(Box::new(Sprite { raw: raw }))
46			}
47		})
48	}
49	/// Sets whether the depth buffer should be written to when rendering the sprite.
50	pub fn set_depth_write(&mut self, val: bool) {
51		unsafe { sprite_set_depth_write(self.raw(), if val { 1 } else { 0 }) };
52	}
53	/// Gets whether the depth buffer should be written to when rendering the sprite.
54	pub fn is_depth_write(&self) -> bool {
55		return unsafe { sprite_is_depth_write(self.raw()) != 0 };
56	}
57	/// Sets the alpha reference value for alpha testing. Pixels with alpha values less than or equal to this value will be discarded.
58	/// Only works with `sprite.effect = SpriteEffect::new("builtin:vs_sprite", "builtin:fs_spritealphatest");`.
59	pub fn set_alpha_ref(&mut self, val: f32) {
60		unsafe { sprite_set_alpha_ref(self.raw(), val) };
61	}
62	/// Gets the alpha reference value for alpha testing. Pixels with alpha values less than or equal to this value will be discarded.
63	/// Only works with `sprite.effect = SpriteEffect::new("builtin:vs_sprite", "builtin:fs_spritealphatest");`.
64	pub fn get_alpha_ref(&self) -> f32 {
65		return unsafe { sprite_get_alpha_ref(self.raw()) };
66	}
67	/// Sets the texture rectangle for the sprite.
68	pub fn set_texture_rect(&mut self, val: &crate::dora::Rect) {
69		unsafe { sprite_set_texture_rect(self.raw(), val.raw()) };
70	}
71	/// Gets the texture rectangle for the sprite.
72	pub fn get_texture_rect(&self) -> crate::dora::Rect {
73		return unsafe { crate::dora::Rect::from(sprite_get_texture_rect(self.raw())) };
74	}
75	/// Gets the texture for the sprite.
76	pub fn get_texture(&self) -> Option<crate::dora::Texture2D> {
77		return unsafe { crate::dora::Texture2D::from(sprite_get_texture(self.raw())) };
78	}
79	/// Sets the blend function for the sprite.
80	pub fn set_blend_func(&mut self, val: crate::dora::BlendFunc) {
81		unsafe { sprite_set_blend_func(self.raw(), val.to_value()) };
82	}
83	/// Gets the blend function for the sprite.
84	pub fn get_blend_func(&self) -> crate::dora::BlendFunc {
85		return unsafe { crate::dora::BlendFunc::from(sprite_get_blend_func(self.raw())) };
86	}
87	/// Sets the sprite shader effect.
88	pub fn set_effect(&mut self, val: &crate::dora::SpriteEffect) {
89		unsafe { sprite_set_effect(self.raw(), val.raw()) };
90	}
91	/// Gets the sprite shader effect.
92	pub fn get_effect(&self) -> crate::dora::SpriteEffect {
93		return unsafe { crate::dora::SpriteEffect::from(sprite_get_effect(self.raw())).unwrap() };
94	}
95	/// Sets the texture wrapping mode for the U (horizontal) axis.
96	pub fn set_uwrap(&mut self, val: crate::dora::TextureWrap) {
97		unsafe { sprite_set_uwrap(self.raw(), val as i32) };
98	}
99	/// Gets the texture wrapping mode for the U (horizontal) axis.
100	pub fn get_uwrap(&self) -> crate::dora::TextureWrap {
101		return unsafe { core::mem::transmute(sprite_get_uwrap(self.raw())) };
102	}
103	/// Sets the texture wrapping mode for the V (vertical) axis.
104	pub fn set_vwrap(&mut self, val: crate::dora::TextureWrap) {
105		unsafe { sprite_set_vwrap(self.raw(), val as i32) };
106	}
107	/// Gets the texture wrapping mode for the V (vertical) axis.
108	pub fn get_vwrap(&self) -> crate::dora::TextureWrap {
109		return unsafe { core::mem::transmute(sprite_get_vwrap(self.raw())) };
110	}
111	/// Sets the texture filtering mode for the sprite.
112	pub fn set_filter(&mut self, val: crate::dora::TextureFilter) {
113		unsafe { sprite_set_filter(self.raw(), val as i32) };
114	}
115	/// Gets the texture filtering mode for the sprite.
116	pub fn get_filter(&self) -> crate::dora::TextureFilter {
117		return unsafe { core::mem::transmute(sprite_get_filter(self.raw())) };
118	}
119	/// Removes the sprite effect and sets the default effect.
120	pub fn set_effect_as_default(&mut self) {
121		unsafe { sprite_set_effect_as_default(self.raw()); }
122	}
123	/// A method for creating a Sprite object.
124	///
125	/// # Returns
126	///
127	/// * `Sprite` - A new instance of the Sprite class.
128	pub fn new() -> Sprite {
129		unsafe { return Sprite { raw: sprite_new() }; }
130	}
131	/// A method for creating a Sprite object.
132	///
133	/// # Arguments
134	///
135	/// * `texture` - The texture to be used for the sprite.
136	/// * `texture_rect` - An optional rectangle defining the portion of the texture to use for the sprite. If not provided, the whole texture will be used for rendering.
137	///
138	/// # Returns
139	///
140	/// * `Sprite` - A new instance of the Sprite class.
141	pub fn with_texture_rect(texture: &crate::dora::Texture2D, texture_rect: &crate::dora::Rect) -> Sprite {
142		unsafe { return Sprite { raw: sprite_with_texture_rect(texture.raw(), texture_rect.raw()) }; }
143	}
144	/// A method for creating a Sprite object.
145	///
146	/// # Arguments
147	///
148	/// * `texture` - The texture to be used for the sprite.
149	///
150	/// # Returns
151	///
152	/// * `Sprite` - A new instance of the Sprite class.
153	pub fn with_texture(texture: &crate::dora::Texture2D) -> Sprite {
154		unsafe { return Sprite { raw: sprite_with_texture(texture.raw()) }; }
155	}
156	/// A method for creating a Sprite object.
157	///
158	/// # Arguments
159	///
160	/// * `clip_str` - The string containing format for loading a texture file. Can be "Image/file.png" and "Image/items.clip|itemA". Supports image file format: jpg, png, dds, pvr, ktx.
161	///
162	/// # Returns
163	///
164	/// * `Option<Sprite>` - A new instance of the Sprite class. If the texture file is not found, it will return `None`.
165	pub fn with_file(clip_str: &str) -> Option<Sprite> {
166		unsafe { return Sprite::from(sprite_with_file(crate::dora::from_string(clip_str))); }
167	}
168}