1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::{
    bone::Bone,
    c::{
        spAttachment, spPointAttachment, spPointAttachment_computeWorldPosition,
        spPointAttachment_computeWorldRotation,
    },
    c_interface::{NewFromPtr, SyncPtr},
};

#[cfg(feature = "mint")]
use mint::Vector2;

/// A lightweight, single point attachment with a translation and rotation.
///
/// [Spine API Reference](http://esotericsoftware.com/spine-api-reference#PointAttachment)
#[derive(Debug)]
pub struct PointAttachment {
    c_point_attachment: SyncPtr<spPointAttachment>,
}

impl NewFromPtr<spPointAttachment> for PointAttachment {
    unsafe fn new_from_ptr(c_point_attachment: *mut spPointAttachment) -> Self {
        Self {
            c_point_attachment: SyncPtr(c_point_attachment),
        }
    }
}

impl PointAttachment {
    fn attachment(&self) -> &spAttachment {
        unsafe { &self.c_ptr_ref().super_0 }
    }

    #[must_use]
    pub fn compute_world_position(&self, bone: &Bone) -> (f32, f32) {
        let mut x = 0.;
        let mut y = 0.;
        unsafe {
            spPointAttachment_computeWorldPosition(self.c_ptr(), bone.c_ptr(), &mut x, &mut y);
        }
        (x, y)
    }

    #[must_use]
    pub fn compute_world_rotation(&self, bone: &Bone) -> f32 {
        unsafe { spPointAttachment_computeWorldRotation(self.c_ptr(), bone.c_ptr()) }
    }

    c_attachment_accessors!();
    c_accessor_color!(
        /// The color of the point attachment as it was in Spine, or a default color if nonessential
        /// data was not exported. Point attachments are not usually rendered at runtime.
        color,
        color
    );
    c_accessor_mut!(rotation, set_rotation, rotation, f32);
    c_accessor_mut!(x, set_x, x, f32);
    c_accessor_mut!(y, set_y, x, f32);
    c_ptr!(c_point_attachment, spPointAttachment);
}

/// Functions available if using the `mint` feature.
#[cfg(feature = "mint")]
impl PointAttachment {
    #[must_use]
    pub fn translation(&self) -> Vector2<f32> {
        Vector2 {
            x: self.x(),
            y: self.y(),
        }
    }
}