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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::fmt::Debug;

use crate::internal;

pub struct MaaRectBuffer {
    pub(crate) handle: internal::MaaRectHandle,
    destroy_at_drop: bool,
}

impl Debug for MaaRectBuffer {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MaaRectBuffer")
            .field("x", &self.x())
            .field("y", &self.y())
            .field("width", &self.width())
            .field("height", &self.height())
            .finish()
    }
}

impl From<internal::MaaRectHandle> for MaaRectBuffer {
    fn from(handle: internal::MaaRectHandle) -> Self {
        MaaRectBuffer {
            handle,
            destroy_at_drop: false,
        }
    }
}

unsafe impl Send for MaaRectBuffer {}
unsafe impl Sync for MaaRectBuffer {}

impl MaaRectBuffer {
    pub fn new() -> Self {
        let handle = unsafe { internal::MaaCreateRectBuffer() };

        MaaRectBuffer {
            handle,
            destroy_at_drop: true,
        }
    }

    pub fn x(&self) -> i32 {
        unsafe { internal::MaaGetRectX(self.handle) }
    }

    pub fn y(&self) -> i32 {
        unsafe { internal::MaaGetRectY(self.handle) }
    }

    pub fn width(&self) -> i32 {
        unsafe { internal::MaaGetRectW(self.handle) }
    }

    pub fn height(&self) -> i32 {
        unsafe { internal::MaaGetRectH(self.handle) }
    }

    pub fn set_x(self, x: i32) -> Self {
        unsafe {
            internal::MaaSetRectX(self.handle, x);
        };

        self
    }

    pub fn set_y(self, y: i32) -> Self {
        unsafe {
            internal::MaaSetRectY(self.handle, y);
        };

        self
    }

    pub fn set_width(self, width: i32) -> Self {
        unsafe {
            internal::MaaSetRectW(self.handle, width);
        }

        self
    }

    pub fn set_height(self, height: i32) -> Self {
        unsafe {
            internal::MaaSetRectH(self.handle, height);
        }

        self
    }
}

impl Default for MaaRectBuffer {
    fn default() -> Self {
        MaaRectBuffer::new()
    }
}

impl Drop for MaaRectBuffer {
    fn drop(&mut self) {
        if self.destroy_at_drop {
            unsafe {
                internal::MaaDestroyRectBuffer(self.handle);
            }
        }
    }
}