-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathmod.rs
387 lines (338 loc) · 13.4 KB
/
mod.rs
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
//! Built-in types like `Vector2`, `GodotString` and `Variant`.
//!
//! # Background on the design of vector algebra types
//!
//! The basic vector algebra types like `Vector2`, `Matrix4` and `Quaternion` are re-implemented
//! here, with an API similar to that in the Godot engine itself. There are other approaches, but
//! they all have their disadvantages:
//!
//! - We could invoke API methods from the engine. The implementations could be generated, but it
//! is slower and prevents inlining.
//!
//! - We could re-export types from an existing vector algebra crate, like `glam`. This removes the
//! duplication, but it would create a strong dependency on a volatile API outside our control.
//! The `gdnative` crate started out this way, using types from `euclid`, but [found it
//! impractical](https://github.com/godot-rust/gdnative/issues/594#issue-705061720). Moreover,
//! the API would not match Godot's own, which would make porting from GDScript (slightly)
//! harder.
//!
//! - We could opaquely wrap types from an existing vector algebra crate. This protects users of
//! `gdextension` from changes in the wrapped crate. However, direct field access using `.x`,
//! `.y`, `.z` is no longer possible. Instead of `v.y += a;` you would have to write
//! `v.set_y(v.get_y() + a);`. (A `union` could be used to add these fields in the public API,
//! but would make every field access unsafe, which is also not great.)
//!
//! - We could re-export types from the [`mint`](https://crates.io/crates/mint) crate, which was
//! explicitly designed to solve this problem. However, it falls short because [operator
//! overloading would become impossible](https://github.com/kvark/mint/issues/75).
// Re-export macros.
pub use crate::{array, dict, varray};
pub use array_inner::{Array, VariantArray};
pub use basis::*;
pub use color::*;
pub use dictionary_inner::Dictionary;
pub use math::*;
pub use node_path::*;
pub use others::*;
pub use packed_array::*;
pub use projection::*;
pub use quaternion::*;
pub use rid::*;
pub use string::*;
pub use string_name::*;
pub use transform2d::*;
pub use transform3d::*;
pub use variant::*;
pub use vector2::*;
pub use vector2i::*;
pub use vector3::*;
pub use vector3i::*;
pub use vector4::*;
pub use vector4i::*;
/// Meta-information about variant types, properties and class names.
pub mod meta;
/// Specialized types related to arrays.
pub mod array {
pub use super::array_inner::Iter;
}
/// Specialized types related to dictionaries.
pub mod dictionary {
pub use super::dictionary_inner::{Iter, Keys, TypedIter, TypedKeys};
}
// ----------------------------------------------------------------------------------------------------------------------------------------------
// Implementation
// Modules exporting declarative macros must appear first.
mod macros;
mod vector_macros;
// Rename imports because we re-export a subset of types under same module names.
#[path = "array.rs"]
mod array_inner;
#[path = "dictionary.rs"]
mod dictionary_inner;
mod basis;
mod color;
mod glam_helpers;
mod math;
mod node_path;
mod others;
mod packed_array;
mod projection;
mod quaternion;
mod rid;
mod string;
mod string_chars;
mod string_name;
mod transform2d;
mod transform3d;
mod variant;
mod vector2;
mod vector2i;
mod vector3;
mod vector3i;
mod vector4;
mod vector4i;
#[doc(hidden)]
pub mod inner {
pub use crate::gen::builtin_classes::*;
}
pub(crate) fn to_i64(i: usize) -> i64 {
i.try_into().unwrap()
}
pub(crate) fn to_usize(i: i64) -> usize {
i.try_into().unwrap()
}
pub(crate) fn to_isize(i: usize) -> isize {
i.try_into().unwrap()
}
pub(crate) fn u8_to_bool(u: u8) -> bool {
match u {
0 => false,
1 => true,
_ => panic!("Invalid boolean value {u}"),
}
}
/// Clippy often complains if you do `f as f64` when `f` is already an `f64`. This trait exists to make it easy to
/// convert between the different reals and floats without a lot of allowing clippy lints for your code.
pub trait RealConv {
/// Cast this [`real`] to an [`f32`] using `as`.
// Clippy complains that this is an `as_*` function but it takes a `self`
// however, since this uses `as` internally it makes much more sense for
// it to be named `as_f32` rather than `to_f32`.
#[allow(clippy::wrong_self_convention)]
fn as_f32(self) -> f32;
/// Cast this [`real`] to an [`f64`] using `as`.
// Clippy complains that this is an `as_*` function but it takes a `self`
// however, since this uses `as` internally it makes much more sense for
// it to be named `as_f64` rather than `to_f64`.
#[allow(clippy::wrong_self_convention)]
fn as_f64(self) -> f64;
/// Cast an [`f32`] to a [`real`] using `as`.
fn from_f32(f: f32) -> Self;
/// Cast an [`f64`] to a [`real`] using `as`.
fn from_f64(f: f64) -> Self;
}
#[cfg(not(feature = "double-precision"))]
mod real_mod {
//! Definitions for single-precision `real`.
/// Floating point type used for many structs and functions in Godot.
///
/// This is not the `float` type in GDScript; that type is always 64-bits. Rather, many structs in Godot may use
/// either 32-bit or 64-bit floats such as [`Vector2`](super::Vector2). To convert between [`real`] and [`f32`] or
/// [`f64`] see [`RealConv`](super::RealConv).
///
/// See also the [Godot docs on float](https://docs.godotengine.org/en/stable/classes/class_float.html).
///
/// _Godot equivalent: `real_t`_
// As this is a scalar value, we will use a non-standard type name.
#[allow(non_camel_case_types)]
pub type real = f32;
impl super::RealConv for real {
#[inline]
fn as_f32(self) -> f32 {
self
}
#[inline]
fn as_f64(self) -> f64 {
self as f64
}
#[inline]
fn from_f32(f: f32) -> Self {
f
}
#[inline]
fn from_f64(f: f64) -> Self {
f as f32
}
}
pub use std::f32::consts;
/// A 2-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RVec2 = glam::Vec2;
/// A 3-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RVec3 = glam::Vec3;
/// A 4-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RVec4 = glam::Vec4;
/// A 2x2 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RMat2 = glam::Mat2;
/// A 3x3 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RMat3 = glam::Mat3;
/// A 4x4 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RMat4 = glam::Mat4;
/// A matrix from [`glam`] quaternion representing an orientation. Using a floating-point format compatible
/// with [`real`].
pub type RQuat = glam::Quat;
/// A 2D affine transform from [`glam`], which can represent translation, rotation, scaling and
/// shear. Using a floating-point format compatible with [`real`].
pub type RAffine2 = glam::Affine2;
/// A 3D affine transform from [`glam`], which can represent translation, rotation, scaling and
/// shear. Using a floating-point format compatible with [`real`].
pub type RAffine3 = glam::Affine3A;
}
#[cfg(feature = "double-precision")]
mod real_mod {
//! Definitions for double-precision `real`.
/// Floating point type used for many structs and functions in Godot.
///
/// This is not the `float` type in GDScript; that type is always 64-bits. Rather, many structs in Godot may use
/// either 32-bit or 64-bit floats such as [`Vector2`](super::Vector2). To convert between [`real`] and [`f32`] or
/// [`f64`] see [`RealConv`](super::RealConv).
///
/// See also the [Godot docs on float](https://docs.godotengine.org/en/stable/classes/class_float.html).
///
/// _Godot equivalent: `real_t`_
// As this is a scalar value, we will use a non-standard type name.
#[allow(non_camel_case_types)]
pub type real = f64;
impl super::RealConv for real {
#[inline]
fn as_f32(self) -> f32 {
self as f32
}
#[inline]
fn as_f64(self) -> f64 {
self
}
#[inline]
fn from_f32(f: f32) -> Self {
f as f64
}
#[inline]
fn from_f64(f: f64) -> Self {
f
}
}
pub use std::f64::consts;
/// A 2-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RVec2 = glam::DVec2;
/// A 3-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RVec3 = glam::DVec3;
/// A 4-dimensional vector from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RVec4 = glam::DVec4;
/// A 2x2 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RMat2 = glam::DMat2;
/// A 3x3 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RMat3 = glam::DMat3;
/// A 4x4 column-major matrix from [`glam`]. Using a floating-point format compatible with [`real`].
pub type RMat4 = glam::DMat4;
/// A matrix from [`glam`] quaternion representing an orientation. Using a floating-point format
/// compatible with [`real`].
pub type RQuat = glam::DQuat;
/// A 2D affine transform from [`glam`], which can represent translation, rotation, scaling and
/// shear. Using a floating-point format compatible with [`real`].
pub type RAffine2 = glam::DAffine2;
/// A 3D affine transform from [`glam`], which can represent translation, rotation, scaling and
/// shear. Using a floating-point format compatible with [`real`].
pub type RAffine3 = glam::DAffine3;
}
pub use crate::real;
pub(crate) use real_mod::*;
pub use real_mod::{consts as real_consts, real};
/// A macro to coerce float-literals into the real type. Mainly used where
/// you'd normally use a suffix to specity the type, such as `115.0f32`.
///
/// ### Examples
/// Rust will not know how to infer the type of this call to `to_radians`:
/// ```compile_fail
/// use godot_core::builtin::real;
///
/// let radians: real = 115.0.to_radians();
/// ```
/// But we can't add a suffix to the literal, since it may be either `f32` or
/// `f64` depending on the context. So instead we use our macro:
/// ```
/// use godot_core::builtin::real;
///
/// let radians: real = godot_core::real!(115.0).to_radians();
/// ```
#[macro_export]
macro_rules! real {
($f:literal) => {{
let f: $crate::builtin::real = $f;
f
}};
}
// ----------------------------------------------------------------------------------------------------------------------------------------------
/// Implementations of the `Export` trait for types where it can be done trivially.
mod export {
use crate::builtin::*;
use crate::obj::Export;
macro_rules! impl_export_by_clone {
($ty:path) => {
impl Export for $ty {
fn export(&self) -> Self {
// If `Self` does not implement `Clone`, this gives a clearer error message
// than simply `self.clone()`.
Clone::clone(self)
}
}
};
}
impl_export_by_clone!(bool);
impl_export_by_clone!(isize);
impl_export_by_clone!(usize);
impl_export_by_clone!(i8);
impl_export_by_clone!(i16);
impl_export_by_clone!(i32);
impl_export_by_clone!(i64);
impl_export_by_clone!(u8);
impl_export_by_clone!(u16);
impl_export_by_clone!(u32);
impl_export_by_clone!(u64);
impl_export_by_clone!(f32);
impl_export_by_clone!(f64);
// impl_export_by_clone!(Aabb); // TODO uncomment once Aabb implements Clone
impl_export_by_clone!(Basis);
impl_export_by_clone!(Color);
impl_export_by_clone!(GodotString);
impl_export_by_clone!(NodePath);
impl_export_by_clone!(PackedByteArray);
impl_export_by_clone!(PackedColorArray);
impl_export_by_clone!(PackedFloat32Array);
impl_export_by_clone!(PackedFloat64Array);
impl_export_by_clone!(PackedInt32Array);
impl_export_by_clone!(PackedInt64Array);
impl_export_by_clone!(PackedStringArray);
impl_export_by_clone!(PackedVector2Array);
impl_export_by_clone!(PackedVector3Array);
// impl_export_by_clone!(Plane); // TODO uncomment once Plane implements Clone
impl_export_by_clone!(Projection);
impl_export_by_clone!(Quaternion);
// impl_export_by_clone!(Rect2); // TODO uncomment once Rect2 implements Clone
// impl_export_by_clone!(Rect2i); // TODO uncomment once Rect2i implements Clone
impl_export_by_clone!(Rid);
impl_export_by_clone!(StringName);
impl_export_by_clone!(Transform2D);
impl_export_by_clone!(Transform3D);
impl_export_by_clone!(Vector2);
impl_export_by_clone!(Vector2i);
impl_export_by_clone!(Vector3);
impl_export_by_clone!(Vector3i);
impl_export_by_clone!(Vector4);
// TODO investigate whether these should impl Export at all, and if so, how
// impl_export_by_clone!(Callable);
// impl_export_by_clone!(Signal);
}