dora_ssr/dora/
tile_node.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 tilenode_type() -> i32;
11	fn tilenode_set_depth_write(slf: i64, val: i32);
12	fn tilenode_is_depth_write(slf: i64) -> i32;
13	fn tilenode_set_blend_func(slf: i64, val: i64);
14	fn tilenode_get_blend_func(slf: i64) -> i64;
15	fn tilenode_set_effect(slf: i64, val: i64);
16	fn tilenode_get_effect(slf: i64) -> i64;
17	fn tilenode_set_filter(slf: i64, val: i32);
18	fn tilenode_get_filter(slf: i64) -> i32;
19	fn tilenode_get_layer(slf: i64, layer_name: i64) -> i64;
20	fn tilenode_new(tmx_file: i64) -> i64;
21	fn tilenode_with_with_layer(tmx_file: i64, layer_name: i64) -> i64;
22	fn tilenode_with_with_layers(tmx_file: i64, layer_names: i64) -> i64;
23}
24use crate::dora::IObject;
25use crate::dora::INode;
26impl INode for TileNode { }
27/// The TileNode class to render Tilemaps from TMX file in game scene tree hierarchy.
28pub struct TileNode { raw: i64 }
29crate::dora_object!(TileNode);
30impl TileNode {
31	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
32		(unsafe { tilenode_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
33			match raw {
34				0 => None,
35				_ => Some(Box::new(TileNode { raw: raw }))
36			}
37		})
38	}
39	/// Sets whether the depth buffer should be written to when rendering the tilemap.
40	pub fn set_depth_write(&mut self, val: bool) {
41		unsafe { tilenode_set_depth_write(self.raw(), if val { 1 } else { 0 }) };
42	}
43	/// Gets whether the depth buffer should be written to when rendering the tilemap.
44	pub fn is_depth_write(&self) -> bool {
45		return unsafe { tilenode_is_depth_write(self.raw()) != 0 };
46	}
47	/// Sets the blend function for the tilemap.
48	pub fn set_blend_func(&mut self, val: crate::dora::BlendFunc) {
49		unsafe { tilenode_set_blend_func(self.raw(), val.to_value()) };
50	}
51	/// Gets the blend function for the tilemap.
52	pub fn get_blend_func(&self) -> crate::dora::BlendFunc {
53		return unsafe { crate::dora::BlendFunc::from(tilenode_get_blend_func(self.raw())) };
54	}
55	/// Sets the tilemap shader effect.
56	pub fn set_effect(&mut self, val: &crate::dora::SpriteEffect) {
57		unsafe { tilenode_set_effect(self.raw(), val.raw()) };
58	}
59	/// Gets the tilemap shader effect.
60	pub fn get_effect(&self) -> crate::dora::SpriteEffect {
61		return unsafe { crate::dora::SpriteEffect::from(tilenode_get_effect(self.raw())).unwrap() };
62	}
63	/// Sets the texture filtering mode for the tilemap.
64	pub fn set_filter(&mut self, val: crate::dora::TextureFilter) {
65		unsafe { tilenode_set_filter(self.raw(), val as i32) };
66	}
67	/// Gets the texture filtering mode for the tilemap.
68	pub fn get_filter(&self) -> crate::dora::TextureFilter {
69		return unsafe { core::mem::transmute(tilenode_get_filter(self.raw())) };
70	}
71	/// Get the layer data by name from the tilemap.
72	///
73	/// # Arguments
74	///
75	/// * `layerName` - The name of the layer in the TMX file.
76	///
77	/// # Returns
78	///
79	/// * `Dictionary` - The layer data as a dictionary object.
80	pub fn get_layer(&self, layer_name: &str) -> Option<crate::dora::Dictionary> {
81		unsafe { return crate::dora::Dictionary::from(tilenode_get_layer(self.raw(), crate::dora::from_string(layer_name))); }
82	}
83	/// Creates a `TileNode` object that will render the tile layers from a TMX file.
84	///
85	/// # Arguments
86	///
87	/// * `tmxFile` - The TMX file for the tilemap. This should be a file created with the Tiled Map Editor (http://www.mapeditor.org) and must be in XML format.
88	///
89	/// # Returns
90	///
91	/// Returns a new instance of the `TileNode` class. If the tilemap file is not found, it will return `None`.
92	pub fn new(tmx_file: &str) -> Option<TileNode> {
93		unsafe { return TileNode::from(tilenode_new(crate::dora::from_string(tmx_file))); }
94	}
95	/// Creates a `TileNode` object that will render the specified tile layer from a TMX file.
96	///
97	/// # Arguments
98	///
99	/// * `tmxFile` - The TMX file for the tilemap. This should be a file created with the Tiled Map Editor (http://www.mapeditor.org) and must be in XML format.
100	/// * `layerName` - The name of the layer in the TMX file.
101	///
102	/// # Returns
103	///
104	/// Returns a new instance of the `TileNode` class. If the tilemap file is not found, it will return `None`.
105	pub fn with_with_layer(tmx_file: &str, layer_name: &str) -> Option<TileNode> {
106		unsafe { return TileNode::from(tilenode_with_with_layer(crate::dora::from_string(tmx_file), crate::dora::from_string(layer_name))); }
107	}
108	/// Creates a `TileNode` object that will render the specified tile layers from a TMX file.
109	///
110	/// # Arguments
111	///
112	/// * `tmxFile` - The TMX file for the tilemap. This should be a file created with the Tiled Map Editor (http://www.mapeditor.org) and must be in XML format.
113	/// * `layerNames` - A vector of names of the layers in the TMX file.
114	///
115	/// # Returns
116	///
117	/// Returns a new instance of the `TileNode` class. If the tilemap file is not found, it will return `None`.
118	pub fn with_with_layers(tmx_file: &str, layer_names: &Vec<&str>) -> Option<TileNode> {
119		unsafe { return TileNode::from(tilenode_with_with_layers(crate::dora::from_string(tmx_file), crate::dora::Vector::from_str(layer_names))); }
120	}
121}