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
107
108
109
use sys;
use get_api;
use Variant;
use GodotType;
use VariantArray;
use Vector2;

use std::mem::transmute;

/// A reference-counted vector of `Vector2` that uses Godot's pool allocator.
pub struct Vector2Array(pub(crate) sys::godot_pool_vector2_array);

impl Vector2Array {
    /// Creates an empty array.
    pub fn new() -> Self { Vector2Array::default() }

    /// Creates an array by trying to convert each variant
    ///
    /// See `Variant::to_vector2`.
    pub fn from_variant_array(array: &VariantArray) -> Self {
        unsafe {
            let mut result = sys::godot_pool_vector2_array::default();
            (get_api().godot_pool_vector2_array_new_with_array)(&mut result, &array.0);
            Vector2Array(result)
        }
    }

    /// Appends a vector to the end of the array.
    pub fn push(&mut self, vector: &Vector2) {
        unsafe {
            (get_api().godot_pool_vector2_array_append)(&mut self.0, transmute(vector));
        }
    }

    /// Appends each vector to the end of the array.
    pub fn push_array(&mut self, vectors: &Vector2Array) {
        unsafe {
            (get_api().godot_pool_vector2_array_append_array)(&mut self.0, transmute(vectors));
        }
    }

    // TODO(error handling)
    /// Inserts a vector at the given offset.
    pub fn insert(&mut self, offset: i32, vector: &Vector2) -> bool {
        unsafe {
            let status = (get_api().godot_pool_vector2_array_insert)(&mut self.0, offset, transmute(vector));
            status != sys::godot_error::GODOT_OK
        }
    }

    /// Inverts the order of the elements in the array.
    pub fn invert(&mut self) {
        unsafe {
            (get_api().godot_pool_vector2_array_invert)(&mut self.0)
        }
    }

    /// Removes an element at the given offset.
    pub fn remove(&mut self, idx: i32) {
        unsafe {
            (get_api().godot_pool_vector2_array_remove)(&mut self.0, idx);
        }
    }

    /// Changes the size of the array, possibly removing elements or pushing default values.
    pub fn resize(&mut self, size: i32) {
        unsafe {
            (get_api().godot_pool_vector2_array_resize)(&mut self.0, size);
        }
    }

    /// Returns a copy of the element at the given offset.
    pub fn get(&self, idx: i32) -> Vector2 {
        unsafe {
            transmute((get_api().godot_pool_vector2_array_get)(&self.0, idx))
        }
    }

    /// Sets the value of the element at the given offset.
    pub fn set(&mut self, idx: i32, vector: &Vector2) {
        unsafe {
            (get_api().godot_pool_vector2_array_set)(&mut self.0, idx, transmute(vector));
        }
    }

    /// Returns the number of elements in the array.
    pub fn len(&self) -> i32 {
        unsafe {
            (get_api().godot_pool_vector2_array_size)(&self.0)
        }
    }

    impl_common_methods! {
        /// Creates a new reference to this array.
        pub fn new_ref(&self) -> Vector2Array : godot_pool_vector2_array_new_copy;
    }
}

impl_basic_traits!(
    for Vector2Array as godot_pool_vector2_array {
        Drop => godot_pool_vector2_array_destroy;
        Default => godot_pool_vector2_array_new;
    }
);

impl GodotType for Vector2Array {
    fn to_variant(&self) -> Variant { Variant::from_vector2_array(self) }
    fn from_variant(variant: &Variant) -> Option<Self> { variant.try_to_vector2_array() }
}