Skip to main content

dora_ssr/dora/
sprite.rs

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