dora_ssr/dora/grid.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 grid_type() -> i32;
11 fn grid_get_grid_x(slf: i64) -> i32;
12 fn grid_get_grid_y(slf: i64) -> i32;
13 fn grid_set_depth_write(slf: i64, val: i32);
14 fn grid_is_depth_write(slf: i64) -> i32;
15 fn grid_set_blend_func(slf: i64, val: i64);
16 fn grid_get_blend_func(slf: i64) -> i64;
17 fn grid_set_effect(slf: i64, val: i64);
18 fn grid_get_effect(slf: i64) -> i64;
19 fn grid_set_texture_rect(slf: i64, val: i64);
20 fn grid_get_texture_rect(slf: i64) -> i64;
21 fn grid_set_texture(slf: i64, val: i64);
22 fn grid_get_texture(slf: i64) -> i64;
23 fn grid_set_pos(slf: i64, x: i32, y: i32, pos: i64, z: f32);
24 fn grid_get_pos(slf: i64, x: i32, y: i32) -> i64;
25 fn grid_set_color(slf: i64, x: i32, y: i32, color: i32);
26 fn grid_get_color(slf: i64, x: i32, y: i32) -> i32;
27 fn grid_move_uv(slf: i64, x: i32, y: i32, offset: i64);
28 fn grid_new(width: f32, height: f32, grid_x: i32, grid_y: i32) -> i64;
29 fn grid_with_texture_rect(texture: i64, texture_rect: i64, grid_x: i32, grid_y: i32) -> i64;
30 fn grid_with_texture(texture: i64, grid_x: i32, grid_y: i32) -> i64;
31 fn grid_with_file(clip_str: i64, grid_x: i32, grid_y: i32) -> i64;
32}
33use crate::dora::IObject;
34use crate::dora::INode;
35impl INode for Grid { }
36/// A struct used to render a texture as a grid of sprites, where each sprite can be positioned, colored, and have its UV coordinates manipulated.
37pub struct Grid { raw: i64 }
38crate::dora_object!(Grid);
39impl Grid {
40 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
41 (unsafe { grid_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
42 match raw {
43 0 => None,
44 _ => Some(Box::new(Grid { raw: raw }))
45 }
46 })
47 }
48 /// Gets the number of columns in the grid. And there are `gridX + 1` vertices horizontally for rendering.
49 pub fn get_grid_x(&self) -> i32 {
50 return unsafe { grid_get_grid_x(self.raw()) };
51 }
52 /// Gets the number of rows in the grid. And there are `gridY + 1` vertices vertically for rendering.
53 pub fn get_grid_y(&self) -> i32 {
54 return unsafe { grid_get_grid_y(self.raw()) };
55 }
56 /// Sets whether depth writes are enabled.
57 pub fn set_depth_write(&mut self, val: bool) {
58 unsafe { grid_set_depth_write(self.raw(), if val { 1 } else { 0 }) };
59 }
60 /// Gets whether depth writes are enabled.
61 pub fn is_depth_write(&self) -> bool {
62 return unsafe { grid_is_depth_write(self.raw()) != 0 };
63 }
64 /// Sets the blend function for the grid.
65 pub fn set_blend_func(&mut self, val: crate::dora::BlendFunc) {
66 unsafe { grid_set_blend_func(self.raw(), val.to_value()) };
67 }
68 /// Gets the blend function for the grid.
69 pub fn get_blend_func(&self) -> crate::dora::BlendFunc {
70 return unsafe { crate::dora::BlendFunc::from(grid_get_blend_func(self.raw())) };
71 }
72 /// Sets the sprite effect applied to the grid.
73 /// Default is `SpriteEffect::new("builtin:vs_sprite", "builtin:fs_sprite")`.
74 pub fn set_effect(&mut self, val: &crate::dora::SpriteEffect) {
75 unsafe { grid_set_effect(self.raw(), val.raw()) };
76 }
77 /// Gets the sprite effect applied to the grid.
78 /// Default is `SpriteEffect::new("builtin:vs_sprite", "builtin:fs_sprite")`.
79 pub fn get_effect(&self) -> crate::dora::SpriteEffect {
80 return unsafe { crate::dora::SpriteEffect::from(grid_get_effect(self.raw())).unwrap() };
81 }
82 /// Sets the rectangle within the texture that is used for the grid.
83 pub fn set_texture_rect(&mut self, val: &crate::dora::Rect) {
84 unsafe { grid_set_texture_rect(self.raw(), val.raw()) };
85 }
86 /// Gets the rectangle within the texture that is used for the grid.
87 pub fn get_texture_rect(&self) -> crate::dora::Rect {
88 return unsafe { crate::dora::Rect::from(grid_get_texture_rect(self.raw())) };
89 }
90 /// Sets the texture used for the grid.
91 pub fn set_texture(&mut self, val: &crate::dora::Texture2D) {
92 unsafe { grid_set_texture(self.raw(), val.raw()) };
93 }
94 /// Gets the texture used for the grid.
95 pub fn get_texture(&self) -> Option<crate::dora::Texture2D> {
96 return unsafe { crate::dora::Texture2D::from(grid_get_texture(self.raw())) };
97 }
98 /// Sets the position of a vertex in the grid.
99 ///
100 /// # Arguments
101 ///
102 /// * `x` - The x-coordinate of the vertex in the grid.
103 /// * `y` - The y-coordinate of the vertex in the grid.
104 /// * `pos` - The new position of the vertex, represented by a Vec2 object.
105 /// * `z` - The new z-coordinate of the vertex.
106 pub fn set_pos(&mut self, x: i32, y: i32, pos: &crate::dora::Vec2, z: f32) {
107 unsafe { grid_set_pos(self.raw(), x, y, pos.into_i64(), z); }
108 }
109 /// Gets the position of a vertex in the grid.
110 ///
111 /// # Arguments
112 ///
113 /// * `x` - The x-coordinate of the vertex in the grid.
114 /// * `y` - The y-coordinate of the vertex in the grid.
115 ///
116 /// # Returns
117 ///
118 /// * `Vec2` - The current position of the vertex.
119 pub fn get_pos(&self, x: i32, y: i32) -> crate::dora::Vec2 {
120 unsafe { return crate::dora::Vec2::from(grid_get_pos(self.raw(), x, y)); }
121 }
122 /// Sets the color of a vertex in the grid.
123 ///
124 /// # Arguments
125 ///
126 /// * `x` - The x-coordinate of the vertex in the grid.
127 /// * `y` - The y-coordinate of the vertex in the grid.
128 /// * `color` - The new color of the vertex, represented by a Color object.
129 pub fn set_color(&mut self, x: i32, y: i32, color: &crate::dora::Color) {
130 unsafe { grid_set_color(self.raw(), x, y, color.to_argb() as i32); }
131 }
132 /// Gets the color of a vertex in the grid.
133 ///
134 /// # Arguments
135 ///
136 /// * `x` - The x-coordinate of the vertex in the grid.
137 /// * `y` - The y-coordinate of the vertex in the grid.
138 ///
139 /// # Returns
140 ///
141 /// * `Color` - The current color of the vertex.
142 pub fn get_color(&self, x: i32, y: i32) -> crate::dora::Color {
143 unsafe { return crate::dora::Color::from(grid_get_color(self.raw(), x, y)); }
144 }
145 /// Moves the UV coordinates of a vertex in the grid.
146 ///
147 /// # Arguments
148 ///
149 /// * `x` - The x-coordinate of the vertex in the grid.
150 /// * `y` - The y-coordinate of the vertex in the grid.
151 /// * `offset` - The offset by which to move the UV coordinates, represented by a Vec2 object.
152 pub fn move_uv(&mut self, x: i32, y: i32, offset: &crate::dora::Vec2) {
153 unsafe { grid_move_uv(self.raw(), x, y, offset.into_i64()); }
154 }
155 /// Creates a new Grid with the specified dimensions and grid size.
156 ///
157 /// # Arguments
158 ///
159 /// * `width` - The width of the grid.
160 /// * `height` - The height of the grid.
161 /// * `grid_x` - The number of columns in the grid.
162 /// * `grid_y` - The number of rows in the grid.
163 ///
164 /// # Returns
165 ///
166 /// * `Grid` - The new Grid instance.
167 pub fn new(width: f32, height: f32, grid_x: i32, grid_y: i32) -> Grid {
168 unsafe { return Grid { raw: grid_new(width, height, grid_x, grid_y) }; }
169 }
170 /// Creates a new Grid with the specified texture, texture rectangle, and grid size.
171 ///
172 /// # Arguments
173 ///
174 /// * `texture` - The texture to use for the grid.
175 /// * `texture_rect` - The rectangle within the texture to use for the grid.
176 /// * `grid_x` - The number of columns in the grid.
177 /// * `grid_y` - The number of rows in the grid.
178 ///
179 /// # Returns
180 ///
181 /// * `Grid` - The new Grid instance.
182 pub fn with_texture_rect(texture: &crate::dora::Texture2D, texture_rect: &crate::dora::Rect, grid_x: i32, grid_y: i32) -> Grid {
183 unsafe { return Grid { raw: grid_with_texture_rect(texture.raw(), texture_rect.raw(), grid_x, grid_y) }; }
184 }
185 /// Creates a new Grid with the specified texture, texture rectangle, and grid size.
186 ///
187 /// # Arguments
188 ///
189 /// * `texture` - The texture to use for the grid.
190 /// * `grid_x` - The number of columns in the grid.
191 /// * `grid_y` - The number of rows in the grid.
192 ///
193 /// # Returns
194 ///
195 /// * `Grid` - The new Grid instance.
196 pub fn with_texture(texture: &crate::dora::Texture2D, grid_x: i32, grid_y: i32) -> Grid {
197 unsafe { return Grid { raw: grid_with_texture(texture.raw(), grid_x, grid_y) }; }
198 }
199 /// Creates a new Grid with the specified clip string and grid size.
200 ///
201 /// # Arguments
202 ///
203 /// * `clip_str` - The clip string to use for the grid. Can be "Image/file.png" and "Image/items.clip|itemA".
204 /// * `grid_x` - The number of columns in the grid.
205 /// * `grid_y` - The number of rows in the grid.
206 ///
207 /// # Returns
208 ///
209 /// * `Grid` - The new Grid instance.
210 pub fn with_file(clip_str: &str, grid_x: i32, grid_y: i32) -> Option<Grid> {
211 unsafe { return Grid::from(grid_with_file(crate::dora::from_string(clip_str), grid_x, grid_y)); }
212 }
213}