dora_ssr/dora/dragon_bone.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 dragonbone_type() -> i32;
11 fn dragonbone_set_hit_test_enabled(slf: i64, val: i32);
12 fn dragonbone_is_hit_test_enabled(slf: i64) -> i32;
13 fn dragonbone_contains_point(slf: i64, x: f32, y: f32) -> i64;
14 fn dragonbone_intersects_segment(slf: i64, x_1: f32, y_1: f32, x_2: f32, y_2: f32) -> i64;
15 fn dragonbone_with_files(bone_file: i64, atlas_file: i64) -> i64;
16 fn dragonbone_new(bone_str: i64) -> i64;
17 fn dragonbone_get_looks(bone_str: i64) -> i64;
18 fn dragonbone_get_animations(bone_str: i64) -> i64;
19}
20use crate::dora::IObject;
21use crate::dora::IPlayable;
22impl IPlayable for DragonBone { }
23use crate::dora::INode;
24impl INode for DragonBone { }
25/// An implementation of the 'Playable' record using the DragonBones animation system.
26pub struct DragonBone { raw: i64 }
27crate::dora_object!(DragonBone);
28impl DragonBone {
29 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
30 (unsafe { dragonbone_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
31 match raw {
32 0 => None,
33 _ => Some(Box::new(DragonBone { raw: raw }))
34 }
35 })
36 }
37 /// Sets whether hit testing is enabled.
38 pub fn set_hit_test_enabled(&mut self, val: bool) {
39 unsafe { dragonbone_set_hit_test_enabled(self.raw(), if val { 1 } else { 0 }) };
40 }
41 /// Gets whether hit testing is enabled.
42 pub fn is_hit_test_enabled(&self) -> bool {
43 return unsafe { dragonbone_is_hit_test_enabled(self.raw()) != 0 };
44 }
45 /// Checks if a point is inside the boundaries of the instance and returns the name of the bone or slot at that point, or `None` if no bone or slot is found.
46 ///
47 /// # Arguments
48 ///
49 /// * `x` - The x-coordinate of the point to check.
50 /// * `y` - The y-coordinate of the point to check.
51 ///
52 /// # Returns
53 ///
54 /// * `String` - The name of the bone or slot at the point.
55 pub fn contains_point(&mut self, x: f32, y: f32) -> String {
56 unsafe { return crate::dora::to_string(dragonbone_contains_point(self.raw(), x, y)); }
57 }
58 /// Checks if a line segment intersects the boundaries of the instance and returns the name of the bone or slot at the intersection point, or `None` if no bone or slot is found.
59 ///
60 /// # Arguments
61 ///
62 /// * `x1` - The x-coordinate of the start point of the line segment.
63 /// * `y1` - The y-coordinate of the start point of the line segment.
64 /// * `x2` - The x-coordinate of the end point of the line segment.
65 /// * `y2` - The y-coordinate of the end point of the line segment.
66 ///
67 /// # Returns
68 ///
69 /// * `String` - The name of the bone or slot at the intersection point.
70 pub fn intersects_segment(&mut self, x_1: f32, y_1: f32, x_2: f32, y_2: f32) -> String {
71 unsafe { return crate::dora::to_string(dragonbone_intersects_segment(self.raw(), x_1, y_1, x_2, y_2)); }
72 }
73 /// Creates a new instance of 'DragonBone' using the specified bone file and atlas file. This function only loads the first armature.
74 ///
75 /// # Arguments
76 ///
77 /// * `bone_file` - The filename of the bone file to load.
78 /// * `atlas_file` - The filename of the atlas file to load.
79 ///
80 /// # Returns
81 ///
82 /// * A new instance of 'DragonBone' with the specified bone file and atlas file. Returns `None` if the bone file or atlas file is not found.
83 pub fn with_files(bone_file: &str, atlas_file: &str) -> Option<DragonBone> {
84 unsafe { return DragonBone::from(dragonbone_with_files(crate::dora::from_string(bone_file), crate::dora::from_string(atlas_file))); }
85 }
86 /// Creates a new instance of 'DragonBone' using the specified bone string.
87 ///
88 /// # Arguments
89 ///
90 /// * `bone_str` - The DragonBone file string for the new instance. A DragonBone file string can be a file path with the target file extension like "DragonBone/item" or file paths with all the related files like "DragonBone/item_ske.json|DragonBone/item_tex.json". An armature name can be added following a separator of ';'. like "DragonBone/item;mainArmature" or "DragonBone/item_ske.json|DragonBone/item_tex.json;mainArmature".
91 ///
92 /// # Returns
93 ///
94 /// * A new instance of 'DragonBone'. Returns `None` if the bone file or atlas file is not found.
95 pub fn new(bone_str: &str) -> Option<DragonBone> {
96 unsafe { return DragonBone::from(dragonbone_new(crate::dora::from_string(bone_str))); }
97 }
98 /// Returns a list of available looks for the specified DragonBone file string.
99 ///
100 /// # Arguments
101 ///
102 /// * `bone_str` - The DragonBone file string to get the looks for.
103 ///
104 /// # Returns
105 ///
106 /// * A `Vec<String>` representing the available looks.
107 pub fn get_looks(bone_str: &str) -> Vec<String> {
108 unsafe { return crate::dora::Vector::to_str(dragonbone_get_looks(crate::dora::from_string(bone_str))); }
109 }
110 /// Returns a list of available animations for the specified DragonBone file string.
111 ///
112 /// # Arguments
113 ///
114 /// * `bone_str` - The DragonBone file string to get the animations for.
115 ///
116 /// # Returns
117 ///
118 /// * A `Vec<String>` representing the available animations.
119 pub fn get_animations(bone_str: &str) -> Vec<String> {
120 unsafe { return crate::dora::Vector::to_str(dragonbone_get_animations(crate::dora::from_string(bone_str))); }
121 }
122}