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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/// The base class of most Godot classes.
///
/// ## Official documentation
///
/// See the [documentation of this class](https://godot.readthedocs.io/en/latest/classes/class_object.html) in the Godot engine's official documentation.
///
/// ## Memory management
///
/// Non reference counted objects such as the ones of this type are usually owned by the engine.
///
/// `Object` is an unsafe pointer, and all of its methods are unsafe.
///
/// In the cases where Rust code owns an object of this type, for example if the object was just
/// created on the Rust side and not passed to the engine yet, ownership should be either given
/// to the engine or the object must be manually destroyed using `Object::free`.
#[derive(Copy, Clone)]
#[allow(non_camel_case_types)]
#[derive(Debug)]
pub struct Object {
    #[doc(hidden)]
    pub this: *mut sys::godot_object,
}

#[repr(u32)]
#[allow(non_camel_case_types)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum ObjectConnectFlags {
    ConnectDeferred = 1,
    ConnectPersist = 2,
    ConnectOneshot = 4,
    ConnectReferenceCounted = 8,
}
/// Constants
#[allow(non_upper_case_globals)]
impl Object {
    pub const CONNECT_DEFERRED: i64 = 1;
    pub const CONNECT_PERSIST: i64 = 2;
    pub const CONNECT_REFERENCE_COUNTED: i64 = 8;
    pub const CONNECT_ONESHOT: i64 = 4;
    pub const NOTIFICATION_POSTINITIALIZE: i64 = 0;
    pub const NOTIFICATION_PREDELETE: i64 = 1;
}
impl Object {

    /// Constructor.
    ///
    /// Because this type is not reference counted, the lifetime of the returned object
    /// is *not* automatically managed.
    /// Immediately after creation, the object is owned by the caller, and can be
    /// passed to the engine (in which case the engine will be responsible for
    /// destroying the object) or destroyed manually using `Object::free`.
    pub fn new() -> Self {
        unsafe {
            let gd_api = get_api();
            let ctor = ObjectMethodTable::get(gd_api).class_constructor.unwrap();
            let this = ctor();

            Object {
                this
            }
        }
    }

    /// Manually deallocate the object.
    #[inline]
    pub unsafe fn free(self) {
        (get_api().godot_object_destroy)(self.this);
    }
    #[inline]
    pub unsafe fn _get(&mut self, property: GodotString) -> Variant {
        Object__get(self.this, property)
    }

    #[inline]
    pub unsafe fn _get_property_list(&mut self) -> VariantArray {
        Object__get_property_list(self.this)
    }

    #[inline]
    pub unsafe fn _init(&mut self) -> () {
        Object__init(self.this)
    }

    #[inline]
    pub unsafe fn _notification(&mut self, what: i64) -> () {
        Object__notification(self.this, what)
    }

    #[inline]
    pub unsafe fn _set(&mut self, property: GodotString, value: Variant) -> bool {
        Object__set(self.this, property, value)
    }

    #[inline]
    pub unsafe fn _to_string(&mut self) -> GodotString {
        Object__to_string(self.this)
    }

    #[inline]
    pub unsafe fn add_user_signal(&mut self, signal: GodotString, arguments: VariantArray) -> () {
        Object_add_user_signal(self.this, signal, arguments)
    }

    #[inline]
    pub unsafe fn call(&mut self, method: GodotString, varargs: &[Variant]) -> Variant {
        Object_call(self.this, method, varargs)
    }

    #[inline]
    pub unsafe fn call_deferred(&mut self, method: GodotString, varargs: &[Variant]) -> Variant {
        Object_call_deferred(self.this, method, varargs)
    }

    #[inline]
    pub unsafe fn callv(&mut self, method: GodotString, arg_array: VariantArray) -> Variant {
        Object_callv(self.this, method, arg_array)
    }

    #[inline]
    pub unsafe fn can_translate_messages(&self) -> bool {
        Object_can_translate_messages(self.this)
    }

    #[inline]
    pub unsafe fn connect(&mut self, signal: GodotString, target: Option<Object>, method: GodotString, binds: VariantArray, flags: i64) -> GodotResult {
        Object_connect(self.this, signal, target, method, binds, flags)
    }

    #[inline]
    pub unsafe fn disconnect(&mut self, signal: GodotString, target: Option<Object>, method: GodotString) -> () {
        Object_disconnect(self.this, signal, target, method)
    }

    #[inline]
    pub unsafe fn emit_signal(&mut self, signal: GodotString, varargs: &[Variant]) -> Variant {
        Object_emit_signal(self.this, signal, varargs)
    }

    #[inline]
    pub unsafe fn get(&self, property: GodotString) -> Variant {
        Object_get(self.this, property)
    }

    #[inline]
    pub unsafe fn get_class(&self) -> GodotString {
        Object_get_class(self.this)
    }

    #[inline]
    pub unsafe fn get_incoming_connections(&self) -> VariantArray {
        Object_get_incoming_connections(self.this)
    }

    #[inline]
    pub unsafe fn get_indexed(&self, property: NodePath) -> Variant {
        Object_get_indexed(self.this, property)
    }

    #[inline]
    pub unsafe fn get_instance_id(&self) -> i64 {
        Object_get_instance_id(self.this)
    }

    #[inline]
    pub unsafe fn get_meta(&self, name: GodotString) -> Variant {
        Object_get_meta(self.this, name)
    }

    #[inline]
    pub unsafe fn get_meta_list(&self) -> StringArray {
        Object_get_meta_list(self.this)
    }

    #[inline]
    pub unsafe fn get_method_list(&self) -> VariantArray {
        Object_get_method_list(self.this)
    }

    #[inline]
    pub unsafe fn get_property_list(&self) -> VariantArray {
        Object_get_property_list(self.this)
    }

    #[inline]
    pub unsafe fn get_script(&self) -> Option<Reference> {
        Object_get_script(self.this)
    }

    #[inline]
    pub unsafe fn get_signal_connection_list(&self, signal: GodotString) -> VariantArray {
        Object_get_signal_connection_list(self.this, signal)
    }

    #[inline]
    pub unsafe fn get_signal_list(&self) -> VariantArray {
        Object_get_signal_list(self.this)
    }

    #[inline]
    pub unsafe fn has_meta(&self, name: GodotString) -> bool {
        Object_has_meta(self.this, name)
    }

    #[inline]
    pub unsafe fn has_method(&self, method: GodotString) -> bool {
        Object_has_method(self.this, method)
    }

    #[inline]
    pub unsafe fn has_user_signal(&self, signal: GodotString) -> bool {
        Object_has_user_signal(self.this, signal)
    }

    #[inline]
    pub unsafe fn is_blocking_signals(&self) -> bool {
        Object_is_blocking_signals(self.this)
    }

    #[inline]
    pub unsafe fn is_class(&self, class: GodotString) -> bool {
        Object_is_class(self.this, class)
    }

    #[inline]
    pub unsafe fn is_connected(&self, signal: GodotString, target: Option<Object>, method: GodotString) -> bool {
        Object_is_connected(self.this, signal, target, method)
    }

    #[inline]
    pub unsafe fn is_queued_for_deletion(&self) -> bool {
        Object_is_queued_for_deletion(self.this)
    }

    #[inline]
    pub unsafe fn notification(&mut self, what: i64, reversed: bool) -> () {
        Object_notification(self.this, what, reversed)
    }

    #[inline]
    pub unsafe fn property_list_changed_notify(&mut self) -> () {
        Object_property_list_changed_notify(self.this)
    }

    #[inline]
    pub unsafe fn remove_meta(&mut self, name: GodotString) -> () {
        Object_remove_meta(self.this, name)
    }

    #[inline]
    pub unsafe fn set(&mut self, property: GodotString, value: Variant) -> () {
        Object_set(self.this, property, value)
    }

    #[inline]
    pub unsafe fn set_block_signals(&mut self, enable: bool) -> () {
        Object_set_block_signals(self.this, enable)
    }

    #[inline]
    pub unsafe fn set_deferred(&mut self, property: GodotString, value: Variant) -> () {
        Object_set_deferred(self.this, property, value)
    }

    #[inline]
    pub unsafe fn set_indexed(&mut self, property: NodePath, value: Variant) -> () {
        Object_set_indexed(self.this, property, value)
    }

    #[inline]
    pub unsafe fn set_message_translation(&mut self, enable: bool) -> () {
        Object_set_message_translation(self.this, enable)
    }

    #[inline]
    pub unsafe fn set_meta(&mut self, name: GodotString, value: Variant) -> () {
        Object_set_meta(self.this, name, value)
    }

    #[inline]
    pub unsafe fn set_script(&mut self, script: Option<Reference>) -> () {
        Object_set_script(self.this, script)
    }

    #[inline]
    pub unsafe fn to_string(&mut self) -> GodotString {
        Object_to_string(self.this)
    }

    #[inline]
    pub unsafe fn tr(&self, message: GodotString) -> GodotString {
        Object_tr(self.this, message)
    }


    /// Generic dynamic cast.
    pub unsafe fn cast<T: GodotObject>(&self) -> Option<T> {
        object::godot_cast::<T>(self.this)
    }
}
/// Base class of all reference-counted types. Inherits `Object`.
///
/// ## Official documentation
///
/// See the [documentation of this class](https://godot.readthedocs.io/en/latest/classes/class_reference.html) in the Godot engine's official documentation.
///
/// ## Memory management
///
/// The lifetime of this object is automatically managed through reference counting.
///
/// ## Class hierarchy
///
/// Reference inherits methods from:
/// - [Object](struct.Object.html)
#[allow(non_camel_case_types)]
#[derive(Debug)]
pub struct Reference {
    #[doc(hidden)]
    pub this: *mut sys::godot_object,
}

impl Reference {

    // Constructor
    pub fn new() -> Self {
        unsafe {
            let gd_api = get_api();
            let ctor = ReferenceMethodTable::get(gd_api).class_constructor.unwrap();
            let obj = ctor();
            object::init_ref_count(obj);

            Reference {
                this: obj
            }
        }
    }


    /// Creates a new reference to the same reference-counted object.
    pub fn new_ref(&self) -> Self {
        unsafe {
            object::add_ref(self.this);

            Self {
                this: self.this,
            }
        }
    }

    #[inline]
    pub fn init_ref(&mut self) -> bool {
        unsafe { Reference_init_ref(self.this) }
    }

    /// Up-cast.
    #[inline]
    pub fn to_object(&self) -> Object {
        // Not reference-counted.
        Object { this: self.this }
    }

    /// Generic dynamic cast.
    pub fn cast<T: GodotObject>(&self) -> Option<T> {
        object::godot_cast::<T>(self.this)
    }
}