dora_ssr/dora/vgnode.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 vgnode_type() -> i32;
11 fn vgnode_get_surface(slf: i64) -> i64;
12 fn vgnode_render(slf: i64, func0: i32);
13 fn vgnode_new(width: f32, height: f32, scale: f32, edge_aa: i32) -> i64;
14}
15use crate::dora::IObject;
16use crate::dora::INode;
17impl INode for VGNode { }
18/// A node for rendering vector graphics.
19pub struct VGNode { raw: i64 }
20crate::dora_object!(VGNode);
21impl VGNode {
22 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
23 (unsafe { vgnode_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
24 match raw {
25 0 => None,
26 _ => Some(Box::new(VGNode { raw: raw }))
27 }
28 })
29 }
30 /// Gets The surface of the node for displaying frame buffer texture that contains vector graphics.
31 /// You can get the texture of the surface by calling `vgNode.get_surface().get_texture()`.
32 pub fn get_surface(&self) -> crate::dora::Sprite {
33 return unsafe { crate::dora::Sprite::from(vgnode_get_surface(self.raw())).unwrap() };
34 }
35 /// The function for rendering vector graphics.
36 ///
37 /// # Arguments
38 ///
39 /// * `renderFunc` - The closure function for rendering vector graphics. You can do the rendering operations inside this closure.
40 ///
41 /// # Example
42 ///
43 /// ```
44 /// vgNode.render(|| {
45 /// Nvg::begin_path();
46 /// Nvg::move_to(100.0, 100.0);
47 /// Nvg::line_to(200.0, 200.0);
48 /// Nvg::close_path();
49 /// Nvg::stroke();
50 /// });
51 /// ```
52 pub fn render(&mut self, mut render_func: Box<dyn FnMut()>) {
53 let func_id0 = crate::dora::push_function(Box::new(move || {
54 render_func()
55 }));
56 unsafe { vgnode_render(self.raw(), func_id0); }
57 }
58 /// Creates a new VGNode object with the specified width and height.
59 ///
60 /// # Arguments
61 ///
62 /// * `width` - The width of the node's frame buffer texture.
63 /// * `height` - The height of the node's frame buffer texture.
64 /// * `scale` - The scale factor of the VGNode.
65 /// * `edge_aa` - The edge anti-aliasing factor of the VGNode.
66 ///
67 /// # Returns
68 ///
69 /// * The newly created VGNode object.
70 pub fn new(width: f32, height: f32, scale: f32, edge_aa: i32) -> VGNode {
71 unsafe { return VGNode { raw: vgnode_new(width, height, scale, edge_aa) }; }
72 }
73}