dora_ssr/dora/
draw_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 drawnode_type() -> i32;
11	fn drawnode_set_depth_write(slf: i64, val: i32);
12	fn drawnode_is_depth_write(slf: i64) -> i32;
13	fn drawnode_set_blend_func(slf: i64, val: i64);
14	fn drawnode_get_blend_func(slf: i64) -> i64;
15	fn drawnode_draw_dot(slf: i64, pos: i64, radius: f32, color: i32);
16	fn drawnode_draw_segment(slf: i64, from: i64, to: i64, radius: f32, color: i32);
17	fn drawnode_draw_polygon(slf: i64, verts: i64, fill_color: i32, border_width: f32, border_color: i32);
18	fn drawnode_draw_vertices(slf: i64, verts: i64);
19	fn drawnode_clear(slf: i64);
20	fn drawnode_new() -> i64;
21}
22use crate::dora::IObject;
23use crate::dora::INode;
24impl INode for DrawNode { }
25/// A scene node that draws simple shapes such as dots, lines, and polygons.
26pub struct DrawNode { raw: i64 }
27crate::dora_object!(DrawNode);
28impl DrawNode {
29	pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
30		(unsafe { drawnode_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
31			match raw {
32				0 => None,
33				_ => Some(Box::new(DrawNode { raw: raw }))
34			}
35		})
36	}
37	/// Sets whether to write to the depth buffer when drawing (default is false).
38	pub fn set_depth_write(&mut self, val: bool) {
39		unsafe { drawnode_set_depth_write(self.raw(), if val { 1 } else { 0 }) };
40	}
41	/// Gets whether to write to the depth buffer when drawing (default is false).
42	pub fn is_depth_write(&self) -> bool {
43		return unsafe { drawnode_is_depth_write(self.raw()) != 0 };
44	}
45	/// Sets the blend function for the draw node.
46	pub fn set_blend_func(&mut self, val: crate::dora::BlendFunc) {
47		unsafe { drawnode_set_blend_func(self.raw(), val.to_value()) };
48	}
49	/// Gets the blend function for the draw node.
50	pub fn get_blend_func(&self) -> crate::dora::BlendFunc {
51		return unsafe { crate::dora::BlendFunc::from(drawnode_get_blend_func(self.raw())) };
52	}
53	/// Draws a dot at a specified position with a specified radius and color.
54	///
55	/// # Arguments
56	///
57	/// * `pos` - The position of the dot.
58	/// * `radius` - The radius of the dot.
59	/// * `color` - The color of the dot.
60	pub fn draw_dot(&mut self, pos: &crate::dora::Vec2, radius: f32, color: &crate::dora::Color) {
61		unsafe { drawnode_draw_dot(self.raw(), pos.into_i64(), radius, color.to_argb() as i32); }
62	}
63	/// Draws a line segment between two points with a specified radius and color.
64	///
65	/// # Arguments
66	///
67	/// * `from` - The starting point of the line.
68	/// * `to` - The ending point of the line.
69	/// * `radius` - The radius of the line.
70	/// * `color` - The color of the line.
71	pub fn draw_segment(&mut self, from: &crate::dora::Vec2, to: &crate::dora::Vec2, radius: f32, color: &crate::dora::Color) {
72		unsafe { drawnode_draw_segment(self.raw(), from.into_i64(), to.into_i64(), radius, color.to_argb() as i32); }
73	}
74	/// Draws a polygon defined by a list of vertices with a specified fill color and border.
75	///
76	/// # Arguments
77	///
78	/// * `verts` - The vertices of the polygon.
79	/// * `fill_color` - The fill color of the polygon.
80	/// * `border_width` - The width of the border.
81	/// * `border_color` - The color of the border.
82	pub fn draw_polygon(&mut self, verts: &Vec<crate::dora::Vec2>, fill_color: &crate::dora::Color, border_width: f32, border_color: &crate::dora::Color) {
83		unsafe { drawnode_draw_polygon(self.raw(), crate::dora::Vector::from_vec2(verts), fill_color.to_argb() as i32, border_width, border_color.to_argb() as i32); }
84	}
85	/// Draws a set of vertices as triangles, each vertex with its own color.
86	///
87	/// # Arguments
88	///
89	/// * `verts` - The list of vertices and their colors. Each element is a tuple where the first element is a `Vec2` and the second element is a `Color`.
90	pub fn draw_vertices(&mut self, verts: &Vec<crate::dora::VertexColor>) {
91		unsafe { drawnode_draw_vertices(self.raw(), crate::dora::Vector::from_vertex_color(verts)); }
92	}
93	/// Clears all previously drawn shapes from the node.
94	pub fn clear(&mut self) {
95		unsafe { drawnode_clear(self.raw()); }
96	}
97	/// Creates a new DrawNode object.
98	///
99	/// # Returns
100	///
101	/// * A new `DrawNode` object.
102	pub fn new() -> DrawNode {
103		unsafe { return DrawNode { raw: drawnode_new() }; }
104	}
105}